PDA

View Full Version : Trouble with material settings



nexusone
01-30-2003, 05:52 PM
I have this little demo I am working on, and it seeem like the material settings are not working.... take a look....
also am trying to do a render to texture that is not working to well ether...




// 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;

XYZ_BUFFER xyz_buffer[128];

// lighting
GLfloat LightAmbient[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 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;

GLdouble triangle[3][3] = {{0.0, 1.0, 0.0},{-1.0,-1.0,1.0},{1.0,-1.0,1.0}};

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

GLdouble pryamidc[18][3] = {{0.0f,1.0f,0.0f},{1.0f,0.0f,0.0f},{0.0f,0.0f,1.0f },
{0.0f,1.0f,0.0f},{1.0f,1.0f,1.0f},{1.0f,0.0f,1.0f} ,
{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f} ,
{0.0f,1.0f,0.0f},{1.0f,1.0f,1.0f},{0.0f,1.0f,1.0f} ,
{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f} ,
{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f} };

GLint list[2];
GLint Texstore[1];
static unsigned int ptexture[128 * 128 * 3];

int Win_h, Win_w;

// 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(GLdouble * vect) //scales a vector a length of 1
{
GLdouble length;
int a;
//A^2 + B^2 + C^2 = length^2
length=sqrt( 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(GLdouble *c,GLdouble a[3], GLdouble 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(GLdouble *norm,GLdouble pointa[3],GLdouble pointb[3],GLdouble pointc[3])
{
GLdouble vect[2][3];
int a,b;
GLdouble 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 > 4.0f) | (bc->x < -4.0f))
{
bc->dir_x = bc->dir_x * -1;
};
if ((bc->y > 4.0f) | (bc->y < -4.0f))
{
bc->dir_y = bc->dir_y * -1;
};

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

}

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

void Draw_objects(void)
{
int i;
for( i = 0; i < spawn + 1; i++) // was spawn + 1
{
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glScalef( 0.25f, 0.25f, 0.25f);
glRotatef( 90.0f * ( (float) xyz_buffer[i].z + (float)xyz_buffer[i].x + (float)xyz_buffer[i].y) / 3 , 1.0f, 1.0f, 1.0f );
glTranslatef((float) xyz_buffer[i].x, (float) xyz_buffer[i].y, (float) xyz_buffer[i].z);

if (xyz_buffer[i].shape == TRUE)
{
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.5, 0.5, 0.5, 1.0);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.0, 0.0, 0.0, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 1.0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.5, 0.5, 0.5, 1.0);

pyramid();
}
glPopMatrix();
};
}

