Exercise 4

Fra Wikipedia, den frie encyklopedi.

Innholdsfortegnelse

Bouncing Ball or Rotating Cube

Alternative 1: Bouncing Ball

We can simulate many realistic effects using computer graphics incorporating simple physics in the model. Simulate a bouncing ball in two dimensions, incorporating both gravity and elastic collisions with a surface. You can model the ball using a closed polygon that has a sufficient number of sides to look smooth.

Give different keys different functions like acceleration, deceleration (opposite of acceleration - negative acceleration that is :) and reseting the ball into the first position.

One should also be able to select the color of the ball and quitting the program by using keys or menu options.

Try to make several balls bounce on the screen at the same time maybe using the mouse putting them there.


Alternative 2: Rotating Cube

Alternatively you may draw a three dimensional cube which uses the keyboard for zooming and the mouse for rotating the cube.

The latter task let you try out 3D programming, while the first focus on animation.


Compiling OpenGL source code

Skeleton code and compile instructions for Windows, Solaris (UNIX), Mac OS-X and Linux can be found under eksempelkode (http://www.idi.ntnu.no/emner/tdt4195/kode/opengl/). Also, you can look at nehe.gamedev.net for some nice tutorials on how to program with OpenGL. GL commands (http://www.mevis.de/%7Euwe/opengl/opengl.html), and OpenGL Programming Guide (http://fly.srk.fer.hr/~unreal/theredbook/) are also very usefull.


Skeleton code

/* OpenGL skeleton code
   Compiles cleanly on Windows, Solaris, Linux and Mac OS-X */

// Include header files according to platform
#if defined _WIN32
# define WIN32_LEAN_AND_MEAN
# include <windows.h>
# include <gl\gl.h>
# include <gl\glu.h>
# include <gl\glut.h>
#elif defined __APPLE__
# include <OpenGL/gl.h>
# include <OpenGL/glu.h>
# include <GLUT/glut.h>
#else
# include <GL/gl.h>
# include <GL/glu.h>
# include <GL/glut.h>
#endif

#include <math.h>		// trigonometry support
#ifndef M_PI
# define M_PI 3.14159265358979323846
#endif
#ifndef NULL
# define NULL ((void *)(0))
#endif



/*  Display function.
 *  Renders the picture that is going to be displayed  */
void display (void) {
	//clear image- and depth buffer to get a clean canvas
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Set up viewport
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	// set up a perspective camera at point (0,0,0) and looking in negative Z-direction
	// angle of view is 90 degrees. Near and far clipping planes are 10 and 1000
	gluPerspective(90.0, 1.0, 10, 1000);

	// set up geometric transformations
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	// TODO: INSERT GEOMETRIC TRANSFORMATION CODE HERE

	//Render object(s)
	// TODO: REPLACE CODE HERE
	glBegin(GL_QUADS);
	glColor3f(1,1,0); // yellow square
	glVertex3f(-5.0f, -5.0f, -20.0f);
	glVertex3f(-5.0f,  5.0f, -20.0f);
	glVertex3f( 5.0f,  5.0f, -20.0f);
	glVertex3f( 5.0f, -5.0f, -20.0f);
	glEnd();

	// Flush renderings
	glFlush();
	// Swap on-screen/off-screen buffers (double buffering)
	glutSwapBuffers();
}


/*  Idle function,
 *  activated when the event queue is empty (OpenGL has nothing else to do)  */
void idle  (void) {
	// update position/state of objects
	// TODO: INSERT CODE HERE


	// tell OpenGL to call the display callback
	glutPostRedisplay();
}



/*  Mouse callback function
 *  x and y are the mouse coordinates when the click occured  */
void mouse (int button, int state, int x, int y) {
	if(state==GLUT_DOWN && button == GLUT_LEFT_BUTTON){
		// TODO: INSERT CODE HERE
	}
	if(state==GLUT_DOWN && button == GLUT_RIGHT_BUTTON){
		// TODO: INSERT CODE HERE
	}
}



/*  The keyboard callback function
 *  x and y are the mouse coordinates when the character was typed  */
void keyboard (unsigned char key, int x, int y) {
	switch(key){
		case 'q':
			exit(0); //Quit the program
			break;
		// TODO: INSERT CODE HERE

	}
}


/*  Main function:
 *  program entry point  */
int main (int argc, char **argv) {
	//Initializing GLUT
	glutInit(&argc, argv);
	// we want:     double buffering, rgb color and Z-buffer
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	// initialize window with size and name
	glutInitWindowSize(640, 480);
	glutCreateWindow("My OpenGL program");

	//Initialize the program
	// TODO: INSERT CODE HERE

	// set up callback functions
	glutKeyboardFunc(keyboard);
	glutMouseFunc(mouse);
	glutDisplayFunc(display);
	glutIdleFunc(idle);

	// enter main loop
	// (will cause OpenGL to begin calling the callbacks)
	glutMainLoop();

	return 0;
}


Makefile

default:
	gcc -o test gl_framework.c -lglut -lMesaGL -lMesaGLU -lXmu -lX11 -lXi -lm
run:
	./test
clean:
	rm test

Build instructions

How To Compile/Build OpenGL Programs.

Windows

Download the GLUT binary library from http://www.xmission.com/~nate/glut.html. Copy glut32.dll to C:\WINDOWS\system32 Copy glut32.lib to C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Lib Copy glut.h to C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\PlatformSDK\Include\gl Link to glut32.lib, glu32.lib and opengl32.lib when compiling (will probably be done automatically)

UNIX/Linux

Make sure Mesa and GLUT is installed Compile with the following parameters: -lglut -lMesaGL -lMesaGLU -lXmu -lX11 -lXi -lm

Example commands to compile and run: gcc -o test_app test_app.c -lglut -lMesaGL -lMesaGLU -lXmu -lX11 -lXi -lm ./program_name


Mac OS-X

Requires the parameters: -framework OpenGL -framework GLUT -framework Foundation

Example command to compile: gcc -o test_app test_app.c -framework OpenGL -framework GLUT -framework Foundation

You might also take a look at http://203.79.121.211/~keith/XcodeGLUT/ if you're developing using Xcode.

Personal tools