0
\$\begingroup\$

To Apply shading to an object. Let's say earth (which was textured using an image). So how to apply smooth shading to that? What are the steps I need to write? Using which functions? Thank you.

Code

void drawScene()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, textureOfSun);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix(); // Push matrices

// Draw Sun

gluQuadricTexture(quadricSun,1);
gluSphere(quadricSun,2,20,20);
glPopMatrix();

//Draw Earth
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureOfEarth);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

//Rotation around Sun


//Revolve of Earth

glPopMatrix();
gluQuadricTexture(quadricEarth,1);
gluSphere(quadricEarth,0.75,20,20);

glutSwapBuffers();
}

This is the rendering part,

void initRendering() 
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
quadricSun = gluNewQuadric();
quadricEarth = gluNewQuadric();

textureOfSun = LoadTexture(location1);
textureOfEarth = LoadTexture(location2); /* location1 and 2 says the locations of images */
}
\$\endgroup\$
  • \$\begingroup\$ Are you able to render the object without shading? If so, please share the code. \$\endgroup\$ – SurvivalMachine Nov 23 '16 at 16:14
  • \$\begingroup\$ That seems to be old and deprecated API of OpenGL. You probably want to learn about the new stuff with Vertex Buffer Objects and Shaders before you continue. Or did you have a specific reason to go with the older API? LearnOpenGL.com is a great resource to learn from. \$\endgroup\$ – Lasse Nov 23 '16 at 16:36
  • \$\begingroup\$ @Lasse Yeah. I'm new to this OpenGL. So my teacher asked to use these. So I need help to add shading to objects and light to objects. Ex: as Sun (One object of my animation is Sun) behaves in real life. \$\endgroup\$ – User9125 Nov 23 '16 at 16:40
  • \$\begingroup\$ In that case, you could check some legacy tutorial, like NEHE Legacy OpenGL tutorial. \$\endgroup\$ – Lasse Nov 23 '16 at 16:41
  • 2
    \$\begingroup\$ I am going to cast a vote to close this question, as it is a "how to get started" type of a question that is not really very well fit for the site and can fairly easily be solved by making a google search. If you have a question about something very specific during your trial-and-error phase, go ahead and ask a new question about that problem. \$\endgroup\$ – Lasse Nov 23 '16 at 16:44
0
\$\begingroup\$

Google should have had an answer for you on this on, if you did try :).

But try putting this in your function 'initRendering' at the beginning for example:

glEnable(GL_LIGHTING);
GLfloat global_ambient[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightModelfv(glShadeModel(GL_SMOOTH), global_ambient);
glShadeModel(GL_SMOOTH);

This is old legacy code

\$\endgroup\$
  • \$\begingroup\$ Glad I could help! \$\endgroup\$ – walkingBird Feb 23 '17 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.