// first_3D.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <GL/glut.h>
#include <math.h> // 将立方体的八个顶点保存到一个数组里面
static const GLfloat vertex_list[][] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
};
//变换后数组
GLfloat vertex_list_new[][] = {
-0.5f, -0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
-0.5f, 0.5f, -0.5f,
0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
0.5f, 0.5f, 0.5f,
}; void work(float T[][])
{
GLfloat list_temp[][];
for(int i=;i<;i++)
for(int j=;j<;j++)
list_temp[i][j] = vertex_list_new[i][j];
for(int i=;i<;i++)
{
for(int j=;j<;j++)
{
float sum = ;
for(int k=;k<;k++)
{
sum+=list_temp[i][k]*T[k][j];
}
vertex_list_new[i][j] = sum;
}
}
return;
}
// 将要使用的顶点的序号保存到一个数组里面
static const GLint index_list[][] = {
, , , ,
, , , ,
, , , ,
, , , ,
, , , ,
, , , ,
}; void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//gluLookAt(1.5,1.0,1.0,0.0,0.0,0.0,0.0,1.0,0.0);//视点 /* /坐标轴
glLineWidth(1);
glColor3f( 0.0, 0.0, 0.0); // 黑色
glBegin(GL_LINES);
glVertex3f(-0xFFFFFFF,0,0);
glVertex3f(0xFFFFFFF,0,0);
glVertex3f(0,-0xFFFFFFF,0);
glVertex3f(0,0xFFFFFFF,0);
glVertex3f(0,0,-0xFFFFFFF);
glVertex3f(0,0,0xFFFFFFF);
glEnd();
//坐标轴绘制结束*/ /* /绘制原立方体
glColor3f( 1.0, 1.0, 0.0);
//glBegin(GL_QUADS);
for(int i=0; i<6; ++i) // 有六个面,循环六次
{
glBegin( GL_LINE_LOOP);
for(int j=0; j<4; ++j) // 每个面有四个顶点,循环四次
{
glVertex3fv(vertex_list[index_list[i][j]]);
}
glEnd();
}
//立方体绘制结束*/ //绘制新立方体
glColor3f( 1.0, 0.0, 0.0);
//glBegin(GL_QUADS);
for(int i=; i<; ++i) // 有六个面,循环六次
{
glBegin( GL_LINE_LOOP);
for(int j=; j<; ++j) // 每个面有四个顶点,循环四次
{
glVertex3fv(vertex_list_new[index_list[i][j]]);
}
glEnd();
}
//立方体绘制结束
glFlush();
glutSwapBuffers();
} void reshape(int w,int h)
{
glViewport(,,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
} void init()
{
glClearColor( , , , 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-4.0,4.0,-4.0,4.0,-4.0,4.0);
glMatrixMode(GL_MODELVIEW);
} int main(int argc,char** argv)
{
//计算部分
float a,b,c;//Ov
a = ;
b = ;
c = ;
float R = sqrt(a*a+b*b+c*c);
float d = 1.0;
float costh = c/sqrt(a*a+c*c);
float sinth = a/sqrt(a*a+c*c);
float cosfy = b/R;
float sinfy = sqrt(a*a+c*c)/R; float T1[][] = {{,,,},{,,,},{,,,},{-a,-b,-c,}}; //原点到视点平移变换矩阵
float T2[][] = {{-costh,,-sinth,},{,,,},{sinth,,-costh,},{,,,}}; //绕y1轴旋转变换
float T3[][] = {{,,,},{,sinfy,-cosfy,},{,cosfy,sinfy,},{,,,}}; //绕x2轴旋转变换
float T4[][] = {{-,,,},{,,,},{,,,},{,,,}}; //关于y3Ovz3面的反射变换
//work(T1);work(T2);work(T3);work(T4); //世界--观察坐标系 合成矩阵
float Tv[][] = {{cosfy,-cosfy*sinth,-sinfy*sinth,},{,sinfy,-cosfy,},{-sinth,-cosfy*costh,-sinfy*costh,},{,,R,}};
//work(Tv); float Tpersp[][] = {{,,,},{,,,},{,,,/d},{,,,}}; //透视矩阵
float Tproj[][] = {{-,,,},{,,,},{,,,},{,,,}}; //投影矩阵
//work(Tpersp);work(Tproj);
float Ts[][] = {{,,,},{,,,},{,,,/d},{,,,}}; //合成
//work(Ts); //总合成
float T[][] = {{costh,-costh*sinth,,-sinfy*sinth/d},{,sinfy,,-cosfy/d},{-sinth,-cosfy*costh,,-sinfy*costh/d},{,,,R/d}};
work(T); float T_1[][] = {{,,,},{,,,},{,,,-/d},{,,,R/d}};
//work(T_1); glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(,);
glutInitWindowPosition(,);
glutCreateWindow("立方体");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
init();
glutMainLoop();
return ;
}

opengl 正方体+模拟视角旋转的更多相关文章

  1. Matlab之视角旋转函数[转]

    Matlab中有两个视角旋转函数:view和rotate,下面详细介绍: view: 一: view(az,el):az是方位角,el是仰角,单位均是度.具体: 以x轴从左到右(即从小到大)平行放置在 ...

  2. OpenGL中平移、旋转、缩放矩阵堆栈操作

    在OpenGL中,图元的几何变换均为线性变换,通过矩阵变换实现.OpenGL中的坐标用齐次坐标表示,即(x,y,z)表示成(x',y',z',h),其中x=x'/h; y=y'/h; z=z'/h. ...

  3. OpenGL ES 响应屏幕旋转 iOS

    iOS下使用OpenGL 如果使用GLKit View 那么不用担心屏幕旋转的问题,说明如下: If you change the size, scale factor, or drawable pr ...

  4. 让camera实现类似cs第一人称视角旋转和位移

    直接把这个脚本挂在摄像机上就可: using System.Collections; using System.Collections.Generic; using UnityEngine; /* * ...

  5. OPENGL 显示BMP图片+旋转

    VS2010/Windows 7/ 1. 需包含头文件 stdio.h, glaux.h, glut.h.需要对应的lib,并添加包含路径 2. 窗口显示用glut库的函数 3. bmp图片从本地读取 ...

  6. [CSP-S模拟测试]:旋转子段(数学)

    题目描述 $ZYL$有$N$张牌编号分别为$1,2,...,N$.他把这$N$张牌打乱排成一排,然后他要做一次旋转使得旋转后固定点尽可能多.如果第$i$个位置的牌的编号为$i$,我们就称之为固定点.旋 ...

  7. OpenGL的glRotatef旋转变换函数详解

    OpenGL的glRotatef旋转变换函数详解 先看一下函数定义:void glRotatef(GLfloat angle,  GLfloat x,     GLfloat y,     GLflo ...

  8. WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体

    原文:WPF 3D:简单的Point3D和Vector3D动画创造一个旋转的正方体 运行结果: 事实上很简单,定义好一个正方体,处理好纹理.关于MeshGeometry3D的正确定义和纹理这里就不多讲 ...

  9. three.js 对象绕任意轴旋转--模拟门转动

    说了几篇的数学方法,这篇放松一下,郭先生说说绕任意轴转动.说一说其中一种方法,也是比较容易理解的一种,它的原理就是将子对象放到一个盒子中,然后改变子对象相对于父对象的位置(因为子对象的原点默认还是在盒 ...

随机推荐

  1. fragment详解(官方文档)

    原作者为: 苍山.感谢他分享的内容,现在分享出来给eoeAndroid的各位同胞. 详情参考http://www.eoeandroid.com/thread-71642-1-1.html和http:/ ...

  2. C# 调用其他的动态库开发应注意的问题

    1.背景 程序开发语言可以说是五花八门,这就引出了一个新问题 ,不同语言开发的系统进行对接时相关调用的问题. 下面我主要说一下我自己在做接口开发时遇到的问题及解决方法仅供参考,我使用的C#开发进行对接 ...

  3. java enum的用法

    原始的常量定义: public static fianl MON=“Mon”; public static final TUE="Tue"; 语法(定义) 创建枚举类型要使用 en ...

  4. read和onload jquery.val

    $(document).load(); 当web页面以及其附带的资源文件,如CSS,Scripts,图片等,加载完毕后执行此方法.常用于检测页面(及其附带资源)是否加载完毕. $(document). ...

  5. 已经包含了#include <atlcom.h> #include <comutil.h>还是报错

    在WTL工程的.h中 #include <atlbase.h>#include <atlcom.h>#include <atlcomcli.h>#include & ...

  6. Linux学习之nfs实例

    在对exports文件进行了正确的配置后,就可以启动NFS服务器了. 1.启动NFS服务器 为了使NFS服务器能正常工作,需要启动portmap和nfs两个服务,并且portmap一定要先于nfs启动 ...

  7. 关于ThreadAbortExcption异常处理

    之前程序中,使用Thread.Abort()方法来终止线程的运行,但它是抛出ThreadAbortException异常来终止线程. 异常信息摘要: Unhandled Exception:Threa ...

  8. 关于arguments.callee的用途

    arguments为js函数中两个隐藏属性中的一个(另一个为this) arguments表示所有传入的参数,为类数组(array-like)类型,arguments.length表示传入参数的长度, ...

  9. Stream与byte转换

    将 Stream 转成 byte[] /// <summary> /// 将 Stream 转成 byte[] /// </summary> public byte[] Str ...

  10. JAVA并发,BlockingQuene

    BlockingQueue也是java.util.concurrent下的主要用来控制线程同步的工具. BlockingQueue有四个具体的实现类,根据不同需求,选择不同的实现类1.ArrayBlo ...