PDA

View Full Version : help!!!



Gabo
02-02-2004, 09:00 AM
I am writing a program that takes input from the keyboard(coordinates of vertices). I have to display the polygon using glfrustum but i am not able to calculate the values to call that function. i have tried finding the max and min x, y,z values but all i get is a big scare covering my whole screen
please help me i have spent days trying to find this out

[This message has been edited by Gabo (edited 02-02-2004).]

M/\dm/\n
02-02-2004, 11:28 AM
Code please...

Gabo
02-02-2004, 11:49 AM
Here is the code: thank you for your help


#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>


#define LINE_LENGTH 100

/*

STRUCTS


*/
typedef struct{

GLfloat x_comp;
GLfloat y_comp;
GLfloat z_comp;
} VERTEX;

typedef struct{

GLint num_vertex_perside; //number of vertices per side of the polygon
GLint *polygon_side;
} POLYGONE;

typedef struct{
char type_objects[3]; //type of object (for this assignment only pm)
GLfloat obj_colorR; // onjects color red
GLfloat obj_colorG; // onjects color green
GLfloat obj_colorB; // onjects color blue
GLint num_vertices; //number of vertices of that object
GLint num_polygones; // number of polygones of this object
POLYGONE *poly_array; //array containing all the polygones of that object
VERTEX *vert_array; //array containing the coordinates of all the vertices of that object
GLint x_flag, y_flag, z_flag; //flags used to find the min and max of the object
GLfloat x_max,x_min,y_max,y_min,z_max,z_min;
}AN_OBJECT;

/*

GLOBALS
*/
VERTEX at;
VERTEX from;
VERTEX up;
VERTEX thecenter;
VERTEX max_bounds;
VERTEX min_bounds;

AN_OBJECT *obj_array;

GLint num_objects; //number of objects
GLfloat X0 = 0.0, Y0 = 0.0,Z0 = 13.0;//I hardcoded these to see if i could go ahead and keep working on my program
GLfloat xref = 0.0, yref = 0.0 , zref = 0.0;
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;

GLfloat xwmin = -40.0, ywmin = -70.0,xwmax = 40.0, ywmax = 70.0;
GLfloat dnear = 25.0, dfar = 80.0;
GLfloat cosrot = cos(0.1);
GLfloat sinrot = sin(0.1);

/*

CALCULATIONS FUNCTIONS


*/

GLfloat themax(GLfloat x, GLfloat y){
if(x>=y){
return x;
}
else
return y;
}//end the max

GLfloat themin(GLfloat x, GLfloat y){
if(x<=y){
return x;
}
else
return y;
}//end the min


//finds the min from the object array and assigns xmax, ymax, zmax
void find_max(AN_OBJECT obj_array[], GLint numofobj, VERTEX *max){
GLfloat xmax;
GLfloat ymax;
GLfloat zmax;
GLint i;
if(numofobj == 1){
(*max).x_comp = (obj_array[0].x_max);
(*max).y_comp = (obj_array[0].y_max);
(*max).z_comp = (obj_array[0].z_max);
}//end if
else{
for(i = 0; i <(numofobj-1); i++){
xmax = (themax(obj_array[i].x_max,obj_array[i+1].x_max));
ymax = (themax(obj_array[i].y_max,obj_array[i+1].y_max));
zmax = (themax(obj_array[i].z_max,obj_array[i+1].z_max));
}//end for
(*max).x_comp = xmax;
(*max).y_comp = ymax;
(*max).z_comp = zmax;
}//end else
return;
}//end find_max

//finds the min from the object array and assigns xmin, ymin, zmin to a vertex global
void find_min(AN_OBJECT obj_array[], GLint numofobj, VERTEX *min){
GLfloat xmin;
GLfloat ymin;
GLfloat zmin;
GLint i;
if(numofobj == 1){
(*min).x_comp = (obj_array[0].x_min);
(*min).y_comp = (obj_array[0].y_min) ;
(*min).z_comp = (obj_array[0].z_min) ;
}//end if
else{
for(i = 0; i <(numofobj-1); i++){
xmin = (themin(obj_array[i].x_min,obj_array[i+1].x_min));
ymin = (themin(obj_array[i].y_min,obj_array[i+1].y_min));
zmin = (themin(obj_array[i].z_min,obj_array[i+1].z_min));
}//end for
(*min).x_comp = xmin;
(*min).y_comp = ymin;
(*min).z_comp = zmin;
}//end else
return;
}//end find_max

