Welcome! Log In Create A New Profile

Advanced

colored cube

Posted by Anonymous User 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Anonymous User
colored cube
September 19, 2006 08:54PM
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[][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}};
GLfloat colors[][3] = {{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[4] = { 0.5, 0.5, 1.5, 1.0 };
GLfloat diffuseMaterial2[4] = { 1.5, 0.0, 1.5, 1.0 };
GLfloat diffuseMaterial3[4] = { 1.5, 1, 1.5, 0.0 };
GLfloat diffuseMaterial4[4] = { 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);

glLoadIdentity();
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);

glMatrixMode(GL_PROJECTION); glLoadIdentity();
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"winking smiley;
// Set up callback functions

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}
Re: colored cube
September 20, 2006 09:49AM
#include <GL/glut.h>
#include <math.h>

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

GLfloat vertices[][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}
};

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

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

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[3];
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);

glLoadIdentity();
// 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);
glLoadIdentity();
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"winking smiley;
// Set up callback functions

glutDisplayFunc(display);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}
Anonymous User
Re: colored cube
September 26, 2006 02:56PM
Thanks man

What exactly does glNormal3fv(n); do?

Re: colored cube
October 05, 2006 07:59AM
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.

Click here to login