• 实验7 OpenGL光照


     

    一.实验目的:

    了解掌握OpenGL程序的光照与材质,能正确使用光源与材质函数设置所需的绘制效果。

    二.实验内容:

    (1)下载并运行Nate Robin教学程序包中的lightmaterial程序,试验不同的光照与材质系数;

    (2)运行示范代码1,了解光照与材质函数使用。

    三.实验原理:

    为在场景中增加光照,需要执行以下步骤:

    (1) 设置一个或多个光源,设定它的有关属性;

    (2) 选择一种光照模型;

    (3) 设置物体的材料属性。

    具体见教材第8章8.6节用OpenGL生成真实感图形的相关内容。

    四.示范代码:

      1 #include <GL/glut.h>
      2 
      3 #include <stdlib.h>
      4 
      5 // Initialize material property, light source, lighting model, and depth buffer.
      6 
      7 void init(void)
      8 
      9 {
     10 
     11 GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
     12 
     13 GLfloat mat_shininess[] = { 50.0 };
     14 
     15 GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
     16 
     17 GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
     18 
     19 GLfloat Light_Model_Ambient[] = { 0.2 , 0.2 , 0.2 , 1.0 }; //
     20 
     21 glClearColor (0.0, 0.0, 0.0, 0.0);
     22 
     23 glShadeModel (GL_SMOOTH);
     24 
     25 glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
     26 
     27 glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
     28 
     29 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
     30 
     31 glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
     32 
     33 glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
     34 
     35 glLightModelfv( GL_LIGHT_MODEL_AMBIENT , Light_Model_Ambient ); //
     36 
     37 glEnable(GL_LIGHTING);
     38 
     39 glEnable(GL_LIGHT0);
     40 
     41 glEnable(GL_DEPTH_TEST);
     42 
     43 }
     44 
     45 void display(void)
     46 
     47 {
     48 
     49 glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     50 
     51 //glutSolidSphere (1.0, 20, 16);
     52 
     53 glutSolidTeapot(0.5);
     54 
     55 glFlush ();
     56 
     57 }
     58 
     59 void reshape (int w, int h)
     60 
     61 {
     62 
     63 glViewport (0, 0, (GLsizei) w, (GLsizei) h);
     64 
     65 glMatrixMode (GL_PROJECTION);
     66 
     67 glLoadIdentity();
     68 
     69 if (w <= h)
     70 
     71 glOrtho (-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
     72 
     73 1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
     74 
     75 else
     76 
     77 glOrtho (-1.5*(GLfloat)w/(GLfloat)h,
     78 
     79 1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
     80 
     81 glMatrixMode(GL_MODELVIEW);
     82 
     83 glLoadIdentity();
     84 
     85 }
     86 
     87 int main(int argc, char** argv)
     88 
     89 {
     90 
     91 glutInit(&argc, argv);
     92 
     93 glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
     94 
     95 glutInitWindowSize (500, 500);
     96 
     97 glutInitWindowPosition (100, 100);
     98 
     99 glutCreateWindow (argv[0]);
    100 
    101 init ();
    102 
    103 glutDisplayFunc(display);
    104 
    105 glutReshapeFunc(reshape);
    106 
    107 glutMainLoop();
    108 
    109 return 0;
    110 
    111 }

    附VC++工程代码(VC++2008)

    5. 实验提高

    在实验5太阳系模型的基础上,尝试为其增加光照与材质效果,如图A.5(b)所示。

    clip_image002 clip_image004

    (a)太阳系模型     (b)增加光照后的效果

  • 相关阅读:
    javascript中依赖属性(Dependency Property)的实现
    金山卫士UI原理解析(2)CBkWindow
    WTL学习笔记(5)双缓冲技术和动画(BufferedPaint)
    数据结构队列
    Windows下如何自定义窗体控件
    金山卫士UI原理解析(1)
    WTL学习笔记(4)控件加强
    WTL学习笔记(5)系统皮肤管理
    小笨开始冬眠了:)
    送时尚数码展门票
  • 原文地址:https://www.cnblogs.com/opengl/p/3789247.html
Copyright © 2020-2023  润新知