# colored cube

Posted by Anonymous User
Announcements Last Post SoC Curricula 09/30/2017 01:08PM Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM School of Computing Short Learning Programmes 11/24/2014 08:37AM Unisa contact information 07/28/2011 01:28PM
 Anonymous User colored cube September 19, 2006 08:54PM IP/Host: ---.saix.net Rating: 0
Has anyone got any idea why this cube changes colors??

here is the code

#include <GL/glut.h>
double f=0.3,d = 0.01,j=1,angel = 29;;
GLfloat vertices[] = {{-1,-1,-1},{1,-1,-1},
{1,1,-1},{-1,1,-1},{-1,-1,1},
{1,-1,1},{1,1,1},{-1,1,1}};
GLfloat colors[] = {{0.0,0.0,0.0},{1.0,0.0,0.0},{1.0,1.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},{1.0,0.0,1.0},{1.0,1.0,1.0},{0.0,1.0,1.0}};
GLfloat light_pos[] = {0.0,1.0,0.0,0.0};
GLfloat diffuseMaterial = { 0.5, 0.5, 1.5, 1.0 };
GLfloat diffuseMaterial2 = { 1.5, 0.0, 1.5, 1.0 };
GLfloat diffuseMaterial3 = { 1.5, 1, 1.5, 0.0 };
GLfloat diffuseMaterial4 = { 1, 1, 1, 1.0 };
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };

void polygon(int a,int b,int c ,int d)
{

glBegin(GL_POLYGON);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);

glVertex3fv(vertices[a]);glNormal3f(1.0,1.0,1.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial2);
glNormal3f(0.0,1.0,0.0);
glVertex3fv(vertices);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial3);
glNormal3f(0.0,0.0,1.0);
glVertex3fv(vertices[c]);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial4);
glVertex3fv(vertices[d]);
glEnd();
}

void display()
{

glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glColor3f(1,0.0,0.0);
//glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glRotatef(angel,1,1,1);

polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);

glFlush();
glutSwapBuffers();
angel+=1;

glutPostRedisplay();
}
void reshape(int x, int y)
{ glViewport(0,0,x,y);glEnable(GL_DEPTH_TEST);

glOrtho(-2.0 * (GLfloat)x/ (GLfloat) y,2.0 * (GLfloat) x/ (GLfloat) y,-2.0
,2.0,-10.0,10.0);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glMatrixMode(GL_MODELVIEW);

}

int main(int argc, char** argv)
{
glutInit(&argc,argv);
// Need double buffering for animation
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
// Create and position the graphics window
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
glutCreateWindow("Solar System Demo" ;
// Set up callback functions

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}
 Re: colored cube September 20, 2006 09:49AM IP/Host: ---.enetonline.co.za Registered: 14 years ago Posts: 78 Rating: 0
#include <GL/glut.h>
#include <math.h>

double f=0.3, d = 0.01, j=1, angel = 29;

GLfloat vertices[] = {
{-1,-1,-1}, {1,-1,-1},
{1,1,-1}, {-1,1,-1},
{-1,-1,1}, {1,-1,1},
{1,1,1}, {-1,1,1}
};

GLfloat diffuseMaterial = { 0.5, 0.5, 1.5, 1.0 };
GLfloat diffuseMaterial2 = { 1.5, 0.0, 1.5, 1.0 };
GLfloat diffuseMaterial3 = { 1.5, 1, 1.5, 0.0 }; // completely transparent color
GLfloat diffuseMaterial4 = { 1, 1, 1, 1.0 };

void cross(const GLfloat u[], const GLfloat v[], GLfloat w[])
{
w = u*v - u*v;
w = u*v - u*v;
w = u*v - u*v;
}

void normalize(GLfloat w[])
{
float d = 0.0; int i;

for (i=0; i< 3; i++) d+= w*w;
d = sqrt(d);

if (d > 0.0)
for(i=0; i < 3; i++)
w /= d;
}

void polygon(int a,int b,int c ,int d)
{

glBegin(GL_POLYGON);

// your normals were wrong - recalculating them is overkill, but I was in a hurry.
GLfloat n;
cross(vertices[a], vertices, n);
normalize(n);
glNormal3fv(n);

glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
glVertex3fv(vertices[a]);

glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial2);
glVertex3fv(vertices);

glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial3);
glVertex3fv(vertices[c]);

glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial4);
glVertex3fv(vertices[d]);

glEnd();
}

void display()
{
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// glColor3f(1,0.0,0.0); // achieves nothing if lighting is enabled
glRotatef(angel,1,1,1);

polygon(0,3,2,1);
polygon(2,3,7,6);
polygon(0,4,7,3);
polygon(1,2,6,5);
polygon(4,5,6,7);
polygon(0,1,5,4);

glFlush();
glutSwapBuffers();
angel += 1;

glutPostRedisplay();
}

void reshape(int x, int y)
{
glViewport(0,0,x,y);
glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);
glOrtho(
-2.0 * (GLfloat)x/ (GLfloat) y,2.0 * (GLfloat) x/ (GLfloat) y,
-2.0
,2.0,
-10.0,10.0
);

glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

glMatrixMode(GL_MODELVIEW);
}

int main(int argc, char** argv)
{
glutInit(&argc,argv);
// Need double buffering for animation
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
// Create and position the graphics window
glutInitWindowPosition(0, 0);
glutInitWindowSize(500, 500);
glutCreateWindow("Solar System Demo" ;
// Set up callback functions

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}
 Anonymous User Re: colored cube September 26, 2006 02:56PM IP/Host: ---.saix.net Rating: 0
Thanks man

What exactly does glNormal3fv(n); do?

 Re: colored cube October 05, 2006 07:59AM IP/Host: ---.enetonline.co.za Registered: 14 years ago Posts: 78 Rating: 0
sorry for the delayed post (was focusing on cos311) the glNormal functions specify the normal vector to use for all subsequent glVertex calls. these are ignored if lighting is off, which is why we didn't need to worry about them early on in the course. with flat shading OpenGL only uses the normal defined for the first vertex on a face. and for smooth shading it uses the normal at every vertex to calculate a colour at that vertex, it then linearly interpolates the colours between the vertices (this is gourad shading).

Sorry, only registered users may post in this forum.