简体   繁体   English

具有不同形状的 OpenGL glDrawArrays

[英]OpenGL glDrawArrays with different shapes

For an assignment I need to write a program that on the first click will draw a point, on the second click will draw a line and on the third click will make the triangle.对于作业,我需要编写一个程序,第一次单击时将绘制一个点,第二次单击时将绘制一条线,第三次单击时将绘制三角形。 All of this is done in OpenGL.所有这些都是在 OpenGL 中完成的。

I don't need any code, but as I'm brand new to all of this I'm having a difficult time understanding how to send this to the GPU with glDrawArrays(), as the function takes in a mode .我不需要任何代码,但由于我对所有这些都是全新的,我很难理解如何使用 glDrawArrays() 将其发送到 GPU,因为该函数采用mode I know I want to keep appending the points to an array - but as the mode keeps changing (GL_POINTS, GL_LINE_LOOP, GL_TRIANGLES) I don't know how to store it.我知道我想继续将点附加到数组 - 但随着模式不断变化(GL_POINTS、GL_LINE_LOOP、GL_TRIANGLES),我不知道如何存储它。

Any and all conceptual help would be really appreciated.任何和所有概念上的帮助都将不胜感激。

You said you don't want code, but honestly I could try and give a fluffy explanation with lots of words, or I could just explain it concisely in code.你说你不想要代码,但老实说,我可以尝试用很多词来做一个蓬松的解释,或者我可以用代码简洁地解释它。

Let's say you have N vertices in your vertex buffer.假设您的顶点缓冲区中有N个顶点。 It seems like you want something like this:看起来你想要这样的东西:

int num_extra_verts = N % 3;
int num_tri_verts = N - num_extra_verts;

// Draw triangles
if (num_tri_verts > 0)
    glDrawArrays(GL_TRIANGLES, 0, num_tri_verts);

// Draw point or line
if (num_extra_verts == 1)
    glDrawArrays(GL_POINTS, num_tri_verts, 1);
else if (num_extra_verts == 2)
    glDrawArrays(GL_LINES, num_tri_verts, 2);  // GL_LINE_LOOP not needed for single line

Provided you keep adding new points to the end of your vertex buffer, this will draw all the triangles created so far, plus any single point or line for the most recent not-yet-whole-triangle.如果您不断向顶点缓冲区的末尾添加新点,这将绘制迄今为止创建的所有三角形,以及最近尚未完整三角形的任何单个点或线。

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

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