OpenGL backlight issues

I am having problems with the ability to see objects that I created when I turned on lighting in OpenGL. I have an object that is imported from 3D Max, that the lighting works correctly, but there isn’t the rest of my scene. I know that I need to specify the normals, but that didn't seem to help. Although, if I create a simple polygon in my display () function, which works correctly, other polygons created in class methods and called in display () function are not displayed

Here is my lighting code

glewInit(); glClearColor(0.0,0.0,0.0,0.0); glClear(GL_COLOR_BUFFER_BIT) glShadeModel(GL_SMOOTH); //light position and colour GLfloat light_position[] = { 0.0, 0.0, 20.0,0.0 }; GLfloat white_light[] = {0.8,0.8,0.8,0.0}; GLfloat diff_light[] = {1.0,1.0,1.0,0.0}; GLfloat spec_light[] = {1.0,1.0,1.0,0.0}; glLightfv(GL_LIGHT0, GL_AMBIENT, white_light); glLightfv(GL_LIGHT0, GL_DIFFUSE, diff_light); glLightfv(GL_LIGHT0, GL_SPECULAR, spec_light); glLightfv(GL_LIGHT0, GL_POSITION, light_position); //ambient light GLfloat ambient[] = {0.3,0.3,0.3}; glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); //diffuse material component GLfloat diff[] = {0.6,0.6,0.6}; glMaterialfv(GL_FRONT, GL_DIFFUSE, diff); //specular material component GLfloat WhiteSpec[] = {1,1,1}; glMaterialfv(GL_FRONT, GL_SPECULAR, WhiteSpec); GLfloat shininess = 50; glMaterialf(GL_FRONT, GL_SHININESS, shininess); //ENABLE LIGHTING AND DEPTH TEST glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glEnable(GL_DEPTH_TEST); 

This is my class method that creates my sea.

 glColor3f(0,0,1); glPushMatrix(); //enable texturing glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBindTexture(GL_TEXTURE_2D, seaTex); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); for(int i = 0, k = 0; i < (getWidth()/10); i++){ for(int j = 0; j < (getLength()/10); j++){ if(i >= Sea::waveLoc1 && i <= Sea::waveLoc1+Sea::sinArrayLength){ int nextK = k+1; if(nextK == Sea::sinArrayLength){ nextK = 0; } if(i == Sea::waveLoc1+Sea::sinArrayLength){ //front of wave glBegin(GL_POLYGON); glNormal3f(0.0f, 1.0f, 0.0f); glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::sinVals[nextK], Sea::seaGrid[i][j].z); glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i][j+1].z); glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::seaGrid[i+1][j+1].y , Sea::seaGrid[i+1][j+1].z); glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::seaGrid[i+1][j].y, Sea::seaGrid[i+1][j].z); glEnd(); }else{ //rest of wave glBegin(GL_POLYGON); glNormal3f(0.0f, 1.0f, 0.0f); glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::sinVals[k], Sea::seaGrid[i][j].z); glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::sinVals[k], Sea::seaGrid[i][j+1].z); glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j+1].z); glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::sinVals[nextK], Sea::seaGrid[i+1][j].z); glEnd(); } }else{ //draw flat sea glBegin(GL_POLYGON); glNormal3f(0.0f, 1.0f, 0.0f); glTexCoord2f(0.0, 1.0);glVertex3f(Sea::seaGrid[i+1][j].x, Sea::seaGrid[i+1][j].y, Sea::seaGrid[i+1][j].z); glTexCoord2f(0.0, 0.0);glVertex3f(Sea::seaGrid[i+1][j+1].x, Sea::seaGrid[i+1][j+1].y, Sea::seaGrid[i+1][j+1].z); glTexCoord2f(1.0, 0.0);glVertex3f(Sea::seaGrid[i][j+1].x, Sea::seaGrid[i][j+1].y, Sea::seaGrid[i][j+1].z); glTexCoord2f(1.0, 1.0);glVertex3f(Sea::seaGrid[i][j].x, Sea::seaGrid[i][j].y, Sea::seaGrid[i][j].z); glEnd(); } } //increment k if i is in the area of the wave if(k < Sea::sinArrayLength-1 && (i >= Sea::waveLoc1 && i <= Sea::waveLoc1+Sea::sinArrayLength)){ k++; }else if(k == Sea::sinArrayLength){ k = 0; } } if(Sea::waveLoc1 < 100 && Sea::waveInc == Sea::waveSpeedLimiter){ Sea::waveLoc1 +=1; }else if(Sea::waveLoc1 >= 100){ Sea::waveLoc1 = 0; } //limits speed of wave if(Sea::waveInc < Sea::waveSpeedLimiter){ Sea::waveInc++; }else{ Sea::waveInc = 0; } //disable texturing glDisable(GL_BLEND); glDisable(GL_TEXTURE_2D); glPopMatrix(); 

Then called in my display () function as below

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)screenWidth/(GLfloat)screenHeight,0.1f,1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); camera.updateCameraPosition(mouse_x,mouse_y,screenWidth,screenHeight); sea.buildSeaPlane(); scene.buildEdges(); glPushMatrix(); glColor3f(0,1,0); glTranslatef(0, 20, 200); model.speedDisplayFaceNormals(); glPopMatrix(); glPushMatrix(); plane.updatePlanePosition(); glBegin(GL_POLYGON); glNormal3f(0.0f, 1.0f, 0.0f); glVertex3f(0, 25, 10); glVertex3f(2, 25, 10); glVertex3f(2, 25, 20); glVertex3f(0, 25, 20); glEnd(); glPopMatrix(); glFlush(); 

Any idea why I don't see any of this?

UPDATE:

I can see my sea if I turn off texturing. How can I fix this to use textures and lighting?

UPDATE 2:

Changed the texture to GL_MODULATE, but I also had to remove the blend to make it work. Do I need to enable blending?

+4
source share
3 answers
 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 

says to replace the lighting calculation with a texturing result. If you want lighting to affect texturing, we are GL_MODULATE instead of GL_REPLACE .

Edit to add:

You only need blending if you want your geometry to be translucent (which is commonly used for blending). In your case, the code has a number of problems:

  • your light colors are completely transparent (they all have 0. as the 4th component). Thus, your light makes everything invisible. change it to 1.
  • There is no 4th component in your materials. This is even worse as glMaterialfv expects 4 floats. It can even crash your application or reformat your disk. Add a fourth component, preferably up to 1., to make it completely opaque.
+2
source

Try adding ambient light to the entire scene. Sort of:

 GLfloat lightColor[] = {1.0f, 1.0f, 1.0f, 1.0f}; glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightColor); 
0
source

In your calls to glMaterialfv the color parameter should have 4 values, not 3. Usually the 4th value is alpha 1.0. I'm not sure if OK to pass 0.0 as the 4th value for glLightfv .

0
source

Source: https://habr.com/ru/post/1382977/


All Articles