| | |
glColor problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Oct 2008
Posts: 89
Reputation:
Solved Threads: 0
I'm drawing a sphere and some ground. I used GL_POLYGON to draw a rectangle and colored it green. When I added a sphere, my ground just turned black. Is this because the sphere is a 3d object and the polygon is 2d or is there something else? Here is the code for my sphere and my ground, which are just in my display func.
C++ Syntax (Toggle Plain Text)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Draw sphere glPushMatrix(); glTranslatef (-4.0, 5.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(.5, 50, 50); glPopMatrix(); //ground glMatrixMode(GL_PROJECTION); glColor3f(0.0,1.0,0.0); glBegin(GL_POLYGON); glVertex3d(-8,-8,0); glVertex3d(8,-8,0); glVertex3d(8,-2,0); glVertex3d(-8,-2,0); glEnd();
0
#2 Oct 29th, 2009
Its because you don't have your normals calculated for rectangles.
Plus put your lighting function in our initGL function.
Plus put your lighting function in our initGL function.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
0
#4 Oct 30th, 2009
Did you call glNormalize in your init function ?
By having proper normals the light "reflects" of the shape and thus it works out as planned. If you don't calculate normals then your object
feels the effect of ambient lighting, thus looks dark (in most case).
Try this :
By having proper normals the light "reflects" of the shape and thus it works out as planned. If you don't calculate normals then your object
feels the effect of ambient lighting, thus looks dark (in most case).
Try this :
C++ Syntax (Toggle Plain Text)
glBegin(GL_POLYGON); glNormal(0.0f,0.0f,1.0f); glVertex3d(-8,-8,0); glVertex3d(8,-8,0); glVertex3d(8,-2,0); glVertex3d(-8,-2,0); glEnd();
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
•
•
Join Date: Oct 2008
Posts: 89
Reputation:
Solved Threads: 0
0
#5 33 Days Ago
•
•
•
•
Did you call glNormalize in your init function ?
By having proper normals the light "reflects" of the shape and thus it works out as planned. If you don't calculate normals then your object
feels the effect of ambient lighting, thus looks dark (in most case).
Try this :
C++ Syntax (Toggle Plain Text)
glBegin(GL_POLYGON); glNormal(0.0f,0.0f,1.0f); glVertex3d(-8,-8,0); glVertex3d(8,-8,0); glVertex3d(8,-2,0); glVertex3d(-8,-2,0); glEnd();
0
#6 32 Days Ago
wanna send me the file so I can take a look at it. Make sure you
have enabled lighting and LIGHT0
have enabled lighting and LIGHT0
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
•
•
Join Date: Oct 2008
Posts: 89
Reputation:
Solved Threads: 0
0
#7 32 Days Ago
Here is my code. It is supposed to be a sky background, with a green floor and a sphere in the air. Doesn't really matter what color the sphere is. I think it is blue now. I really appreciate your help!
C++ Syntax (Toggle Plain Text)
#define GLUT_DISABLE_ATEXIT_HACK #include <stdlib.h> #include <GL/glut.h> // Globals void display(); void myInit(); //Set up material GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 }; GLfloat mat_ambient_color[] = { 1,1,1,1 }; GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat no_shininess[] = { 0.0 }; GLfloat low_shininess[] = { 5.0 }; GLfloat high_shininess[] = { 100.0 }; GLfloat mat_emission[] = {1,1,1,1}; //Set up lighting GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0}; GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0}; GLfloat ambient0[]={1.0, 1.0, 1.0, 1.0}; GLfloat specular0[]={1.0, 1.0, 1.0, 1.0}; GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 }; GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 }; GLfloat local_view[] = { 0.0 }; GLfloat global_ambient[]={0,0,0,0}; void myReshape(int w, int h); void drawSphere(); GLsizei wh=800, ww=800; void myInit() { glClearColor(0.7f, 0.9f, 1.0f, 1.0f); // Set background color to sky blue. glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_POSITION, position); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Draw sphere glPushMatrix(); glTranslatef (-4.0, 5.0, 0.0); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); glutSolidSphere(.5, 50, 50); glPopMatrix(); //ground glMatrixMode(GL_PROJECTION); glColor3f(0.0,1.0,0.0); glBegin(GL_POLYGON); glNormal3f(0.0f,0.0f,1.0f); glVertex3d(-8,-8,0); glVertex3d(8,-8,0); glVertex3d(8,-2,0); glVertex3d(-8,-2,0); glEnd(); glFlush(); glutReshapeFunc(myReshape); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= (h * 2)) glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0); else glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ww=w; wh=h; } int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (800, 800); glutCreateWindow("Sphere"); myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMainLoop(); return 0; }
1
#8 32 Days Ago
You forgot to enable GL_COLOR_MATERIAL, when using lighting. I
changed it a little, take a look at the comments.
changed it a little, take a look at the comments.
C++ Syntax (Toggle Plain Text)
#define GLUT_DISABLE_ATEXIT_HACK #include <stdlib.h> #include <GL/glut.h> // Globals void display(); void myInit(); //Set up material GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 }; GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 }; GLfloat mat_ambient_color[] = { 1,1,1,1 }; GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 }; GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat no_shininess[] = { 0.0 }; GLfloat low_shininess[] = { 5.0 }; GLfloat high_shininess[] = { 100.0 }; GLfloat mat_emission[] = {1,1,1,1}; //Set up lighting GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0}; GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0}; GLfloat ambient0[]={0.4, 1.0, 1.0, 1.0}; GLfloat specular0[]={1.0, 1.0, 1.0, 1.0}; GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 }; GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 }; GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 }; GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 }; GLfloat local_view[] = { 0.0 }; GLfloat global_ambient[]={0,0,0,0}; void myReshape(int w, int h); void drawSphere(); GLsizei wh=800, ww=800; void myInit() { glClearColor(0.7f, 0.9f, 1.0f, 1.0f); // Set background color to sky blue. glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_POSITION, position); glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient); glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_NORMALIZE); //NEW glEnable(GL_COLOR_MATERIAL); glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient); glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient); glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess); glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); } void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); //Draw sphere glPushMatrix(); glTranslatef (-4.0, 5.0, 0.0); glColor3f(1.0f,1.0f,0.0f); glutSolidSphere(.5, 50, 50); glPopMatrix(); //ground glBegin(GL_POLYGON); glColor3f(0.0,1.0,0.0); glNormal3f(0.0f,0.0f,1.0f); glVertex3d(-8,-8,0); glVertex3d(8,-8,0); glVertex3d(8,-2,0); glVertex3d(-8,-2,0); glEnd(); //dont use glFlush() //glFlush(); //use these two instead glutSwapBuffers(); glutPostRedisplay(); //just call this once in your main glutReshapeFunc(myReshape); } void myReshape(int w, int h) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (w <= (h * 2)) glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w, 3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0); else glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2), 6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); ww=w; wh=h; } int main(int argc, char** argv) { glutInit(&argc, argv); //use GLUT_DOUBLE not GLUT_SINGLE glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize (800, 800); glutCreateWindow("Sphere"); myInit(); glutReshapeFunc(myReshape); glutDisplayFunc(display); glutMainLoop(); return 0; }
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
![]() |
Other Threads in the C++ Forum
- Previous Thread: Radix Sort Code
- Next Thread: Splitting a string into tokens using strtok and string as parameter
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






