I can not get this thing to work. I have worked on it for days. Whenever I try to switch between ortho and frustum using a flag in my reshape func, It just blanks the screen. Also, I can not get any shading to work. The surface looks flat. If someone could please look over my code it would be greatly appreciated The load_obj() func reads in my object file…
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
//#include <list>
typedef float point3[3];
char *filename; //used for file name input
FILE *fp;
char dataString[80];
int nbrVertices, nbrFaces;
int **faces;
int w, h;
int flag=1;
float **vertices;
GLfloat xmin=0.0;
GLfloat xmax=0.0;
GLfloat ymin=0.0;
GLfloat ymax=0.0;
GLfloat zmin=0.0;
GLfloat zmax=0.0;
typedef struct {
char dir;
double d_angle;
} delta;
void load_obj() {
int nbrVert, i, j, nbr, clmz;
long offset;
char str[80];
vertices = (float **)malloc((3*nbrVertices)sizeof(float) + nbrVerticessizeof(float *));
for(i =0; i < nbrVertices; i++) {
vertices[i] = (float )(vertices +(i * 3)(sizeof(float)/sizeof(float *)) + nbrVertices);
}
fgetc(fp);
for (i=0; i < nbrVertices; i++) {
fgets(str, 80, fp);
sscanf(str, “%f%f%f”, &vertices[i][0], &vertices[i][1], &vertices[i][2]);
if (vertices[i][0] > xmax)
xmax = vertices[i][0];
else
if(vertices[i][0] < xmin)
xmin = vertices[i][0];
if (vertices[i][1] > ymax)
ymax = vertices[i][1];
else
if(vertices[i][1] < ymin)
ymin = vertices[i][1];
if (vertices[i][2] > zmax)
zmax = vertices[i][2];
else
if(vertices[i][2] < zmin)
zmin = vertices[i][2];
printf("%f, %f, %f
", vertices[i][0], vertices[i][1], vertices[i][2]);
}
offset = ftell(fp);
clmz=0;
for (i=0; i < nbrFaces; i++) {
if (fgets(str, 80, fp) == NULL){
printf(“error”);
exit(0);
}
sscanf(str, “%d”, &nbr);
if (nbr > clmz)
clmz = nbr;
}
clmz++;
fseek(fp, offset, SEEK_SET);
faces = (int **)malloc((clmz*nbrFaces)sizeof(int) + nbrFacessizeof(int *));
for(i =0; i < nbrFaces; i++) {
faces[i] = (int )(faces +(i * clmz)(sizeof(int)/sizeof(int *)) + nbrFaces);
}
for (i = 0;i < nbrFaces; i++) {
fscanf(fp, “%d” , &faces[i][0]);
for (j=0; j < faces[i][0]; j++ ) {
fscanf(fp, “%d” , &faces[i][j+1]);
}
printf("%d sided,Vertices: %d, %d, %d, %d
", faces[i][0], faces[i][1], faces[i][2], faces[i][3], faces[i][4]);
}
fclose(fp);
}
void draw_poly(int j) {
int k;
glBegin(GL_POLYGON);
for (k=0; k < faces[j][0]; k++) {
glVertex3f(vertices[(faces[j][k+1])-1][0], vertices[(faces[j][k+1])-1][1], vertices[(faces[j][k+1])-1][2]);
}
glEnd();
}
void draw_obj() {
int i;
for (i=0;i < nbrFaces; i++) {
draw_poly(i);
}
}
void init() {
GLfloat light_ambient[]={1.0, 0.0, 0.0, 1.0};
GLfloat light_diffuse[]={1.0, 0.0, 0.0, 1.0};
GLfloat light_specular[]={1.0, 1.0, 1.0, 1.0};
GLfloat mat_specular[]={1.0, 1.0, 1.0, 1.0};
GLfloat mat_diffuse[]={1.0, 0.8, 0.0, 1.0};
GLfloat mat_ambient[]={0.2, 0.2, 0.2, 1.0};
GLfloat light_dir[] = { -1.0, 0.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_dir);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glShadeModel(GL_SMOOTH); /*enable smooth shading /
glEnable(GL_LIGHTING); / enable lighting /
glEnable(GL_LIGHT0); / enable light 0 /
glEnable(GL_DEPTH_TEST); / enable z buffer */
glClearColor (0.0, 0.0, 0.0, 1.0);
glLoadIdentity();
glScalef(2/(xmax-xmin), 2/(ymax-ymin), 2/(zmax-zmin));
glTranslatef(-(xmax+xmin)/2, -(ymax+ymin)/2, -(zmax+zmin)/2);
gluLookAt(0.0, 0.0, 2*zmax, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
/void keys(unsigned char key, int x, int y) {
// Use x, X, y, Y, z, and Z keys to move object
if(key == ‘x’) theta[0]-= 10;
if(key == ‘X’) theta[0]+= 10;//edit these
if(key == ‘y’) theta[1]-= 10;
if(key == ‘Y’) theta[1]+= 10;
if(key == ‘z’) theta[2]-= 10;
if(key == ‘Z’) theta[2]+= 10;
if(key == ‘+’) ;
if(key == ‘-’);
glutPostRedisplay();
}/
void reshape(w, h){
printf("%d
", flag);
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (flag==1) {
if(w <= h)
glOrtho(2xmin, 2xmax, 2ymin * (GLfloat) h / (GLfloat) w,
2ymax * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(2xmin * (GLfloat) w / (GLfloat) h,
2xmax * (GLfloat) w / (GLfloat) h, 2ymin, 2ymax, -10.0, 10.0);
}
if(flag==2) {
if(w<=h) glFrustum(2xmin, 2xmax, 2ymin * (GLfloat) h/ (GLfloat) w,
2ymax* (GLfloat) h / (GLfloat) w, 2zmax, zmax10.0);
else glFrustum(2xmin, 2xmax, 2ymin * (GLfloat) w/ (GLfloat) h,
2ymax* (GLfloat) w / (GLfloat) h, 2zmax, xmax10.0);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glutPostRedisplay();
}
void menu(int mj) {
if (mj == 1) {
flag=1;
reshape(w,h);
}
if (mj == 2) {
flag=2;
reshape(w,h);
}
else if(mj == 3) {
free(vertices);
free(faces);
exit(0);
}
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
draw_obj();
glutSwapBuffers();
}
int main(int argc, char **argv) {
if (argc > 1)
filename = argv[1];
else {
fprintf (stderr, "Hey dummy, what’cha want me to do?
");
fprintf (stderr, "%s: usage <filename>
", argv[0]);
exit(0);
}
if ((fp = fopen (filename, “r”)) == NULL) {
fprintf (stderr, “can’t open %s
“, filename);
exit(1);
}
if ( fscanf(fp,”%s%d%d”, dataString, &nbrVertices, &nbrFaces) < 3) {
fprintf (stderr, "error
“);
exit(2);
}
printf (”%s, #Vertices = %d, #Faces = %d
", dataString, nbrVertices, nbrFaces);
load_obj();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(500, 500);
glutCreateWindow(“Simple 3D Object Viewer”);
glutCreateMenu(menu);
glutAddMenuEntry(“Perspective”, 1);
glutAddMenuEntry(“Orthographic”, 2);
glutAddMenuEntry(“Exit :]”, 3);
glutAttachMenu(GLUT_RIGHT_BUTTON);
//glutKeyboardFunc(keys);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return(0);
}