PDA

View Full Version : my rotation with the z axis is flat!



fandecine
11-23-2002, 03:04 PM
Hi!
I have a project to do. I load a file of polygons with x,y and z points. I have to translate/rotate in the 3 axis. When I translate with z, it doesn't look like it going closer or further (the graphic disappears and reappears). Also, when I rotate, the graphic is sometimes flat (a simple line). I am sure it has something to do when I create the window. I would appreciate any feedback (I am here to learn from you!)
The following is my code:

#include "package.h" // glut, math, stdio...

typedef struct // Structure For 3D Points
{
float x, y, z, vx, vy, vz; // X, Y & Z Points
} VERTEX; // Called VERTEX

typedef struct // Structure For An Object
{
int verts; // Number Of Vertices For The Object
float cR, cG, cB; // RGB color
VERTEX *p; // One Vertice (Vertex x,y,z, vx, vy & vz)
} OBJECT;

static int iChoice, nbPOLY, totalPOLY;
static int FKey=0, nbDraw;
static int nbGroup,nbPolygon,nbVertice, indexColor;
static float tx,ty,tz,tvx,tvy,tvz,tcR,tcG,tcB;
static float min, max, coef;
static float xrot,yrot,zrot, // X, Y & Z Rotation
cx=0,cy=0,cz=0; // X, Y & Z Position
OBJECT POLY[6000]; // Create an Array of OBJECT
bool loaded[7];

void objallocate(OBJECT *k,int n) // Allocate Memory For Each Object
{ // And Defines points
k->p=(VERTEX*)malloc(sizeof(VERTEX)*n); // Sets points Equal To VERTEX * Number Of Vertices
}// objallocate // (3 Points For Each Vertice)

void objfree(OBJECT *k) // Frees The Object (Releasing The Memory)
{
free(k->p); // Frees Points
}// objfree

void updatePOLY(int opt,OBJECT *k, int loop){
switch(opt){
case 0:
k->verts=nbVertice;
objallocate(k,nbVertice); // Jumps To Code That Allocates Ram To Hold The Object
break;
case 1:
k->p[loop].x=tx;
k->p[loop].y=ty;
k->p[loop].z=tz;
k->p[loop].vx=tvx;
k->p[loop].vy=tvy;
k->p[loop].vz=tvz;
break;
}// switch
}// updatePOLY

void Draw(OBJECT *k){
for (int i=0; i<k->verts; i++)
{
glVertex3f((k->p[i].x*coef)+cx, (k->p[i].y*coef)+cy, (k->p[i].y*coef)+cz);
}// for

}// Draw

