PDA

View Full Version : Lighting changes between compilers?



nexusone
12-27-2002, 05:16 AM
I have a strange problem, I wrote this program at work and compiled it.
Works fine on my work computer, take it home run the program it works fine.
But I take the source code and recompile it on my home computer, the ligthing no longer works. I have tried it with two diffrent compilers lcc-win32 and DEV-C++ on my home computer both give the same results.
I have an older lcc-win32 at work, but its
exe files seems to work diffrent.

Here is the code, would like to see what results some of you guys get, lighting effect works or not when compiled.
There is an option to turn lighting on and off, but by default is on.





// Glutpyramids.c
// By Eric Stringer 2002
// Simple examples of OpenGL and Glut usage.
// Keyboard input
// 'v' = view ortho/perspective
// 'l' = lighting on/off


#include <windows.h> // This header file will be needed for some windows compilers
//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers
//#include <GL/glu.h>
#include <GL/glut.h> // glut (gl utility toolkit) basic windows functions, keyboard, mouse.
#include <stdio.h> // standard (I/O library)
#include <stdlib.h> // standard library (set of standard C functions
#include <math.h> // Math library (Higher math functions )
#include "mystuff.h"

int spawn;
int spawn_rate;


float TWOPI = M_PI * 2;
float radi;


XYZ_BUFFER xyz_buffer[128];

// lighting
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[]= { 5.0f, 5.0f, -10.0f, 1.0f };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };


static int view_state = 0, light_state = 0;

int spin;

GLfloat pryamid[18][3] = {{0.0f,1.0f,0.0f},{-1.0f,-1.0f,1.0f},{1.0f,-1.0f,1.0f},
{0.0f,1.0f,0.0f},{1.0f,-1.0f,1.0f},{1.0f,-1.0f,-1.0f},
{0.0f,1.0f,0.0f},{1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,-1.0f},
{0.0f,1.0f,0.0f},{-1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,1.0f},
{-1.0f,-1.0f,1.0f},{1.0f,-1.0f,1.0f},{1.0f,-1.0f,-1.0f},
{1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,-1.0f},{-1.0f,-1.0f,1.0f}};

GLint list[1];

// I use this to put text on the screen
void Sprint( int x, int y, char *st)
{
int l,i;

l=strlen( st ); // see how many characters are in text string.
glRasterPos2i( x, y); // location to start printing text
for( i=0; i < l; i++) // loop until i is greater then l
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the screen
}

}


void normalize(float * vect) //scales a vector a length of 1
{
float length;
int a;

length=sqrt( //A^2 + B^2 + C^2 = length^2
pow(vect[0],2)+
pow(vect[1],2)+
pow(vect[2],2)
);

for (a=0;a<3;++a) //divides vector by its length to normalise
{
vect[a]/=length;
}
}


void crossProduct(float *c,float a[3], float b[3]) //finds the cross product of two vectors
{
c[0]=a[1]*b[2] - b[1]*a[2];
c[1]=a[2]*b[0] - b[2]*a[0];
c[2]=a[0]*b[1] - b[0]*a[1];
}


void getFaceNormal(float *norm,float pointa[3],float pointb[3],float pointc[3])
{
float vect[2][3];
int a,b;
float point[3][3];

for (a=0;a<3;++a)
{
point[0][a]=pointa[a]; //copies points into point[][]
point[1][a]=pointb[a];
point[2][a]=pointc[a];
}

for (a=0;a<2;++a) //calculates vectors from point[0] to point[1]
{ //and point[0] to point[2]
for (b=0;b<3;++b)
{
vect[a][B]=point[2-a][B]-point[0][B];
}
}

crossProduct(norm,vect[0],vect[1]); //calculates vector at 90 to to 2 vectors
normalize(norm); //makes the vector length 1
}

// This creates the spinning of the cube.
static void TimeEvent(int te)
{

spin++; // increase cube rotation by 1
if (spin > 360) spin = 0; // if over 360 degress, start back at zero.
glutPostRedisplay(); // Update screen with new rotation data
glutTimerFunc( 100, TimeEvent, 1); // Reset our timmer.
}


void boundary_check(XYZ_BUFFER *bc ) // if object has hit the boundary change directions
{
if ((bc->x > 2.0f) | (bc->x < -2.0f))
{
bc->dir_x = bc->dir_x * -1;
};
if ((bc->y > 2.0f) | (bc->y < -2.0f))
{
bc->dir_y = bc->dir_y * -1;
};

if ((bc->z > 6.0f) | (bc->z < -6.0f))
{
bc->dir_z = bc->dir_z * -1;
};

}

void pyramid()
{
glCallList( list[0] );
}


// Setup our Opengl world, called once at startup.
void init(void)
{
int ix,iy,iz;
float n1[3];

GLUtriangulatorObj *tobj;

glClearColor (0.0, 0.0, 0.0, 0.0); // When screen cleared, use black.
glShadeModel (GL_SMOOTH); // How the object color will be rendered smooth or flat
glEnable(GL_DEPTH_TEST); // Check depth when rendering
// Lighting is added to scene
glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient);
glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition);
glEnable(GL_LIGHTING); // Turn on lighting
glEnable(GL_LIGHT1); // Turn on light 1

