• LearnOpenGL1.3:着色器


    存在问题:

            1.glDrawElements的偏移量不能乱改。。。

            2.GLSL没提示老是写错

    1. #include
    2. #include
    3. #include
    4. void frambuffer_size_callback(GLFWwindow *window, int width,int height);
    5. void processInput(GLFWwindow *window);
    6. const unsigned int HEIGHT = 600;
    7. const unsigned int WIDTH = 800;
    8. // shader
    9. const char* vertexShaderSource = "#version 330 core\n"
    10. "layout (location = 0) in vec3 aPos;\n"
    11. "layout (location = 1) in vec3 aColor;\n"
    12. "out vec3 ourColor;\n"
    13. "void main()\n"
    14. "{\n"
    15. " gl_Position = vec4(aPos, 1.0);\n"
    16. " ourColor = aColor;\n"
    17. "}\0";
    18. const char *fragmentShaderSource = "#version 330 core\n"
    19. "out vec4 FragColor;\n"
    20. "in vec3 ourColor;\n"
    21. "void main()\n"
    22. "{\n"
    23. " FragColor = vec4(ourColor, 1.0);\n"
    24. "}\n\0";
    25. int main() {
    26. // 配置
    27. glfwInit();
    28. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    29. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    30. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    31. // MacOSX
    32. //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    33. GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "LearnOpenGL", NULL, NULL);
    34. if (window == NULL) {
    35. std::cout << "Failed to create GLFW window" << std::endl;
    36. return -1;
    37. }
    38. // 设置当前线程的主上下文
    39. glfwMakeContextCurrent(window);
    40. glfwSetFramebufferSizeCallback(window, frambuffer_size_callback);
    41. // glad管理函数指针
    42. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
    43. std::cout << "Failed to initialize GLAD" << std::endl;
    44. return -1;
    45. }
    46. // 坐标转换
    47. //glViewport(0, 0, HEIGHT, HEIGHT);
    48. unsigned int vertexShader;
    49. vertexShader = glCreateShader(GL_VERTEX_SHADER);
    50. glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    51. // 动态编译
    52. glCompileShader(vertexShader);
    53. unsigned int fragmentShader;
    54. fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    55. glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    56. glCompileShader(fragmentShader);
    57. // link
    58. unsigned int shaderProgram;
    59. shaderProgram = glCreateProgram();
    60. glAttachShader(shaderProgram, vertexShader);
    61. glAttachShader(shaderProgram, fragmentShader);
    62. glLinkProgram(shaderProgram);
    63. glDeleteShader(vertexShader);
    64. glDeleteShader(fragmentShader);
    65. //float vertices[] = {
    66. //-0.5f,-0.5f,0.0f,
    67. //0.5f,-0.5f,0.0f,
    68. //0.0f,0.5f,0.0f,
    69. //0.5f,-0.5f,0.0f,
    70. //1.0f,0.5f,0.0f,
    71. //0.0f,0.5f,0.0f,
    72. //};
    73. // 使用索引缓冲区
    74. float vertices[] = {
    75. // 位置 // 颜色
    76. 0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f,// 右上角
    77. 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, // 右下角
    78. -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, // 左下角
    79. -0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, // 左上角
    80. };
    81. unsigned int indices[] =
    82. {
    83. 0, 1, 3,
    84. 1, 2, 3
    85. };
    86. unsigned int VBO, VAO;
    87. // create obj
    88. glGenVertexArrays(1, &VAO);
    89. glGenBuffers(1, &VBO);
    90. glBindVertexArray(VAO);
    91. // bind to target
    92. glBindBuffer(GL_ARRAY_BUFFER, VBO);
    93. // copy vertices to gpu
    94. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    95. unsigned int EBO;
    96. glGenBuffers(1, &EBO);
    97. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    98. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
    99. // 告知opengl如何解析
    100. // 位置属性 0为layout=0
    101. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)0);
    102. // 启用顶点属性
    103. glEnableVertexAttribArray(0);
    104. // 颜色属性 最后一个参数为offset
    105. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6*sizeof(float), (void*)(3*sizeof(float)));
    106. glEnableVertexAttribArray(1);
    107. // 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
    108. //glBindBuffer(GL_ARRAY_BUFFER, 0);
    109. // You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
    110. // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
    111. //glBindVertexArray(0);
    112. // uncomment this call to draw in wireframe polygons.
    113. //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    114. // shader很少变化,所以不是动态改变的也行
    115. //glUseProgram(shaderProgram);
    116. //
    117. // render
    118. while (!glfwWindowShouldClose(window)) {
    119. processInput(window);
    120. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    121. glClear(GL_COLOR_BUFFER_BIT);
    122. glUseProgram(shaderProgram);
    123. // ourColor随着时间变化
    124. //float timeValue = glfwGetTime();
    125. 0-1 不知道为啥这么写
    126. //float newColorValue = (sin(timeValue) / 2.0f) + 0.5f;
    127. 查询位置值
    128. //int vertextColorLocation = glGetUniformLocation(shaderProgram, "ourColor");
    129. 设置
    130. //glUniform4f(vertextColorLocation, 1.0f, newColorValue, 0.0f, 1.0f);
    131. glBindVertexArray(VAO);
    132. glDrawArrays(GL_TRIANGLES, 0, 6);
    133. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    134. // 代替glDrawArrays 0:指定EBO中的偏移量
    135. glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
    136. glfwSwapBuffers(window);
    137. // 检查事件,调用对应的回调函数
    138. glfwPollEvents();
    139. }
    140. glDeleteVertexArrays(1, &VAO);
    141. glDeleteBuffers(1, &VBO);
    142. glDeleteProgram(shaderProgram);
    143. // 释放/删除资源
    144. glfwTerminate();
    145. return 0;
    146. }
    147. void frambuffer_size_callback(GLFWwindow* window, int width, int height) {
    148. glViewport(0, 0, HEIGHT, HEIGHT);
    149. }
    150. void processInput(GLFWwindow* window) {
    151. if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
    152. glfwSetWindowShouldClose(window, true);
    153. }

    运行结果:

            

  • 相关阅读:
    1小时学会SpringBoot3+Vue3前后端分离开发
    详细了解 synchronized 锁升级过程
    对NFT市场前景的7个看法
    【微信小程序】6天精准入门(第5天:利用案例与后台的数据交互)附源码
    神经网络 torch.nn---nn.LSTM()
    k8s使用rbd作为存储
    UE构建基础和实践:一、概述
    驱动人生国庆宅家指南,这些游戏值得一刷!
    CSDN的MD编辑器【写作技巧】
    安装mysql
  • 原文地址:https://blog.csdn.net/LearnToStick/article/details/126693579