// 范例P124.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); //清除所有像素
glutSolidSphere(1.0,20,6);
/*在缓存中绘制4对顶点坐标的矩形*/
glFlush(); //立即显示出来
}
void init()
{
glClearColor(0.0,0.0,0.0,0.0); //用黑色清除背景
GLfloat mat_specular[]={1.0, 1.0, 1.0, 1.0};
GLfloat mat_shininess[]={50.0};
GLfloat light_position[]={1.0,1.0,1.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);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
glLightfv(GL_LIGHT0,GL_POSITION,light_position);
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(-1.5,1.5,-1.5*(GLfloat)h/(GLfloat)w,1.5*(GLfloat)h/(GLfloat)w,-10.0,10.0);
else
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,1.5*(GLfloat)w/(GLfloat)h,-1.5,1.5,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc,char ** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(250,250); //初始化窗口大小
glutInitWindowPosition(0,0); //初始化窗口位置
glutCreateWindow("hello"); //创建标题为“hello”的窗口
init(); //调用初始化函数
glutDisplayFunc(display); //注册回调函数
glutReshapeFunc(reshape);
glutMainLoop(); //进入主循环处理事件
return 0;
}
评论