OpenGL / GLUT example program

This is a simple OpenGL example program using the GLUT toolkit for window creation and event management.

Image

This is the code used to create the above window:

//
// shape.c - OpenGL / GLUT example program
// Written by Ted Burke - 14-2-2012
//
// To compile: gcc -o shape shape.c -lglut
//

#include <GL/glut.h>
#include <stdio.h>

void renderScene(void);

int main(int argc, char** argv)
{
	int w;

	printf("OpenGL / GLUT example program\n");

	glutInit(&argc, argv);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 300);
	glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
	w = glutCreateWindow("OpenGL / GLUT Example Program");

	glutDisplayFunc(renderScene);

	glutMainLoop();

	return 0;
}

void renderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
	glColor3f(1.0, 0.0, 0.0);
	glVertex3f(-0.5, -0.5, 0.0);
	glColor3f(0.0, 1.0, 0.0);
	glVertex3f(+0.5, -0.5, 0.0);
	glColor3f(0.0, 0.0, 1.0);
	glVertex3f(-0.5, +0.5, 0.0);
	glEnd();

	glutSwapBuffers();
}

To compile this program with gcc, I used the following command:

gcc -o shape shape.c -lglut

I installed GLUT and OpenGL on my CrunchBang Linux netbook using the following command:

sudo apt-get install libglut3-dev
Advertisements
This entry was posted in Uncategorized and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s