void readstr(FILE *f,char *string) // Reads A String From File (f)
{
do
{
fgets(string, 255, f); // Gets A String Of 255 Chars Max From f (File)
} while ((string[0] == '/') &#0124; &#0124; (string[0] == '\n'));
return;
}// readstr

void loadfile(char *name){
for (int i=0; i<nbPOLY; i++)
objfree(&POLY[i]); // FREE RAM
FILE *filein; // Filename To Open
char oneline[255];
nbGroup=0; nbPolygon=0; nbVertice=0; indexColor=0; // Reset values
tx=0; ty=0; tz=0; tvx=0; tvy=0; tvz=0; tcR=0; tcG=0; tcB=0;
totalPOLY=0; min=0; max=0; nbPOLY=0; nbDraw=0;

filein = fopen(name, "rt"); // Opens The File For Reading Text In Translated Mode

readstr(filein,oneline); // Read the c
readstr(filein,oneline);
sscanf(oneline, "%i", &nbGroup); // Number of Groups
//cout<<"Number of Groups --> "<<nbGroup<<"\n";

for (int a=0;a<nbGroup;a++)
{
readstr(filein,oneline);
sscanf(oneline, "%i", &nbPolygon); // Number of Polygons
//cout<<"Number of Polygons --> "<<nbPolygon<<"\n";
totalPOLY+=nbPolygon;
for (int b=0; b<nbPolygon; b++)
{
readstr(filein,oneline);
sscanf(oneline, "%i", &nbVertice); // Number of Vertices
//cout<<"Number of Vertices --> "<<nbVertice<<"\n";
updatePOLY(0, &POLY[nbPOLY],0);

for (int c=0; c<nbVertice; c++)
{
readstr(filein,oneline);
sscanf(oneline, "%f %f %f %f %f %f", &tx, &ty, &tz, &tvx, &tvy, &tvz);// Points
//cout<<"6 points --> "<<tx<<" "<<ty<<" "<<tz<<" "<<tvx<<" "<<tvy<<" "<<tvz<<" "<<"\n";
updatePOLY(1, &POLY[nbPOLY], c);
if (tx>max) max=tx;
if (ty>max) max=ty;
if (tx<min) min=tx;
if (ty<min) min=ty;

}// for nbVertice
nbPOLY++; // Increment the array
}// for nbPolygon

readstr(filein,oneline);// Color
sscanf(oneline, "%f %f %f", &tcR, &tcG, &tcB);
//cout<<"Color R G B --> "<<tcR<<" "<<tcG<<" "<<tcB<<"\n\n\n";
for (int d=indexColor; d<nbPOLY; d++) // Update the RGB color for each POLY up to now
{
POLY[d].cR=tcR;
POLY[d].cG=tcG;
POLY[d].cB=tcB;
}// for
indexColor=nbPOLY; // change the index of indexColor for the next group of polygons
}// for nbGroup
fclose(filein); // Close The File
cout<<"Total Polygons: "<<totalPOLY<<"\n";
cout<<"Total Created : "<<nbPOLY<<"\n";
cout<<"max :"<<max<<"\n";
cout<<"min :"<<min<<"\n";
cout<<"file closed."<<endl;
}// loadfile


void ProcessMenu(int value) {
if (loaded[value]==false){
for (int i=0; i<7; i++)
loaded[i]=false;
loaded[value]=true;
if (value==1){
loadfile("data/biplane_dat.txt");
coef=40.0;
}// if
if (value==2){
loadfile("data/cow_dat.txt");
coef=30.0;
}// if
if (value==3){
loadfile("data/apple_dat.txt");
coef=0.333;
}// if
if (value==4){
loadfile("data/skull_dat.txt");
coef=0.333;
}// if
if (value==5){
loadfile("data/x29_dat.txt");
coef=6.0;
}// if
if (value==6){
loadfile("data/beethoven_dat.txt");
coef=0.25;
}// if
if (value==7){
cx=0; cy=0; cz=0; xrot=0; yrot=0; zrot=0;
}// if
}// if
glutPostRedisplay();
}// ProcessMenu

void RenderScene(void){ // Called to draw scene
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the window
glLoadIdentity(); // Reset The View
glTranslatef(cx,cy,cz); // Translate The The Current Position To Start Drawing
glRotatef(xrot,1,0,0); // Rotate On The X Axis By xrot
glRotatef(yrot,0,1,0); // Rotate On The Y Axis By yrot
glRotatef(zrot,0,0,1); // Rotate On The Z Axis By zrot
//xrot+=10; //yrot+=yspeed; zrot+=zspeed; // Increase xrot,yrot & zrot by xspeed, yspeed & zspeed
for (int i=0; i<nbPOLY; i++){
glBegin(GL_POLYGON);
glColor3f(POLY[i].cR, POLY[i].cG, POLY[i].cB);
Draw(&POLY[i]);
glEnd();
}// for
glPopMatrix(); // Restore transformations
glutSwapBuffers(); // Flush drawing commands
}// RenderScene

void SetupRC(){
glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Black background
glColor3ub(0, 0, 255); // Set drawing color to blue
}// SetupRC

void SpecialKeys(int key, int x, int y)
{
if(key == GLUT_KEY_F1) FKey=1; // x
if(key == GLUT_KEY_F2) FKey=2; // y
if(key == GLUT_KEY_F3) FKey=3; // z
if(key == GLUT_KEY_F4) xrot+=10;
if(key == GLUT_KEY_F5) xrot-=10;
if(key == GLUT_KEY_F6) yrot+=10;
if(key == GLUT_KEY_F7) yrot-=10;
if(key == GLUT_KEY_F8) zrot+=10;
if(key == GLUT_KEY_F9) zrot-=10;

if(key == GLUT_KEY_UP){
if (FKey==1) {}
if (FKey==2) cy+=10.0;
if (FKey==3) cz+=10.0;
}// if
if(key == GLUT_KEY_DOWN){
if (FKey==1) {}
if (FKey==2) cy-=10.0;
if (FKey==3) cz-=10.0;
}// if
if(key == GLUT_KEY_LEFT){
if (FKey==1) cx-=10.0;
if (FKey==2) {}
if (FKey==3) {}
}// if
if(key == GLUT_KEY_RIGHT){
if (FKey==1) cx+=10.0;
if (FKey==2) {}
if (FKey==3) {}
}// if
glutPostRedisplay();
}// SpecialKeys

void ChangeSize(int w, int h){
GLfloat nRange = 200.0f;
// Prevent a divide by zero
if(h == 0) h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset projection matrix stack
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
// Reset Model view matrix stack
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}// ChangeSize

void processMouse(int button, int state, int x, int y) {
}// processMouse


void main(int argc, char* argv[])
{
int nMainMenu;

glutInit (&argc, argv) ;
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ;
glutInitWindowSize (300, 300) ;
glutInitWindowPosition (10, 10) ;
glutCreateWindow ("Project 3") ;

nMainMenu = glutCreateMenu(ProcessMenu);
glutAddMenuEntry("biplane",1);
glutAddMenuEntry("cow",2);
glutAddMenuEntry("apple",3);
glutAddMenuEntry("skull",4);
glutAddMenuEntry("x29",5);
glutAddMenuEntry("beethoven",6);
glutAddMenuEntry("RESET",7);

glutAttachMenu(GLUT_RIGHT_BUTTON);

glutReshapeFunc(ChangeSize);
glutSpecialFunc(SpecialKeys);
glutMouseFunc(processMouse);
glutDisplayFunc(RenderScene);
SetupRC();
glutMainLoop();
}// Main

rgpc
11-23-2002, 04:17 PM
http://www.opengl.org/discussion_boards/ubb/eek.gif

At a cursorary glance I would guess that you aren't using quaternions. This leaves you open to what is known as Gimbal Lock (a technical term that describes your rotations stuffing up http://www.opengl.org/discussion_boards/ubb/smile.gif ).

Can't say I've got around to implementing Quaternions but there are plenty of resources available through your local Google search engine...

One reasonable appearing link is.
http://www.cs.berkeley.edu/~laura/cs184/quat/quaternion.html

fandecine
11-24-2002, 02:08 PM
Thanks rgpc for taking the time to look at my problem. In fact it was very simple. If you look at my code, I put:
glVertex3f((k->p[i].x*coef)+cx, (k->p[i].y*coef)+cy, (k->p[i].y*coef)+cz);

y coordinate was put for the z!
But I really appreciated your effort.
LD