为什么我的 glDrawArrays 没有绘制我存储的点?

Why does my glDrawArrays is not drawing the points I have stored?

我正在尝试创建一个简单的程序,在我单击的地方绘制点。 我声明了一个 struct 来保存每个点的坐标和颜色。 我声明了一个保存点列表的全局 vector< Point >
鼠标输入被很好地记录并打印在 cout 上,但是点没有被绘制。
知道为什么我在 GLUT 的初始化中遗漏了吗?

#include <GL/freeglut_std.h>
#include <GL/gl.h>
#include <vector>
#include <iostream>
using namespace std;

struct Point
{
  float x, y;
  unsigned char r, g, b, a;
};

vector< Point > points;

void drawPoints() 
{
  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 OnMouseClick(int button, int state, int x, int y)
{
  if (button == GLUT_LEFT_BUTTON && state == GLUT_UP)
  {
    //store the x,y value where the click happened
    Point p = Point();
    p.x = (float)x;
    p.y = (float)y;
    p.r = 255;
    p.g = 255;
    p.b = 255;
    p.a = 255;
    points.push_back(p);
    for (vector<Point>::iterator it = points.begin(); it != points.end(); ++it) 
    {
       cout << it->x << " " << it->y << "   " << flush;
    }
    cout << endl;
    glutPostRedisplay();
  }
}

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

  glutInitWindowSize(500, 500);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("OpenGL - Drawing points");
  glutDisplayFunc(drawPoints);
  glutMouseFunc(OnMouseClick);
  glutMainLoop();
  return 0;
}

坐标系不匹配,要么将 GLUT 的鼠标坐标转换为您选择的 glOrhto() 坐标系,要么更改该调用以匹配 GLUT 的鼠标坐标系。

第二种方式的例子:

#include <GL/glut.h>
#include <vector>
using namespace std;

struct Point
{
    float x, y;
    unsigned char r, g, b, a;
};

vector< Point > points;

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

    glMatrixMode( GL_PROJECTION );
    glLoadIdentity();
    GLdouble w = glutGet( GLUT_WINDOW_WIDTH );
    GLdouble h = glutGet( GLUT_WINDOW_HEIGHT );
    glOrtho( 0, w, h, 0, -1, 1 );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();

    // draw
    if( !points.empty() )
    {
        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 );
    }

    glutSwapBuffers();
}

void OnMouseClick( int button, int state, int x, int y )
{
    if( button == GLUT_LEFT_BUTTON && state == GLUT_UP )
    {
        //store the x,y value where the click happened
        Point p = Point();
        p.x = (float)x;
        p.y = (float)y;
        p.r = 255;
        p.g = 255;
        p.b = 255;
        p.a = 255;
        points.push_back( p );
        glutPostRedisplay();
    }
}

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

    glutInitWindowSize( 500, 500 );
    glutInitWindowPosition( 100, 100 );
    glutCreateWindow( "OpenGL - Drawing points" );
    glutDisplayFunc( drawPoints );
    glutMouseFunc( OnMouseClick );
    glutMainLoop();
    return 0;
}