資源簡(jiǎn)介
3D場(chǎng)景漫游、
樹和水波紋、
鍵盤操縱漫游。
包含光照 貼圖。
代碼片段和文件信息
//?OpenGLStarter.cpp?:?Defines?the?entry?point?for?the?console?application.
//
#include?“stdafx.h“
//需要包含的頭文件
#include?
#include?
//#include?
//#include?
#include?
#include?“ImageLoader.h“
//定義輸出窗口的大小
#define?WINDOW_HEIGHT?300
#define?WINDOW_WIDTH?500
//攝像機(jī)離物體的距離
float?G_fDistance?=?3.6f;
//物體的旋轉(zhuǎn)角度?
float?G_fAngle_horizon?=?0.0;
float?G_fAngle_vertical?=?0.0f;
////////////////////////////////////////////////
//光照參數(shù)
float?G_vLit0Position[4]?=?{?5.0f?0.0f?5.0f?1.0f?};
float?G_vLit0Ambient[4]?=?{?0.8f?0.8f?0.8f?1.0f?};
float?G_vLit0Diffuse[4]?=?{?0.8f?0.8f?0.8f?1.0f?};
float?G_vLit0Specular[4]?=?{?0.5f?0.5f?0.5f?1.0f?};
float?G_vMaterialSpecu[4]?=?{?0.8f?0.8f?0.8f?1.0f?};
float?G_vLit1Position[4]?=?{?0.0f?0.0f?0.0f?1.0f?};
//texture?related?parameters
static?GLuint?G_texNameArray[2];
GLfloat?G_fWaterTexOffset?=?0.0f;
////////////////////////////////////////////////
void?myinit(void);
void?myReshape(GLsizei?w?GLsizei?h);
void?display(void);
//響應(yīng)鍵盤輸入?從而設(shè)定物體移近移遠(yuǎn)以及旋轉(zhuǎn)的回調(diào)函數(shù)
void?processSpecialKeys(int?key?int?x?int?y);
void?processNormalKeys(unsigned?char?keyint?xint?y);
void?waterMove(int?value);
//生成紋理
void?loadTexImages(void);
void?setTextureFiltering(void);
////////////////////////////////////////////////
//主函數(shù)
int?main(int?argc?char*?argv[])
{
glutInit(&argc?argv);
//初始化OPENGL顯示方式
glutInitDisplayMode?(GLUT_DOUBLE?|?GLUT_RGBA);
//設(shè)定OPENGL窗口位置和大小
glutInitWindowSize?(500?500);?
glutInitWindowPosition?(100?100);
//打開窗口
glutCreateWindow?(“OpenGL“);
printf(“上、下鍵---繞x軸旋轉(zhuǎn)\n“);
printf(“左、右鍵---繞y軸旋轉(zhuǎn)\n“);
printf(“a/A鍵------拉近/推遠(yuǎn)\n“);
//調(diào)用初始化函數(shù)
????myinit();
//設(shè)定窗口大小變化的回調(diào)函數(shù)
glutReshapeFunc(myReshape);
//設(shè)定鍵盤控制的回調(diào)函數(shù)
glutSpecialFunc(processSpecialKeys);
glutKeyboardFunc(processNormalKeys);
glutTimerFunc(10?waterMove?0);
//開始OPENGL的循環(huán)
glutDisplayFunc(display);?
glutMainLoop();
return?0;
}
////////////////////////////////////////////////
//用戶初始化函數(shù)
void?myinit(void)
{
????//your?initialization?code
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK?GL_AMBIENT);
//setting?the?lighting?parameters
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glLightfv(GL_LIGHT0?GL_POSITION?G_vLit0Position); //設(shè)置光源的位置
glLightfv(GL_LIGHT0?GL_AMBIENT?G_vLit0Ambient);
glLightfv(GL_LIGHT0?GL_DIFFUSE?G_vLit0Diffuse);
glLightfv(GL_LIGHT0?GL_SPECULAR?G_vLit0Specular);
//Setting?the?textures
loadTexImages();
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA?GL_ONE_MINUS_SRC_ALPHA);
}
//窗口大小變化時(shí)的回調(diào)函數(shù)
void?myReshape(GLsizei?w?GLsizei?h)
{
//設(shè)定視區(qū)
????glViewport(0?0?w?h);
//設(shè)定透視方式
????glMatrixMode(GL_PROJECTION);
????glLoadIdentity();
gluPerspective(60.0?1.0*(GLfloat)w/(GLfloat)h?1.0?30.0);
}
void?display(void)
{
//設(shè)置清除屏幕的顏色
評(píng)論
共有 條評(píng)論