正文

(OPENGL编程指南第四版)OPENGL范例程序5.82008-03-24 20:04:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yuqiexing/33553.html

分享到:

// 范例P140.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <gl/glut.h>

void display()
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);     //清除所有像素
 
 //材质设置

 //四列之间的镜面反射色不同
 GLfloat mat_specular[][4]={1.0,1.0,1.0,1.0, 0.0,0.0,0.0,1.0};
 //四列之间的光洁度不同
 GLfloat mat_shininess[3]={0,5,100};
 //三行之间的环境光反射不同
 GLfloat mat_ambient[][4]={0.0,0.0,0.0,1.0, 0.7,0.7,0.7,1.0, 0.8,0.8,0.2,1.0};
 //散射光颜色全部相同
 GLfloat mat_diffuse[]={0.1,0.5,0.8,1.0};
 //第四列能够发光
 GLfloat mat_emission[][4]={0.3,0.2,0.2,0.0, 0.0,0.0,0.0,1.0};

 glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
 for(int i=0;i<3;i++)
 {
  glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient[i]);
  for(int j=0;j<4;j++)
  {
   if(j==0||j==4)
    glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular[1]);
   else
    glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular[0]);
   if(j==4)
    glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission[0]);
   else
    glMaterialfv(GL_FRONT,GL_EMISSION,mat_emission[1]);
   glMaterialf(GL_FRONT,GL_SHININESS,mat_shininess[j%3]);
   glPushMatrix();
   glTranslatef(-3.75+j*2.5,3.0-i*2.5,0.0);
   glutSolidSphere(1.0,16,16);
   glPopMatrix();
  }
 }
 glFlush();          //立即显示出来
}

void init()
{
 glClearColor(0.0,0.0,0.0,0.0);     //用黑色清除背景
 
 GLfloat light_position[]={2.0,2.0,2.0,0.0};
 GLfloat white_light[]={1.0,1.0,1.0,1.0};
 GLfloat lmodel_ambient[]={0.1,0.1,0.1,1.0};
 
 glShadeModel(GL_SMOOTH);

 glPushMatrix();
 glLightfv(GL_LIGHT0,GL_POSITION,light_position);
 glPopMatrix();
 glLightfv(GL_LIGHT0,GL_DIFFUSE,white_light);
 glLightfv(GL_LIGHT0,GL_SPECULAR,white_light);
 //glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
 
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
 glEnable(GL_DEPTH_TEST);
}
void reshape(int w,int h)
{
 glViewport(0,0,(GLsizei)w,(GLsizei)h);
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 if(w<=h)
  glOrtho(-9,9,-9*(GLfloat)h/(GLfloat)w,9*(GLfloat)h/(GLfloat)w,-20.0,20.0);
 else
  glOrtho(-9*(GLfloat)w/(GLfloat)h,9*(GLfloat)w/(GLfloat)h,-9,9,-20.0,20.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
 
}
int main(int argc,char ** argv)
{
 glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
 glutInitWindowSize(500,500);     //初始化窗口大小
 glutInitWindowPosition(0,0);    //初始化窗口位置
 glutCreateWindow("hello");      //创建标题为“hello”的窗口
 init();           //调用初始化函数
 glutDisplayFunc(display);      //注册回调函数
 glutReshapeFunc(reshape);
 glutMainLoop();         //进入主循环处理事件
 return 0;
}


阅读(2780) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册