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;
}

Saturday, May 21, 2011

Open GL : Create Simple Open GL Program

The simple program bellow displays a triangle consists of 3 lines. The program below is, I believe simple, to get me going to more complex OpenGL program

/*

Joko Adianto
This Source Code is released under GPL
*/
#include "GL/glut.h"
void menggambarGaris(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,0.0,0.0);
glBegin(GL_LINES);
glVertex2i(50, 50);
glVertex2i(50, 200);
glEnd();
glBegin(GL_LINES);
glVertex2i(50, 50);
glVertex2i(200, 50);
glEnd();
glBegin(GL_LINES);
glVertex2i(50, 200);
glVertex2i(200, 50);
glEnd();

glFlush();
}

int main(int argc, char** argv)
{
/*
glutInit is used to initialize the GLUT library.
glutInit will initialize the GLUT library and negotiate a session with the window system. During this process, glutInit may cause the termination of the GLUT program with an error message to the user if GLUT cannot be properly initialized. Examples of this situation include the failure to connect to the window system, the lack of window system support for OpenGL, and invalid command line options.
glutInit also processes command line options, but the specific options parse are window system dependent.
*/
glutInit(&argc, argv);

/*
glutInitDisplayMode sets the initial display mode.
GLUT_SINGLE Bit mask to select a single buffered window. This is the default if neither GLUT_DOUBLE or GLUT_SINGLE are specified.
GLUT_RGB An alias for GLUT_RGBA.
*/
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);

/*
glutInitWindowPosition and glutInitWindowSize set the initial window position and size respectively
*/
glutInitWindowSize(640, 480);
glutInitWindowPosition(300, 600);

/*
glutCreateWindow creates a top-level window
*/
glutCreateWindow("OpenGL");

/*
Specifies which matrix stack is the target
for subsequent matrix operations.
Three values are accepted:
GL_MODELVIEW,
GL_PROJECTION, and
GL_TEXTURE.
The initial value is GL_MODELVIEW.
Additionally, if the ARB_imaging extension is supported,
GL_COLOR is also accepted.
GL_PROJECTION Applies subsequent matrix operations to the projection matrix stack.
*/
glMatrixMode(GL_PROJECTION);

/*
glLoadIdentity replaces the current matrix with the identity matrix.
It is semantically equivalent to calling glLoadMatrix
with the identity matrix
*/
glLoadIdentity();

/*
gluOrtho2D — define a 2D orthographic projection matrix
left, right Specify the coordinates for the left and right vertical clipping planes.
bottom, top Specify the coordinates for the bottom and top horizontal clipping planes.
*/
/*
Orthographic projection (or orthogonal projection) is a means of representing a three-dimensional object in two dimensions. It is a form of parallel projection, where all the projection lines are orthogonal to the projection plane,[1] resulting in every plane of the scene appearing in affine transformation on the viewing surface. It is further divided into multiview orthographic projections and axonometric projections. A lens providing an orthographic projection is known as an (object-space) telecentric lens.

The term orthographic is also sometimes reserved specifically for depictions of objects where the axis or plane of the object is also parallel with the projection plane,[1] as in multiview orthographic projections.
*/
gluOrtho2D(0.0, 640.0, 0.0, 480.0);

glutDisplayFunc(menggambarGaris);
glutMainLoop();
}