Drawing A Yellow Rectangle

C, OpenGL, GLUT on Linux and Windows

This solution requires a C compiler with OpenGL and GLUT headers and libraries; i have been using FreeGLUT.

The program has been compiled with GCC using the following commandline:

gcc -Wall -Werror -pedantic -o rectanGLe rectanGLe.c -lGL -lglut

The program has been tested using Visual C++ 2017 on Windows 10 with an OpenGL-capable PCIe graphics card as well as GCC on GNU/Linux with an X server supporting OpenGL version 2.1.

Known restriction: The program performs display in windowed mode, see also the note below.

/* rectanGLe.c - Draw a yellow rectangle using OpenGL. */
/* Released into the Public Domain on Mar 11 2018 by Tilman Kranz . */

#include<GL/glut.h>

#define WIDTH 640
#define HEIGHT 480

void handleKeypress(unsigned char key, int x, int y) {
    exit(0);
}

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1,1,0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glBegin(GL_QUADS);
    glVertex2f(10.0, 20.0);
    glVertex2f(100.0, 20.0);
    glVertex2f(100.0, 75.0);
    glVertex2f(10.0, 75.0);
    glEnd();

   glutSwapBuffers();
}

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

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize(WIDTH, HEIGHT);

    glutCreateWindow("Drawing a yellow rectangle.");

    glViewport(0, 0, WIDTH, HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WIDTH, HEIGHT, 0, -1, 1);

    glutDisplayFunc(drawScene);
    glutKeyboardFunc(handleKeypress);

    glutMainLoop();

    return 1;
}

Note:

Instead of glutCreateWindow(), this alternative was successfully tested on GNU/Linux with Xorg but did not work in Windows 10:

    // glutCreateWindow("Drawing a yellow rectangle.");
    glutGameModeString("640x480:8");
    glutEnterGameMode();
Imprint RSS