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 1spawn = 10;
spawn_rate = 1;glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &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, &glVertex3dv); gluTessCallback(tobj, GLU_TESS_BEGIN, &glBegin); gluTessCallback(tobj, GLU_TESS_END, &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, &glVertex3dv); //gluTessCallback(tobj, GLU_TESS_BEGIN, &glBegin); //gluTessCallback(tobj, GLU_TESS_END, &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( &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 screenglMatrixMode (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( &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 matrixglEnable(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(&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;
}