PDA

View Full Version : glTraslate, glRotate, a cube and some steps

tirengarfio
02-25-2007, 11:10 PM
Hello,

the code below gives 12 positions and 12 angle rotations on the Y axis to a cube.

The positions and the angles are inside of two matrices.

When you push the key 'b' you go to next position and angle.

My problem is at the 7th step: the window doesnt show the view it should and I cannot understand why.

#include <stdafx.h>
#include <glut.h>
#include <math.h>

#include <iostream>
using namespace std;

double m=5;
double n=6;
double o=10;
double b=m/n;
double c=2*b;
double f=3*b;
int i=0;
double p=2;
double q=12;
double d1=(m*sqrt(p))/q;

double array[12][3]={ 0, 0, 0, //posicion0
0, 0, 0, //1
0, 0, b, //2
0, 0, c, //3
0, 0, c, //4
-d1, 0, c+d1, //5
-2*d1, 0, c+d1*2, //6
-2*d1, 0, c+d1*2, //7 -> //THIS IS THE STEP I CANT UNDERStand
-d1, 0, c+d1*3, //8
0, 0, c+d1*4, //9
0, 0, c+d1*4, //10
0, 0, f+d1*4}; //11

float xpos = 0, ypos = 0, zpos = 0, xrot = 0, yrot = 0, angle=0.0;

double array2[12]={ 0, //posicion inicial
0, //1
0,
0,
-45, //4
-45,
-45,
45, //7 -> THIS IS THE STEP I CANT NOT UNDERSTAND
45, //8
45,
0,
0, //11
};

void init (void) {

}

void enable (void) {
glEnable (GL_DEPTH_TEST); //enable the depth testing
//dglEnable (GL_LIGHTING); //enable the lighting
glEnable (GL_LIGHT0); //enable LIGHT0, our Diffuse Light
}

void camera (void) {

glTranslated(-array[i][0],-array[i][1],-array[i][2]);

glRotatef(array2[i], 0.0, 1.0, 0.0);

}

void display (void) {
glClearColor (0.0,0.0,0.0,1.0); //clear the screen to black
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //clear the color buffer and the depth buffer
gluLookAt (0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0); //camera position, x,y,z, looking at x,y,z, Up Positions of the camera
camera();
enable();

glColor3f(0.0f,1.0f,0.0f); // Color Blue
glVertex3f( 2.5f, 2.5f, 0.0f); // Top Right Of The Quad (Top)
glVertex3f(-2.5f, 2.5f, 0.0f); // Top Left Of The Quad (Top)
glVertex3f(-2.5f, 2.5f, 5.0f); // Bottom Left Of The Quad (Top)
glVertex3f( 2.5f, 2.5f, 5.0f); // Bottom Right Of The Quad (Top)

glColor3f(1.0f,0.5f,0.0f); // Color Orange
glVertex3f( 2.5f,-2.5f, 5.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-2.5f,-2.5f, 5.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-2.5f,-2.5f, 0.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f( 2.5f,-2.5f, 0.0f); // Bottom Right Of The Quad (Bottom)

glColor3f(1.0f,0.0f,0.0f); // Color Red
glVertex3f( 2.5f, 2.5f, 5.0f); // Top Right Of The Quad (Front)
glVertex3f(-2.5f, 2.5f, 5.0f); // Top Left Of The Quad (Front)
glVertex3f(-2.5f,-2.5f, 5.0f); // Bottom Left Of The Quad (Front)
glVertex3f( 2.5f,-2.5f, 5.0f); // Bottom Right Of The Quad (Front)

glColor3f(1.0f,1.0f,0.0f); // Color Yellow
glVertex3f( 2.5f,-2.5f, 0.0f); // Top Right Of The Quad (Back)
glVertex3f(-2.5f,-2.5f, 0.0f); // Top Left Of The Quad (Back)
glVertex3f(-2.5f, 2.5f, 0.0f); // Bottom Left Of The Quad (Back)
glVertex3f( 2.5f, 2.5f, 0.0f); // Bottom Right Of The Quad (Back)

glColor3f(0.0f,0.0f,1.0f); // Color Blue
glVertex3f(-2.5f, 2.5f, 5.0f); // Top Right Of The Quad (Left)
glVertex3f(-2.5f, 2.5f, 0.0f); // Top Left Of The Quad (Left)
glVertex3f(-2.5f,-2.5f, 0.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-2.5f,-2.5f, 5.0f); // Bottom Right Of The Quad (Left)

glColor3f(1.0f,0.0f,1.0f); // Color Violet
glVertex3f( 2.5f, 2.5f, 0.0f); // Top Right Of The Quad (Right)
glVertex3f( 2.5f, 2.5f, 5.0f); // Top Left Of The Quad (Right)
glVertex3f( 2.5f,-2.5f, 5.0f); // Bottom Left Of The Quad (Right)
glVertex3f( 2.5f,-2.5f, 0.0f); // Bottom Right Of The Quad (Right)

glEnd();

glutSwapBuffers(); //swap the buffers
angle++; //increase the angle
}

void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h); //set the viewport to the current window specifications
glMatrixMode (GL_PROJECTION); //set the matrix to projection
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0); //set the perspective (angle of sight, width, height, , depth)
glMatrixMode (GL_MODELVIEW); //set the matrix back to model
}

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

if (key=='b')
{

cout << "Posicion" << i+1 << endl;
cout << array[i+1][0] << endl << array[i+1][1] << endl << array[i+1][2] << endl;
cout << array2[i+1] << endl << endl;
i++;
}

}

int main (int argc, char **argv) {
glutInit (&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH); //set the display to Double buffer, with depth
glutInitWindowSize (500, 500); //set the window size
glutInitWindowPosition (100, 100); //set the position of the window
glutCreateWindow ("A basic OpenGL Window"); //the caption of the window
init (); //call the init function
glutDisplayFunc (display); //use the display function to draw everything
glutIdleFunc (display); //update any variables in display, display can be changed to anyhing, as long as you move the variables to be updated, in this case, angle++;
glutReshapeFunc (reshape); //reshape the window accordingly
glutKeyboardFunc (keyboard); //check the keyboard
glutMainLoop (); //call the main loop
return 0;
}

dorbie
03-03-2007, 08:14 PM
You don't describe what you see or what you expect to see.

You may want to interpolate the positions and rotations for a few frames just so you know what is going on.

An alternative would be to place the same transformation on the modelview (inverse or transpose) draw an arrow and view it all from a birdseye vantage point.