计算机图形学6——3D scene roaming(三维场景漫游)

2023-11-10 13:50

本文主要是介绍计算机图形学6——3D scene roaming(三维场景漫游),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

核心代码有:
前进操作:

	void move_up(void){P0.x=P0.x+step*v.x;P0.y=P0.y+step*v.y;P0.z=P0.z+step*v.z;}

左旋转操作:

	void turn_left(void){u.x=u.x*cos(turn_a)-n.x*sin(turn_a);u.y=u.y*cos(turn_a)-n.y*sin(turn_a);u.z=u.z*cos(turn_a)-n.z*sin(turn_a);n.x=u.x*sin(turn_a)+n.x*cos(turn_a);n.y=u.y*sin(turn_a)+n.y*cos(turn_a);n.z=u.z*sin(turn_a)+n.z*cos(turn_a);}

完整代码如下:

// ====== Computer Graphics Experiment #8 ======
// |             3D scene roaming              |
// =============================================
//
// Requirement:
// (1) Implement translation and rotation of view reference frame
// (2) Change smooth shading to flat shading and observe the effects
// (3) Carefully read and understand the rest of the source code#include <windows.h>
#include <GL/glut.h>
#include <math.h>#define PI 3.14159265// 3D vector class
class CVector3D
{
public:float x, y, z;// ConstructorsCVector3D(void) {x=0.0; y=0.0; z=0.0;}CVector3D(float x0, float y0, float z0){x=x0; y=y0; z=z0;}
};
/*view_frame.u=CVector3D(0.0, 1.0, 0.0);view_frame.v=CVector3D(0.0, 0.0, 1.0);view_frame.n=CVector3D(1.0, 0.0, 0.0);*/
// View reference frame class
class CViewFrame
{
public:float step; // step sizefloat turn_a; // turn anglefloat pitch_a; // pitch anglefloat roll_a; // roll angleCVector3D P0; // View reference pointCVector3D u; // unit vector in xv directionCVector3D v; // unit vector in yv directionCVector3D n; // unit vector in zv directionvoid move_up(void){P0.x=P0.x+step*v.x;P0.y=P0.y+step*v.y;P0.z=P0.z+step*v.z;}void move_down(void){P0.x=P0.x-step*v.x;P0.y=P0.y-step*v.y;P0.z=P0.z-step*v.z;}void move_left(void){P0.x=P0.x-step*u.x;P0.y=P0.y-step*u.y;P0.z=P0.z-step*u.z;}void move_right(void){P0.x=P0.x+step*u.x;P0.y=P0.y+step*u.y;P0.z=P0.z+step*u.z;}void move_forward(void){P0.x=P0.x-step*n.x;P0.y=P0.y-step*n.y;P0.z=P0.z-step*n.z;}void move_backward(void){P0.x=P0.x+step*n.x;P0.y=P0.y+step*n.y;P0.z=P0.z+step*n.z;}void turn_left(void){u.x=u.x*cos(turn_a)-n.x*sin(turn_a);u.y=u.y*cos(turn_a)-n.y*sin(turn_a);u.z=u.z*cos(turn_a)-n.z*sin(turn_a);n.x=u.x*sin(turn_a)+n.x*cos(turn_a);n.y=u.y*sin(turn_a)+n.y*cos(turn_a);n.z=u.z*sin(turn_a)+n.z*cos(turn_a);}void turn_right(void){u.x=u.x*cos(turn_a)+n.x*sin(turn_a);u.y=u.y*cos(turn_a)+n.y*sin(turn_a);u.z=u.z*cos(turn_a)+n.z*sin(turn_a);n.x=-u.x*sin(turn_a)+n.x*cos(turn_a);n.y=-u.y*sin(turn_a)+n.y*cos(turn_a);n.z=-u.z*sin(turn_a)+n.z*cos(turn_a);}void look_up(void){v.x=v.x*cos(pitch_a)+n.x*sin(pitch_a);v.y=v.y*cos(pitch_a)+n.y*sin(pitch_a);v.z=v.z*cos(pitch_a)+n.z*sin(pitch_a);n.x=-v.x*sin(pitch_a)+n.x*cos(pitch_a);n.y=-v.y*sin(pitch_a)+n.y*cos(pitch_a);n.z=-v.z*sin(pitch_a)+n.z*cos(pitch_a);}void look_down(void){v.x=v.x*cos(pitch_a)-n.x*sin(pitch_a);v.y=v.y*cos(pitch_a)-n.y*sin(pitch_a);v.z=v.z*cos(pitch_a)-n.z*sin(pitch_a);n.x=v.x*sin(pitch_a)+n.x*cos(pitch_a);n.y=v.y*sin(pitch_a)+n.y*cos(pitch_a);n.z=v.z*sin(pitch_a)+n.z*cos(pitch_a);}void roll_left(void){u.x=u.x*cos(roll_a)+v.x*sin(roll_a);u.y=u.y*cos(roll_a)+v.y*sin(roll_a);u.z=u.z*cos(roll_a)+v.z*sin(roll_a);v.x=-u.x*sin(roll_a)+v.x*cos(roll_a);v.y=-u.y*sin(roll_a)+v.y*cos(roll_a);v.z=-u.z*sin(roll_a)+v.z*cos(roll_a);}void roll_right(void){u.x=u.x*cos(roll_a)-v.x*sin(roll_a);u.y=u.y*cos(roll_a)-v.y*sin(roll_a);u.z=u.z*cos(roll_a)-v.z*sin(roll_a);v.x=u.x*sin(roll_a)+v.x*cos(roll_a);v.y=u.y*sin(roll_a)+v.y*cos(roll_a);v.z=u.z*sin(roll_a)+v.z*cos(roll_a);}
};CViewFrame view_frame;// Initialization function
void init(void)
{static GLfloat light_ambient[] = { 0.01, 0.01, 0.01, 1.0 };static GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };static GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };glClearColor (0.0, 0.0, 0.0, 0.0);glShadeModel (GL_SMOOTH); // Set shading model// Set light source properties for light source #0glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);glEnable(GL_LIGHTING); // Enable lightingglEnable(GL_LIGHT0); // Enable light source #0glEnable(GL_DEPTH_TEST); // Enable depth buffer testglEnable(GL_NORMALIZE); // Enable auto normalizationglEnable(GL_CULL_FACE); // Enable face culling// Set initial properties for view reference frameview_frame.P0=CVector3D(500.0, 0.0, 100.0);view_frame.u=CVector3D(0.0, 1.0, 0.0);view_frame.v=CVector3D(0.0, 0.0, 1.0);view_frame.n=CVector3D(1.0, 0.0, 0.0);view_frame.step=2;view_frame.turn_a=PI/18;view_frame.pitch_a=PI/18;view_frame.roll_a=PI/18;
}// Function to draw chess board on xy plane
void draw_chess_board(float sx, float sy, float sz, int nx, int ny)
// sx, sy, sz: size of the chess board
// nx, ny: Number of chess grids in x and y direction
{static GLfloat mat1_color[] = { 0.8, 0.8, 0.8, 1.0 };static GLfloat mat2_color[] = { 0.2, 0.2, 0.2, 1.0 };int i, j, iflag, jflag;float x, y, dx, dy;float *pcolor;dx=sx/(float)nx;dy=sy/(float)ny;for (i=0; i<nx; ++i){iflag=i%2;x=(i+0.5)*dx-0.5*sx;for (j=0; j<ny; ++j){jflag=j%2;y=(j+0.5)*dy-0.5*sy;if (iflag==jflag) pcolor=mat1_color;else pcolor=mat2_color;glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, pcolor);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pcolor);glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0);glPushMatrix();glTranslatef(x, y, 0.0);glScalef(dx, dy, sz);glutSolidCube(1.0);glPopMatrix();}}}// Function to draw chess pieces
void draw_chess_piece(float sx, float sy, float sz, int nx, int ny)
// sx, sy, sz: size of the chess board
// nx, ny: Number of chess grids in x and y direction
{static GLfloat mat_color[4][4] = {{ 1.0, 0.0, 0.0, 1.0 },{ 0.0, 1.0, 0.0, 1.0 },{ 0.0, 0.0, 1.0, 1.0 },{ 1.0, 1.0, 0.0, 1.0 }};float x, y, dx, dy, size;dx=sx/(float)nx;dy=sy/(float)ny;if (dx<dy) size=dx;else size=dy;//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);// Draw a sphereglMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_color[0]);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_color[0]);glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0);x=(0+0.5)*dx-0.5*sx;y=(2+0.5)*dy-0.5*sy;glPushMatrix();glTranslatef(x, y, 0.5*(sz+size));glutSolidSphere(0.5*size, 10, 10);glPopMatrix();// Draw a torusglMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_color[1]);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_color[1]);glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0);x=(1+0.5)*dx-0.5*sx;y=(0+0.5)*dy-0.5*sy;glPushMatrix();glTranslatef(x, y, 0.5*(sz+size));glRotatef(-90, 1.0, 0.0, 0.0);glutSolidTorus(0.1*size, 0.4*size, 10, 20);glPopMatrix();// Draw a coneglMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_color[2]);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_color[2]);glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0);x=(2+0.5)*dx-0.5*sx;y=(1+0.5)*dy-0.5*sy;glPushMatrix();glTranslatef(x, y, 0.5*sz);glutSolidCone(0.4*size, size, 10, 5);glPopMatrix();// Draw a rectangular solidglMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, mat_color[3]);glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_color[3]);glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 64.0);x=(3+0.5)*dx-0.5*sx;y=(3+0.5)*dy-0.5*sy;glPushMatrix();glTranslatef(x, y, 0.5*(sz+0.9*size));glScalef(0.9*dx, 0.9*dy, 0.9*size);glutSolidCube(1.0);glPopMatrix();//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}// Display callback function
void display(void)
{static GLfloat light_pos[4]={200.0, 200.0, 200.0, 1.0};// Clear frame buffer and depth bufferglClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glPushMatrix(); //Save current ModelView matrix// Set viewing transformation matrixCVector3D look_at;look_at.x=view_frame.P0.x-view_frame.n.x;look_at.y=view_frame.P0.y-view_frame.n.y;look_at.z=view_frame.P0.z-view_frame.n.z;gluLookAt(view_frame.P0.x, view_frame.P0.y, view_frame.P0.z,look_at.x, look_at.y, look_at.z,view_frame.v.x, view_frame.v.y, view_frame.v.z);// Set light source positionglLightfv(GL_LIGHT0, GL_POSITION, light_pos);// Draw the scenedraw_chess_board(400.0, 400.0, 40.0, 4, 4);draw_chess_piece(400.0, 400.0, 40.0, 4, 4);glPopMatrix(); //Restore ModelView matrixglutSwapBuffers(); // Swap front and back buffer
}// Reshape callback function
void reshape (int w, int h)
{float wsize=500.0;// Set viewport as the entire program windowglViewport (0, 0, w, h);// Set symmetric perspective projectionglMatrixMode (GL_PROJECTION);glLoadIdentity ();gluPerspective(60.0, (float)w/(float)h, 10.0, 100000.0);// Reset modelview transformation matrix to identityglMatrixMode (GL_MODELVIEW);glLoadIdentity ();
}// Keyboard callback function
void keyboard (unsigned char key, int x, int y)
{switch (key){case 27:exit(0); break;case 'w':view_frame.move_forward();glutPostRedisplay(); break;case 's'://move backwardview_frame.move_backward();glutPostRedisplay(); break;case 'a'://move leftview_frame.move_left();glutPostRedisplay(); break;case 'd'://move rightview_frame.move_right();glutPostRedisplay(); break;case 'q'://roll leftview_frame.roll_left();glutPostRedisplay(); break;case 'e'://roll rightview_frame.roll_right();glutPostRedisplay(); break;}
}// Special key callback function
void special_key(int key, int x, int y)
{switch (key){case GLUT_KEY_LEFT:view_frame.turn_left();glutPostRedisplay(); break;case GLUT_KEY_RIGHT://turn rightview_frame.turn_right();glutPostRedisplay(); break;case GLUT_KEY_UP://look upview_frame.look_up();glutPostRedisplay(); break;case GLUT_KEY_DOWN://look downview_frame.look_down();glutPostRedisplay(); break;case GLUT_KEY_PAGE_UP://move upview_frame.move_up();glutPostRedisplay(); break;case GLUT_KEY_PAGE_DOWN://move downview_frame.move_down();glutPostRedisplay(); break;}
}// Main program
int main(int argc, char* argv[])
{glutInit(&argc, argv);glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);glutInitWindowSize (500, 300);glutInitWindowPosition (50, 50);glutCreateWindow ("3D Scene Roaming");init();glutDisplayFunc(display);glutReshapeFunc(reshape);glutKeyboardFunc(keyboard);glutSpecialFunc(special_key);glutMainLoop();return 0;
}

在这里插入图片描述

这篇关于计算机图形学6——3D scene roaming(三维场景漫游)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/383041

相关文章

无人叉车3d激光slam多房间建图定位异常处理方案-墙体画线地图切分方案

墙体画线地图切分方案 针对问题:墙体两侧特征混淆误匹配,导致建图和定位偏差,表现为过门跳变、外月台走歪等 ·解决思路:预期的根治方案IGICP需要较长时间完成上线,先使用切分地图的工程化方案,即墙体两侧切分为不同地图,在某一侧只使用该侧地图进行定位 方案思路 切分原理:切分地图基于关键帧位置,而非点云。 理论基础:光照是直线的,一帧点云必定只能照射到墙的一侧,无法同时照到两侧实践考虑:关

Hadoop企业开发案例调优场景

需求 (1)需求:从1G数据中,统计每个单词出现次数。服务器3台,每台配置4G内存,4核CPU,4线程。 (2)需求分析: 1G / 128m = 8个MapTask;1个ReduceTask;1个mrAppMaster 平均每个节点运行10个 / 3台 ≈ 3个任务(4    3    3) HDFS参数调优 (1)修改:hadoop-env.sh export HDFS_NAMENOD

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

计算机毕业设计 大学志愿填报系统 Java+SpringBoot+Vue 前后端分离 文档报告 代码讲解 安装调试

🍊作者:计算机编程-吉哥 🍊简介:专业从事JavaWeb程序开发,微信小程序开发,定制化项目、 源码、代码讲解、文档撰写、ppt制作。做自己喜欢的事,生活就是快乐的。 🍊心愿:点赞 👍 收藏 ⭐评论 📝 🍅 文末获取源码联系 👇🏻 精彩专栏推荐订阅 👇🏻 不然下次找不到哟~Java毕业设计项目~热门选题推荐《1000套》 目录 1.技术选型 2.开发工具 3.功能

MiniGPT-3D, 首个高效的3D点云大语言模型,仅需一张RTX3090显卡,训练一天时间,已开源

项目主页:https://tangyuan96.github.io/minigpt_3d_project_page/ 代码:https://github.com/TangYuan96/MiniGPT-3D 论文:https://arxiv.org/pdf/2405.01413 MiniGPT-3D在多个任务上取得了SoTA,被ACM MM2024接收,只拥有47.8M的可训练参数,在一张RTX

PostgreSQL核心功能特性与使用领域及场景分析

PostgreSQL有什么优点? 开源和免费 PostgreSQL是一个开源的数据库管理系统,可以免费使用和修改。这降低了企业的成本,并为开发者提供了一个活跃的社区和丰富的资源。 高度兼容 PostgreSQL支持多种操作系统(如Linux、Windows、macOS等)和编程语言(如C、C++、Java、Python、Ruby等),并提供了多种接口(如JDBC、ODBC、ADO.NET等

Vector3 三维向量

Vector3 三维向量 Struct Representation of 3D vectors and points. 表示3D的向量和点。 This structure is used throughout Unity to pass 3D positions and directions around. It also contains functions for doin

计算机视觉工程师所需的基本技能

一、编程技能 熟练掌握编程语言 Python:在计算机视觉领域广泛应用,有丰富的库如 OpenCV、TensorFlow、PyTorch 等,方便进行算法实现和模型开发。 C++:运行效率高,适用于对性能要求严格的计算机视觉应用。 数据结构与算法 掌握常见的数据结构(如数组、链表、栈、队列、树、图等)和算法(如排序、搜索、动态规划等),能够优化代码性能,提高算法效率。 二、数学基础

SAM2POINT:以zero-shot且快速的方式将任何 3D 视频分割为视频

摘要 我们介绍 SAM2POINT,这是一种采用 Segment Anything Model 2 (SAM 2) 进行零样本和快速 3D 分割的初步探索。 SAM2POINT 将任何 3D 数据解释为一系列多向视频,并利用 SAM 2 进行 3D 空间分割,无需进一步训练或 2D-3D 投影。 我们的框架支持各种提示类型,包括 3D 点、框和掩模,并且可以泛化到不同的场景,例如 3D 对象、室