My ground is not lighting up - opengl

Im a bit stuck with my lighting. My light can light up meshes but it does not light up my ground.
Here is some of my code
The light:
glEnable(GL_LIGHT0);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0f);
glEnable(GL_COLOR_MATERIAL);
GLfloat ambient_light0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position_light0[] = { 0.0f, 8.0f, 0.0f, 0.9f };
GLfloat spotDirection_light0[] = {0.0, -1.0, 0.0 };
GLfloat specularLightcolor[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv( GL_LIGHT0, GL_POSITION, position_light0 );
glLightfv( GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection_light0 );
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 0.5);
glLightfv( GL_LIGHT0, GL_AMBIENT, ambient_light0 );
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLightcolor);
glLightf( GL_LIGHT0, GL_SPOT_CUTOFF, 60.0f );
glLightf( GL_LIGHT0, GL_SPOT_EXPONENT, 50.0f );
The ground:
void drawGround(int id) {
glEnable(GL_TEXTURE_2D);
glEnable(GL_COLOR_MATERIAL);
getTexture(id);
glBindTexture(GL_TEXTURE_2D, id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
GLfloat material_diffuse[] = {0.9, 0.9, 0.9, 1.0 };
GLfloat material_specular[] = { 1, 1, 1, 1 };
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_specular);
glBegin(GL_QUADS);
for(int i = 0; i<3; i++){
for(int j= 0; j<3; j++){
glNormal3f(0.0f, 1.0f, 0.0f);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, material_specular);
glMaterialf(GL_FRONT, GL_SHININESS, 80.0);
glTexCoord2f(1, 1);
glVertex3f(-floorSize +((j)*(2*floorSize)/3), 0 ,-floorSize +((i)*(2*floorSize)/3));
glTexCoord2f(1, 0);
glVertex3f(-floorSize +((j)*(2*floorSize)/3), 0 ,-floorSize +((i+1)*(2*floorSize)/3));
glTexCoord2f(0,0);
glVertex3f(-floorSize +((j+1)*(2*floorSize)/3), 0 ,-floorSize +((i+1)*(2*floorSize)/3));
glTexCoord2f(0, 1);
glVertex3f(-floorSize +((j+1)*(2*floorSize)/3), 0 ,-floorSize +((i)*(2*floorSize)/3));
}
}
glDepthMask( 1.0 );
glDisable(GL_TEXTURE_2D);
glEnd();
}
Drawing of the ground happens after the light is called.
Has anyone any idea why my ground is not lighting up?
here is a screen shot of my problem --->

The problem is, that default fixed function OpenGL lighting does the illumination at the vertices only, and then just interpolates the resulting color. Since all the 4 vertices of the ground are dimly lit, the whole quad gets dim.
Solutions:
further subdivide your ground, so that there are more light sampling points. So far you've got only 3×3 points, which is not nearly sufficient. Think 100×100 or even more, to get a nice illumination effect.
or
use a fragment shader to implement per pixel lighting.
You're running into a typical OpenGL newbie pitfall there, BTW.
Update:
On a side note: You also did not supply normals for the plane. Therefore lighting will not work at all.

Related

Not all objects with same material look the same - Open GL/C++

