【问题标题】:OpenGL Glut Drawing Points Not WorkingOpenGL Glut 绘图点不起作用
【发布时间】:2016-07-19 07:35:45
【问题描述】:

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

#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;
}

【问题讨论】:

    标签: opengl draw glut point


    【解决方案1】:
        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() 不在其中:

    在 glBegin 和 glEnd 之间只能使用 GL 命令的子集。 命令是 gl顶点, glColor, glSecondaryColor, 总索引, gl正常, glFogCoord, glTexCoord, glMultiTexCoord, glVertexAttrib, glEvalCoord, glEvalPoint, glArrayElement, glMaterial 和 glEdgeFlag。 还, 可以使用 glCallList 或 要执行的 glCallLists 显示仅包含上述命令的列表。 如果在 glBegin 和 glEnd 之间执行了任何其他 GL 命令, 设置错误标志并忽略该命令。

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

    【讨论】:

    • 真的吗?好的,我会移动它,但这不是它无法绘制点/点的原因。
    【解决方案2】:

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

    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);
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 2011-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多