// Setup our Opengl world, called once at startup.
void init(void)
{
int i;
int ix,iy,iz;
GLdouble 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

spawn = 10;
spawn_rate = 1;

glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &amp;Texstore);
glBindTexture(GL_TEXTURE_2D, Texstore[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGB, GL_UNSIGNED_INT, ptexture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


for( 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(2);
tobj = gluNewTess();

gluTessCallback(tobj, GLU_TESS_VERTEX, &amp;glVertex3dv);
gluTessCallback(tobj, GLU_TESS_BEGIN, &amp;glBegin);
gluTessCallback(tobj, GLU_TESS_END, &amp;glEnd);
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);

glNewList(list[0], GL_COMPILE);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

for(iy=0; iy < 6; iy++)
{
gluTessBeginPolygon( tobj, NULL );
getFaceNormal(n1, pryamid[(3 * iy)], pryamid[1 + (3 * iy)], pryamid[2 + (3 * iy)]);
glNormal3dv( n1 );
gluTessBeginContour( tobj );
//gluTessNormal( tobj, n1[0], n1[1], n1[2]);
for(ix=0;ix < 3;ix++)
{

glColor4d(pryamidc[ix +(3 * iy)],pryamidc[ix +(3 * iy) + 1],pryamidc[ix +(3 * iy) + 2], 0.3);
gluTessVertex( tobj, pryamid[ix + (3 * iy)], pryamid[ix + (3 * iy)]);

}
gluTessEndContour( tobj );
gluTessEndPolygon( tobj );
}


glEndList();
gluDeleteTess( tobj);

//tobj = gluNewTess();

//gluTessCallback(tobj, GLU_TESS_VERTEX, &amp;glVertex3dv);
//gluTessCallback(tobj, GLU_TESS_BEGIN, &amp;glBegin);
//gluTessCallback(tobj, GLU_TESS_END, &amp;glEnd);
//gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);

glNewList(list[1], GL_COMPILE);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

for(iy=0; iy < 1; iy++)
{
glBegin(GL_QUADS);
//gluTessBeginPolygon( tobj, NULL );
getFaceNormal(n1, pryamid[(3 * iy)], pryamid[1 + (3 * iy)], pryamid[2 + (3 * iy)]);
glNormal3dv( n1 );
//gluTessBeginContour( tobj );
//gluTessNormal( tobj, n1[0], n1[1], n1[2]);
for(ix=0;ix < 3;ix++)
{

glColor3dv( pryamidc[ix +(3 * iy)]);
glVertex3dv( &amp;pryamid[ix + (3 * iy)]);

}
glEnd();
//gluTessEndContour( tobj );
//gluTessEndPolygon( tobj );
}


glEndList();
//gluDeleteTess( tobj);


glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

}

void Draw_texture(void)
{
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texstore[0]);
glColor4f( 1, 1, 1, 0.9);

glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f); glVertex2f(-4, -4);
glTexCoord2f(0.0f, 0.0f); glVertex2f(-4, 4);
glTexCoord2f(1.0f, 0.0f); glVertex2f(4, 4);
glTexCoord2f(1.0f, 1.0f); glVertex2f(4, -4);
glEnd();

}


// 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(-4.0, 4.0, -4.0, 4.0, -6.0, 6.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

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, 0, "Ortho view");
}


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

//glViewport(0 , 0, 128, 128);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glTranslatef( 0.0, 0.0, -2.0);
glRotatef( spin, 1.0, 1.0, 0.0);

Draw_objects();

glEnable(GL_BLEND);
glDisable(GL_COLOR_MATERIAL);
glColor3f( 0.0, 1.0, 0.0);
glEnable(GL_COLOR_MATERIAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.5, 0.5, 0.5, 0.3);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.10, 0.10, 0.10, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 0.3);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.85, 0.85, 0.85, 0.3);
glutSolidCube( 2 );
//glLoadIdentity();
//glDisable(GL_DEPTH_TEST);
//Draw_texture();
//glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);

glBindTexture(GL_TEXTURE_2D, Texstore[0]);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 128, 128, 0);


// Update objects
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;
}

/* 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;
}; */

/* glViewport(0,0,Win_w, Win_h);
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(-4.0, 4.0, -4.0, 4.0, -6.0, 6.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
Draw_texture();

glTranslatef( 0.0, 0.0, -2.0);
glRotatef( spin, 1.0, 1.0, 0.0);

Draw_objects();
*/
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;
}

shinpaughp
01-30-2003, 07:02 PM
Hey, cool little demo. Not sure what you are looking for, though. Material appeared to work fine. After manually setting up your XYZ_BUFFER and a few other items which were probably included in your mystuff.h, it runs well and the lighting works, and the view change works, though it does look better without lighting if only because the colors look better, more contrast.

Let me know what it is you find wrong with it and I'll take a look again, or let you know the modifications I made if for some reason that made it work better (though, I doubt it - my compiler just won't allow 2d array to be index by single[], and the only other change I made was texstore from GLint to GLuint).

nexusone
01-31-2003, 04:26 AM
The color's of the pryamids look washed out, and adjusting the material settings don't seem to have any effect with the lighting on.

Would the normals not being correct cause that to happen?

I was suppecting that it maybe has something to do with the compiler I am using now. Dev-C++?

If some other could compile it and see if they get the same results of material settings not working or if they start would give me a clue to the problem.

here is my header file:




/* Mystuff.h

by Eric Stringer

*/


