php中文网 | cnphp.com

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 167|回复: 0

opengl场景漫游代码

[复制链接]

2572

主题

2579

帖子

9100

积分

管理员

Rank: 9Rank: 9Rank: 9

UID
1
威望
0
积分
6406
贡献
0
注册时间
2021-4-14
最后登录
2024-3-28
在线时间
653 小时
QQ
发表于 2022-12-5 09:00:45 | 显示全部楼层 |阅读模式
[mw_shl_code=cpp,true]#define NDEBUG
#include<gl/freeglut.h>
#include<stdio.h>
#include<iostream>
#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"

const GLdouble FRUSTDIM = 100.0f;
GLfloat door = 0.0;
GLfloat sticker = 45.0;
GLfloat screen = 0.0;
GLfloat arm = 20.0;
GLfloat bobo = -60.0;
int option = 0;
int doorOpen = 0;
float l1 = 0.25;
float ll = 1.0;



void init()
{
        glEnable(GL_NORMALIZE);               
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_LIGHTING);
        glEnable(GL_LIGHT0);
        glEnable(GL_LIGHT1);
        glFrontFace(GL_CCW);
        glEnable(GL_COLOR_MATERIAL);
        glEnable(GL_TEXTURE_2D);
}

void display(void)
{
        glPushMatrix();
        //glClearColor(1.0, 1.0, 1.0, 0.0);
        glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
       
       
        // 放置光源
        GLfloat lightpos[] = { 0.f, 100.f, -450.f, ll };                       
        GLfloat light0_mat1[] = { 0.2, 0.2, 0.2, 0.3f };
        GLfloat light0_diff[] = { 1.0, 1.0, 1.0, 0.3 };
        GLfloat lightpos2[] = { 0.0,0.0,0.0,ll };
        GLfloat light1_mat1[] = { 0.1,0.1,0.1,0.3 };
        GLfloat light1_diff[] = { 0.1,0.1,0.1,0.3 };
        glLightfv(GL_LIGHT0,GL_POSITION,lightpos);
        glLightfv(GL_LIGHT0,GL_AMBIENT,light0_mat1);
        glLightfv(GL_LIGHT0,GL_DIFFUSE,light0_diff);
        glLightfv(GL_LIGHT1,GL_POSITION,lightpos2);
        glLightfv(GL_LIGHT1,GL_AMBIENT,light1_mat1);
        glLightfv(GL_LIGHT1,GL_DIFFUSE,light1_diff);

        GLfloat no_mat[] = { 0.0,0.0,0.0,1.0 };
        GLfloat mat_ambient[] = { 0.2,0.2,0.2,1.0 };
        GLfloat mat_diffuse[] = { 0.9,0.9,0.9,1.0 };
        GLfloat mat_specular[] = { 0.3,0.3,0.3,1.0 };
        GLfloat high_shininess[] = { 20.0 };
        GLfloat high_mat[] = { l1,l1,l1,1.0 };

        glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
        glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
        glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
        glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
        glMaterialfv(GL_FRONT, GL_EMISSION, high_mat);

        glTranslatef(0.0, 0.0, -300.0);
       
        //绘制墙面和物体
        glPushMatrix();
        static GLfloat wall_mat[] = { 1.f, 1.f, 1.f, 1.f };
        glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, wall_mat);

        // 后墙
        glColor3f(0.9, 0.5, 0.5);
        glBegin(GL_QUADS);
        glNormal3f(0.0, 0.0, 1.0);
        glVertex3f(100.0, 100.0, -300.0);
        glVertex3f(-100.0, 100.0, -300.0);
        glVertex3f(-100.0, -100.0, -300.0);
        glVertex3f(100.0, -100.0, -300.0);
       

        // 底部
        glColor3f(0.7, 0.7, 0.9);
        glBegin(GL_QUADS);
        glNormal3f(0.0, 1.0, 0.0);
        glVertex3f(100.0, -100.0, 0.0);
        glVertex3f(100.0, -100.0, -300.0);
        glVertex3f(-100.0, -100.0, -300.0);
        glVertex3f(-100.0, -100.0, 0.0);
        glEnd();

        // 顶部
        glColor3f(0.7, 0.7, 0.9);
        glBegin(GL_QUADS);
        glNormal3f(0.0, -1.0, 0.0);
        glVertex3f(100.0, 100.0, 0.0);
        glVertex3f(-100.0, 100.0, 0.0);
        glVertex3f(-100.0, 100.0, -300.0);
        glVertex3f(100.0, 100.0, -300.0);
        glEnd();

        // 左墙
        glColor3f(0.5, 0.6, 0.6);
        glBegin(GL_QUADS);
        glNormal3f(1.0, 0.0, 0.0);
        glVertex3f(-100.0, -100.0, 0.0);
        glVertex3f(-100.0, -100.0, -300.0);
        glVertex3f(-100.0, 100.0, -300.0);
        glVertex3f(-100.0, 100.0, 0.0);
        glEnd();

        // 右墙
        glColor3f(0.5, 0.6, 0.6);
        glBegin(GL_QUADS);
        glNormal3f(-1.0, 0.0, 0.0);
        glVertex3f(100.0, -100.0, 0.0);
        glVertex3f(100.0, 100.0, 0.0);
        glVertex3f(100.0, 100.0, -300.0);
        glVertex3f(100.0, -100.0, -300.0);
        glEnd();

        // 人物
        glPushMatrix();
        glTranslatef(bobo, -10.0, -30.0);               
        // 胳膊
        glColor3f(0.9, 0.7, 0.6);
        glPushMatrix();
        glTranslatef(-12.5, -20.0, 0.0);
        glRotatef(arm, 0.0, 0.0, -1.0);               
        glTranslatef(0.0, -15.0, 0.0);
        glScalef(5.0, 30.0, 5.0);
        glutSolidCube(1.0);
        glPopMatrix();
        glPushMatrix();
        glTranslatef(12.5, -20.0, 0.0);
        glRotatef(arm, 0.0, 0.0, 1.0);
        glTranslatef(0.0, -15.0, 0.0);
        glScalef(5.0, 30.0, 5.0);
        glutSolidCube(1.0);
        glPopMatrix();
        // 头
        glTranslatef(0.0, -5.0, 0.0);
        glColor3f(0.9, 0.7, 0.6);
        glutSolidSphere(12.0, 50, 50);
        // 脖子
        glTranslatef(0.0, -10.0, 0.0);
        glScalef(8.0, 10.0, 10.0);
        glutSolidCube(1.0);
        // 身体
        glColor3f(0.2, 0.2, 0.2);
        glScalef(0.125, 0.1, 0.1);
        glTranslatef(0.0, -18.0, 0.0);
        glScalef(25.0, 35.0, 15.0);
        glutSolidCube(1.0);
        // 腿
        glColor3f(0.3, 0.3, 0.3);
        glScalef(1.0 / 25.0, 1.0 / 35.0, 1.0 / 15.0);
        glTranslatef(-9.0, -35.0, 0.0);
        glScalef(7.0, 35.0, 15.0);
        glutSolidCube(1.0);
        glScalef(1.0 / 7.0, 1.0 / 35.0, 1.0 / 15.0);
        glTranslatef(18.0, 0.0, 0.0);
        glScalef(7.0, 35.0, 15.0);
        glutSolidCube(1.0);
        glPopMatrix();

        // 楼梯
        glPushMatrix();
        glTranslatef(-30.0, -100.0, -275.0);
        glColor3f(0.3, 0.3, 0.3);

        glPushMatrix();
        glTranslatef(65.0, 13.0, -13.0);
        glScalef(65.0, 13.0, 13.0);
        glutSolidCube(2.0);
        glPopMatrix();

        glColor3f(0.3, 0.3, 0.3);
        glPushMatrix();
        glTranslatef(54.0, 39.0, -13.0);
        glScalef(54.0, 13.0, 13.0);
        glutSolidCube(2.0);
        glPopMatrix();

        glColor3f(0.3, 0.3, 0.3);
        glPushMatrix();
        glTranslatef(43.0, 65.0, -13.0);
        glScalef(43.0, 13.0, 13.0);
        glutSolidCube(2.0);
        glPopMatrix();

        glColor3f(0.3, 0.3, 0.3);
        glPushMatrix();
        glTranslatef(32.0, 91.0, -13.0);
        glScalef(32.0, 13.0, 13.0);
        glutSolidCube(2.0);
        glPopMatrix();

        glColor3f(0.3, 0.3, 0.3);
        glPushMatrix();
        glTranslatef(21.0, 117.0, -13.0);
        glScalef(21.0, 13.0, 13.0);//各轴缩放比例,负值的话就是镜像
        glutSolidCube(2.0);
        glPopMatrix();

        glPopMatrix();

        // 灯
        glPushMatrix();
        glColor3f(1.0, 1.0, 0.5);
        glTranslatef(0.0, 94.0, -150.0);
        glScalef(80.0, 4.0, 80.0);//各轴缩放比例,负值的话就是镜像
        glutSolidCube(1.0);
        glPopMatrix();
        glPushMatrix();
        glColor3f(0.6, 0.5, 0.5);
        glTranslatef(0.0, 99.0, -150.0);
        glScalef(100.0, 2.0, 100.0);
        glutSolidCube(1.0);
        glPopMatrix();

        // 门
        glPushMatrix();
        // 地毯
        glPushMatrix();
        glColor3f(0.0, 0.1, 0.0);
        glTranslatef(75.0, -99.9, -40.0);
        glBegin(GL_QUADS);
        glVertex3f(25.0, 0.0, 35.0);
        glVertex3f(25.0, 0.0, -35.0);
        glVertex3f(-25.0, 0.0, -35.0);
        glVertex3f(-25.0, 0.0, 35.0);
        glEnd();
        glPopMatrix();
        // 门外
        glTranslatef(99.9, -50.0, -40.0);
        glColor3f(0.0, 0.0, 0.0);
        glBegin(GL_QUADS);
        glVertex3f(0.0, -50.0, 35.0);
        glVertex3f(0.0, 50.0, 35.0);
        glVertex3f(0.0, 50.0, -35.0);
        glVertex3f(0.0, -50.0, -35.0);
        glEnd();
        // 门
        glColor3f(0.8, 0.6, 0.4);
        glTranslatef(0.0, 0.0, -35.0);
        glRotatef(door, 0.0, -1.0, 0.0);                       
        glTranslatef(0.0, 0.0, 35.0);
        glScalef(6.0, 100.0, 70.0);//各轴缩放比例,负值的话就是镜像
        glutSolidCube(1.0);
        // 猫眼
        glScalef(1.0 / 6.0, 0.01, 1.0 / 70.0);//各轴缩放比例,负值的话就是镜像
        glTranslatef(4.0, 30.0, 0.0);
        glColor3f(0.1, 0.1, 0.1);
        glutSolidSphere(1.0, 10, 10);

        glTranslatef(-8.0, 0.0, 0.0);
        glutSolidSphere(1.0, 10, 10);
        glTranslatef(12.0, 0.0, 0.0);

        // 门贴
        glTranslatef(-4.5, -35.0, 0.0);
        glColor3f(1.0, 0.0, 0.0);
        glRotatef(sticker, 1.0, 0.0, 0.0);               
        glBegin(GL_QUADS);
        glVertex3f(0.0, 15.0, 15.0);
        glVertex3f(0.0, 15.0, -15.0);
        glVertex3f(0.0, -15.0, -15.0);
        glVertex3f(0.0, -15.0, 15.0);
        glEnd();
        glPopMatrix();

        glPushMatrix();
        // 桌子
        glColor3f(0.22, 0.52, 1.0);
        glTranslatef(20.0, -50.0, -225.0);
        glScalef(80.0, 4.0, 50.0);//各轴缩放比例,负值的话就是镜像
        glutSolidCube(1.0);
        glScalef(1.0 / 60.0, 0.25, 0.02);
        // 电脑
        glTranslatef(-5.0, 2.0, 0.0);
        glColor3f(0.0, 0.0, 0.0);
        glScalef(40.0, 2.0, 30.0);
        glutSolidCube(1.0);
        glScalef(1.0 / 40.0, 0.5, 1.0 / 30.0);//各轴缩放比例,负值的话就是镜像
        glTranslatef(0.0, 15.0, -20.0);
        glScalef(40.0, 30.0, 2.0);
        glColor3f(0.0, 0.0, screen);                       
        glutSolidCube(1.0);
        glScalef(1.0 / 40.0, 1.0 / 30.0, 0.5);//各轴缩放比例,负值的话就是镜像
        // 茶壶
        glTranslatef(27.0, -10.0, 20.0);
        glColor3f(0.5, 0.3, 0.1);
        glutSolidTeapot(5.0);
        // 茶壶
        glTranslatef(21.0, -15.0, 20.0);
        glColor3f(0.5, 0.3, 0.1);
        glutSolidTeapot(5.0);
        // 椅子
        glTranslatef(-47.0, -50.0, 60.0);
        glColor3f(1.0, 0.5, 0.13);
        glRotatef(-90.0, 1.0, 0.0, 0.0);
        GLUquadricObj* g_text;
        g_text = gluNewQuadric();
        gluCylinder(g_text, 10.0, 10.0, 30.0, 50, 4);
        glRotatef(90.0, 1.0, 0.0, 0.0);
        glTranslatef(0.0, 30.0, 0.0);
        glScalef(35.0, 5.0, 35.0);//各轴缩放比例,负值的话就是镜像
        glutSolidCube(1.0);
        glPopMatrix();
        glPopMatrix();

        glEnd();
        //glLoadIdentity();
        glPopMatrix();
        glFlush();
        glutSwapBuffers();
}

//改变窗口形状,所绘制的物体不变形,只是大小发生变化
void reshape(int w, int h)
{
        glViewport(0, 0, (GLsizei)w, (GLsizei)h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, 300., 800.);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
}

void keyboard(unsigned char key, int x, int y)
{
        switch (key)
        {               
                // 键盘事件
        case'1':
                printf("打开电脑\n");
                option = 1;
                break;
        case'2':
                printf("关闭电脑\n");
                option = 2;
                break;
        case'3':
                printf("抬起胳膊\n");
                option = 3;
                break;
        case'4':
                printf("放下胳膊\n");
                option = 4;
                break;
        case'5':
                printf("向左走\n");
                option = 5;
                break;
        case'6':
                printf("向右走\n");
                option = 6;
                break;
        case'7':
                printf("开门");
                option = 7;
                break;
        case'8':
                printf("关门\n");
                option = 8;
                break;
        case'9':
                printf("开灯\n");
                option = 9;
                break;
        case'0':
                printf("关灯\n");
                option = 10;
                break;
        default:
                printf("错误\n");
        }
}

void idle()
{
        if (option == 1) {
                if (screen <= 1.0) {
                        Sleep(100);
                        screen += 0.1;
                }
        }
        if (option == 2) {
                if (screen >= 0.0) {
                        Sleep(100);
                        screen -= 0.1;
                }
        }
        if (option == 3) {
                if (arm <= 130.0) {
                        Sleep(5);
                        arm += 1;
                }
        }
        if (option == 4) {
                if (arm >= 20.0) {
                        Sleep(5);
                        arm -= 1;
                }
        }
        if (option == 5) {
                if (bobo >= -60.0) {
                        if (doorOpen == 1) {
                                Sleep(5);
                                bobo -= 1;
                        }
                }
        }
        if (option == 6) {
                if (doorOpen == 1) {
                        if (bobo <= 150.0) {
                                Sleep(5);
                                bobo += 1;
                        }
                }
                else {
                        if (bobo <= 15.0) {
                                Sleep(5);
                                bobo += 1;
                        }
                }
        }
        if (option == 7) {
                doorOpen = 1;
                if (door <= 90.0) {
                        Sleep(5);
                        door += 1;
                }
        }
        if (option == 8) {
                doorOpen = 0;
                if (door >= 0.0) {
                        Sleep(5);
                        door -= 1;
                }
        }
        if (option == 9) {
                l1 = 0.25;
                ll = 1.0;
        }
        if (option == 10) {
                l1 = 0.0;
                ll = 0.0;
        }
        glutPostRedisplay();
}

void main(int argc, char** argv)
{
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
        glutInitWindowSize(600, 600);
        glutInitWindowPosition(100, 100);
        glutCreateWindow("20990316任菠");                       

        init();       
        glutReshapeFunc(reshape);
        glutDisplayFunc(display);
        glutKeyboardFunc(keyboard);
        glutIdleFunc(idle);
        glutMainLoop();
}[/mw_shl_code]





上一篇:C#授权许可限制试用天数明码暗码注册机
下一篇:ModbusTest源码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|php中文网 | cnphp.com ( 赣ICP备2021002321号-2 )51LA统计

GMT+8, 2024-3-29 04:55 , Processed in 0.250673 second(s), 38 queries , Gzip On.

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2020, Tencent Cloud.

申明:本站所有资源皆搜集自网络,相关版权归版权持有人所有,如有侵权,请电邮(fiorkn@foxmail.com)告之,本站会尽快删除。

快速回复 返回顶部 返回列表