radi = 0;

spawn = 10;
spawn_rate = 0;

for(int i = 0; i < 10; i++)
{
xyz_buffer[i].x = 1.0f;
xyz_buffer[i].y = 1.0f;
xyz_buffer[i].z = 1.0f;
xyz_buffer[i].dir_x = 1;
xyz_buffer[i].dir_y = -1;
xyz_buffer[i].dir_z = 1;
xyz_buffer[i].shape = 1;
};

list[0] = glGenLists(1);
tobj = gluNewTess();

gluTessCallback(tobj, GLU_BEGIN, glBegin);
gluTessCallback(tobj, GLU_VERTEX, glVertex3fv);
gluTessCallback(tobj, GLU_END, glEnd);


glNewList(list[0], GL_COMPILE);


for(iy=0; iy < 6; iy++)
{
getFaceNormal(n1, pryamid[(3 * iy)], pryamid[1 + (3 * iy)], pryamid[2 + (3 * iy)]);
glNormal3fv( n1 );

gluBeginPolygon( tobj );
for(ix=0;ix < 3;ix++)
{
glColor3fv(pryamid[ix +(3 * iy)]);
gluTessVertex( tobj, pryamid[ix + (3 * iy)], pryamid[ix + (3 * iy)]);

}

gluEndPolygon( tobj );
}


glEndList();
gluDeleteTess( tobj);

glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );


}


// Draw our world
void display(void)
{

int i;
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glLoadIdentity(); // Clear the matrix
glOrtho(-2.0, 2.0, -2.0, 2.0, -1.0, 20.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix

glDisable(GL_COLOR_MATERIAL);
// Setup view, and print view state on screen
if (view_state == 1)
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 0, "Perspective view");
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 30);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0, 0, 2, 0, 0, 0, 0, 1, 0);
}else
{
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 4, "Ortho view");
}

// Lighting on/off
if (light_state == 1)
{
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
}else
{
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
}


for( i = 0; i < spawn + 1; i++)
{
glPushMatrix();
glMatrixMode(GL_MODELVIEW);

glScalef( 0.25f, 0.25f, 0.25f);
glRotatef( 90.0f * (xyz_buffer[i].z + xyz_buffer[i].x + xyz_buffer[i].y) / 3 , 1.0f, 1.0f, 1.0f );
glTranslatef( xyz_buffer[i].x, xyz_buffer[i].y, xyz_buffer[i].z);

if (xyz_buffer[i].shape == TRUE)
{
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.5, 0.5, 0.5, 1.0 - i * 0.1);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.10, 0.10, 0.10, 1.0 - i * 0.01);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 1.0 - i * 0.1);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.85, 0.85, 0.85, 1.0 - i * 0.1);
glEnable(GL_BLEND);
pyramid();
}
glPopMatrix();
};
glDisable(GL_BLEND);

for(i = spawn + 1; i >= 0; i--)
{
xyz_buffer[i+1] = xyz_buffer[i];
};

xyz_buffer[0].x = xyz_buffer[0].x + (0.12f * xyz_buffer[0].dir_x);

xyz_buffer[0].y = xyz_buffer[0].y + (0.12f * xyz_buffer[0].dir_y);

xyz_buffer[0].z = xyz_buffer[0].z + (0.12f * xyz_buffer[0].dir_z);

boundary_check( &amp;xyz_buffer[0] );

spawn_rate++;
if (spawn_rate > 20)
{
spawn_rate = 0;
spawn++;
if (spawn > 127) spawn = 127;
}
radi = radi + 0.050;
if (radi > TWOPI) radi = 0;

/* if ((xyz_buffer[63].dir_x + xyz_buffer[63].dir_y + xyz_buffer[63].dir_z) == TRUE)
{
xyz_buffer[63].shape = xyz_buffer[63].shape * -1;
}; */


glutSwapBuffers();
}

// This is called when the window has been resized.
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}


// Read the keyboard
void keyboard (unsigned char key, int x, int y)
{
switch (key)
{

case 'v':
case 'V':
view_state = abs(view_state -1);
break;
case 'l':
case 'L':
light_state = abs(light_state -1);
break;
case 27:
exit(0); // exit program when [ESC] key presseed
break;
default:
break;
}


}


// Main program
int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (10, 10);
glutCreateWindow (argv[0]);
glutSetWindowTitle("GlutWindow");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}


include file



/* Mystuff.h

by Eric Stringer

*/


typedef struct // this structure definded for the location of each pyramid
{
int shape;
float x;
float y;
float z;
int dir_x;
int dir_y;
int dir_z;
}XYZ_BUFFER;


typedef struct
{
XYZ_BUFFER lz;
char *name;
int speed;
int r_rate;
XYZ_BUFFER target;
}MyGL_object;


struct XYZ_BOUND
{
float ax;
float bx;
int timex;
float ay;
float by;
int timey;
float az;
float bz;
int timez;
};



[This message has been edited by nexusone (edited 12-27-2002).]

nexusone
12-27-2002, 10:01 AM
I found it is a math problem with the use of floats and doubles.

Seems the older program was more forgiving in the mixing of the two, while the newer programs required me to switch to using doubles for my math routines.