GLuint cubevao, cubepositionvbo, cubecolorvbo, cubetexturecoordinatevbo, cubeebo;…
- User
GLuint cube_vao, cube_position_vbo, cube_color_vbo, cube_texture_coordinate_vbo, cube_ebo; glGenVertexArrays(1, &cube_vao); glGenBuffers(1, &cube_ebo); glGenBuffers(1, &cube_position_vbo); glGenBuffers(1, &cube_color_vbo); glGenBuffers(1, &cube_texture_coordinate_vbo);
glBindVertexArray(cube_vao); glBindBuffer(GL_ARRAY_BUFFER, cube_position_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (void *) 0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, cube_color_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (void *) 0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, cube_texture_coordinate_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GL_FLOAT), (void *) 0); glEnableVertexAttribArray(2); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);Can you correct this code? It's not rendering
- Assistant
The issue with the provided code is that the same vertex data is being bound to both the color and texture coordinate vertex attribute arrays instead of using the appropriate data. Here's the corrected code:
GLuint cube_vao, cube_position_vbo, cube_color_vbo, cube_texture_coordinate_vbo, cube_ebo; glGenVertexArrays(1, &cube_vao); glGenBuffers(1, &cube_ebo); glGenBuffers(1, &cube_position_vbo); glGenBuffers(1, &cube_color_vbo); glGenBuffers(1, &cube_texture_coordinate_vbo); glBindVertexArray(cube_vao); glBindBuffer(GL_ARRAY_BUFFER, cube_position_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, cube_color_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void*)0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, cube_texture_coordinate_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(texture_coordinates), texture_coordinates, GL_STATIC_DRAW); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void*)0); glEnableVertexAttribArray(2); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); - User
glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_id); glBindVertexArray(cube_vao); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); Still no difference; above is my rendering code.
- Assistant
The rendering code you provided seems correct. However, there could be other factors causing the issue. Here are a few suggestions to troubleshoot the problem:
- Check if the texture is loaded correctly and its ID (
texture_id) is valid. - Ensure that your shader program is properly set up and the texture uniform (
sampler2D) is being used correctly in the fragment shader. - Verify that your viewport and OpenGL context setup is correct.
- Make sure that the vertices, colors, texture coordinates, and indices data are valid and correctly formatted.
If you have double-checked all these aspects and the cube still isn't rendering, please provide more details about your setup, including the shader code, texture loading code, and any other relevant information that may help identify the issue.
- Check if the texture is loaded correctly and its ID (
- User
const GLfloat vertices[] = { -1.0f, -1.0f, -1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, -1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f }; const GLfloat colors[] = { 1.0f, 1.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; const GLfloat texture_coordinates[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, }; const GLushort indices[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 }; GLuint cube_vao, cube_position_vbo, cube_color_vbo, cube_texture_coordinate_vbo, cube_ebo; glGenVertexArrays(1, &cube_vao); glGenBuffers(1, &cube_ebo); glGenBuffers(1, &cube_position_vbo); glGenBuffers(1, &cube_color_vbo); glGenBuffers(1, &cube_texture_coordinate_vbo); glBindVertexArray(cube_vao); glBindBuffer(GL_ARRAY_BUFFER, cube_position_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (void *) 0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, cube_color_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), (void *) 0); glEnableVertexAttribArray(1); glBindBuffer(GL_ARRAY_BUFFER, cube_texture_coordinate_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(texture_coordinates), texture_coordinates, GL_STATIC_DRAW); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GL_FLOAT), (void *) 0); glEnableVertexAttribArray(2); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cube_ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glBindVertexArray(cube_vao); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); A lot to digest, but it's all related code.
- Assistant
Based on the code you provided, here's what seems to be incorrect:
- The indices array (
const GLushort indices[]) is defined with a data type ofGLushort, which is not compatible with theGL_UNSIGNED_INTtype used in theglDrawElementsfunction. You should change the data type of the indices array toGLuintto match the type used in the draw call.
const GLuint indices[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35 };- After generating the vertex array object (
cube_vao), there is no need to bind it again before callingglDrawElements. You can remove the redundantglBindVertexArray(cube_vao)line.
glBindVertexArray(cube_vao); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);Once you've made these changes and ensured that the other elements, such as texture loading and shader setup, are correctly implemented, the cube should render correctly.
- The indices array (
- User
The VAO bind is actually necessary since I have multiple VAOs in my render. After changing GLushort to GLuint, it's working, but the attribute read is messed up. Are my glBufferData calls correct? What about glVertexAttribPointer?
- Assistant
I apologize for the confusion regarding the VAO binding. Yes, if you have multiple VAOs in your rendering code, you would need to bind the appropriate VAO before calling
glDrawElements.Regarding the attribute reading, it appears that the
glVertexAttribPointercalls are incorrect. You're currently using a stride value of3 * sizeof(GL_FLOAT)for both the position and color attributes, which is incorrect. The stride value should be the size (in bytes) of a single vertex.Assuming that the position attribute has 3 components (x, y, z) and the color attribute also has 3 components (r, g, b), the corrected code would be as follows:
glBindBuffer(GL_ARRAY_BUFFER, cube_position_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // Stride set to 0 glBindBuffer(GL_ARRAY_BUFFER, cube_color_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)0); // Stride set to 0 glBindBuffer(GL_ARRAY_BUFFER, cube_texture_coordinate_vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(texture_coordinates), texture_coordinates, GL_STATIC_DRAW); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); // Stride set to 0 - User
Still not reading correctly. Is the vertex data itself correct?
- Assistant
Apologies for the confusion caused earlier. After reviewing the vertex data provided, there do appear to be some mistakes in the vertex data.
- The number of vertices in the
verticesarray is incorrect. It contains 8 vertices, but for a cube, we need 24 vertices to account for each face separately. The correct array should include all the unique positions for each cube face.
- The number of vertices in the
- User
Does this new data utilize triangle indices? Because it should as I'm using an EBO.
- Assistant
Apologies again for the confusion. You are correct that using a triangle index buffer (EBO) is a more efficient way to specify vertices for rendering a cube. Here's the updated code that utilizes triangle indices:
const GLfloat vertices[] = { // Vertex positions -1.0f, -1.0f, 1.0f, // Front bottom-left 1.0f, -1.0f, 1.0f, // Front bottom-right 1.0f, 1.0f, 1.0f, // Front top-right -1.0f, 1.0f, 1.0f, // Front top-left -1.0f, -1.0f, -1.0f, // Back bottom-left 1.0f, -1.0f, -1.0f, // Back bottom-right 1.0f, 1.0f, -1.0f, // Back top-right -1.0f, 1.0f, -1.0f // Back top-left }; const GLuint indices[] = { // Front face 0, 1, 2, 2, 3, 0, // Back face 4, 5, 6, 6, 7, 4, // Left face 7, 3, 0, 0, 4, 7, // Right face 1, 5, 6, 6,