How to achieve brighter shininess using GL_LIGHT1? - c++

I have this image
Is there a way to achieve brighter look of the light without editing specular material part of the code?
This is my current code:
void drawTeapot() {
GLfloat mat_diffuse[] = {0.2, 1.0, 0.8, 1.0};
GLfloat mat_specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[] = {128.0};
GLfloat lmodel_ambient[] = {0.1, 0.1, 0.1, 1.0};
GLfloat light_position[] = {1.1, 1.1, 1.0, 1.0};
GLfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};
glLightfv(GL_LIGHT1, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_SPECULAR, mat_specular);
glLightfv(GL_LIGHT1, GL_SHININESS, mat_shininess);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glutSolidTeapot(0.5);
}
I have to use GL_LIGHT1 because there is another function that reacts to mouse clicks and turns that light on and off. Therefore, I can't have the specular on the object before the light turns on. It has to remain matte, sort of. As you can see, shininess is on max, specular also, but it's not really what I'm going for. I basically want it to be tiny bit bigger that it is now, more dispersed.

Related

OpenGL Lighitng

I am new to OpenGL, I made this to switch menu in between ambient, diffuse and specular, position light. It works but looks weird
The code (relevant part ) is:
void gfxinit(void)
{
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 0.0, 1.0, 1.0 };
GLfloat light_direction[] = { -1, -2, -1, 1 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
GLfloat global_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
glClearColor(0.0, 0.0, 0.0, 1.0); //background Color
list = glGenLists(1);
glNewList(list, GL_COMPILE);
glutSolidSphere(1.0, 30, 12);
glEndList();
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, global_ambient);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT2, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT3, GL_POSITION, light_position);
glLightf(GL_LIGHT4, GL_SPOT_EXPONENT, 2.0);
}
and
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.0, 0.0, tdist);
glRotatef((GLfloat)spinx, 1.0, 0.0, 0.0);
glRotatef((GLfloat)spiny, 0.0, 1.0, 0.0);
glRotatef((GLfloat)spinz, 0.0, 0.0, 1.0);
glMaterialfv(GL_FRONT, GL_AMBIENT, gray);
glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
glMaterialfv(GL_FRONT, GL_SPECULAR, gray);
glMaterialfv(GL_FRONT, GL_POSITION, white);
glMaterialf(GL_FRONT, GL_SHININESS, 70);
glMaterialfv(GL_FRONT, GL_SPECULAR, light_param);
glEnable(GL_LIGHTING);
glEnable(GL_POLYGON_OFFSET_FILL);
if (value == 2) {//ambient
glEnable(GL_LIGHT0);
glPolygonOffset(polyfactor, polyunits);
glCallList(list);
glDisable(GL_LIGHT0);
}
else if (value == 3) {//diffuse
glEnable(GL_LIGHT1);
glPolygonOffset(polyfactor, polyunits);
glCallList(list);
glDisable(GL_LIGHT1);
}
Really thanks if someone can tell me whether there is mistake here or not! Thanks!

Lighting and material color problems

I can't change any object color when lighting is enabled.
This is my init function (the part of the light and material):
glEnable (GL_LIGHTING);
glEnable (GL_LIGHT0);
GLfloat light_ambient[] = {0.5, 0.5, 0.5, 1.0};
GLfloat light_diffuse[] = {0.0, 0.5, 0.5, 1.0};
GLfloat light_specular[] = {0.0, 0.0, 0.5, 1.0};
GLfloat light_position[]={0, -1.0, 0, 0.0};
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);
Each object has a vector of colors. This is how I draw my objects:
float mat_a[] = {0.0, 0.0, 0.0, 1.0};
float mat_d[] = {0.0, 0.0, 0.0, 1.0};
float mat_s[] = {0.0, 0.0, 0.0, 1.0};
...
colors_vect = some_object->getColor();
glDisable(GL_COLOR_MATERIAL);
mat_a[0] = colors_vect.at(0);
mat_a[1] = colors_vect.at(1);
mat_a[2] = colors_vect.at(2);
mat_d[0] = colors_vect.at(3); //The values are correct.
mat_d[1] = colors_vect.at(4);
mat_d[2] = colors_vect.at(5);
mat_s[0] = colors_vect.at(6);
mat_s[1] = colors_vect.at(7);
mat_s[2] = colors_vect.at(8);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_a);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_d);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_s);
glEnable(GL_COLOR_MATERIAL);
glBegin(GL_POLYGON);
...
This code doesn't work. I tried to give the mat some random values and still nothing changes.
What am I doing wrong? Any help would be appreciated!
Your problem is caused by this line:
glEnable(GL_COLOR_MATERIAL);
This enables functionality that allows material properties to track the current color, which is the color set with regular glColor() calls. If this is enabled, glColorMaterial() controls which parts of the material parameters track the current color. The default is GL_AMBIENT_AND_DIFFUSE, which means that the current color is used for the ambient and diffuse material parameters.
You need to make a choice about how you want to update your material properties:
If you need to change colors very frequently, it can be most efficient to enable GL_COLOR_MATERIAL, and use glColor() to update the material properties.
You can use glColorMaterialfv(), which gives you more control, and e.g. allows you to set different colors for ambient and diffuse. For this approach, you need to keep GL_COLOR_MATERIAL disabled.
You used a mix of these two, where you set the material properties with glColorMaterialfv(), but then also enabled GL_COLOR_MATERIAL. This means that the ambient and diffuse material properties you set are not used. Instead the current color is used for those two properties. These properties would then change any time you call glColor(). If you never call glColor(), they would use the default value for the current color, which is white (1.0, 1.0, 1.0, 1.0).

OpenGL: missing shadows on the cube shape

Why the shadows are gone from unlighted faces of my cube shape when I am using arrays of:
vertices[]
indices[]
colors[]
normals[]
texcoords[]
// and draw them by
glDrawElements();
But when I use:
glBegin(...);
glVertex3f(...);
glEnd(...);
The shadows are ok.
Here you got an image of what is happening:
My light is set as below:
glShadeModel (GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glColorMaterial( GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
GLfloat AmbientLight[] = {0.0, 0.0, 0,0, 1.0};
GLfloat DiffuseLight[] = {1.0, 1.0, 1,0, 1.0};
GLfloat SpecularLight[] = {1.0, 1.0, 1.0, 1.0};
GLfloat Shininess[] = { 90.0 };
GLfloat Emission[] = {0.0, 0.0, 0.0, 1.0};
glLightfv(GL_LIGHT0, GL_AMBIENT, AmbientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, SpecularLight);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, AmbientLight);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, DiffuseLight);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, SpecularLight);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, Shininess);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Emission);
You are probably missing a call to
glEnableClientState( GL_NORMAL_ARRAY );

Lighting not working on OpenGL - Solid color

The lighting doesn't work, the object appears with solid color. The color itself changes with the material or lighting parameters but no shadow or anything just solid grey.
Here's part of the code:
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glFrontFace(GL_CCW);
glShadeModel(GL_FLAT);
glEnable(GL_LIGHTING);
GLfloat amb[] = {0.6, 0.6, 0.6, 1.0};
GLfloat dif[] = {0.8, 0.8, 0.8, 1.0};
GLfloat spec[] = {0.5, 0.5, 0.5, 1.0};
GLfloat pos[] = {0.0, 0.0, 30.0};
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, dif);
glLightfv(GL_LIGHT0, GL_SPECULAR, spec);
glLightfv(GL_LIGHT0, GL_POSITION, pos);
GLfloat co[4]={0.5, 0.5, 0.5, 0.5};
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, co);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
for(int i=0; i < numofTr; i++)
{
glBegin(GL_TRIANGLES);
glNormal3f(tr[i].v1->n.x, tr[i].v1->n.y, tr[i].v1->n.z);
glVertex3f(tr[i].v1->x, tr[i].v1->y, tr[i].v1->z);
glNormal3f(tr[i].v2->n.x, tr[i].v2->n.y, tr[i].v2->n.z);
glVertex3f(tr[i].v2->x, tr[i].v2->y, tr[i].v2->z);
glNormal3f(tr[i].v3->n.x, tr[i].v3->n.y, tr[i].v3->n.z);
glVertex3f(tr[i].v3->x, tr[i].v3->y, tr[i].v3->z);
glEnd();
}
I don't know what I'm missing, the normals look good when displayed along with the object.
You are using dim light on a gray object. Obviously the result is gray.
If you mean that faces are flat shaded, that's because of glShadeModel(GL_FLAT);. Change it to GL_SMOOTH to enable interpolation.
If the object is in 3D, I think that the fact you are only passing 3 floats as position might be relevant; default value is [0,0,1,0], and the function expects four floats (4th being the W coordinate, which should in most cases be equal to 1, or 0 for lights in infinite distance).
Also double check that the data you give to glVertex and glNormal is correct. When rendering a sphere at 0,0,0, the normal of every vertex is the normalized position of such vertex (only holds for spheres, obviously).
As a side note, this code is obviously outdated, and as a disclaimer I would ditch it alltoghether, if you are only learning, and switch to something that's relatively modern and uses programmable pipeline.

OpenGL lighting: half of triangles not lit

I'm using the marching cubes algorithm described here http://paulbourke.net/geometry/polygonise/ to turn a scalar field into a triangle mesh, and then displaying the mesh using OpenGL's GL_TRIANGLES mode. However, I'm having trouble with the lighting.
It seems like half the triangles are not being lit at all, and it's occurring in a distinct pattern.
In the program depicted below I've set the background to red, the front face color to green, and the back face color to blue. There is one light source with position (1.0, 1.0, 1.0, 0.0) and a sphere included for reference.
http://i.imgur.com/JutXhbb.png
And here it is with the triangles flipped, as in the drawing order of the last two vertices are switched:
http://i.imgur.com/iUgqm3N.png (back face color is blue)
Whats really confusing me is why the flipped version has color everywhere whereas the normal version doesn't. I figured that even if I drew stuff wrong, at least the flipped version should reflect the same error. Any idea what's happening?
Here is the complete lighting initialization code:
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_color1[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat mat_color2[] = { 0.0, 1.0, 0.0, 1.0 };
glMaterialfv(GL_BACK, GL_DIFFUSE, mat_color1);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_color2);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glMaterialfv(GL_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_BACK, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);