Bryan Yeo
Bryan Yeo

Reputation: 21

OpenGL Glut Drawing Points Not Working

Can someone help me out in checking my code? I'm trying to draw a simple dot by clicking in the window, but I can't see any points drawn. The point recording system is copied as an example from one of the stack overflow posts.

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

Upvotes: 1

Views: 10977

Answers (2)

Bryan Yeo
Bryan Yeo

Reputation: 21

Found out that glcolor3f and glpointsize had to be initialized first in my Init() function.

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

Upvotes: 1

genpfault
genpfault

Reputation: 52164

    glBegin(GL_POINTS);
        glColor3f(0.3, 0.3, 0.3);
        glPointSize(5.0f);  // wat
        glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
    glEnd();    

There's a short list of GL functions you can call inside a glBegin()/glEnd() pair and glPointSize() is not on it:

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.

Move the glPointSize() outside the glBegin()/glEnd() pair:

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

All together:

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

Upvotes: 2

Related Questions