GL_TEXTURE_2D making problems for lighting? - opengl

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

Related

How to display specular spot in an object in OpenGL

I am creating a sphere. The light source (ambient and diffuse lighting) works well. Only the specular light that doesn't show up in any part of the sphere.
The material code seems not working. I deleted the line and the result is also the same.
The result I want is the sphere rotates with white spot on it in order to look real.
Am I missing any required configuration for lighting?
Is there any solution?
Thanks.
float angle = 0.0f;
void initRendering(){
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}
void draw(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glRotatef(angle, 0.0, 1.0, 0.0);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();
GLfloat ambientColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat diffuseColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specularColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat lightPosition[] = {0.0f, 5.0f, -3.0f, 0.0f};
GLfloat mat_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMateriali(GL_FRONT, GL_SHININESS, 100);
angle+=0.1f;
glutPostRedisplay();
glutSwapBuffers();
}
main(){
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Sphere");
glClearColor(1.0, 1.0, 1.0, 1.0);
initRendering();
glutDisplayFunc(draw);
glutMainLoop();
}
The default for specular light is to add to the colour evaulated in the diffuse illumination step. The specular intensity is modulated by the specular set. You've set this colour to "black", so it adds literally zero.
GLfloat mat_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
// …
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
I'd start by actually not inhibiting the specular term.

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!

Spotlight issue OpenGL

I have a problem with spotlight in OpenGL library. A scene is black, light is unseen if the GL_SPOT_CUTOFF is less than 125. I supposed that issue concerns direction of the light source but I have tried plenty of options both for position and direction of spotlight. Here is my code:
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 };
glPushMatrix();
glLoadIdentity();
GLfloat spot_direction[] = { 0.0, -1.0, 0.0 ,0.0};
GLfloat spot_position[] = { 1.0,1.0,0.0,1.0 };
glLightfv(GL_LIGHT1, GL_POSITION, spot_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 60);
glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 100);
glPopMatrix();
glEnable(GL_LIGHT1);
Solved. Problem was with too high GL_SPOT_EXPONENT factor (light was too much focused) and with direction of the light stream.

Color changes when scaling a gluQuadric

I have a problem when I draw a scaled (with glScale) gluSphere.
The Color changes after Scaling and the darker faces are not as dark as they should be...
This is how I set up the light:
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
GLfloat LAmbient[4] = {0.2f, 0.2f, 0.2f, 1.0};
GLfloat LDiffuse[4] = {1.0f, 1.0f, 1.0f, 1.f};
GLfloat LSpecular[4] = {1.0f, 1.0f, 1.0f, 0.0f};
GLfloat LPosition[4] = {100.0f, -200.0f, -50.0f, 0.0f};
GLfloat LSpotDirec[3] = {0.0,0.0,0.0};
GLfloat LSpotCutOff = 180.0f;
GLfloat LSpotExponent = 0.0f;
GLfloat LAttenuationConst = 1.0f;
GLfloat LAttenuationLinear = 0.0f;
GLfloat LAttenuationQuadrat = 0.0f;
glLightfv(GL_LIGHT0, GL_AMBIENT, LAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, LPosition);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, LSpotDirec);
glLightfv(GL_LIGHT0, GL_SPOT_CUTOFF, &LSpotCutOff);
glLightfv(GL_LIGHT0, GL_SPOT_EXPONENT, &LSpotExponent);
glLightfv(GL_LIGHT0, GL_CONSTANT_ATTENUATION, &LAttenuationConst);
glLightfv(GL_LIGHT0, GL_LINEAR_ATTENUATION, &LAttenuationLinear);
glLightfv(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, &LAttenuationQuadrat);
Here is my code, that sets up a new glList, where the glu Sphere is drawn:
//Sphere
GLUquadric * pSphere = gluNewQuadric();
glNewList(LIST_SPHERE, GL_COMPILE);//'LIST_SPHERE' is defined above
gluQuadricTexture(pSphere, GL_FALSE);
gluQuadricOrientation(pSphere, GLU_OUTSIDE);
gluQuadricDrawStyle(pSphere, GLU_FILL);
gluQuadricNormals(pSphere, GLU_SMOOTH);
gluQuadricCallback(pSphere, GLU_ERROR, NULL);
gluSphere(pSphere, 1.0f, 20, 10);
glEndList();
and here is the code, where the List is called:
glPushMatrix();
//drawing a white Sphere with a radius of 6
glColor3f(1.0f, 1.0f, 1.0f);
glScalef(6.f,6.f,6.f);
glCallList(LIST_SPHERE);
glPopMatrix();
Sphere drawn with 'glScalef(1.0f, 1.0f, 1.0f)' (no scale)
Sphere drawn with 'glScalef(6.0f, 6.0f, 6.0f)'
I hope you have any Idea, why things are not working probably.
The normals are not being scaled correctly (they are non-unit length after scaling).
You have two options to solve this:
GL_NORMALIZE -- This will renormalize your normals, which is costly.
GL_RESCALE_NORMAL -- This will simply rescale them.
Option #2 is what you want here, since you applied a uniform scale (6.0x in every direction). If you had applied a non-uniform scale (e.g. glScalef (1.0f, 6.0f, 3.0f)) then this would not be a valid option and you would have to resort to option #1.
In either case, all you need to do is enable GL_NORMALIZE or GL_RESCALE_NORMAL to solve this problem.

Lighting planes (openGL)

I'd like to enable lighting in my openGL program, i already works for objects drawn with GLUT.
but when i draw my cuuboid
glBegin(5);
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_+(l/2)));
glEnd();
with
glColor3f(6,0,0);
lightning isn't applied on its surfaces. (the cuboid is drawn correct)
my lighning is set up like this:
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_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[] = { 2.0f, 5.0f, 1.0, 1.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 };
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);
does anyone know the flaw i make?
You didn't give it any normals. You need to use glNormal3f to specify the normals.
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0,1,0);
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glNormal3f(-1,0,0);
// etc.