You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

506 lines
12 KiB

#include "scene.h"
#include "utils.h"
#include "skybox.h"
#include "model.h"
#include "ground.h"
#include "light.h"
#include "camera.h"
GLuint texture;
SkyBox skybox;
Model model;
Ground ground;
DirectionLight light(GL_LIGHT0);
PointLight light1(GL_LIGHT1);
PointLight light2(GL_LIGHT2);
SpotLight light3(GL_LIGHT3);
Camera camera;
void Init() {
glMatrixMode(GL_PROJECTION);
gluPerspective(50.0f, 800.0f / 600.0f, 0.1f, 1000.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
texture = CreateTexture2DFromBMP("Res/earth.bmp");
skybox.Init("Res/");
model.Init("Res/Sphere.obj");
model.mTexture = CreateTexture2DFromBMP("Res/earth.bmp");
light.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
light.SetDiffuseColor(0.8f, 0.8f, 0.8f, 1.0f);
light.SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
light.SetPosition(0.0f, 1.0f, 0.0f);
light1.SetAmbientColor(0.1f,0.1f,0.1f,1.0f);
light1.SetDiffuseColor(0.8f,0.8f,0.8f,1.0f);
light1.SetSpecularColor(1.0f,1.0f,1.0f,1.0f);
light1.SetPosition(0.0f,0.0f,0.0f);//把点光源放到摄像机位置
light1.SetConstAttenuation(1.0f);
light1.SetLinearAttenuation(0.2f);
light2.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
light2.SetDiffuseColor(0.1f, 0.4f, 0.6f, 1.0f);
light2.SetSpecularColor(1.0f, 1.0f, 1.0f, 1.0f);
light2.SetPosition(0.0f, 0.0f, -30.0f);//把点光源放到摄像机位置
light2.SetConstAttenuation(1.0f);
light2.SetLinearAttenuation(0.2f);
light3.SetAmbientColor(0.1f, 0.1f, 0.1f, 1.0f);
light3.SetDiffuseColor(0.0f, 0.8f, 0.0f, 1.0f);
light3.SetSpecularColor(1.0f,0.0f,0.0f,1.0f);
light3.SetPosition(10.0f,50.0f,-30.0f);
light3.SetDirection(0.0,-1.0f,0.0f);//设置朝向
light3.SetExpone(5.0f);//设置聚光度 5度
light3.SetCutoff(10.0f);//设置照射范围 10度
model.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f);
model.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f);
model.SetSpecularMaterial(1.0f, 1.0f, 1.0f, 1.0f);
ground.SetAmbientMaterial(0.1f, 0.1f, 0.1f, 1.0f);
ground.SetDiffuseMaterial(0.4f, 0.4f, 0.4f, 1.0f);
ground.SetSpecularMaterial(0.0f, 0.0f, 0.0f, 1.0f);
camera.mViewportWidth = 800.0f;
camera.mViewportHeight = 600.0f;
}
void DrawMutipl() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0f, 0.0f, -5.0f);
static float r = 0;
glPushMatrix();
glRotatef(r, 0.0f, 1.0f, 0.0f);
r += 0.1f;
glBegin(GL_TRIANGLES);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
glEnd();
glPopMatrix();
glPopMatrix();
}
void DrawPushMatrix() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();//重置模型视口矩阵为单位矩阵
glPushMatrix();
glTranslatef(-1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
glEnd();
glPopMatrix();
}
void DrawRoated() {
glClearColor(30.0f / 255.0f, 30.0f / 255.0f, 30.0f / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
static float r = 0.0f;
glLoadIdentity();
glRotated(r, 0, 0, 1);// (角度,x,y,z)
r += 0.1f;
glBegin(GL_TRIANGLES);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, -2.5f);
glEnd();
}
void DrawTranslate() {
glClearColor(30.0f / 255.0f, 30.0f / 255.0f, 30.0f / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -2.5f);
glBegin(GL_TRIANGLES);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, 0.0f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, 0.0f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.0f, 0.25f, 0.0f);
glEnd();
}
void DrawPolygon() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(1.0f, 0.25f, -2.5f);
glColor4ub(255, 0, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(-0.1f, 0.1f, -2.5f);
glEnd();
}
void DrawQuadStrip() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUAD_STRIP);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.25f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.25f, -0.25f, -2.5f);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.25f, 0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.25, 0.25, -2.5f);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.25f, 0.5f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.25, 0.5, -2.5f);
glEnd();
}
void DrawQuads() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_QUADS);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(1.0f, 0.5f, -2.5f);
glColor4ub(255, 0, 0, 255); glVertex3f(0.5f, 0.5f, -2.5f);
glEnd();
}
void DrawLineStrip() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(10.0f);
glBegin(GL_LINE_STRIP);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
glEnd();
}
void DrawLineLoop() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(10.0f);
glBegin(GL_LINE_LOOP);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
glEnd();
}
void DrawLines() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(10.0f);
glBegin(GL_LINES);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
glEnd();
}
void DrawPoints() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glPointSize(10.0f);
glBegin(GL_POINTS);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glEnd();
}
void DrawTriangleFan() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_FAN);
glColor4ub(255, 0, 0, 255); glVertex3f(0.0f, -0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.4f, 0.0f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.2f, 0.15f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.0f, 0.2f, -2.5f);
glEnd();
}
void DrawTriangleStrip() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLE_STRIP);
glColor4ub(255, 0, 0, 255); glVertex3f(-0.5f, -0.25f, -2.5f);
glColor4ub(0, 0, 255, 255); glVertex3f(0.5f, -0.25, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(-0.5f, 0.25f, -2.5f);
glColor4ub(0, 255, 0, 255); glVertex3f(0.5f, 0.25f, -2.5f);
glEnd();
}
void DrawTriangles() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor4ub(255, 255, 255, 255);
glVertex3f(-0.2f, -0.2f, -1.5f);
glColor4ub(255, 0, 0, 255);
glVertex3f(0.2f, -0.2f, -1.5f);
glColor4ub(0, 255, 0, 255);
glVertex3f(0.0f, 0.2f, -1.5f);
glEnd();
}
void DrawEnableLight() {
glClearColor(30.0f / 255.0f, 30.0f / 255.0f, 30.0f / 255.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_CULL_FACE);//????
float lightPos[] = { 0.0f,1.0f,0.0f,0.0f };
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);//设置方向光的2位置
float whiteColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, whiteColor);//设置环境光
float ambientMat[] = { 0.07f,0.07f, 0.07f, 1.0f };//设置物体表面材质的反射系数
glMaterialfv(GL_FRONT, GL_AMBIENT, ambientMat);
float diffuseMat[] = { 0.4f,0.4f,0.4f,1.0f };//设置漫反射系数
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMat);
float specularMat[] = { 0.9f,0.9f,0.9f,1.0f };//设置镜面反射系数
glMaterialfv(GL_FRONT, GL_SPECULAR, specularMat);
glBegin(GL_QUADS);
glColor4ub(255, 255, 255, 255);
glVertex3f(-0.5f, -0.2f, -0.5f);
glVertex3f(0.5f, -0.2f, -0.5f);
glVertex3f(0.5f, -0.2f, -1.0f);
glVertex3f(-0.5f, -0.2f, -1.0f);
glEnd();
}
void DrawTexture() {
glClearColor(0.1f, 0.4f, 0.6f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//开启纹理
glEnable(GL_TEXTURE_2D);
//绑定纹理
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glColor4ub(255, 255, 255, 255);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.1f, -0.1f, -0.4f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.1f, -0.1f, -0.4f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.1f, 0.1f, -0.4f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.1f, 0.1f, -0.4f);
glEnd();
}
void DrawDepthBk() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除深度缓冲区和颜色缓存区
glEnable(GL_DEPTH_TEST);//开启深度测试
glBegin(GL_QUADS);
glColor4ub(255, 0, 0, 0);
glVertex3f(-0.1f, -0.1f, -0.4f);
glVertex3f(0.1f, -0.1f, -0.4f);
glVertex3f(0.1f, 0.1f, -0.4f);
glVertex3f(-0.1f, 0.1f, -0.4f);
glEnd();
glBegin(GL_QUADS);
glColor4ub(0, 255, 0, 0);
glVertex3f(-0.1f, -0.1f, -0.6f);
glVertex3f(0.1f, -0.1f, -0.6f);
glVertex3f(0.1f, 0.1f, -0.6f);
glVertex3f(-0.1f, 0.1f, -0.6f);
glEnd();
}
void DrawDepth() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除深度缓冲区和颜色缓存区
//glEnable(GL_DEPTH_TEST);//开启深度测试
GLuint texture2;
texture2 = CreateTexture2DFromBMP("Res/test.bmp");
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture2);
glBegin(GL_QUADS);
glColor4ub(255, 255, 255, 255);
glTexCoord2d(0.0f, 0.0f); glVertex3f(-0.1f, -0.1f, -0.4f);
glTexCoord2d(1.0f, 0.0f); glVertex3f(0.1f, -0.1f, -0.4f);
glTexCoord2d(1.0f, 1.0f); glVertex3f(0.1f, 0.1f, -0.4f);
glTexCoord2d(0.0f, 1.0f); glVertex3f(-0.1f, 0.1f, -0.4f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glBegin(GL_QUADS);
glColor4ub(0, 255, 0, 0);
glVertex3f(-0.1f, -0.1f, -0.6f);
glVertex3f(0.1f, -0.1f, -0.6f);
glVertex3f(0.1f, 0.1f, -0.6f);
glVertex3f(-0.1f, 0.1f, -0.6f);
glEnd();
}
void DrawSkyBox() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除深度缓冲区和颜色缓存区
skybox.Draw();
glEnable(GL_DEPTH_TEST);//开启深度测试
glBindTexture(GL_TEXTURE_2D, 0);
glBegin(GL_QUADS);
glColor4ub(0, 255, 0, 0);
glVertex3f(-0.1f, -0.1f, -0.6f);
glVertex3f(0.1f, -0.1f, -0.6f);
glVertex3f(0.1f, 0.1f, -0.6f);
glVertex3f(-0.1f, 0.1f, -0.6f);
glEnd();
}
void DrawModel() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除深度缓冲区和颜色缓存区
light.Enable();
skybox.Draw();
model.Draw();
ground.Draw();
}
void DrawLight() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除深度缓冲区和颜色缓存区
light1.Enable();
light2.Enable();
light3.Enable();
ground.Draw();
}
void DrawCamera() {
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//清除深度缓冲区和颜色缓存区
float frameTime = GetFrameTime();
camera.Update(frameTime);
light1.Enable();
light1.Update(camera.mPos.x, camera.mPos.y, camera.mPos.z);
light2.Enable();
light2.Update(camera.mPos.x, camera.mPos.y, camera.mPos.z);
light3.Enable();
light3.Update(camera.mPos.x, camera.mPos.y, camera.mPos.z);
ground.Draw();
}
void Draw() {
DrawCamera();
}
void OnKeyDown(char code)
{
switch (code) {
case 'A':
camera.mbMoveLeft = true;
break;
case 'D':
camera.mbMoveRight = true;
break;
case 'W':
camera.mbMoveForward = true;
break;
case 'S':
camera.mbMoveBack = true;
break;
}
}
void OnKeyUp(char code)
{
switch (code) {
case 'A':
camera.mbMoveLeft = false;
break;
case 'D':
camera.mbMoveRight = false;
break;
case 'W':
camera.mbMoveForward = false;
break;
case 'S':
camera.mbMoveBack = false;
break;
}
}
void OnMouseMove(float deltaX, float deltaY)
{
float angleRotateByUp = deltaX / 1000.0f;
float angleRotateByRight = deltaY / 1000.0f;
camera.Yaw(-angleRotateByUp);
camera.Picth(-angleRotateByRight);
}