So I have drawn 5 tori on my scene and a cube behind them and besides LIGHT0 I also have LIGHT1 enabled on click. I need to create two different materials where when I press on number 1 material 1 turns on, and when I press 2 material two turns on instead of material one. But what happens is, those materials get turned on only for tori, not the cube behind them. Here is everything in the code explained:
void light_1() {
GLfloat light_position[] = {1.0f, 1.0f, 1.0f, 0.0f};
GLfloat light_specular[] = {1.0f, 1.0f, 1.0f, 5.0f};
glLightfv(GL_LIGHT1, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
}
Cube and tori have same exact properties.
void drawCube() {
GLfloat mat_diffuse[] = {0.3, 0.0, 0.7, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
glutSolidCube(7.5);
}
void greenTorus() {
GLfloat mat_diffuse[] = {0.0, 1.0, 0.0, 1.0};
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
glutSolidTorus(0.08, 0.8, 30, 30);
}
Here is my display function. Ignore the variables, they are controled by different stuff and are irrelevant.
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75.0, 4.0/3.0, 1.0, 200.0);
light_1();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0+LR, 0.0+UD, 2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glTranslatef(0.0+tyg/2, 0.0-tbr/2, -5.0);
glRotated(a, 0, 0, 1);
glScaled(0.5+s, 0.5+s, 0.5+s);
greenTorus();
glPopMatrix();
glPushMatrix();
glTranslatef(0.0, -0.5, -9.0);
drawCube();
glPopMatrix();
glFlush();
glutPostRedisplay();
}
Here is the material that is used for bottom image.
void material_1() {
GLfloat m_ambient[] = {0.33f, 0.23f, 0.03f, 1.0f};
GLfloat m_diffuse[] = {0.8f, 0.6f, 0.1f, 1.0f};
GLfloat m_specular[] = {2.0f, 2.0f, 2.0f, 1.0f};
GLfloat m_shininess = 128.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, m_ambient);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, m_diffuse);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, m_specular);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, m_shininess);
}
Does anyone see the issue? Here is a screenshot of the scene whole program renders when lights 0 and 1 are both turned on and material_1 is turned on. Why isn't the cube also highlighted? Why does LIGHT0 light on all objects, but LIGHT1 only on tori?

Problems with camera view and lighting (openGL & GLUT)

