Skip to main content
gamedev.net
🔒 Locked

What is wrong here?

Started by DanG Dec 21, 2001 at 8:26 PM 4 replies 450+ views
Original Post
DanG
DanG
I have a basic program to draw a lighted red cube on the screen with one light source, with all sides having the same material. However, it is not working. I had a mucc more complecated program, but when it didn''t work i methodicly took out pieces to find the problem but cam up with nothing. I''m am new to OpenGL programming, but i thought i was beyond this kind of simple lighting, so what is wrong with this program? (its a GLUT console app that is supposed to draw a green cube on the screen, but draws merely a white square. [ccode] #include "GL/glut.h" 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 normals[][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}}; // the light we will use GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f }; GLfloat LightPosition[]= { 4.0f, 0.0f, 2.0f, 1.0f }; // the material GLfloat MaterialAmbient[] = {1,0,0,1}; GLfloat MaterialDiffuse[] = {1,0,0,1}; GLfloat MaterialSpecular[] = {1,0,0,1}; GLfloat MaterialShininess = 100; void polygon(int a, int b, int c, int d) { // draw one square from vertex array glBegin(GL_POLYGON); glNormal3fv(normals ' Target=_Blank>Link RELIGION IS THE ROOT OF ALL EVIL
Ambassador: Mr. Bush are you stoned or just really, REALLY dumb?Pres. Bush - I assure you I am not stoned.
DanG
DanG
Ok, what went wrong here? THe code looks like cr*p. Here it is again if it isn''t showing right.

#include "GL/glut.h"


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


// the light we will use
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 4.0f, 0.0f, 2.0f, 1.0f };


// the material
GLfloat MaterialAmbient[] = {1,0,0,1};
GLfloat MaterialDiffuse[] = {1,0,0,1};
GLfloat MaterialSpecular[] = {1,0,0,1};
GLfloat MaterialShininess = 100;


void polygon(int a, int b, int c, int d)
{
// draw one square from vertex array
glBegin(GL_POLYGON);
glNormal3fv(normals glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light
glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light
}

int main(int argc, char* argv[])
{
init();

// pretty standard GLUT startup
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(500,500);
glutCreateWindow("Cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

' Target=_Blank>Link


RELIGION IS THE ROOT OF ALL EVIL
Ambassador: Mr. Bush are you stoned or just really, REALLY dumb?Pres. Bush - I assure you I am not stoned.
TheSalmon
TheSalmon
That''s a nice link you got there

---
No game will ever rule more than CBT!
---Mikael Lax
DanG
DanG
How do i get the code to show up on these forums?! I just copied and pasted and the site seems to have intermpreted one of
the [] as a link tag or something. How do i post code in those white boxes people have? Any help appreciated.

RELIGION IS THE ROOT OF ALL EVIL
Ambassador: Mr. Bush are you stoned or just really, REALLY dumb?Pres. Bush - I assure you I am not stoned.
TheSalmon
TheSalmon
Does this work?


DanG''s code:
  
#include "GL/glut.h"


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


// the light we will use

GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat LightPosition[]= { 4.0f, 0.0f, 2.0f, 1.0f };


// the material

GLfloat MaterialAmbient[] = {1,0,0,1};
GLfloat MaterialDiffuse[] = {1,0,0,1};
GLfloat MaterialSpecular[] = {1,0,0,1};
GLfloat MaterialShininess = 100;


void polygon(int a, int b, int c, int d)
{
// draw one square from vertex array

glBegin(GL_POLYGON);
glNormal3fv(normals[a]);
glVertex3fv(vertices[a]);
glNormal3fv(normals[b]);
glVertex3fv(vertices[b]);
glNormal3fv(normals[c]);
glVertex3fv(vertices[c]);
glNormal3fv(normals[d]);
glVertex3fv(vertices[d]);
glEnd();
}

void cube()
{
// set the material

glMaterialfv(GL_FRONT, GL_AMBIENT, MaterialAmbient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, MaterialDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, MaterialSpecular);
glMaterialf(GL_FRONT, GL_SHININESS, MaterialShininess);

// draw each of the 6 sides of the cube

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);
}



void display()
{
glClearColor(0,0,0,1); // clear to black

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

cube(); // draw the cube


glFlush();
glutSwapBuffers();
}

void myReshape(int w, int h)
{
glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION); // Select The Projection Matrix

glLoadIdentity(); // Reset The Projection Matrix


// Calculate The Aspect Ratio Of The Window

gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix

glLoadIdentity();
glTranslatef(0,0,-5); // move out to view the seen

}

void init()
{
glEnable(GL_LIGHTING); // allow lighting

glEnable(GL_LIGHT1); // the light we''ll use

glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light

glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light

glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light

}

int main(int argc, char* argv[])
{
init();

// pretty standard GLUT startup

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(500,500);
glutCreateWindow("Cube");
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
---Mikael Lax
iNsAn1tY
iNsAn1tY
I think you may have forgotten to get the glMaterial calls to track the glColor calls. You have to insert the following code after the glLightfv calls in void init():

// Enable color tracking...
glEnable(GL_COLOR_MATERIAL);

// ...and set it to track glColor calls...
glColorMaterial(GL_FRONT, GL_AMBIENT);

Here, you enable colour tracking, then set the system to track the colours, set by glColor3f calls, on the front of polygons that are drawn, in ambient light conditions. You can set it to track GL_SPECUALR and GL_DIFFUSE light as well, and for the GL_BACK of polygons. Plus, you haven''t made any glColor calls. So the system won''t know what colour to render all your polygons in. All you''ve set is the material conditions to render...Have you read The OpenGL Superbible? If not, get it. It sorts all kinds of problems, and it''s taught me everything I know about OpenGL, and helped me out of a tight spot like this more than once...

iNsAn1tY - the place where imagination and the real world merge...
Try http://uk.geocities.com/mentalmantle
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.