In this program I start to put a key interaction. The function that provide such functionality is void captureKeyPressed(unsigned char key, int x, int y). The program can zoom in or zoom out box by pressing = and - keys. The program is listed below :
/*
Joko Adianto
This Source Code is released under GPL
*/
#include "GL/freeglut.h"
int window;
float r;
float ztranslation;
void drawBox()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.0f,0.0f,ztranslation);
glRotatef(r,1.0f,1.0f,1.0f);
//Front
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f, 0.0f);
glEnd();
//Back
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.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();
//Left
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
//Right
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f, 0.0f);
glEnd();
//Top
glBegin(GL_QUADS);
glColor3f(0.33f,0.33f,0.34f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(-1.0f,1.0f, 0.0f);
glEnd();
//Top
glBegin(GL_QUADS);
glColor3f(0.33f,0.33f,0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd(); r = r +0.2f;
glutSwapBuffers();
}
void captureKeyPressed(unsigned char key, int x, int y)
{
if (key == 27) /*ASCII code value for escape key*/
{
glutDestroyWindow(window);
exit(0);
}
if (key == 61) /*ASCII code value for = key*/
{
ztranslation++;
}
if (key == 45) /*ASCII code value for - key*/
{
ztranslation--;
}
}
int main(int argc, char **argv)
{
ztranslation = -50.0f;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH );
glutInitWindowSize(640, 640);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Display a rotating box : zoom in and out");
glutDisplayFunc(&drawBox);
glutIdleFunc(&drawBox);
glutKeyboardFunc(&captureKeyPressed);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f,(GLfloat)640/640,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}
Showing posts with label OpenGL. Show all posts
Showing posts with label OpenGL. Show all posts
Friday, June 10, 2011
Third Open GL Program : Drawing a Rotating Box
This is my third open GL Program. This program draw a boxe and rotate it. This is my stepping stone to creating a space invader like or a brick link game.
This program uses Idle function to animate the boxe. To run it :
1. Copy the programs below, paste it into a blank gedit text document
2. Save it with the name DrawARotatingBox.c
3. Compile it : gcc -s DrawARotatingBox.c -o drab -lglut -lGLU
The source code for this program is listed below :
/*
Joko Adianto
This Source Code is released under GPL
*/
#include "GL/freeglut.h"
int window;
float r;
void drawBox()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.0f,0.0f,-50.0f);
glRotatef(r,1.0f,1.0f,1.0f);
//Front
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f, 0.0f);
glEnd();
//Back
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.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();
//Left
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
//Right
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f, 0.0f);
glEnd();
//Top
glBegin(GL_QUADS);
glColor3f(0.33f,0.33f,0.34f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(-1.0f,1.0f, 0.0f);
glEnd();
//Top
glBegin(GL_QUADS);
glColor3f(0.33f,0.33f,0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd(); r = r +0.2f;
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH );
glutInitWindowSize(640, 640);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Display a Rotating Box");
glutDisplayFunc(&drawBox);
glutIdleFunc(&drawBox);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f,(GLfloat)640/640,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}
This program uses Idle function to animate the boxe. To run it :
1. Copy the programs below, paste it into a blank gedit text document
2. Save it with the name DrawARotatingBox.c
3. Compile it : gcc -s DrawARotatingBox.c -o drab -lglut -lGLU
The source code for this program is listed below :
/*
Joko Adianto
This Source Code is released under GPL
*/
#include "GL/freeglut.h"
int window;
float r;
void drawBox()
{
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.0f,0.0f,-50.0f);
glRotatef(r,1.0f,1.0f,1.0f);
//Front
glBegin(GL_QUADS);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f, 0.0f);
glEnd();
//Back
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.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();
//Left
glBegin(GL_QUADS);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd();
//Right
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f,-1.0f,-1.0f);
glVertex3f(1.0f,-1.0f, 0.0f);
glEnd();
//Top
glBegin(GL_QUADS);
glColor3f(0.33f,0.33f,0.34f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(-1.0f,1.0f, 0.0f);
glEnd();
//Top
glBegin(GL_QUADS);
glColor3f(0.33f,0.33f,0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glEnd(); r = r +0.2f;
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH );
glutInitWindowSize(640, 640);
glutInitWindowPosition(100, 100);
window = glutCreateWindow("Display a Rotating Box");
glutDisplayFunc(&drawBox);
glutIdleFunc(&drawBox);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f,(GLfloat)640/640,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glutMainLoop();
return 0;
}
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;
}
/*
Joko Adianto
This Source Code is released under GPL
*/
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();
}
/*
Joko Adianto
This Source Code is released under GPL
*/
#include
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();
}
Subscribe to:
Posts (Atom)
