OpenGL 点数组

发布于 2024-12-12 20:07:25 字数 240 浏览 0 评论 0原文

我有以下代码来绘制一组点,但它只在中心绘制一个点。如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不顾 2024-12-19 20:07:25

需要 GLUT 进行窗口和上下文管理:

#include <GL/glut.h>
#include <vector>
#include <cstdlib>

struct Point
{
    float x, y;
    unsigned char r, g, b, a;
};
std::vector< Point > points;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw
    glColor3ub( 255, 255, 255 );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
    glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
    glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
    glPointSize( 3.0 );
    glDrawArrays( GL_POINTS, 0, points.size() );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow("Random Points");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

     // populate points
    for( size_t i = 0; i < 1000; ++i )
    {
        Point pt;
        pt.x = -50 + (rand() % 100);
        pt.y = -50 + (rand() % 100);
        pt.r = rand() % 255;
        pt.g = rand() % 255;
        pt.b = rand() % 255;
        pt.a = 255;
        points.push_back(pt);
    }    

    glutMainLoop();
    return 0;
}

屏幕截图

Requires GLUT for window and context management:

#include <GL/glut.h>
#include <vector>
#include <cstdlib>

struct Point
{
    float x, y;
    unsigned char r, g, b, a;
};
std::vector< Point > points;

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-50, 50, -50, 50, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // draw
    glColor3ub( 255, 255, 255 );
    glEnableClientState( GL_VERTEX_ARRAY );
    glEnableClientState( GL_COLOR_ARRAY );
    glVertexPointer( 2, GL_FLOAT, sizeof(Point), &points[0].x );
    glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(Point), &points[0].r );
    glPointSize( 3.0 );
    glDrawArrays( GL_POINTS, 0, points.size() );
    glDisableClientState( GL_VERTEX_ARRAY );
    glDisableClientState( GL_COLOR_ARRAY );

    glFlush();
    glutSwapBuffers();
}

void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
}

int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);

    glutInitWindowSize(640,480);
    glutCreateWindow("Random Points");

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);

     // populate points
    for( size_t i = 0; i < 1000; ++i )
    {
        Point pt;
        pt.x = -50 + (rand() % 100);
        pt.y = -50 + (rand() % 100);
        pt.r = rand() % 255;
        pt.g = rand() % 255;
        pt.b = rand() % 255;
        pt.a = 255;
        points.push_back(pt);
    }    

    glutMainLoop();
    return 0;
}

Screenshot

仙女山的月亮 2024-12-19 20:07:25

您在哪里设置 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().

人疚 2024-12-19 20:07:25

你定义了 x[i] 和 y[i] 是什么吗?否则它们将自动设置为 0(因此居中)。此外,创建具有两个元素的数组但访问 10 个元素是非常糟糕的,因为您正在访问您无法控制的内存位置。

你应该做类似的事情:

GLint NumberOfPoints = 10;
GLfloat x[10],y[10];

for(int i = 0; i < NumberOfPoints; i++){
    x[i] = y[i] = (GLfloat) i;
}

glBegin( GL_POINTS );

for ( int i = 0; i < NumberOfPoints; ++i )
{
    glVertex2f( x[i], y[i] );

}
glEnd();

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 :

GLint NumberOfPoints = 10;
GLfloat x[10],y[10];

for(int i = 0; i < NumberOfPoints; i++){
    x[i] = y[i] = (GLfloat) i;
}

glBegin( GL_POINTS );

for ( int i = 0; i < NumberOfPoints; ++i )
{
    glVertex2f( x[i], y[i] );

}
glEnd();
橘虞初梦 2024-12-19 20:07:25

你的代码就可以工作了。用随机值填充数组xy,这将绘制随机点。

问题可能是你无法“看到”你画的点。这是显而易见的,因为:

  1. 默认情况下,点的颜色为黑色(0, 0, 0,rgb),您可能需要使用 glColor3f 或此类函数将其设置为其他值.

  2. 您绘制的点太少,并且每个点都太小(实际上屏幕上只有 1 个像素大小)。您可能想要绘制圆圈,或者绘制数千个或更多像素并再次检查。

顺便说一下,请格式化你的问题并让代码正常显示。


已编辑:

请参阅上面我的评论。如果您没有设置有效的 OpenGL 上下文或者您只是不知道如何设置,请检查此 http://openglbook.com/the-book/chapter-1-getting-started/ 并开始使用您的第一个工作 OpenGL 程序。

Your code just works. Fill the array x and y 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:

  1. 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.

  2. 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文