OpenGL challenge, need quick feedback with keyboard input (interaction with user)

adlep

Diamond Member
Mar 25, 2001
5,287
6
81
Baiscally I have to make a program which will create a spinning cube and then, it should take the user's input from the keyboard, and depending
from the input the program should switch perspective between parallel and perspective projections...
I was wondering if the idea I have will work...:?

original function:
void myReshape(int w, int h) {
glViewport(0, 0, w, h);

/* Use a perspective view */

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(w<=h) {
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
} else {
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);
}

if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);

glMatrixMode(GL_MODELVIEW);
}

So now I am modifying it like that:

void myReshape(unsigned char key, int w, int h) { // here use the extra parameter for keyboard keys
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

switch(key) { // switch to process the keyboard keys

case GLUT_KEY_N : //user presses N
/* Use a perspective view */
if(w<=h) {
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
} else {
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);
}
break;

case GLUT_KEY_M : // user presses M
/* Use a parallel view */
if (w <= h){
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
}else{
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
}
break;

glMatrixMode(GL_MODELVIEW);
}
Will my modification work?
Help :confused:
 

adlep

Diamond Member
Mar 25, 2001
5,287
6
81
I am at work == No visual studio or any kind of compiler....:-(
But the modification might work per this tutorial
Just want to make sure if I can add an aditional parameter to myReshape function...:confused:
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Silly people using glut. Why not just to it the REAL way and code the keyboard and mouse handlers by hand?

There are a few pieces of code which are missing that I would have to see before I could definitely say it will work. Never used glut before though, looked at it a few times - too complicated for what it does. Either way, I'm assuming that the switch statement is part of your myReshape function, and it looks like it'll work, assuming you clear the viewport and rerender after you reshape it.
 

adlep

Diamond Member
Mar 25, 2001
5,287
6
81
Folks, thanks for the replies...
SunnyD:
I have to use glut, because that is the way my teacher wants it...
Anyway, if you care,here is the entire code (long read...):
#include <stdlib.h>
#include <GL/glut.h>


GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat normals[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},
{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0},
{1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};

GLfloat colors[][3] = {{0.0,0.0,0.0},{1.0,0.0,0.0},
{1.0,1.0,0.0}, {0.0,1.0,0.0}, {0.0,0.0,1.0},
{1.0,0.0,1.0}, {1.0,1.0,1.0}, {0.0,1.0,1.0}};

void polygon(int a, int b, int c , int d) {
glBegin(GL_POLYGON);
glColor3fv(colors[a]);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glColor3fv(colors[Be]);
glNormal3fv(normals[Be]);
glVertex3fv(vertices[Be]); // [Be] is actually just B <---stupid forums
glColor3fv(colors[c]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glColor3fv(colors[d]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();
}

void colorcube() {
polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);
}

static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
static GLdouble viewer[]= {0.0, 0.0, 5.0}; /* initial viewer location */

void display(void) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

/* Update viewer position in modelview matrix */
glLoadIdentity();
gluLookAt(viewer[0],viewer[1],viewer[2], 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

/* rotate cube */
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);

colorcube();

glFlush();
glutSwapBuffers();
}

void mouse(int btn, int state, int x, int y) {
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
theta[axis] += 2.0;
if( theta[axis] > 360.0 ) theta[axis] -= 360.0;
display();
}

void keys(unsigned char key, int x, int y) {

/* Use x, X, y, Y, z, and Z keys to move viewer */

if(key == 'x') viewer[0]-= 1.0;
if(key == 'X') viewer[0]+= 1.0;
if(key == 'y') viewer[1]-= 1.0;
if(key == 'Y') viewer[1]+= 1.0;
if(key == 'z') viewer[2]-= 1.0;
if(key == 'Z') viewer[2]+= 1.0;
display();
}

void myReshape(unsigned char key, int w, int h) { // here use the extra parameter for keyboard keys
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

switch(key) { // switch to process the keyboard keys

case GLUT_KEY_N : //user presses N
/* Use a perspective view */
if(w<=h) {
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) h/ (GLfloat) w,
2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
} else {
glFrustum(-2.0, 2.0, -2.0 * (GLfloat) w/ (GLfloat) h,
2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);
}
break;

case GLUT_KEY_M : // user presses M
/* Use a parallel view */
if (w <= h){
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
}else{
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);
}
break;

glMatrixMode(GL_MODELVIEW);
}

void main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("colorcube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
 

adlep

Diamond Member
Mar 25, 2001
5,287
6
81
Last one...
I will try on my own this time (back from work)
 

SunnyD

Belgian Waffler
Jan 2, 2001
32,675
146
106
www.neftastic.com
Sorry - was at work all day (non-computer related). Looks like you should be fine, but note I just skimmed over the code briefly - on the way to go see the Matrix: Revolutions.

I've found that even if teachers want it one way, and you give it to them via a technically more challenging route, it's easy to get A's in about half the time, and show up your prof too.