简体   繁体   中英

OpenGL - Extra line appears when I draw a border on a cube

I've been trying to figure this out for a while, but I'm failing. I need to draw a cube, and then add a border around it. The cube works fine, and I got the border to draw, but there's always an extra line sticking in. I use the GL_ARRAY_BUFFER to store data and switch from GL_TRIANGLES (to draw the cube itself) to GL_LINES (to draw the outline borders). Each offset in the buffer has its own set of vertices.

My code for the display function (GLUT):

// cubeLen = number of cube's vertices in buffer
// sidesLen = number of side vertices in buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, cubeLen);
glDrawArrays(GL_LINES, cubeLen, cubeLen + sidesLen);
glutSwapBuffers();

My code to set up the buffers:

GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors)
                + sizeof(points2) + sizeof(colors2),
                NULL, GL_STATIC_DRAW );

glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors),
                sizeof(points2), points2);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors) + sizeof(points2),
                sizeof(colors2), colors2);

I use 12 triangles (3 vertices each) for each side of the cube, and 12 lines (24 vertices) to cover all of the edges. The cube and edges are in sycn sitting at the center of the screen (centered at 0,0,0). The extra unwanted line (which appears to stretch from the center-left 3D position to the center-front 3D position) to comes in even if I disable drawing the cube in the display function. It does, however, go away when I do not set up vertices for the cube. Any ideas why this might be happening? Thanks for any and all help.

The extra line was a misplaced index to the buffer when it called the display function. Basically, it tried to display the color data instead of the actual vertex data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM