arrays - OpenGL点数组

标签 arrays opengl graphics 3d

我有以下代码来绘制点数组,但它只在中心绘制一个点。如何使用 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();

最佳答案

需要 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;
}

Screenshot

关于arrays - OpenGL点数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7960552/

相关文章:

JavaScript 数组过滤和映射会在数组条目之间产生逗号吗?

arrays - 如何找到数组中非递减子序列的数量?

c++ - 计算与加载 OpenGL 法线

c# - 调整 GIF 大小并保存动画?

python - Tkinter:按钮打开另一个窗口(并关闭当前窗口)

javascript - 将对象数组转换为已排序的数组数组

opengl - 您能想到的在 OpenGL 显示器上临时为 'panic button' 绘制纯黑色的最简单方法是什么?

c++ - 在 reading/dev/fb0 上没有得到预期的输出

Java `DisplayMode` 在 Linux 上的位深度是 `-1`

javascript - `query[name_value[0]] = name_value[1];` 中的数组解构是什么?