typedef struct // this structure definded for the location of each pyramid
{
int shape;
double x;
double y;
double 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;
};





Originally posted by shinpaughp:
Hey, cool little demo. Not sure what you are looking for, though. Material appeared to work fine. After manually setting up your XYZ_BUFFER and a few other items which were probably included in your mystuff.h, it runs well and the lighting works, and the view change works, though it does look better without lighting if only because the colors look better, more contrast.

Let me know what it is you find wrong with it and I'll take a look again, or let you know the modifications I made if for some reason that made it work better (though, I doubt it - my compiler just won't allow 2d array to be index by single[], and the only other change I made was texstore from GLint to GLuint).

Relic
01-31-2003, 05:31 AM
Took a ten second look at the code:
You use glScale() but no glEnable(GL_NORMALIZE).

shinpaughp
01-31-2003, 09:27 AM
I tried a variety of modifications, mainly to the lighting. Multiple lights for one, changing light positions, but I did find that commenting out the the ambient portion of the light resulted in much brighter sharper color for the pyramids. So, you probably want to adjust your ambient characteristics to lower values like 0.1 or 0.2. The default is (0, 0, 0, 1).

I just tried negative (-1.0, -1.0, -1.0, 1.0). Kinda cool. But, can't read the words or most of the cube in that state.

[This message has been edited by shinpaughp (edited 01-31-2003).]

nexusone
01-31-2003, 03:24 PM
Thanks guys for the suggestions.

I added the glEnable(GL_NORMALIZE) and the ligthing settings that seem to help.
I guess in my head how the settings for the light work and did it in reverse.

Now just to get my render to a texture routine to work.

nexusone
01-31-2003, 05:04 PM
I got the render to texture to work... left a variable blank.....

Here is the newest release of the code... enjoy.. cool effect.... remember header posted in previous post.




// 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;

XYZ_BUFFER xyz_buffer[128];

// lighting
GLfloat LightAmbient[]= { 0.2f, 0.2f, 0.2f, 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[] = { 0.2, 0.2, 0.2, 1.0 };


static int view_state = 0, light_state = 0;

int spin;

GLdouble triangle[3][3] = {{0.0, 1.0, 0.0},{-1.0,-1.0,1.0},{1.0,-1.0,1.0}};

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

GLdouble pryamidc[18][3] = {{0.0f,1.0f,0.0f},{1.0f,0.0f,0.0f},{0.0f,0.0f,1.0f },
{0.0f,1.0f,0.0f},{1.0f,1.0f,1.0f},{1.0f,0.0f,1.0f} ,
{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f} ,
{0.0f,1.0f,0.0f},{1.0f,1.0f,1.0f},{0.0f,1.0f,1.0f} ,
{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f} ,
{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f},{1.0f,1.0f,0.0f} };

GLint list[2];
GLuint Texstore[1];
static unsigned int ptexture[128 * 128 * 4];

int Win_h, Win_w;

// 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(GLdouble * vect) //scales a vector a length of 1
{
GLdouble length;
int a;
//A^2 + B^2 + C^2 = length^2
length=sqrt( 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(GLdouble *c,GLdouble a[3], GLdouble 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(GLdouble *norm,GLdouble pointa[3],GLdouble pointb[3],GLdouble pointc[3])
{
GLdouble vect[2][3];
int a,b;
GLdouble 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 > 4.0f) | (bc->x < -4.0f))
{
bc->dir_x = bc->dir_x * -1;
};
if ((bc->y > 4.0f) | (bc->y < -4.0f))
{
bc->dir_y = bc->dir_y * -1;
};

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

}

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

void Draw_objects(void)
{
int i;
for( i = 0; i < spawn + 1; i++) // was spawn + 1
{
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glScalef( 0.25f, 0.25f, 0.25f);
glRotatef( 90.0f * ( (float) xyz_buffer[i].z + (float)xyz_buffer[i].x + (float)xyz_buffer[i].y) / 3 , 1.0f, 1.0f, 1.0f );
glTranslatef((float) xyz_buffer[i].x, (float) xyz_buffer[i].y, (float) xyz_buffer[i].z);

if (xyz_buffer[i].shape == TRUE)
{
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.25, 0.25, 0.25, 1.0);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.0, 0.0, 0.0, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 1.0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.5, 0.5, 0.5, 1.0);

pyramid();
}
glPopMatrix();
};
}

