Handout 31

Using gluOrtho2D and glFrustum Together*


The example below illustrantes using gluOrtho2D and glFrustum to create a control panel on-screen.



/* based on an implementation from S. Dennis, April, 2003 */
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>   
#include <stdlib.h>  
#include "PushButton.h"

	int RadioButton=0, windowWidth = 500, windowHeight = 500;

	/* lighting properities */
	GLfloat light_ambient[] = {0.0f, 0.5f, 0.0f, 1.0f};  
	GLfloat light_diffuse[] = {0.5f, 0.5f, 0.5f, 0.6f};  
	GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 1.0f};  
	GLfloat light_position[] = {-3.0f, 2.0f, 1.0f, 0.0f};

	/* material properities */
	GLfloat mat_ambient[] = {0.3f, 0.3f, 0.3f, 0.0f};
	GLfloat mat_diffuse[] = {0.5f, 0.1f, 0.5f, 0.0f};
	GLfloat mat_specular[] = {0.5f, 0.1f, 0.5f, 0.0f};
	GLfloat mat_emission[] = {0.0f, 0.1f, 0.0f, 0.0f};
	GLfloat shiness[] = {20.0f};

	/* define the radio buttons (round) */
	PushButton Button0(70,  50, 50, "Zero", 0.5f, 1.0f, 0.0f);
	PushButton Button1(140, 50, 50, "One",  0.5f, 1.0f, 0.0f);
	PushButton Button2(210, 50, 50, "Two",  0.5f, 1.0f, 0.0f);
	PushButton Button3(280, 50, 50, "Three",  0.5f, 1.0f, 0.0f);
	PushButton Button4(350, 50, 50, "Four",  0.5f, 1.0f, 0.0f);
	PushButton Button5(420, 50, 50, "Five",  0.5f, 1.0f, 0.0f);

void resizeWindow(int h, int w) {
	/* force the window to be the same size */
	glutReshapeWindow(windowWidth, windowHeight);
}

void init() {
	/* set light source parameters  */
	glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);

	/* specify material parameters for the lighting model  */
	glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission );
	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, shiness);
}

void display() {

	GLfloat model_ambient[] = { 1.0, 1.0, 1.0, 0.0};

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

	/* start 3D mode */
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	glShadeModel(GL_SMOOTH);

	/* specify the ambient RGBA intensity of the entire scene */
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, model_ambient);

	glMatrixMode(GL_PROJECTION); /* determines which matrix will be modified */
    glLoadIdentity(); /* clear the currently modifiable matrix for transformation commands */
	glFrustum (-0.5f, 0.5f, -0.5f, 0.5f, 1.0f, 100.0);
	gluLookAt(8.0f, 5.0f, 8.0f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,0.0f);

	glMatrixMode(GL_MODELVIEW); /* determines which matrix will be modified */

    glPushMatrix(); /* pushes all matrices in the current stack down one level */
		/* set the cone color */
		switch(RadioButton){
			case 0: glColor3f(0.4f, 0.4f, 0.4f); break;
			case 1: glColor3f(0.8f, 0.0f, 0.2f); break;
			case 2: glColor3f(0.6f, 0.0f, 0.4f); break;
			case 3: glColor3f(0.4f, 0.0f, 0.6f); break;
			case 4: glColor3f(0.2f, 0.2f, 0.8f); break;
			case 5: glColor3f(0.1f, 0.0f, 0.6f); break;
		}
		/* draw the cone */
		glRotatef(-90.0, 1.0, 0.0, 0.0);
		glutSolidCone(3.0, 5.0, 25, 25);
	glPopMatrix(); /* pops the top matrix off the stack */

    glPushMatrix(); /* pushes all matrices in the current stack down one level */
		glBegin(GL_LINES);
			glColor3f(1.0f, 0.8f, 0.0f);
			// grid on the x/z plane
			for(int i = 0; i < 10; i++){
				glVertex3i(-10, 0, i); glVertex3i(10, 0, i);
				glVertex3i(-10, 0, -i); glVertex3i(10, 0, -i);
				//other way
				glVertex3i(i, 0, -10); glVertex3i(i, 0, 10);
				glVertex3i(-i, 0, -10); glVertex3i(-i, 0, 10);
			}
		glEnd();
    glPopMatrix(); /* pops the top matrix off the stack */

    glPushMatrix(); /* pushes all matrices in the current stack down one level */
		/* 2D mode */
		glDisable(GL_LIGHTING);
		glDisable(GL_LIGHT0);
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_COLOR_MATERIAL);

		glMatrixMode(GL_PROJECTION);
		glLoadIdentity();
		gluOrtho2D(0.0, 500.0, 0.0, 500.0);
		glMatrixMode(GL_MODELVIEW);

		/* draw radio button panel */
		glBegin(GL_POLYGON);
			glColor3f( 0.5f, 1.0f, 0.0f);
			glVertex2i(20, 20);
			glVertex2i(20, 80);
			glVertex2i(480,80);
			glVertex2i(480,20);
		glEnd();

		/* set the buttons */
		if(RadioButton==0)	Button0.Depress(); else Button0.Draw();
		if(RadioButton==1)	Button1.Depress(); else Button1.Draw();
		if(RadioButton==2)	Button2.Depress(); else Button2.Draw();
		if(RadioButton==3)	Button3.Depress(); else Button3.Draw();
		if(RadioButton==4)	Button4.Depress(); else Button4.Draw();
		if(RadioButton==5)	Button5.Depress(); else Button5.Draw();
    glPopMatrix(); /* pops the top matrix off the stack */

	glutSwapBuffers(); /* display */
}

void MouseFunction(int mouse_btn, int state, int x, int y) {
	/* determine who was clicked */
	if (mouse_btn == GLUT_LEFT_BUTTON && state == GLUT_UP) {
		if(Button0.WasClicked(x, y)) RadioButton=0;    
		if(Button1.WasClicked(x, y)) RadioButton=1;
		if(Button2.WasClicked(x, y)) RadioButton=2;    
		if(Button3.WasClicked(x, y)) RadioButton=3;
		if(Button4.WasClicked(x, y)) RadioButton=4;
		if(Button5.WasClicked(x, y)) RadioButton=5;
	}
	glutPostRedisplay(); /* now redisplay */
} 

void main(int argc, char** argv) {
        glutInit(&argc,argv);
        glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
        glutInitWindowSize(windowWidth, windowHeight); 
        glutInitWindowPosition(0,0); /* place window top left on display */
        glutCreateWindow("Ortho and Frustrum Example"); /* window title */
		init();
		glutMouseFunc(MouseFunction);
		glutReshapeFunc(resizeWindow);
        glutDisplayFunc(display); /* display callback invoked when window opened */
        glutMainLoop(); /* enter event loop */
}

hand31.cpp PushButton.cpp PushButton.h


* based on an implementation by S. Dennis and an example from here.