I am trying to implement the bottom subwindow with three torus, but failed to display anything on the subwindow..
main function for subwindow:
instrument_window = glutCreateSubWindow(main_window, GAP, view_height + 3*GAP, 2*(view_width+GAP), INSTRUMENT_HEIGHT);
glutDisplayFunc(display);
//lighting
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 600, 1200, 1200, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
//glClearColor(0.33,0.33,0.33,0.33);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
//end of lighting
glutMainLoop();
for display function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float width = 2*(GAP+view_width);
float height = INSTRUMENT_HEIGHT;
const float ar = (float) width / (float) height;
//glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(width/4.0, width*3.0/4.0, height*3.0/4.0, height/4.0, 2.0, 100.0);
//gluPerspective (160.0, 2*view_width/INSTRUMENT_HEIGHT, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
/*
glMatrixMode(GL_PROJECTION);
// Reset transformations
glLoadIdentity();
gluPerspective (160.0, 2*view_width/INSTRUMENT_HEIGHT, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Draw first
draw (view_width+GAP-400, INSTRUMENT_HEIGHT/2);
// Draw second
draw (view_width+GAP-150, INSTRUMENT_HEIGHT/2);
// Draw third
draw (view_width+GAP+100, INSTRUMENT_HEIGHT/2);
for draw function:
void draw(double cx,double cf)
glColor3f(0.34f,0.34f,0.34f);
glPushMatrix();
glTranslated(0.0, cx, cy);
glRotated(-10, 1.0, 0.0, 0.0);
glutSolidTorus(OUTER_DIAL_RADIUS, OUTER_DIAL_RADIUS+30, 100, 200);
glPopMatrix();
end
really frustrating since it just display nothing and don't know how to debug. Any hints are highly appreciate!!!!
To be honest, its difficult to answer this question from the code example supplied. Half of the 'displayFunction' is commented out from the /* so nothing below it will get called. Even then its difficult to deduce the problem since there is little code relating to multiple viewports here. Since you are using fixed pipeline GL look at NeHe which has a good tutorial on employing multiple viewports.
http://nehe.gamedev.net/tutorial/multiple_viewports/20002/
Debugging is the single most important tool a developer has in their arsenal to resolve problems. It would be wise to learn how to do this as it would certainly save you time in the long run and you will find the answers to most (if not all) of your coding problems!

GL_TEXTURE_2D making problems for lighting?

I wanted to ask you about texture and lighting.
I am using a 3ds Object in OpenGL, and a BITMAP texture. It is working perfectly. When I use lighting it didn't reflect the light. While I was searching I just commented the line: glEnable(GL_TEXTURE_2D); and the Texture was gone but lighting works!
Is there any opportunity that I can leave TEXTURE and also add lighting? Why is this happening? Anyone any idea?
EDITED
this is in INIT() function
void initialize(){
glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)
glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
// Light model parameters:
// -------------------------------------------
GLfloat lmKa[] = {0.0, 0.0, 0.0, 0.0 };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmKa);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, 1.0);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, 0.0);
// -------------------------------------------
// Spotlight Attenuation
GLfloat spot_direction[] = {1.0, -1.0, -1.0 };
GLint spot_exponent = 30;
GLint spot_cutoff = 180;
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLighti(GL_LIGHT0, GL_SPOT_EXPONENT, spot_exponent);
glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, spot_cutoff);
GLfloat Kc = 1.0;
GLfloat Kl = 0.0;
GLfloat Kq = 0.0;
glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION,Kc);
glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, Kl);
glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, Kq);
// -------------------------------------------
// Lighting parameters:
GLfloat light_pos[] = {0.0f, 5.0f, 5.0f, 1.0f};
GLfloat light_Ka[] = {1.0f, 0.5f, 0.5f, 1.0f};
GLfloat light_Kd[] = {1.0f, 0.1f, 0.1f, 1.0f};
GLfloat light_Ks[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_Ka);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_Kd);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_Ks);
// -------------------------------------------
// Material parameters:
GLfloat material_Ka[] = {0.5f, 0.0f, 0.0f, 1.0f};
GLfloat material_Kd[] = {0.4f, 0.4f, 0.5f, 1.0f};
GLfloat material_Ks[] = {0.8f, 0.8f, 0.0f, 1.0f};
GLfloat material_Ke[] = {0.1f, 0.0f, 0.0f, 0.0f};
GLfloat material_Se = 20.0f;
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material_Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE, material_Kd);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material_Ks);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material_Ke);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material_Se);
}
this is the DISPLAY() function (I am using QT)
void pointGL(){
if(change)
{
ThreeDModels objectClass;
objectClass.SpaceShip();
change = false;
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glScalef(0.05, 0.05, 0.05);
ThreeDModels objectiModel;
objectiModel.objectCreation(l_index);
glPopMatrix();
}
Method which other method for Loading 3ds object and add Texture
void ThreeDModels::SpaceShip()
{
Load3DS(&objecti, "C:/Users/Documents/3DModelRendering/3DModels/Spaceship/spaceship.3ds");
int a = LoadBitmap2("C:/Users/Documents/3DModelRendering/3DModels/Spaceship/spaceshiptexture.bmp");
}
void ThreeDModels::objectCreation(int l_index)
{
glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)
for (l_index = 0; l_index<objecti.polygons_qty; l_index++)
{
//----------------- FIRST VERTEX -----------------
// Texture coordinates of the first vertex
glTexCoord2f(objecti.mapcoord[objecti.polygon[l_index].a].u,
objecti.mapcoord[objecti.polygon[l_index].a].v);
// Coordinates of the first vertex
glVertex3f(objecti.vertex[objecti.polygon[l_index].a].x,
objecti.vertex[objecti.polygon[l_index].a].y,
objecti.vertex[objecti.polygon[l_index].a].z); //Vertex definition
//----------------- SECOND VERTEX -----------------
// Texture coordinates of the second vertex
glTexCoord2f(objecti.mapcoord[objecti.polygon[l_index].b].u,
objecti.mapcoord[objecti.polygon[l_index].b].v);
// Coordinates of the second vertex
glVertex3f(objecti.vertex[objecti.polygon[l_index].b].x,
objecti.vertex[objecti.polygon[l_index].b].y,
objecti.vertex[objecti.polygon[l_index].b].z);
//----------------- THIRD VERTEX -----------------
// Texture coordinates of the third vertex
glTexCoord2f(objecti.mapcoord[objecti.polygon[l_index].c].u,
objecti.mapcoord[objecti.polygon[l_index].c].v);
// Coordinates of the Third vertex
glVertex3f(objecti.vertex[objecti.polygon[l_index].c].x,
objecti.vertex[objecti.polygon[l_index].c].y,
objecti.vertex[objecti.polygon[l_index].c].z);
}
glEnd();
}
I found the Answer guys:
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); was in the LoadBitmap() method, just removing this one, and it worked ;D

OpenGL Lighting and Material Error When Switching Between Different Scenes

I write a switch structure below to choose different scenes. For each case with a different scene, I enable something like lights, color material and depth test while I also disable them respectively in the end of this case. However, when switching between those scenes, something come wrong. Like, if I go to scene 3 first then go to scene 1, scene 1 seems affected by something extra from scene 3, such as unexpected light and material property would appear in scene 1.
Since I use different LIGHT in each scene, I don't think it's caused by lighting. However, I failed to see anything wrong in the material setting.
Could anyone give me some hint?
switch(m_iSceneNum)
{
case 1:
glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight);
glLightfv(GL_LIGHT1, GL_AMBIENT, fLowLight);
glLightfv(GL_LIGHT1, GL_DIFFUSE, fBrightLight);
glLightfv(GL_LIGHT1, GL_SPECULAR, fBrightLight);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMateriali(GL_FRONT, GL_SHININESS, 128);
glMaterialfv(GL_FRONT, GL_SPECULAR, fSpec);
////////////////////////////////////////////////////////
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 4000);
gluLookAt(0, 0 , 50.0f ,0.0f, 0.0f , 0.0f , 0.0f , 1.0f , 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT1, GL_POSITION, fLightPos1);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, fLightDirection1);
glLightfv(GL_LIGHT1, GL_SPOT_CUTOFF, fLightCutoff1);
glColor3f(1,0,0);
glBegin(GL_QUADS);
glVertex3f(-10, 10, -10);
glVertex3f(10, 10, -10);
glVertex3f(10, -10, 10);
glVertex3f(-10, -10, 10);
glEnd();
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_LIGHT1);
glDisable(GL_LIGHTING);
glDisable(GL_DEPTH_TEST);
glFlush();
break;
case 3:
glClearColor(0, 0, 0, 1);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight);
glLightfv(GL_LIGHT3, GL_AMBIENT, fLowLight);
glLightfv(GL_LIGHT3, GL_DIFFUSE, fBrightLight);
glLightfv(GL_LIGHT3, GL_SPECULAR, fBrightLight);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT3);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, fSpec);
glMateriali(GL_FRONT, GL_SHININESS, 0);
////////////////////////////////////////////////////////
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, 1, 1, 4000);
gluLookAt(0, 0, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT3, GL_POSITION, fLightPos3);
glRotatef(m_fAngle, 0, 0, 1);
glBegin(GL_TRIANGLES);
glColor3f(1,0,0);
glNormal3f(0, 0, -1);
glVertex3f(0, 5, 0);
glColor3f(0,1,0);
glNormal3f(1, 1 ,1);
glVertex3f(5, 0, 0);
glColor3f(0,0,1);
glNormal3f(0, 1, 1);
glVertex3f(-5, 0, 0);
glEnd();
glDisable(GL_LIGHT3);
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glDisable(GL_DEPTH_TEST);
glFlush();
break;
default:
break;
}
You never provide normal(s) for the quad in case 1.
So you'll get the default normal (if case 1 is the first/only thing executed) or the last normal you set in case 3.
It's as if you're assuming glEnd() resets the current normal to the default somehow. It doesn't.

Problems with OpenGL lighting

I'm having trouble being able to see objects I've created when I have enabled lighting in OpenGL. I have an object that is imported from 3D Max that the lighting works correctly on but the rest of my scene does not. I know that I need to specify normals but this hasn't seemed to have helped. Although if I create a simple polygon in my display() function that works correctly but other polygons that have been created in methods of a class and called in the display() function are not showing up
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 is creating 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();
This is 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 can't see any of this?
UPDATE:
I can see my sea if I disable texturing. How can I fix it so I can use textures and lighting?
UPDATE 2:
Have changed texturing to GL_MODULATE but I also had to remove the blending to make it work. Do I need blending enabled?
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
says to replace the lighting computation with the result of texturing. If you want lighting to affect texturing, us GL_MODULATE instead of GL_REPLACE.
Edit to add:
You only need blending if you want your geometry to be translucent (that's what blending is typically for). In your case, there are a number of issues in the code:
your light colors are fully transparent (all have 0. as the 4th component). So your light is making everything invisible. change it to 1.
your materials don't have a 4th component. That's even worse, as glMaterialfv expects 4 floats. That could even crash your app or reformat your disk. Add a forth component, preferably to 1. to make it fully opaque.
Try adding an ambient light for the whole scene. Something like:
GLfloat lightColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lightColor);
In your glMaterialfv calls, the color parameter should have 4 values, not 3. Normally the 4th value is an alpha of 1.0. I'm not sure if it's OK to pass 0.0 as the 4th value for glLightfv either.