void Draw_object(void)
{
glEnable(GL_COLOR_MATERIAL);
glPushMatrix();
glScalef( 0.25f, 0.25f, 0.25f);
glRotatef( 90.0f * ( (float) xyz_buffer[0].z + (float)xyz_buffer[0].x + (float)xyz_buffer[0].y) / 3 , 1.0f, 1.0f, 1.0f );
glTranslatef((float) xyz_buffer[0].x, (float) xyz_buffer[0].y, (float) xyz_buffer[0].z);

if (xyz_buffer[0].shape == TRUE)
{
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.25, 0.25, 0.25, 1.0);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.0, 0.0, 0.0, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 1.0);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.5, 0.5, 0.5, 1.0);

pyramid();
}
glPopMatrix();

}

// Setup our Opengl world, called once at startup.
void init(void)
{
int i;
int ix,iy,iz;
GLdouble 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
glEnable(GL_NORMALIZE);
spawn = 10;
spawn_rate = 1;

//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glGenTextures(1, &amp;Texstore);
glBindTexture(GL_TEXTURE_2D, Texstore[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, 128, 128, 0, GL_RGB, GL_UNSIGNED_BYTE, ptexture);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


for( 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(2);
tobj = gluNewTess();

gluTessCallback(tobj, GLU_TESS_VERTEX, &amp;glVertex3dv);
gluTessCallback(tobj, GLU_TESS_BEGIN, &amp;glBegin);
gluTessCallback(tobj, GLU_TESS_END, &amp;glEnd);
gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);

glNewList(list[0], GL_COMPILE);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

for(iy=0; iy < 6; iy++)
{
gluTessBeginPolygon( tobj, NULL );
getFaceNormal(n1, pryamid[(3 * iy)], pryamid[1 + (3 * iy)], pryamid[2 + (3 * iy)]);
glNormal3dv( n1 );
gluTessBeginContour( tobj );
//gluTessNormal( tobj, n1[0], n1[1], n1[2]);
for(ix=0;ix < 3;ix++)
{

glColor3dv(pryamidc[ix +(3 * iy)]);
gluTessVertex( tobj, pryamid[ix + (3 * iy)], pryamid[ix + (3 * iy)]);

}
gluTessEndContour( tobj );
gluTessEndPolygon( tobj );
}


glEndList();
gluDeleteTess( tobj);

//tobj = gluNewTess();

//gluTessCallback(tobj, GLU_TESS_VERTEX, &amp;glVertex3dv);
//gluTessCallback(tobj, GLU_TESS_BEGIN, &amp;glBegin);
//gluTessCallback(tobj, GLU_TESS_END, &amp;glEnd);
//gluTessProperty(tobj, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_POSITIVE);

glNewList(list[1], GL_COMPILE);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

for(iy=0; iy < 1; iy++)
{
glBegin(GL_QUADS);
//gluTessBeginPolygon( tobj, NULL );
getFaceNormal(n1, pryamid[(3 * iy)], pryamid[1 + (3 * iy)], pryamid[2 + (3 * iy)]);
glNormal3dv( n1 );
//gluTessBeginContour( tobj );
//gluTessNormal( tobj, n1[0], n1[1], n1[2]);
for(ix=0;ix < 3;ix++)
{

glColor3dv( pryamidc[ix +(3 * iy)]);
glVertex3dv( &amp;pryamid[ix + (3 * iy)]);

}
glEnd();
//gluTessEndContour( tobj );
//gluTessEndPolygon( tobj );
}


glEndList();
//gluDeleteTess( tobj);


glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

}

void Draw_texture(void)
{
glEnable(GL_BLEND);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Texstore[0]);
glColor4f( 1, 1, 1, 0.9);

glBegin(GL_QUADS);
glTexCoord2f(0.0f,0.0f ); glNormal3f(0.0, 0.0, 1.0); glVertex3f(-4.0f,-4.0f,0.0f);
glTexCoord2f(1.0f,0.0f ); glNormal3f(0.0, 0.0, 1.0); glVertex3f( 4.0f,-4.0f,0.0f);
glTexCoord2f(1.0f,1.0f ); glNormal3f(0.0, 0.0, 1.0); glVertex3f( 4.0f, 4.0f,0.0f);
glTexCoord2f(0.0f,1.0f ); glNormal3f(0.0, 0.0, 1.0); glVertex3f(-4.0f, 4.0f,0.0f);
glEnd();

glDisable(GL_TEXTURE_2D);
}


