存在问题:
1.glDrawElements的偏移量不能乱改。。。
2.GLSL没提示老是写错
- #include
- #include
-
- #include
-
- void frambuffer_size_callback(GLFWwindow *window, int width,int height);
- void processInput(GLFWwindow *window);
-
- const unsigned int HEIGHT = 600;
- const unsigned int WIDTH = 800;
-
- // shader
- const char* vertexShaderSource = "#version 330 core\n"
- "layout (location = 0) in vec3 aPos;\n"
- "layout (location = 1) in vec3 aColor;\n"
- "out vec3 ourColor;\n"
- "void main()\n"
- "{\n"
- " gl_Position = vec4(aPos, 1.0);\n"
- " ourColor = aColor;\n"
- "}\0";
-
- const char *fragmentShaderSource = "#version 330 core\n"
- "out vec4 FragColor;\n"
- "in vec3 ourColor;\n"
- "void main()\n"
- "{\n"
- " FragColor = vec4(ourColor, 1.0);\n"
- "}\n\0";
-
-
-
- int main() {
- // 配置
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- // MacOSX
- //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
-
-
- GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
- if (window == NULL) {
- std::cout << "Failed to create GLFW window" << std::endl;
- return -1;
- }
- // 设置当前线程的主上下文
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, frambuffer_size_callback);
-
- // glad管理函数指针
- if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
- std::cout << "Failed to initialize GLAD" << std::endl;
- return -1;
- }
-
- // 坐标转换
- //glViewport(0, 0, HEIGHT, HEIGHT);
-
- unsigned int vertexShader;
- vertexShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
- // 动态编译
- glCompileShader(vertexShader);
-
- unsigned int fragmentShader;
- fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
- glCompileShader(fragmentShader);
-
- // link
- unsigned int shaderProgram;
- shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
-
- glDeleteShader(vertexShader);
- glDeleteShader(fragmentShader);
-
- //float vertices[] = {
- //-0.5f,-0.5f,0.0f,
- //0.5f,-0.5f,0.0f,
- //0.0f,0.5f,0.0f,
-
- //0.5f,-0.5f,0.0f,
- //1.0f,0.5f,0.0f,
- //0.0f,0.5f,0.0f,
- //};
-
- // 使用索引缓冲区
- float vertices[] = {
- // 位置 // 颜色
- 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,// 右上角
- 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // 右下角
- -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // 左下角
- -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // 左上角
- };
-
-
- unsigned int indices[] =
- {
- 0, 1, 3,
- 1, 2, 3
- };
-
- unsigned int VBO, VAO;
- // create obj
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
-
- glBindVertexArray(VAO);
-
- // bind to target
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- // copy vertices to gpu
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
-
- unsigned int EBO;
- glGenBuffers(1, &EBO);
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
-
-
- // 告知opengl如何解析
- // 位置属性 0为layout=0
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0);
- // 启用顶点属性
- glEnableVertexAttribArray(0);
- // 颜色属性 最后一个参数为offset
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float)));
- glEnableVertexAttribArray(1);
-
-
- // note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
- //glBindBuffer(GL_ARRAY_BUFFER, 0);
-
- // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
- // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
- //glBindVertexArray(0);
-
- // uncomment this call to draw in wireframe polygons.
- //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- // shader很少变化,所以不是动态改变的也行
- //glUseProgram(shaderProgram);
- //
- // render
- while (!glfwWindowShouldClose(window)) {
-
- processInput(window);
-
- glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
-
- glUseProgram(shaderProgram);
-
- // ourColor随着时间变化
- //float timeValue = glfwGetTime();
- 0-1 不知道为啥这么写
- //float newColorValue = (sin(timeValue) / 2.0f) + 0.5f;
- 查询位置值
- //int vertextColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
- 设置
- //glUniform4f(vertextColorLocation, 1.0f, newColorValue, 0.0f, 1.0f);
-
- glBindVertexArray(VAO);
- glDrawArrays(GL_TRIANGLES, 0, 6);
-
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- // 代替glDrawArrays 0:指定EBO中的偏移量
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
-
- glfwSwapBuffers(window);
- // 检查事件,调用对应的回调函数
- glfwPollEvents();
- }
-
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- glDeleteProgram(shaderProgram);
- // 释放/删除资源
- glfwTerminate();
-
-
- return 0;
- }
-
- void frambuffer_size_callback(GLFWwindow* window, int width, int height) {
- glViewport(0, 0, HEIGHT, HEIGHT);
- }
-
- void processInput(GLFWwindow* window) {
- if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
- glfwSetWindowShouldClose(window, true);
- }
运行结果: