PDA

View Full Version : model, shade, looksRealistic OpenGL



AeroSujan
10-25-2011, 06:34 AM
Dear Friends
I am successfully able to generate a 3D surface by revolving the profiles by 360 degree. Now my 3d model is in red color so the color is so shining I am not able to distinguish various parts in the model. For this I am trying to use some lighting but I am not successful. Can u help me someone what kind of lighting should I use so that my model looks realistics and I would be able to visualize various parts on the model . Check my snippet I am trying to use this kind of lighting but finding black shadows on the sides not able to visualize properly. Thanks a lot for any help.



void CRevolutionProjView::OnViewShade()
{
// TODO: Add your command handler code here
// TODO: Add your command handler code here

CDC* pDC = GetDC();
wglMakeCurrent(pDC->m_hDC, m_hrc);

glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_FASTEST);
// Enable light and setup 2 light sources (GL_LIGHT0 and GL_LIGHT1)
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glColor3f(1.0f, 0.0f, 0.0f);
// We're setting up twolight sources. One of them is located
// on the left side ofthe model (x = -1.5f) and emits white light. The
// second light sourceis located on the right side of the model (x = 1.5f)
// emitting red light.
// GL_LIGHT0: the whitelight emitting light source
// Create lightcomponents for GL_LIGHT0
GLfloat emissionLight0[] = {0.2f, 0.0f, 0.0f, 1.0f};
GLfloat ambientLight0[] = { 0.2f, 0.2f, 0.2f, 1.0f };
GLfloat diffuseLight0[] = { 0.8f, 0.8f, 0.8, 1.0f };
GLfloat specularLight0[] = { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat position0[] = { -1.5f, 1.0f, -4.0f, 1.0f };

// Assign createdcomponents to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
glLightfv(GL_LIGHT0, GL_EMISSION, emissionLight0);
glLightfv(GL_LIGHT0, GL_POSITION, position0);
// GL_LIGHT1: the redlight emitting light source
// Create lightcomponents for GL_LIGHT1
float ambientLight1[] = { 0.2f, 0.2f, 0.2f, 1.0f };
float diffuseLight1[] = { 0.8f, 0.8f, 0.8f, 1.0f };
float specularLight1[] = { 0.5f, 0.5f, 0.5f, 1.0f };
float emissionLight1[] = { 0.2f, 0.0f, 0.0f, 1.0f };
float position1[]= { -1.5f, 1.0f, -4.0f, 1.0f };
// Assign createdcomponents to GL_LIGHT1
glLightfv(GL_LIGHT1, GL_AMBIENT, ambientLight1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseLight1);
glLightfv(GL_LIGHT1, GL_SPECULAR, specularLight1);
glLightfv(GL_LIGHT1, GL_EMISSION, emissionLight1);
glLightfv(GL_LIGHT1, GL_POSITION, position1);
//---------------------------------
// enable color tracking
glEnable(GL_COLOR_MATERIAL);
// set material properties which will be assigned by glColor
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
// ...Somewhere in the main rendering loop of your program...
// Draw a polygon with material properties set by glColor
float specReflection[] = { 0.8f, 0.8f, 0.8f, 1.0f };
glMaterialfv(GL_FRONT, GL_SPECULAR, specReflection);
glMateriali(GL_FRONT, GL_SHININESS, 96);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
Invalidate();
wglMakeCurrent(NULL,NULL);
}

carsten neumann
10-25-2011, 09:12 PM
The arrays position0 and position1 contain the same values, both lights are in the same location. Faces of your model that are not oriented towards that one location will remain black since they don't receive any light.

AeroSujan
10-26-2011, 06:35 AM
so what do u suggest shall i put those position values different to get better effects. How can I put the lights in position so that all sides get better light avoiding black shadows. Thanks for your help. Sujan

carsten neumann
10-26-2011, 08:40 AM
You are in the best position to effectively place light sources in your scene, because you know your scene much better than I do. Even though OpenGL light sources are only a rough approximation you can still use a mental image like this:
Imagine you are in a dark room, your model is suspended in the middle of that room and you have some flashlights. If you place them in the same location you'll only be able to see the side of the model that faces the lights, the rest will remain in darkness. You can place the light sources at opposite sides of the model to illuminate more of it.
Of course in reality light also bounces around the room so even the faces that are not facing any light source still get a little bit of illumination. OpenGL approximates that with ambient term of light sources (and a global ambient term) and the ambient material colour.

_arts_
10-26-2011, 01:14 PM
You can also try to change the global ambient term. It is not the best for lighting but this allows to have unlit parts less dark.

See this: http://glprogramming.com/red/chapter05.html#name5 for example.

AeroSujan
10-31-2011, 06:34 AM
how to assign surface normals. please give me some hint. Thanks Sujan

carsten neumann
10-31-2011, 07:40 AM
glNormal3f() etc.
Is that really what you are asking for - if not, can you make your question more precise? If that does answer your question, have you considered reading through an OpenGL tutorial to get a better overview of how the API works?

AeroSujan
10-31-2011, 08:58 AM
Dear Friends
Now I am calculating normals and assigning a normals using glNormal3f() for each QUAD......When rendered it's coming fine but some quads near the edges are flickering. How can I resolve this. ? Please help me. Thanks Sujan

AeroSujan
11-01-2011, 12:32 AM
Dear Friends
I am able to implement shading but I dont know why when I am rotating zooming out near the edges its flickering. I am calculating normals for each QUQD like this ...V0,V1,V2,V3 are 4 vertiuces of the quad.


vec1.x = v1[0] - v0[0];
vec1.y = v1[1] - v0[1];
vec1.z = v1[2] - v0[2];

vec2.x = v3[0] - v0[0];
vec2.y = v3[1] - v0[1];
vec2.z = v3[2] - v0[2];

normal1[0] = ((vec1.y*vec2.z)-(vec1.z * vec2.y));
normal1[1] = ((vec1.z * vec2.x)-(vec1.x * vec2.z));
normal1[2] = ((vec1.x * vec2.y)-(vec1.y * vec2.x));

GLfloat len = sqrt((normal1[0]*normal1[0]) + (normal1[1]*normal1[1]) + (normal1[2]*normal1[2]));

normal1[0] /= len;
normal1[1] /= len;
normal1[2] /= len;

glBegin(GL_QUADS); // draw a cube with 6 quads
glNormal3f(normal1[0],normal1[1],normal1[2]);
glVertex3fv(v0); // front face
glVertex3fv(v1);
glVertex3fv(v2);
glVertex3fv(v3);

AeroSujan
11-01-2011, 01:35 AM
my shade algorithms is like this ..


void CRevolutionProjView::OnViewShade()
{
// TODO: Add your command handler code here
// TODO: Add your command handler code here

CDC* pDC = GetDC();
wglMakeCurrent(pDC->m_hDC, m_hrc);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
GLfloat ambientLight0[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat diffuseLight0[] = { 1.0f, 0.5f, 0.5f, 1.0f };
GLfloat specularLight0[] = { 0.0f, 0.0f, 1.0f, 1.0f };
GLfloat emissionLight0[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat position0[] = {1.0f,1.0f,1.0f,1.0f};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight0);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight0);
glLightfv(GL_LIGHT0, GL_EMISSION, emissionLight0);
glLightfv(GL_LIGHT0, GL_POSITION, position0);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
Invalidate();
wglMakeCurrent(NULL,NULL);
}


Its flickering near the edges... while zooming out its minimizing and clickering more.

BionicBytes
11-01-2011, 02:00 AM
Hang on a mo.
You have already started another thread with this question!
Please don't post the same question multiple times!