943,946 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1763
  • C++ RSS
Oct 29th, 2009
0

glColor problem

Expand Post »
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)
  1. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  2.  
  3. //Draw sphere
  4. glPushMatrix();
  5. glTranslatef (-4.0, 5.0, 0.0);
  6. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  7. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  8. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
  9. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  10. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  11. glutSolidSphere(.5, 50, 50);
  12. glPopMatrix();
  13.  
  14. //ground
  15. glMatrixMode(GL_PROJECTION);
  16. glColor3f(0.0,1.0,0.0);
  17. glBegin(GL_POLYGON);
  18. glVertex3d(-8,-8,0);
  19. glVertex3d(8,-8,0);
  20. glVertex3d(8,-2,0);
  21. glVertex3d(-8,-2,0);
  22. glEnd();
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Oct 29th, 2009
0
Re: glColor problem
Its because you don't have your normals calculated for rectangles.
Plus put your lighting function in our initGL function.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Oct 30th, 2009
0
Re: glColor problem
Its because you don't have your normals calculated for rectangles.
Plus put your lighting function in our initGL function.
What do you mean by my normals? It worked fine and was green until until I added my sphere. Now my ground and sphere is just gray.
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Oct 30th, 2009
0
Re: glColor problem
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)
  1. glBegin(GL_POLYGON);
  2. glNormal(0.0f,0.0f,1.0f);
  3. glVertex3d(-8,-8,0);
  4. glVertex3d(8,-8,0);
  5. glVertex3d(8,-2,0);
  6. glVertex3d(-8,-2,0);
  7. glEnd();
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 1st, 2009
0
Re: glColor problem
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)
  1. glBegin(GL_POLYGON);
  2. glNormal(0.0f,0.0f,1.0f);
  3. glVertex3d(-8,-8,0);
  4. glVertex3d(8,-8,0);
  5. glVertex3d(8,-2,0);
  6. glVertex3d(-8,-2,0);
  7. glEnd();
