#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; //如果初始角度设置为0则初始面向X轴正方向,设置为-90面向
//Z轴负方向,符合默认情况下的设计习惯。
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); // 不加此项会出现放大后物体变暗问题
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 };//环境光
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);
完整版代码关注并私聊领取文章来源:https://www.toymoban.com/news/detail-854467.html
}文章来源地址https://www.toymoban.com/news/detail-854467.html
到了这里,关于计算机图形学,OpenGL编写的一个可实现旋转缩放移动的房间,内有数十种交互的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!