Tuesday, May 24, 2011

Second Open GL Program : Displaying a Box

This is my second program. I haven't make any explanation yet, regarding the usage of OpenGL Functions.

/*
Joko Adianto
This Source Code is released under GPL
*/
#include "GL/glut.h"
int window;

/* The main drawing function. */
void drawSquare()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,-7.0f);
glRotatef(45.0f,1.0f,1.0f,1.0f);

glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();
glFlush();
}


int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_ALPHA | GLUT_DEPTH ); // GLUT_DEPT dibuang atas dan depan hilang
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Display 2D Square");
glutDisplayFunc(&drawSquare);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)640/(GLfloat)480,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}