OpenGL 点数组
我有以下代码来绘制一组点,但它只在中心绘制一个点。如何使用 OpenGL 绘制 2D 点数组?
GLint NumberOfPoints = 10;
GLfloat x[2],y[2];
glBegin( GL_POINTS );
for ( int i = 0; i < NumberOfPoints; ++i )
{
glVertex2f( x[i], y[i] );
}
glEnd();
I have the following code to draw an array of points but it only draws one point in the center. How can I draw an array of 2D points using OpenGL?
GLint NumberOfPoints = 10;
GLfloat x[2],y[2];
glBegin( GL_POINTS );
for ( int i = 0; i < NumberOfPoints; ++i )
{
glVertex2f( x[i], y[i] );
}
glEnd();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
需要 GLUT 进行窗口和上下文管理:
Requires GLUT for window and context management:
您在哪里设置 x[0]、x[1]、y[0] 和 y[1] 的值?
如果只在中心绘制一个点,听起来这四个变量的值都设置为 0。在调用 gVertex2f() 中引用它们之前,请务必初始化它们的值。
Where are you setting the values for x[0], x[1], y[0], and y[1]?
If it's only drawing one point in the center, it sounds like the values are set to 0 for all four of those variables. Be sure to initialize their values before you reference them in your call to gVertex2f().
你定义了 x[i] 和 y[i] 是什么吗?否则它们将自动设置为 0(因此居中)。此外,创建具有两个元素的数组但访问 10 个元素是非常糟糕的,因为您正在访问您无法控制的内存位置。
你应该做类似的事情:
Do you define what x[i] and y[i] are? Otherwise they will be set to 0 automatically (hence the centering). Also, creating the arrays with two elements but accessing 10 elements is very bad since you are accessing memory locations that you do not have control over.
You should do something like :
你的代码就可以工作了。用随机值填充数组
x
和y
,这将绘制随机点。问题可能是你无法“看到”你画的点。这是显而易见的,因为:
默认情况下,点的颜色为黑色(0, 0, 0,rgb),您可能需要使用
glColor3f
或此类函数将其设置为其他值.您绘制的点太少,并且每个点都太小(实际上屏幕上只有 1 个像素大小)。您可能想要绘制圆圈,或者绘制数千个或更多像素并再次检查。
顺便说一下,请格式化你的问题并让代码正常显示。
已编辑:
请参阅上面我的评论。如果您没有设置有效的 OpenGL 上下文或者您只是不知道如何设置,请检查此 http://openglbook.com/the-book/chapter-1-getting-started/ 并开始使用您的第一个工作 OpenGL 程序。
Your code just works. Fill the array
x
andy
with randomized values and this would draw random points.The problem may be you can't 'see' the points you draw. That's obvious since:
By default the color of the points is black( 0, 0, 0, in rgb), and you may want to set it to some other value using
glColor3f
or such functions.You've drawn too few points and each point is too small(actually only 1 pixel size on your screen). You may want to draw circles instead or draw thousand or more pixels and check again.
By the way, please format your question and let the code displayed normally.
Edited:
See my comment above. If you didn't set up a valid OpenGL context or you just didn't know how, check this http://openglbook.com/the-book/chapter-1-getting-started/ and get started with your first working OpenGL program.