Search code examples
c++openglopengl-compat

How to use openGL draw the point?(c++)


I want to use openGL draw a Coordinate System,and the code has draw the x-axis and y-axis. However the origin point cannot be draw. How to solve the problem? I think the code is correct and search the resource in the internet. There are no solution for the debug.

Here is my code:

#define FREEGLUT_STATIC
#include <GL/freeglut.h>
void define_to_OpenGL();

void main(int argc, char** argv) {
    glutInit(&argc, argv);

    //task2
    glutInitWindowSize(600, 400);
    glutInitWindowPosition(50, 50);

    glutCreateWindow("Graphics Perimitives");

    glutDisplayFunc(define_to_OpenGL);
    glutMainLoop();
}


void define_to_OpenGL() {
    glClearColor(1, 1, 1, 1);
    glClear(GL_COLOR_BUFFER_BIT);


    //TASK 2        
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity();
    gluOrtho2D( -100, 800, -400, 400); 

    //TASK 3
    glLineWidth(1.0);
    glColor3f(0,0,0);

    glBegin(GL_LINES);
        glVertex2f(0, 0);
        glVertex2f(450, 0);
    glEnd();

    glBegin(GL_LINES);
        glVertex2f(0, -150);
        glVertex2f(0, 150);
    glEnd();


    //TASK 4
    glPointSize(100.0);
    glColor3f(0, 1, 0);
    glBegin(GL_POINT);
        glVertex2f(450, 0);
    glEnd();


    //TASK 5
    //TASK 6,7,8

    glFlush();



}


I know it's a simple problem. Please help me, Thanks!


Solution

  • GL_POINT is not a valid primitive type. The primitiv type for points is GL_POINTS. See Point primitives.
    Furthermore the point size is limited. 100.0 exceeds the limit. Reduce the point size:

    glPointSize(10.0);
    glColor3f(0, 1, 0);
    glBegin(GL_POINTS);
        glVertex2f(450, 0);
    glEnd();
    

    GL_POINT is a enumerator constant that is used to specify the polygon mode (glPolygonMode).

    The maximum point size can be get by glGetFloatv, by the parameter GL_POINT_SIZE_MAX.

    GLfloat max_point_size;
    glGetFloatv(GL_POINT_SIZE_MAX, max_point_size);