//computes the center of all the vertices
VERTEX compute_center(GLfloat xmin,GLfloat xmax,GLfloat ymin,GLfloat ymax,GLfloat zmin,GLfloat zmax){
VERTEX center;

center.x_comp = (xmin+xmax)/2;
center.y_comp = (ymin+ymax)/2;
center.z_comp = (zmin+zmax)/2;

return center;
}//end compute_center

//computes the max extent
GLfloat compute_maxextent(GLfloat xmin,GLfloat xmax,GLfloat ymin,GLfloat ymax,GLfloat zmin,GLfloat zmax){
GLfloat x_extent;
GLfloat y_extent;
GLfloat z_extent;
GLfloat temp;
GLfloat max;

x_extent = xmax - xmin;
y_extent = ymax - ymin;
z_extent = zmax - zmin;

temp = themax(x_extent,y_extent);
max = themax(temp,z_extent);

return max;
}//end compute_max extent
void set_at(VERTEX *at,GLfloat x,GLfloat y,GLfloat z){
(*at).x_comp = x;
(*at).y_comp = y;
(*at).z_comp = z;
}//end set_at

void set_from(VERTEX *from,GLfloat x,GLfloat y,GLfloat z){
(*from).x_comp = x;
(*from).y_comp = y;
(*from).z_comp = z;
}//end set_from

void set_up(VERTEX *up,GLfloat x,GLfloat y,GLfloat z){
(*up).x_comp = x;
(*up).y_comp = y;
(*up).z_comp = z;
}//end set_up


/*

GLUT FUNCTIONS

*/

void init(void){
glClearColor (0.0, 0.0, 0.0, 0.0);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity();
gluLookAt(X0,Y0,Z0,xref,yref,zref,Vx,Vy,Vz);

glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glFrustum(xwmin,xwmax , ywmin , ywmax,dnear,dfar);
}//end init

void drawscene(){

GLint i,j,k,vert_index;

for(i = 0;i < num_objects; i ++){

glColor3f(obj_array[i].obj_colorR,obj_array[i].obj_colorG,obj_array[i].obj_colorB);

for(j = 0; j < obj_array[i].num_polygones; j++){


glBegin (GL_POLYGON);

for(k = 0; k < obj_array[i].poly_array[j].num_vertex_perside; k++){
vert_index = obj_array[i].poly_array[j].polygon_side[k];
glVertex3f(obj_array[i].vert_array[vert_index].x_comp,obj_array[i].vert_array[vert_index].y_comp,obj_array[i].vert_array[vert_index].z_comp);
}//end FOR
glEnd();
}//end for




}//END FOR
glutSwapBuffers();
//glFlush();
}//end draw scene

void reshape(GLint w,GLint h){
glViewport(0,0,w,h);
}//end reshape

void display(){
glClear(GL_COLOR_BUFFER_BIT);


drawscene();

}//end display

void specialKeys(GLint key, int x, int y){
switch(key){


case GLUT_KEY_LEFT:
X0 = (X0*cosrot);
//Y0 = (X0*sinrot)+(X0*cosrot);
glMatrixMode (GL_MODELVIEW);

gluLookAt(X0,Y0,Z0,xref,yref,zref,Vx,Vy,Vz);
break;
case GLUT_KEY_RIGHT:
X0 = -(X0*cosrot);
//Y0 = ((X0*sinrot)-(X0*cosrot));
glMatrixMode (GL_MODELVIEW);

gluLookAt(X0,Y0,Z0,xref,yref,zref,Vx,Vy,Vz);
break;
case GLUT_KEY_UP:

break;
case GLUT_KEY_DOWN:

break;

}
glutPostRedisplay();
}//end specialKeys


/*


MAIN

*/

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

GLint i,j=0,k;
GLint num_vertiperside;
GLint num_vertindex;
char line[LINE_LENGTH];
char *token;
const char delimiter[] = " ";


GLint temp;


GLfloat themaxextent;

