PDA

View Full Version : I'm freaking out



bogaat
04-09-2000, 11:37 PM
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 http://www.opengl.org/discussion_boards/ubb/wink.gif 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) + nbrVertices*sizeof(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\n", 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) + nbrFaces*sizeof(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\n", 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\n", flag);
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (flag==1) {
if(w <= h)
glOrtho(2*xmin, 2*xmax, 2*ymin * (GLfloat) h / (GLfloat) w,
2*ymax * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(2*xmin * (GLfloat) w / (GLfloat) h,
2*xmax * (GLfloat) w / (GLfloat) h, 2*ymin, 2*ymax, -10.0, 10.0);
}
if(flag==2) {
if(w<=h) glFrustum(2*xmin, 2*xmax, 2*ymin * (GLfloat) h/ (GLfloat) w,
2*ymax* (GLfloat) h / (GLfloat) w, 2*zmax, zmax*10.0);
else glFrustum(2*xmin, 2*xmax, 2*ymin * (GLfloat) w/ (GLfloat) h,
2*ymax* (GLfloat) w / (GLfloat) h, 2*zmax, xmax*10.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?\n");
fprintf (stderr, "%s: usage <filename>\n", argv[0]);
exit(0);
}
if ((fp = fopen (filename, "r")) == NULL) {
fprintf (stderr, "can't open %s\n", filename);
exit(1);
}
if ( fscanf(fp,"%s%d%d", dataString, &nbrVertices, &nbrFaces) < 3) {
fprintf (stderr, "error\n");
exit(2);
}
printf ("%s, #Vertices = %d, #Faces = %d\n", 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);
}

bogaat
04-09-2000, 11:41 PM
This is an example of a file I might bring in to the above prog..."cube.d"
data 8 6
-0.5 0.5 0.5
0.5 0.5 0.5
0.5 -0.5 0.5
-0.5 -0.5 0.5
-0.5 0.5 -0.5
0.5 0.5 -0.5
0.5 -0.5 -0.5
-0.5 -0.5 -0.5
4 1 2 3 4
4 5 6 2 1
4 8 7 6 5
4 4 3 7 8
4 2 6 7 3
4 5 1 4 8

Maybe someone wouldn't mind running my prog?
I would be forever in your debt...well, maybe not forever http://www.opengl.org/discussion_boards/ubb/smile.gif

Bob
04-10-2000, 01:51 AM
Hello

To be able to solve the problem, you have to know where the problem is. You said the screen goes blank when you change projection. Does is go blank when you change projection in both direction, or just from frustum to ortho (or the other way)? Have you tried each projection to make sure they are working at all?

Concerning lightning, you must send normals to OpenGL, and i don't see any normals at all in your code, not in your "objectfile" nor any glNormal*() calls in your code.

Bob

bogaat
04-10-2000, 06:03 AM
Ok, I fixed the normals thing and now I can see the shading. I still can't switch between modes. I can bring it up in orthographic, but when I try to even switch to the same mode(orthographic), the screen blanks. I can not get the frustum calls to work at all...this is hurting my brain

Bob
04-10-2000, 08:45 AM
Have another theory now...

The first time you run your program, you call init(), where you setup a cameraposition. When you resize the window, you reset your modelview matrix, causing the "old" camera to be changed to the default one (identity matrix). This is where the problem might be. Maybe everything is ok, but the actual polygon you are drawing is outside the viewing volume (because you changed cameraview).

Bob