Search code examples
opengldrawglutpoint

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

Solution

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