#include <stdlib.h>
#include<stdio.h>
#include<windows.h>
#include <GL/glut.h>
#include <math.h>
#include <gl/GLU.h>
//顏色宏定義
#define white 1.0f, 1.0f, 1.0f
#define black 0.0f, 0.0f, 0.0f
#define red 1.0f, 0.0f, 0.0f
#define blue 0.0f, 0.0f, 1.0f
#define skyBlue 135.0/255.0, 206.0/255.0, 1.0f
#define plum 1.0f, 187.0/255.0, 1.0f //淺紫色
#define pink 1.0f, 181.0/255.0, 197.0/255.0
#define lightGray 211.0/255.0, 211.0/255.0, 211.0/255.0
#define darkGray 156.0/255.0, 156.0/255.0, 156.0/255.0
#define dimGray 105.0/255.0, 105.0/255.0, 105.0/255.0
#define goldenrod 139.0/255.0, 115.0/255.0, 85.0/255.0 //木地板的顏色
#define burlywood 205.0/255.0, 170.0/255.0, 125.0/255.0
#define M_PI 3.14159265358979323846
#define moonlike 0.8392156f,0.9254902,0.94117647
const GLdouble FRUSTDIM = 100.0f;
GLfloat door = 0.0;
GLfloat sticker = 45.0;
GLfloat screen = 0.0;
GLfloat arm = 20.0;
GLfloat xiaom = -60.0;
int option = 0;
int doorOpen = 0;
float l1 = 0.25;
float ll = 1.0;
float win_of = 0.0;
GLdouble moon_x = -120.0, moon_y = 30.0f, moon_z = -180.0f;
// 追蹤觀察點的方向
static GLfloat s_eye[] = { 0,8.0,0 };
static GLfloat s_at[] = { 0.0,0.0,0.0 };
static GLfloat s_angle = -90.0; ???????????????????//如果初始角度設(shè)置為0則初始面向X軸正方向,設(shè)置為-90面向
//Z軸負(fù)方向,符合默認(rèn)情況下的設(shè)計習(xí)慣。
float PI = 3.14159f;
float rad = float(PI * s_angle / 180.0f);
#define SOLID 1
#define WIRE 2
GLdouble door_a = 0.0;
GLdouble c_r = 0.0f, c_g = 0.0f, c_b = 0.0f;
GLdouble chear_y = -75.0f, chear_s = 10.0f;
GLdouble person_x = 50, person_a = 0.0f;
bool mouseLeftDown;
bool mouseRightDown;
float mouseX, mouseY;
float cameraDistance = 0.0;
float cameraAngleX;
float cameraAngleY;
GLdouble door_y = 200.5f;
void drawcube(GLdouble x, GLdouble y, GLdouble z)
{
glPushMatrix();
glTranslatef(x, y, z);
glutSolidCube(5);
glPopMatrix();
}
void init(void) // All Setup For OpenGL Goes Here
{
glEnable(GL_NORMALIZE); // 不加此項會出現(xiàn)放大后物體變暗問題
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glFrontFace(GL_CCW);
glEnable(GL_COLOR_MATERIAL);
}
void display(void) // Here's Where We Do All The Drawing
{
glPushMatrix();
glClearColor(0.0, 0.0, 0.0, 0.0);
glRotatef(cameraAngleX, 1, 0, 0);
glRotatef(cameraAngleY, 0, 1, 0);
glTranslatef(0.0, 0.0, -cameraDistance);
//glTranslatef(0.0, 0.0, -300.0);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); //必須放在display中否則動畫會有bug
// TODO:
// Place light source here
{
GLfloat light0_ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat light0_position[] = { 0.0,85.0, -100, 1.0 };
GLfloat light0_diffuse[] = { 0.2,0.2,0.2, 1.0 };
GLfloat light0_speculer[] = { 0.2,0.2,0.2, 1.0 };//高光
glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0_speculer);
GLfloat spot_derection[] = { 0.0,-1.0,0.0 };
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_derection);
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 100);//聚光燈,照射的范圍
glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 125.0);//聚光的程度
//窗外的光
GLfloat light1_position[] = { moon_x,moon_y,moon_z };//位置
//GLfloat light0_ambient[] = { 0.6, 0.6, 0.6, 1.0 };//環(huán)境光
GLfloat light1_ambient[] = { 0.1, 0.1, 0.1, 1.0 };
//GLfloat light0_diffuse[] = { 0.5, 0.5, 0.5, 0.3 };//漫反射光
GLfloat light1_diffuse[] = { 0.2,0.2,0.2, 1.0 };
glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
完整版代碼關(guān)注并私聊領(lǐng)取文章來源:http://www.zghlxwxcb.cn/news/detail-854467.html
}文章來源地址http://www.zghlxwxcb.cn/news/detail-854467.html
到了這里,關(guān)于計算機圖形學(xué),OpenGL編寫的一個可實現(xiàn)旋轉(zhuǎn)縮放移動的房間,內(nèi)有數(shù)十種交互的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!