0
votes

I installed glut.h in my Codeblocks. I wrote a program that should output a red line over black background. But only he window is showing up. This code runs fine on another PC. What is the problem? Is it my own laptop’s problem?

#include<windows.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include<stdlib.h>
#include<stdio.h>
#define ROUND(x) ((int)(x+0.5))

float xa=0,xb=50,y,m,ya=0,yb=50;

void display (void)
{          
    int dx=xb-xa,dy=yb-ya,steps,k;
    float xIncrement,yIncrement,x=xa,y=ya;
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 0.0, 0.0);
    if(abs(dx)>abs(dy))
        steps=abs(dx);
    else steps=abs(dy);

    xIncrement=dx/(float)steps;
    yIncrement=dy/(float)steps;
    glBegin(GL_POINTS);
    glVertex2s(ROUND(x),ROUND(y));
    for(k=0; k<steps; k++)
    {
        x+=xIncrement;
        y+=yIncrement;
        glVertex2s(ROUND(x),ROUND(y));
    }

    glEnd();
    glFlush();
}

void init(void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glOrtho(-100.0, 100.0, -100.0, 100.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (500, 500);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("DDA algorithm");
    init ();
    glutDisplayFunc(display);
    glutMainLoop();
    return 0;
}
1

1 Answers

1
votes

The reason you're getting a blank screen, is because you're using a single buffered window, while immediately clearing the screen after drawing.

The solution is to make the window double buffered and swapping the buffers after rendering:

void display(void)
{
    // everything else

    glutSwapBuffers();
}

While also replacing:

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)

With:

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB)