PDA

View Full Version : What's wrong with my code???



Ashtray_Waterloo
11-21-2000, 09:32 AM
I'm trying to draw a 3d scene with cubes located at random points. They are to flock using some rules to a leader but let's not worry about that just yet. I want to just be able to see my cubes with lighting when I draw them. Nothing shows up after I run my code. Please help!!!

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

#define MAXX 500
#define MAXY 500
#define MAXZ 10
#define RANDMAX 32767

struct glob {

GLfloat followers[50][3];
GLint numpoints;
GLfloat leader[3];
GLfloat destination[3];
} glob;

struct glob global;

/* Initialize material property, light source, lighting model,
* and depth buffer.
*/
void init(void)
{
int i;

GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { MAXX, MAXY, MAXZ, 0.0 };

glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);

glOrtho(0., (GLfloat)MAXX, 0., (GLfloat)MAXY, (GLfloat)(-1*MAXZ),(GLfloat)MAXZ);

srand(time(NULL));
global.numpoints = (GLint)rand()%50;
global.leader[0] = (GLfloat)(rand()*MAXX)/RANDMAX;
global.leader[1] = (GLfloat)(rand()*MAXY)/RANDMAX;
global.leader[2] = (GLfloat)((rand()*(2*MAXZ))/RANDMAX - MAXZ);
global.destination[0] = (GLfloat)(rand()*MAXX)/RANDMAX;
global.destination[1] = (GLfloat)(rand()*MAXY)/RANDMAX;
global.destination[2] = (GLfloat)((rand()*(2*MAXZ))/RANDMAX - MAXZ);
for(i = 0; i < global.numpoints; i++) {
global.followers[i][0] = (GLfloat)(rand()*MAXX)/RANDMAX;
global.followers[i][1] = (GLfloat)(rand()*MAXY)/RANDMAX;
global.followers[i][2] = (GLfloat)((rand()*(2*MAXZ))/RANDMAX - MAXZ);
}
}

void display(void)
{
int i, j;
long v[8][3] = {
{-1, -1, -1},
{-1, -1, 1},
{-1, 1, 1},
{-1, 1, -1},
{ 1, -1, -1},
{ 1, -1, 1},
{ 1, 1, 1},
{ 1, 1, -1}
};
int path[16] = {
0, 1, 2, 3,
0, 4, 5, 6,
7, 4, 5, 1,
2, 6, 7, 3
};

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
for(j = 0; j < 16; j++){
glNormal3i(v[path[j]][0], v[path[j]][1], v[path[j]][2]);
glVertex3i(v[path[j]][0], v[path[j]][1], v[path[j]][2]);
}
glEnd();
glTranslatef(global.leader[0], global.leader[1], global.leader[2]);
glBegin(GL_QUADS);
for(j = 0; j < 16; j++){
glNormal3i(v[path[j]][0], v[path[j]][1], v[path[j]][2]);
glVertex3i(v[path[j]][0], v[path[j]][1], v[path[j]][2]);
}
glEnd();
glPopMatrix();
glColor3f(0.0f, 0.0f, 1.0f);
for(i = 0; i < global.numpoints; i++) {
glPushMatrix();
glTranslatef(global.followers[i][0], global.followers[i][1], global.followers[i][2]);
glBegin(GL_QUADS);
for(j = 0; j < 16; j++){
glNormal3i(v[path[j]][0], v[path[j]][1], v[path[j]][2]);
glVertex3i(v[path[j]][0], v[path[j]][1], v[path[j]][2]);
}
glEnd();
glPopMatrix();
}
glutSwapBuffers();
glFlush();
//Figure out next position here. Call a function to do it.
}

void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho (0.0, MAXX, 0.0, MAXY*(GLfloat)h/(GLfloat)w, -1.0 * MAXZ - 10.0, MAXZ + 10.0);
else
glOrtho (0, MAXX*(GLfloat)w/(GLfloat)h, 0.0, MAXY, -1.0 * MAXZ - 10.0, MAXZ + 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (MAXX, MAXY);
glutInitWindowPosition (100, 100);
glutCreateWindow ("3D Flocking");
init ();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

Icehawk
11-21-2000, 11:53 AM
I just took a short look at your code but the error seems to be in your init func.

You are multiplying an orthogonal matrix to your modelview matrix I think you wanted to do this with your projection matrix...

so call a glMatrixMode(GL_PRJECTION) befor the glOrtho call and reset the mode to modelview after it.

Good luck.

Ashtray_Waterloo
11-22-2000, 05:52 AM
Actually, it turns out that a lot of stuff was messed up, but most importantly, the calllist function wasn't working properly. I have it working now...

snipe
11-22-2000, 12:41 PM
May I ask what Operating System you are trying to do this on, and what C Compiler?