Ok, I tried that, didn't do anything. I put glEnable(GL_NORMALIZE); in my init() function. Everything is still gray.
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Nov 1st, 2009
0
Re: glColor problem
wanna send me the file so I can take a look at it. Make sure you
have enabled lighting and LIGHT0
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 1st, 2009
0
Re: glColor problem
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)
  1. #define GLUT_DISABLE_ATEXIT_HACK
  2. #include <stdlib.h>
  3. #include <GL/glut.h>
  4.  
  5.  
  6. // Globals
  7. void display();
  8. void myInit();
  9. //Set up material
  10. GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  11. GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
  12. GLfloat mat_ambient_color[] = { 1,1,1,1 };
  13. GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
  14. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  15. GLfloat no_shininess[] = { 0.0 };
  16. GLfloat low_shininess[] = { 5.0 };
  17. GLfloat high_shininess[] = { 100.0 };
  18. GLfloat mat_emission[] = {1,1,1,1};
  19.  
  20. //Set up lighting
  21. GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0};
  22.  
  23. GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0};
  24. GLfloat ambient0[]={1.0, 1.0, 1.0, 1.0};
  25. GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};
  26.  
  27. GLfloat ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  28. GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  29. GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  30. GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
  31. GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
  32. GLfloat local_view[] = { 0.0 };
  33. GLfloat global_ambient[]={0,0,0,0};
  34.  
  35. void myReshape(int w, int h);
  36. void drawSphere();
  37. GLsizei wh=800, ww=800;
  38.  
  39.  
  40.  
  41.  
  42. void myInit()
  43. {
  44.  
  45.  
  46. glClearColor(0.7f, 0.9f, 1.0f, 1.0f); // Set background color to sky blue.
  47. glEnable(GL_DEPTH_TEST);
  48. glShadeModel(GL_SMOOTH);
  49.  
  50. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  51. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  52. glLightfv(GL_LIGHT0, GL_POSITION, position);
  53. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  54. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  55.  
  56. glEnable(GL_LIGHTING);
  57. glEnable(GL_LIGHT0);
  58. glEnable(GL_NORMALIZE);
  59. }
  60.  
  61.  
  62. void display(void)
  63. {
  64. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  65.  
  66. //Draw sphere
  67. glPushMatrix();
  68. glTranslatef (-4.0, 5.0, 0.0);
  69. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  70. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  71. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
  72. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  73. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  74. glutSolidSphere(.5, 50, 50);
  75. glPopMatrix();
  76.  
  77. //ground
  78. glMatrixMode(GL_PROJECTION);
  79. glColor3f(0.0,1.0,0.0);
  80. glBegin(GL_POLYGON);
  81. glNormal3f(0.0f,0.0f,1.0f);
  82. glVertex3d(-8,-8,0);
  83. glVertex3d(8,-8,0);
  84. glVertex3d(8,-2,0);
  85. glVertex3d(-8,-2,0);
  86. glEnd();
  87. glFlush();
  88. glutReshapeFunc(myReshape);
  89. }
  90.  
  91.  
  92. void myReshape(int w, int h)
  93. {
  94. glViewport(0, 0, w, h);
  95. glMatrixMode(GL_PROJECTION);
  96. glLoadIdentity();
  97. if (w <= (h * 2))
  98. glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w,
  99. 3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
  100. else
  101. glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2),
  102. 6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
  103. glMatrixMode(GL_MODELVIEW);
  104. glLoadIdentity();
  105. ww=w;
  106. wh=h;
  107. }
  108.  
  109.  
  110. int main(int argc, char** argv)
  111. {
  112. glutInit(&argc, argv);
  113. glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
  114. glutInitWindowSize (800, 800);
  115. glutCreateWindow("Sphere");
  116. myInit();
  117. glutReshapeFunc(myReshape);
  118. glutDisplayFunc(display);
  119. glutMainLoop();
  120. return 0;
  121. }
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008
Nov 2nd, 2009
1
Re: glColor problem
You forgot to enable GL_COLOR_MATERIAL, when using lighting. I
changed it a little, take a look at the comments.
C++ Syntax (Toggle Plain Text)
  1. #define GLUT_DISABLE_ATEXIT_HACK
  2. #include <stdlib.h>
  3. #include <GL/glut.h>
  4.  
  5.  
  6. // Globals
  7. void display();
  8. void myInit();
  9. //Set up material
  10. GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
  11. GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
  12. GLfloat mat_ambient_color[] = { 1,1,1,1 };
  13. GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
  14. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  15. GLfloat no_shininess[] = { 0.0 };
  16. GLfloat low_shininess[] = { 5.0 };
  17. GLfloat high_shininess[] = { 100.0 };
  18. GLfloat mat_emission[] = {1,1,1,1};
  19.  
  20. //Set up lighting
  21. GLfloat light0_pos[]={20.0, 100.0, 1.0, 0.0};
  22.  
  23. GLfloat diffuse0[]={1.0,1.0, 1.0, 1.0};
  24. GLfloat ambient0[]={0.4, 1.0, 1.0, 1.0};
  25. GLfloat specular0[]={1.0, 1.0, 1.0, 1.0};
  26.  
  27. GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
  28. GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  29. GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
  30. GLfloat position[] = { 0.0, 3.0, 2.0, 0.0 };
  31. GLfloat lmodel_ambient[] = { 0.4, 0.4, 0.4, 1.0 };
  32. GLfloat local_view[] = { 0.0 };
  33. GLfloat global_ambient[]={0,0,0,0};
  34.  
  35. void myReshape(int w, int h);
  36. void drawSphere();
  37. GLsizei wh=800, ww=800;
  38.  
  39. void myInit()
  40. {
  41.  
  42. glClearColor(0.7f, 0.9f, 1.0f, 1.0f); // Set background color to sky blue.
  43. glEnable(GL_DEPTH_TEST);
  44. glShadeModel(GL_SMOOTH);
  45.  
  46. glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  47. glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  48. glLightfv(GL_LIGHT0, GL_POSITION, position);
  49.  
  50. glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  51. glLightModelfv(GL_LIGHT_MODEL_LOCAL_VIEWER, local_view);
  52.  
  53. glEnable(GL_LIGHTING);
  54. glEnable(GL_LIGHT0);
  55. glEnable(GL_NORMALIZE);
  56. //NEW
  57. glEnable(GL_COLOR_MATERIAL);
  58.  
  59. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  60. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  61. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_ambient);
  62. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  63. glMaterialfv(GL_FRONT, GL_EMISSION, no_mat);
  64. }
  65.  
  66.  
  67. void display(void)
  68. {
  69. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  70. glLoadIdentity();
  71.  
  72. //Draw sphere
  73. glPushMatrix();
  74. glTranslatef (-4.0, 5.0, 0.0);
  75. glColor3f(1.0f,1.0f,0.0f);
  76. glutSolidSphere(.5, 50, 50);
  77. glPopMatrix();
  78.  
  79. //ground
  80. glBegin(GL_POLYGON);
  81. glColor3f(0.0,1.0,0.0);
  82. glNormal3f(0.0f,0.0f,1.0f);
  83. glVertex3d(-8,-8,0);
  84. glVertex3d(8,-8,0);
  85. glVertex3d(8,-2,0);
  86. glVertex3d(-8,-2,0);
  87. glEnd();
  88.  
  89. //dont use glFlush()
  90. //glFlush();
  91.  
  92. //use these two instead
  93. glutSwapBuffers();
  94. glutPostRedisplay();
  95.  
  96. //just call this once in your main
  97. glutReshapeFunc(myReshape);
  98. }
  99.  
  100.  
  101. void myReshape(int w, int h)
  102. {
  103. glViewport(0, 0, w, h);
  104. glMatrixMode(GL_PROJECTION);
  105. glLoadIdentity();
  106. if (w <= (h * 2))
  107. glOrtho (-6.0, 6.0, -3.0*((GLfloat)h*2)/(GLfloat)w,
  108. 3.0*((GLfloat)h*2)/(GLfloat)w, -10.0, 10.0);
  109. else
  110. glOrtho (-6.0*(GLfloat)w/((GLfloat)h*2),
  111. 6.0*(GLfloat)w/((GLfloat)h*2), -3.0, 3.0, -10.0, 10.0);
  112. glMatrixMode(GL_MODELVIEW);
  113. glLoadIdentity();
  114. ww=w;
  115. wh=h;
  116. }
  117.  
  118.  
  119. int main(int argc, char** argv)
  120. {
  121. glutInit(&argc, argv);
  122. //use GLUT_DOUBLE not GLUT_SINGLE
  123. glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  124. glutInitWindowSize (800, 800);
  125. glutCreateWindow("Sphere");
  126. myInit();
  127. glutReshapeFunc(myReshape);
  128. glutDisplayFunc(display);
  129. glutMainLoop();
  130. return 0;
  131. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 2nd, 2009
0
Re: glColor problem
Thank you so much! I never would had thought to enable GL_COLOR_MATERIAL.
Reputation Points: 16
Solved Threads: 0
Junior Poster
sfrider0 is offline Offline
149 posts
since Oct 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Radix Sort Code
Next Thread in C++ Forum Timeline: Splitting a string into tokens using strtok and string as parameter





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC