glColor problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training

glColor problem

 
0
  #1
Oct 29th, 2009
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.
  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();
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,252
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
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.
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training
 
0
  #3
Oct 30th, 2009
Originally Posted by firstPerson View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,252
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
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 :
  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();
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training
 
0
  #5
33 Days Ago
Originally Posted by firstPerson View Post
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 :
  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,252
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
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
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training
 
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!
  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,252
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 155
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
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.
  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. }
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 89
Reputation: sfrider0 is an unknown quantity at this point 
Solved Threads: 0
sfrider0 sfrider0 is offline Offline
Junior Poster in Training
 
0
  #9
31 Days Ago
Thank you so much! I never would had thought to enable GL_COLOR_MATERIAL.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC