What is wrong here?
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<A href='http://);
glVertex3fv(vertices[a]);
glNormal3fv(normals<b>);
glVertex3fv(vertices);
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);
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);
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;
}
</b> ' Target=_Blank>Link</a>
RELIGION IS THE ROOT OF ALL EVIL
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<A href='http://);<br> glVertex3fv(vertices[a]);
glNormal3fv(normals);
glVertex3fv(vertices);<br> glNormal3fv(normals[c]);<br> glVertex3fv(vertices[c]); <br> glNormal3fv(normals[d]);<br> glVertex3fv(vertices[d]);<br> glEnd();<br>}<br><br>void cube()<br>{<br> // set the material<br> glMaterialfv(GL_FRONT, GL_AMBIENT, MaterialAmbient);<br> glMaterialfv(GL_FRONT, GL_DIFFUSE, MaterialDiffuse);<br> glMaterialfv(GL_FRONT, GL_SPECULAR, MaterialSpecular);<br> glMaterialf(GL_FRONT, GL_SHININESS, MaterialShininess);<br> <br> // draw each of the 6 sides of the cube<br> polygon(0,3,2,1);<br> polygon(2,3,7,6);<br> polygon(0,4,7,3);<br> polygon(1,2,6,5);<br> polygon(4,5,6,7);<br> polygon(0,1,5,4);<br>}<br><br><br><br>void display()<br>{<br> glClearColor(0,0,0,1); // clear to black<br> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<br><br> cube(); // draw the cube<br><br> glFlush();<br> glutSwapBuffers();<br>}<br><br>void myReshape(int w, int h)<br>{<br> glViewport(0,0,w,h);<br> <br> glMatrixMode(GL_PROJECTION); // Select The Projection Matrix<br> glLoadIdentity(); // Reset The Projection Matrix<br><br> // Calculate The Aspect Ratio Of The Window<br> gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);<br><br> glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix<br> glLoadIdentity();<br> glTranslatef(0,0,-5); // move out to view the seen<br>}<br><br>void init()<br>{<br> glEnable(GL_LIGHTING); // allow lighting<br> glEnable(GL_LIGHT1); // the light we''ll use<br> glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light<br> glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light<br> glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light<br>}<br><br>int main(int argc, char* argv[])<br>{<br> init();<br><br> // pretty standard GLUT startup<br> glutInit(&argc, argv);<br> glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );<br> glutInitWindowSize(500,500);<br> glutCreateWindow("Cube");<br> glutReshapeFunc(myReshape);<br> glutDisplayFunc(display);<br> glutMainLoop();<br> return 0;<br>}<br><br> </b> ' Target=_Blank>Link</a> <br><br>RELIGION IS THE ROOT OF ALL EVIL
#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 href='http://);<br> glVertex3fv(vertices[a]);
glNormal3fv(normals);
glVertex3fv(vertices);<br> glNormal3fv(normals[c]);<br> glVertex3fv(vertices[c]); <br> glNormal3fv(normals[d]);<br> glVertex3fv(vertices[d]);<br> glEnd();<br>}<br><br>void cube()<br>{<br> // set the material<br> glMaterialfv(GL_FRONT, GL_AMBIENT, MaterialAmbient);<br> glMaterialfv(GL_FRONT, GL_DIFFUSE, MaterialDiffuse);<br> glMaterialfv(GL_FRONT, GL_SPECULAR, MaterialSpecular);<br> glMaterialf(GL_FRONT, GL_SHININESS, MaterialShininess);<br> <br> // draw each of the 6 sides of the cube<br> polygon(0,3,2,1);<br> polygon(2,3,7,6);<br> polygon(0,4,7,3);<br> polygon(1,2,6,5);<br> polygon(4,5,6,7);<br> polygon(0,1,5,4);<br>}<br><br><br><br>void display()<br>{<br> glClearColor(0,0,0,1); // clear to black<br> glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);<br><br> cube(); // draw the cube<br><br> glFlush();<br> glutSwapBuffers();<br>}<br><br>void myReshape(int w, int h)<br>{<br> glViewport(0,0,w,h);<br> <br> glMatrixMode(GL_PROJECTION); // Select The Projection Matrix<br> glLoadIdentity(); // Reset The Projection Matrix<br><br> // Calculate The Aspect Ratio Of The Window<br> gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f);<br><br> glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix<br> glLoadIdentity();<br> glTranslatef(0,0,-5); // move out to view the seen<br>}<br><br>void init()<br>{<br> glEnable(GL_LIGHTING); // allow lighting<br> glEnable(GL_LIGHT1); // the light we''ll use<br> glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); // Setup The Ambient Light<br> glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); // Setup The Diffuse Light<br> glLightfv(GL_LIGHT1, GL_POSITION,LightPosition); // Position The Light<br>}<br><br>int main(int argc, char* argv[])<br>{<br> init();<br><br> // pretty standard GLUT startup<br> glutInit(&argc, argv);<br> glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );<br> glutInitWindowSize(500,500);<br> glutCreateWindow("Cube");<br> glutReshapeFunc(myReshape);<br> glutDisplayFunc(display);<br> glutMainLoop();<br> return 0;<br>}<br><br> </b> ' Target=_Blank>Link</a> <br><br>RELIGION IS THE ROOT OF ALL EVIL
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
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
Does this work?
DanG''s code:
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 useGLfloat 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 materialGLfloat 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;}
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
// 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement