1、glBufferData

  1. std::vector<Vertex> mVertices;
  2. glGenVertexArrays( 1, &mVAO );
  3. glGenBuffers( 1, &mVBO );
  4. glBindVertexArray( mVAO );
  5. glBindBuffer( GL_ARRAY_BUFFER, mVBO );
  6. glBufferData( GL_ARRAY_BUFFER,
  7. mVertices.size() * sizeof( Vertex ),
  8. &mVertices[0], // !!!这里不是&mVertices
  9. GL_STATIC_DRAW );
  10. glEnableVertexAttribArray( 0 );
  11. glVertexAttribPointer( 0,
  12. 3,
  13. GL_FLOAT,
  14. GL_FALSE,
  15. sizeof( Vertex ),
  16. ( void * ) 0 ); // 正确写法应该是( void * ) (0 * sizeof(GLfloat)),假设元素为GLfloat类型
  17. glBindVertexArray( 0 );

2、glUseProgram

  1. //shader.use(); // 忘记调用glUseProgram,导致着色器并没有被使用
  2. shader.setInt( "task", TASK_COORD );
  3. shader.setMat4( "modelMatrix", glm::mat4( 1.0f ) );
  4. shader.setMat4( "viewMatrix", viewMatrix );
  5. shader.setMat4( "projectionMatrix", projectionMatrix );
  6. glBindVertexArray( VAO_coord );
  7. glDrawArrays( GL_LINES, 0, 6 );

3、glEnableVertexAttribArray

  1. const GLfloat pos[] = { ... };
  2. const GLfloat normal[] = { ... };
  3. const GLfloat tex[] = { ... };
  4. glGenBuffers( 1, VBO ); // 创建VBO
  5. glBindBuffer( GL_ARRAY_BUFFER, *VBO );
  6. glBufferData( GL_ARRAY_BUFFER, len, vertices, GL_STATIC_DRAW ); // 填充数据
  7. // 填充缓冲
  8. glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(positions), &positions);
  9. glBufferSubData(GL_ARRAY_BUFFER, sizeof(positions), sizeof(normals), &normals);
  10. glBufferSubData(GL_ARRAY_BUFFER, sizeof(positions) + sizeof(normals), sizeof(tex), &tex);
  11. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
  12. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(sizeof(positions)));
  13. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void*)(sizeof(positions) + sizeof(normals)));
  14. glEnableVertexAttribArray( 0 ); // 特别容易漏掉这个
  15. glEnableVertexAttribArray( 1 );
  16. glEnableVertexAttribArray( 2 );

4、着色器中的奇怪问题

下面是一个几何着色器。
项目链接:https://github.com/JackieLong/OpenGL/tree/main/project_geometry_shader_test

  1. #version 330 core
  2. layout(triangles) in;
  3. layout(triangle_strip, max_vertices = 3) out;
  4. in vec2 texCoord[];
  5. uniform float time;
  6. out vec2 outTexCoord;
  7. vec4 explode(vec4 pos, vec3 normal);
  8. vec3 triangleNormal();
  9. void main() {
  10. vec3 normal = triangleNormal();
  11. // 如果用注释的代码,结果就异常,没有加载出模型
  12. gl_Position = explode(vec4(gl_in[0].gl_Position), normal);
  13. // gl_Position = explode(gl_in[0].gl_Position, normal);
  14. outTexCoord = texCoord[0];
  15. EmitVertex();
  16. gl_Position = explode(vec4(gl_in[1].gl_Position), normal);
  17. // gl_Position = explode(gl_in[1].gl_Position, normal);
  18. outTexCoord = texCoord[1];
  19. EmitVertex();
  20. gl_Position = explode(vec4(gl_in[2].gl_Position), normal);
  21. // gl_Position = explode(gl_in[2].gl_Position, normal);
  22. outTexCoord = texCoord[2];
  23. EmitVertex();
  24. EndPrimitive();
  25. }
  26. vec4 explode(vec4 pos, vec3 normal)
  27. {
  28. return pos + vec4(normal * ((1.0 + sin(time)) / 2.0), 0.0);
  29. }
  30. vec3 triangleNormal()
  31. {
  32. vec3 fuck1 = vec3(gl_in[0].gl_Position - gl_in[1].gl_Position);
  33. vec3 fuck2 = vec3(gl_in[2].gl_Position - gl_in[1].gl_Position);
  34. return normalize(cross(fuck1, fuck2));
  35. }