OpenGL Glut 绘图点不起作用

OpenGL Glut Drawing Points Not Working

有人可以帮我检查我的代码吗?我试图通过单击 window 来绘制一个简单的点,但我看不到任何绘制的点。点记录系统是从堆栈溢出帖子之一复制的示例。

#include <stdlib.h>
#include <stdio.h>
//#include <GL\glew.h>
#include <gl/glut.h>
//#include <GL\freeglut.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "draw.h"

//DRAW draw2;

class Point
{
    int Xvalue, Yvalue;
    std::string Text;
public:
    void xy(int x, int y)
    {
        Xvalue = x;
        Yvalue = y;
    }

    void text(std::string text)
    {
        Text = text;
    }

    //return individual x y value
    int x() { return Xvalue; }
    int y() { return Yvalue; }

    //return text value
    std::string rtext() { return Text; }    
};

Point point[30];
int count = 0;


void Init()
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    //glColor3f(0.0, 0.0, 0.0);
    //glPointSize(5);

    glMatrixMode(GL_PROJECTION);    //coordinate system
    //glLoadIdentity();
    gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}


void Display()
{
    glClear(GL_COLOR_BUFFER_BIT); // clear display window
    glColor3f(1.0, 1.0, 1.0);
    //glLoadIdentity();

    /*Drawing here*/
    //redraw 
    for (int i = 0; i < count; i++)
    {
        int x = point[i].x();
        int y = point[i].y();

        //draw2.Dot(x, y);
        std::cout << "drawing dot " << x << " " << y << std::endl;
        glBegin(GL_POINTS);
            glColor3f(0.3, 0.3, 0.3);
            glPointSize(5.0f);
            glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
        glEnd();    
    }
    glFlush();
}


void mouse(int button, int state, int x, int y)
{       
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {       
        point[count].xy(x, y);
        //draw2.Dot(x, y);
        count++;    
    }    
    glutPostRedisplay();
}


int main(int argc, char *argv[])
{
    glutInit(&argc, argv);                          //initialize toolkit
                                                    //Request double buffered true color window with Z-buffer
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );   //display mode
    glutInitWindowSize(1200, 800);                  //set window size
    glutCreateWindow("NURBS Curve");                //open screen window

    Init(); //additional initializations

    //CALLBACK FUNCTIONS
    glutMouseFunc(mouse);
    glutDisplayFunc(Display);   //send graphics to display window    
    /*
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        fprintf(stderr, "GLEW error");
        return 1;
    }
    */    
    //pass control to glut for events, loops
    glutMainLoop();
    return 0;
}
    glBegin(GL_POINTS);
        glColor3f(0.3, 0.3, 0.3);
        glPointSize(5.0f);  // wat
        glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
    glEnd();    

有一个简短的 GL 函数列表,您可以在 glBegin()/glEnd() 对中调用,但 glPointSize() 不在其中:

Only a subset of GL commands can be used between glBegin and glEnd. The commands are glVertex, glColor, glSecondaryColor, glIndex, glNormal, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial, and glEdgeFlag. Also, it is acceptable to use glCallList or glCallLists to execute display lists that include only the preceding commands. If any other GL command is executed between glBegin and glEnd, the error flag is set and the command is ignored.

glPointSize() 移到 glBegin()/glEnd() 对之外:

glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
    int x = point[i].x();
    int y = point[i].y();

    glColor3f(0.3, 0.3, 0.3);
    glVertex2i(x, h - y);
}
glEnd();  

总计:

#include <gl/glut.h>

class Point
{
    int Xvalue, Yvalue;
public:
    void xy(int x, int y)
    {
        Xvalue = x;
        Yvalue = y;
    }

    //return individual x y value
    int x() { return Xvalue; }
    int y() { return Yvalue; }
};

Point point[30];
int count = 0;

void Display()
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT); // clear display window

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    const double w = glutGet( GLUT_WINDOW_WIDTH );
    const double h = glutGet( GLUT_WINDOW_HEIGHT );
    gluOrtho2D(0.0, w, 0.0, h);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glColor3f(1.0, 1.0, 1.0);

    glPointSize(5.0f);
    glBegin(GL_POINTS);
    for (int i = 0; i < count; i++)
    {
        int x = point[i].x();
        int y = point[i].y();

        glColor3f(0.3, 0.3, 0.3);
        glVertex2i(x, h - y);
    }
    glEnd();    

    glFlush();
}

void mouse(int button, int state, int x, int y)
{       
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {       
        point[count].xy(x, y);
        count++;    
    }    
    glutPostRedisplay();
}

int main( int argc, char *argv[] )
{
    glutInit(&argc, argv);                       
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
    glutInitWindowSize(1200, 800);               
    glutCreateWindow("NURBS Curve");             

    glutMouseFunc(mouse);
    glutDisplayFunc(Display);

    glutMainLoop();
    return 0;
}

发现必须先在我的 Init() 函数中初始化 glcolor3f 和 glpointsize。

void Init()
{
    glClearColor(1.0, 1.0, 1.0, 0.0);
    glColor3f(0.0, 0.0, 0.0);   //this one
    glPointSize(5);             //and this one

    glMatrixMode(GL_PROJECTION);    //coordinate system
    //glLoadIdentity();
    gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}