PDA

View Full Version : how can I do shading



AeroSujan
10-17-2011, 12:12 AM
Dear Friends
How can I implement shading for my 3D model. I am calling
glShadeModel (GL_SMOOTH);
In my DrawScene() but its not shading properly.
Please give me some hints about shading. Thanks a lot in advance...Sujan

dorbie
10-17-2011, 02:11 AM
You need to have a color result that is different per vertex for GL_SMOOTH to have an effect.

This could be the specified per vertex color attribute of the result of a lighting calculation where surface normals per vertex are averaged with neighbors.

Basically DrawScene() could be doing anything and GL_SMOOTH just interpolates per-vertex color results (from lighting or whatever) across triangles, what those per-vertex color results are is a result of many other factors that are in your hands.

tksuoran
10-17-2011, 02:16 AM
What kind of shading you have in mind? Is this fixed function or with shaders?

If you mean lighting, then you need to set vertex normals and you need to enable lighting and lights, and set light positions/directions and material properties accordingly. With fixed function you would use glLight*, glMaterial* functions.

AeroSujan
10-17-2011, 04:04 AM
I want to have an effect of the color intensity based on the points position with respect to 0,0 axis. Do I need to use lighting for that. specifying colors for each vertex would so much tedious, is there very simple way that I can achieve shading. Thanks for the reply.

AeroSujan
10-17-2011, 04:30 AM
I am doing like this

glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
float position0[]= { 0.0f, 0.0f, 0.0f, 1.0f };
// Assign createdcomponents to GL_LIGHT0
glLightfv(GL_LIGHT0, GL_POSITION, position0);
float position1[]= { 0.0f, 0.0f, 0.0f, 1.0f };
// Assign createdcomponents to GL_LIGHT1
glLightfv(GL_LIGHT1, GL_POSITION, position1);

But with this the result is not satisfactory. In fact I am loosing my actual model color, i.e, red.

After shading this way model color becomes white and dark shade is coming, in blackbackground the model is not visible at all.

Please give me some suggestions.

_arts_
10-17-2011, 06:04 AM
Color has nothing to do with lighting. You need materials.

I highly suggest you to learn OpenGL among other things. You simply can't do a thing if you know absolutely nothing about it. So, you can read for example this tutorial:

http://glprogramming.com/red/chapter05.html

ugluk
10-17-2011, 06:31 AM
Perhaps rather to and search for opengl:

http://library.nu

AeroSujan
10-24-2011, 09:45 AM
I am currently shading my model like this. But with this the model has dark shade, black shadow is coming, moreover the shading is not realistic. Can u help someone to modify my existing to get a better shading, Thanks a lot for any help. Sujan
Code snippet.



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);
// 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
float ambientLight0[] = { 1.0f, 0.1f, 0.1f, 1.0f };
float diffuseLight0[] = { 1.0f, 0.1f, 0.1f, 1.0f };
float specularLight0[] = { 1.0f, 0.1f, 0.1f, 1.0f };
float position0[]= { 0.0f, 0.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_POSITION, position0);
// GL_LIGHT1: the redlight emitting light source
// Create lightcomponents for GL_LIGHT1
float ambientLight1[] = { 1.0f, 0.1f, 0.1f, 1.0f };
float diffuseLight1[] = { 1.0f, 0.1f, 0.1f, 1.0f };
float specularLight1[] = { 1.0f, 0.1f, 0.1f, 1.0f };
float position1[]= { 0.0f, 0.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_POSITION, position1);
//---------------------------------
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
Invalidate();
wglMakeCurrent(NULL,NULL);
}