简体   繁体   English

OpenGL-当我在立方体上绘制边框时,会出现多余的线条

[英]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). 多维数据集可以正常工作,并且可以绘制边框,但始终会插入额外的线条。我使用GL_ARRAY_BUFFER存储数据,并从GL_TRIANGLES(绘制多维数据集本身)切换到GL_LINES(绘制轮廓边框)。 Each offset in the buffer has its own set of vertices. 缓冲区中的每个偏移量都有自己的一组顶点。

My code for the display function (GLUT): 我的显示功能代码(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. 我对立方体的每一侧使用12个三角形(每个3个顶点),并使用12条线(24个顶点)覆盖所有边缘。 The cube and edges are in sycn sitting at the center of the screen (centered at 0,0,0). 立方体和边缘以sycn的形式位于屏幕的中心(以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. 即使我在显示功能中禁用绘制立方体,也会出现多余的多余线条(似乎从左中3D位置延伸到中前3D位置)。 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. 基本上,它尝试显示颜色数据而不是实际的顶点数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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