// 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(-4.0, 4.0, -4.0, 4.0, -6.0, 6.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

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, 0, "Ortho view");
}


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

glViewport(0 , 0, 128, 128);

glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_DEPTH_TEST);
Draw_texture();
glEnable(GL_DEPTH_TEST);
glTranslatef( 0.0, 0.0, -2.0);
glRotatef( spin, 1.0, 1.0, 0.0);

Draw_objects();

glEnable(GL_BLEND);
glDisable(GL_COLOR_MATERIAL);
glColor3f( 0.0, 1.0, 0.0);
glEnable(GL_COLOR_MATERIAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.5, 0.5, 0.5, 0.3);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.10, 0.10, 0.10, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 0.3);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.85, 0.85, 0.85, 0.3);
//glutSolidCube( 2 );
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
Draw_texture();
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);

glBindTexture(GL_TEXTURE_2D, Texstore[0]);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 128, 128, 0);

// Update objects
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;
}

/* 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;
}; */

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen
glViewport(0,0,Win_w, Win_h);
glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work
glLoadIdentity(); // Clear the matrix
glOrtho(-4.0, 4.0, -4.0, 4.0, -6.0, 6.0); // Setup an Ortho view
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing)
glLoadIdentity(); // Clear the model matrix
//glTranslatef( 0.0, 0.0, -2.0);
//glEnable(GL_BLEND);
//glBlendFunc(GL_ONE,GL_ONE);
//Draw_texture();

glTranslatef( 0.0, 0.0, -2.0);
glRotatef( spin, 1.0, 1.0, 0.0);
glDisable(GL_BLEND);
Draw_object();
glEnable(GL_BLEND);
glDisable(GL_COLOR_MATERIAL);
glColor3f( 0.0, 1.0, 0.0);
glEnable(GL_COLOR_MATERIAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glColorMaterial(GL_FRONT, GL_AMBIENT);
glColor4f(0.5, 0.5, 0.5, 0.3);
glColorMaterial(GL_FRONT, GL_EMISSION);
glColor4f(0.10, 0.10, 0.10, 0.0);
glColorMaterial(GL_FRONT, GL_SPECULAR);
glColor4f(0.5, 0.5, 0.5, 0.3);
glColorMaterial(GL_FRONT, GL_DIFFUSE);
glColor4f(0.85, 0.85, 0.85, 0.3);
glutSolidCube( 2 );

glLoadIdentity(); // Clear the model matrix
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA);
Draw_texture();
glEnable(GL_DEPTH_TEST);
glutSwapBuffers();
}

// This is called when the window has been resized.
void reshape (int w, int h)
{
Win_w = w;
Win_h = 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 | GLUT_ALPHA);
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;
}

shinpaughp
01-31-2003, 05:38 PM
That is cool!!!

Like the way the texture flows across the screen in perspective mode. Kinda reminds me of some of the visualizations in Windows Media Player.

Makes me wish I could actually work on some of my OpenGL projects instead of this crappy MFC app http://www.opengl.org/discussion_boards/ubb/mad.gif I'm writing that will be far easier to do with simple GDI commands - simple bar graphs, histograms, and line charts for analysis of data. Oh, well. One of these days soon I'll finish it, and then I can do something I actually want to do. http://www.opengl.org/discussion_boards/ubb/biggrin.gif