Yongwei Xing
Yongwei Xing

Reputation: 13441

Point doesn't show in OpenGL

I am trying to draw a point using OpenGL like below, but it only displays a black window. Can someone tell me what's the error?

#include "stdafx.h"

#include <gl/GL.h>
#include <gl/GLU.h>
#include <gl/glut.h>

void init(void)
{
    glClearColor(0.0,0.0,0.0,0.0);

    glColor3f(1.0,0,1.0);
    glPointSize(10);
    //glShadeModel(GL_FLAT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D (0.0,0.0,400, 150);
}

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    float pointSize = 5;
    glPointSize(10);
    glBegin(GL_POINTS); // render with points
    glVertex2i(50,40); //display a point
    glEnd();
    glFlush();
}

void reshape(int w,int h)
{
    glViewport(0,0,(GLsizei)w,(GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0,(GLdouble)w,0.0,(GLdouble)h,-1.0,1.0);
}

int _tmain(int argc, char* argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 150);
    glutCreateWindow("Draw Simple Object");
    init();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}

Upvotes: 1

Views: 2491

Answers (2)

Matt N.
Matt N.

Reputation: 1239

Could it be that the points you draw are black and the background is, too? Have you tried adding this line to the beginning of your display function:

glClearColor(1.0f, 1.0f, 1.0f, 1.0f);

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490048

The parameters you're passing to gluOrtho2d look wrong. The order is left, right, top, bottom. You've set both the left and right to 0.0. Based on your glutInitWindowSize call, I'd guess what you want is something like gluOrtho2d(0.0, 400.0, 0.0, 150.0); (or maybe gluOrtho2d(0.0, 400.0, 150.0, 0.0);) instead.

Upvotes: 1

Related Questions