OpenGL programming easy to get started with lighting and textures

Source: Internet
Author: User

The use of light and material can make the object more realistic, with three-dimensional sense. Example 4 is not using light to make the teapot and cube of objects present in front of us without three-dimensional sense.

Example 6: Draw three spheres with different textures.

#include <windows.h>
#include <GL/glut.h>
Glfloat light_position[] = {0.0,3.0,6.0,0.0};
Glfloat no_mat[] = {0.0,0.0,0.0,1.0};
Glfloat mat_grey_ambient[] = {0.5,0.5,0.5,1.0};
Glfloat mat_red_ambient[] = {0.0,0.0,1.0,1.0};
Glfloat mat_diffuse[] = {0.8,0.2,0.5,1.0};
Glfloat mat_specular[] = {1.0,1.0,1.0,1.0};
Glfloat no_shininess[] = {0.0};
Glfloat low_shininess[] = {5.0};
Glfloat high_shininess[] = {100.0};
Glfloat mat_emission[] = {0.3,0.2,0.2,0.0};

void Myinit (void)
{
GLLIGHTFV (gl_light2,gl_position,light_position);//Set Light

Glenable (gl_depth_test);
Gldepthfunc (gl_less);//Specify the values used in the depth comparison
Glenable (gl_lighting);
Glenable (GL_LIGHT0);
Glshademodel (Gl_smooth);
}

void display (void)
{
Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);
Glpushmatrix ();
/* Specify material parameters for the illumination model * *
GLMATERIALFV (Gl_front,gl_diffuse,mat_diffuse);
GLMATERIALFV (Gl_front,gl_specular,mat_specular);
GLMATERIALFV (gl_front,gl_shininess,high_shininess);
GLMATERIALFV (Gl_front,gl_emission,no_mat);

Glcolormaterial (gl_front,gl_ambient);//Make material color keep track of current color
Glenable (gl_color_material);

/* First Sphere/*
Glpushmatrix ();
GLCOLOR3FV (No_mat);
Gltranslatef ( -2.5,1.5,0.0);
Glrotatef (15.0,1.0,0.0,0.0);
Glutsolidsphere (1.2,20.0,20.0);
Glpopmatrix ();

/* The second sphere * *
Glpushmatrix ();
GLCOLOR3FV (mat_grey_ambient);
Glrotatef (15.0,1.0,0.0,0.0);
Glutsolidsphere (1.2,20.0,20.0);
Glpopmatrix ();

/* The Third sphere * *
Glpushmatrix ();
GLCOLOR3FV (mat_red_ambient);
Gltranslatef (2.5,-1.5,0.0);
Glrotatef (15.0,1.0,0.0,0.0);
Glutsolidsphere (1.2,20.0,20.0);
Glpopmatrix ();

Gldisable (gl_color_material);
Glpopmatrix ();

Glflush ();
}

void Myreshape (int w,int h)
{
Glviewport (0,0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();

if (w <= h)
Glortho ( -5.5,5.5,-5.5* (glfloat) h/(glfloat) w,5.5* glfloat (h/) glfloat);
Else
Glortho ( -5.5* (glfloat) w/(glfloat) h,5.5* glfloat (w/) glfloat);

Glmatrixmode (Gl_modelview);
Glloadidentity ();
}

int main (int argc,char * * argv)
{
/* Initialize * *
Glutinit (&AMP;ARGC,ARGV);
Glutinitdisplaymode (glut_single| GLUT_RGB);
Glutinitwindowsize (400,400);
Glutinitwindowposition (100,100);

/* Create window * *
Glutcreatewindow ("light&material");

/* Draw and display * *
Myinit ();
Glutreshapefunc (Myreshape);
Glutdisplayfunc (display);

Glutmainloop ();
return 0;
}



In function Myinit:

Gllight Sets the parameters of the light source. There are three parameters, the first parameter specifies the number of illumination, and the maximum is 8. The second parameter is the single-value light source parameter of the illumination. The third parameter is the value assigned to the second parameter, in this case the value of the light_position is assigned to Gl_position.

In the display function:

glmaterial Specifies the material parameters for the lighting model. The first parameter is the surface that changes the material, and the value must be one of the Gl_front, Gl_back or Gl_front_and_back. The second parameter is the material parameter that needs to be changed. The third parameter is the value assigned to parameter two or a pointer to a value. In this case, they are pointers.

Glcolormateria The material color to keep track of the current color. The first parameter specifies which face to use to keep the material color from tracking the current color. The second parameter specifies which parameter needs to be traced.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.