scanf("%d",&num_objects);
obj_array = (AN_OBJECT*)calloc(num_objects, sizeof(AN_OBJECT));
for(k = 0; k<num_objects;k++){
scanf("%s %f %f %f",&obj_array[k].type_objects,&obj_array[k].obj_colorR,&obj_array[k].obj_colorG,&obj_array[k].obj_colorB);
scanf("%d", &obj_array[k].num_vertices);
obj_array[k].vert_array = (VERTEX*)calloc(obj_array[k].num_vertices, sizeof(VERTEX));
for(i = 0 ; i < obj_array[k].num_vertices; i++){
scanf("%f %f %f",&obj_array[k].vert_array[i].x_comp, &obj_array[k].vert_array[i].y_comp, &obj_array[k].vert_array[i].z_comp);
if(obj_array[k].x_flag == 0 && obj_array[k].y_flag == 0 && obj_array[k].z_flag == 0){
obj_array[k].x_flag = 1;
obj_array[k].y_flag = 1;
obj_array[k].z_flag = 1;
obj_array[k].x_max = obj_array[k].vert_array[i].x_comp;
obj_array[k].x_min = obj_array[k].vert_array[i].x_comp;
obj_array[k].y_max = obj_array[k].vert_array[i].y_comp;
obj_array[k].y_min = obj_array[k].vert_array[i].y_comp;
obj_array[k].z_max = obj_array[k].vert_array[i].z_comp;
obj_array[k].z_min = obj_array[k].vert_array[i].z_comp;
}//end if
else{
if(obj_array[k].vert_array[i].x_comp > obj_array[k].vert_array[i-1].x_comp){
obj_array[k].x_max = obj_array[k].vert_array[i].x_comp;
}//end if
else if(obj_array[k].vert_array[i].x_comp < obj_array[k].vert_array[i-1].x_comp){
obj_array[k].x_min = obj_array[k].vert_array[i].x_comp;
}//end if
if(obj_array[k].vert_array[i].y_comp > obj_array[k].vert_array[i-1].y_comp){
obj_array[k].y_max = obj_array[k].vert_array[i].y_comp;
}//end if
else if(obj_array[k].vert_array[i].y_comp < obj_array[k].vert_array[i-1].y_comp){
obj_array[k].y_min = obj_array[k].vert_array[i].y_comp;
}//end if
if(obj_array[k].vert_array[i].z_comp > obj_array[k].vert_array[i-1].z_comp){
obj_array[k].z_max = obj_array[k].vert_array[i].z_comp;
}//end if
else if(obj_array[k].vert_array[i].z_comp < obj_array[k].vert_array[i-1].z_comp){
obj_array[k].z_min = obj_array[k].vert_array[i].z_comp;
}//end if
}//end else
}
scanf("%d",&obj_array[k].num_polygones);

obj_array[k].poly_array = (POLYGONE*)calloc(obj_array[k].num_polygones, sizeof(POLYGONE));
gets(line);
for(i = 0 ; i < obj_array[k].num_polygones; i++){

gets(line);
token = strtok (line, delimiter);
num_vertiperside = atoi(token);

obj_array[k].poly_array[i].num_vertex_perside = num_vertiperside;

obj_array[k].poly_array[i].polygon_side = (GLint*)calloc(num_vertiperside, sizeof(GLint));
for(j = 0; j < num_vertiperside; j++){
token = strtok (NULL, delimiter);
num_vertindex = atoi(token);
obj_array[k].poly_array[i].polygon_side[j] = num_vertindex;
}//end for loop

}//end for loop

}//end for loop

find_max(obj_array, num_objects,&max_bounds);
find_min(obj_array, num_objects,&min_bounds);
thecenter = compute_center(min_bounds.x_comp,max_bounds.x_comp ,min_bounds.y_comp,max_bounds.y_comp,min_bounds.z_ comp,max_bounds.z_comp);
themaxextent = compute_maxextent(min_bounds.x_comp,max_bounds.x_c omp,min_bounds.y_comp,max_bounds.y_comp,min_bounds .z_comp,max_bounds.z_comp);
set_at(&at,thecenter.x_comp,thecenter.y_comp,thecenter.z_c omp);
set_up(&up,0,1,0);
temp = thecenter.z_comp - (themaxextent/2);
set_from(&from,0,0,temp);



/*


GLUT STUF


*/

glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutCreateWindow("Assignment 2");
init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
// glutSpecialFunc(specialKeys);

glutMainLoop();

}//end main*/