Opengl 转换矩阵在茶壶模型中的应用
我是openGL新手,现在我有几个函数可以生成转换矩阵,一个是4x4,另一个是3x3。我已经打印出矩阵中的各个值,它们是正确的。现在我想将这些值应用到openGL内置的茶壶模型中。(矩阵中填充了glfloat) 我们有没有办法从茶壶中提取矩阵,并用新矩阵中的数据填充它?如果没有,是否有一种方法可以轻松地将该值应用于茶壶 编辑: 以下是我目前掌握的代码:Opengl 转换矩阵在茶壶模型中的应用,opengl,matrix,matrix-multiplication,coordinate-transformation,Opengl,Matrix,Matrix Multiplication,Coordinate Transformation,我是openGL新手,现在我有几个函数可以生成转换矩阵,一个是4x4,另一个是3x3。我已经打印出矩阵中的各个值,它们是正确的。现在我想将这些值应用到openGL内置的茶壶模型中。(矩阵中填充了glfloat) 我们有没有办法从茶壶中提取矩阵,并用新矩阵中的数据填充它?如果没有,是否有一种方法可以轻松地将该值应用于茶壶 编辑: 以下是我目前掌握的代码: #include "stdafx.h" // standard #include <assert.h> #include <
#include "stdafx.h"
// standard
#include <assert.h>
#include <math.h>
#include <Eigen/Dense>
#include <iostream>
// glut
#include <GL/glut.h>
using namespace Eigen;
using namespace std;
//================================
// global variables
//================================
// screen size
int g_screenWidth = 0;
int g_screenHeight = 0;
// frame index
int g_frameIndex = 0;
int g_angle = 0;
void init( void ) {
// init something before main loop...
}
void update( void ) {
// do something before rendering...
// rotation angle
g_angle = ( g_angle + 5 ) % 360;
}
void quatMath(int w, int x, int y, int z)
{
ArrayXXf transform(3,3);
transform(0,0)= 1 -2*(y^2) - 2*(z^2);
//calculating value for top left entry in matrix
transform(0,1) = (2*x*y) - (2*w*y);
//calculating value for row 0 column 1
transform(0,2) = (2*x*z) + (2*w*y);
//row 0 column 2
transform(1,0) = (2*x*y)+(2*w*z);
//middle row, leftmost entry
transform(1,1) = 1 - (2*(x^2)) - (2*(z^2));
//middle row middle column
transform (1,2) = (2*y*z) - (2*w*x);
//middle row right column
transform(2,0) = (2*x*z) - (2*w*y);
//bottom row leftmost column
transform (2,1) = (2*y*z) + (2*w*x);
//bottom row middle column
transform(2,2) = 1 - (2*(x^2)) - (2*(y^2));
//bottom row right column
std::cout << transform;
}
ArrayXXf fixedAngle(GLfloat initialX, GLfloat initialY, GLfloat initialZ, GLfloat rotX, GLfloat rotY, GLfloat rotZ)
{
ArrayXXf z(4,4); //initializing the 4x4 rotation matrixes
ArrayXXf y(4,4);
ArrayXXf x(4,4);
x<<1, 0, 0, 0,
0, cos(rotX), -sin(rotX), 0,
0, sin(rotX), cos(rotX), 0,
0, 0, 0, 1;
y<<cos(rotY), 0, sin(rotY), 0,
0, 1, 0, 0,
-sin(rotY), 0, cos(rotY), 0,
0, 0, 0, 1;
z<< cos(rotZ), -sin(rotZ), 0, 0,
sin(rotZ), cos(rotZ), 0, 0,
0, 0, 1, 0,
0, 0, 0, 1;
ArrayXXf fin(4,4);
fin = x * y * z;
fin(0,3) = initialX;
fin(1,3) = initialY;
fin(2,3) = initialZ;
//now we've moved the translational information into the final matrix
std::cout << fin;
return fin;
}
//================================
// render
//================================
void render( void ) {
// clear buffer
glClearColor (0.0, 0.0, 0.0, 0.0);
glClearDepth (1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// render state
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
// enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
// light source attributes
GLfloat LightAmbient[] = { 0.4f, 0.4f, 0.4f, 1.0f };
GLfloat LightDiffuse[] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat LightSpecular[] = { 0.4f, 0.4f, 0.4f, 1.0f };
GLfloat LightPosition[] = { 5.0f, 5.0f, 5.0f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT , LightAmbient );
glLightfv(GL_LIGHT0, GL_DIFFUSE , LightDiffuse );
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);
// surface material attributes
GLfloat material_Ka[] = { 0.11f, 0.06f, 0.11f, 1.0f };
GLfloat material_Kd[] = { 0.43f, 0.47f, 0.54f, 1.0f };
GLfloat material_Ks[] = { 0.33f, 0.33f, 0.52f, 1.0f };
GLfloat material_Ke[] = { 0.1f , 0.0f , 0.1f , 1.0f };
GLfloat material_Se = 10;
glMaterialfv(GL_FRONT, GL_AMBIENT , material_Ka);
glMaterialfv(GL_FRONT, GL_DIFFUSE , material_Kd);
glMaterialfv(GL_FRONT, GL_SPECULAR , material_Ks);
glMaterialfv(GL_FRONT, GL_EMISSION , material_Ke);
glMaterialf (GL_FRONT, GL_SHININESS , material_Se);
// modelview matrix
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef (0.0, 0.0, -5.0);
glRotated(g_angle, 0.0, 1.0, 0.0);
// render objects
glutSolidTeapot(1.0);
// disable lighting
glDisable(GL_LIGHT0);
glDisable(GL_LIGHTING);
// swap back and front buffers
glutSwapBuffers();
}
//================================
// timer : triggered every 16ms ( about 60 frames per second )
//================================
void timer( int value ) {
// increase frame index
g_frameIndex++;
update();
// render
glutPostRedisplay();
// reset timer
// 16 ms per frame ( about 60 frames per second )
glutTimerFunc( 16, timer, 0 );
}
//================================
// main
//================================
int main( int argc, char** argv ) {
// create opengL window
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH );
glutInitWindowSize( 600, 600 );
glutInitWindowPosition( 100, 100 );
glutCreateWindow( argv[0] );
fixedAngle(6, 4, 3, 5, 6, 3);
cout<<"\n";
quatMath(3, 4, 5, 6);
// init
init();
// set callback functions
glutDisplayFunc( render );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutTimerFunc( 16, timer, 0 );
// main loop
glutMainLoop();
return 0;
}
#包括“stdafx.h”
//标准
#包括
#包括
#包括
#包括
//过剩
#包括
使用名称空间特征;
使用名称空间std;
//================================
//全局变量
//================================
//屏幕大小
int g_屏幕宽度=0;
int g_屏幕高度=0;
//帧索引
int g_frameIndex=0;
int g_角=0;
void init(void){
//在主循环之前初始化一些东西。。。
}
作废更新(作废){
//在渲染之前执行某些操作。。。
//旋转角
g_角=(g_角+5)%360;
}
虚空四元数(整数w、整数x、整数y、整数z)
{
arrayxf变换(3,3);
变换(0,0)=1-2*(y^2)-2*(z^2);
//计算矩阵中左上角项目的值
变换(0,1)=(2*x*y)-(2*w*y);
//正在计算第0行第1列的值
变换(0,2)=(2*x*z)+(2*w*y);
//第0行第2列
变换(1,0)=(2*x*y)+(2*w*z);
//中间行,最左边的条目
变换(1,1)=1-(2*(x^2))-(2*(z^2));
//中间行中间列
变换(1,2)=(2*y*z)-(2*w*x);
//中行右列
变换(2,0)=(2*x*z)-(2*w*y);
//最底行最左边的列
变换(2,1)=(2*y*z)+(2*w*x);
//底行中间列
变换(2,2)=1-(2*(x^2))-(2*(y^2));
//底行右列
std::cout内置是什么意思?如何使用它?在openGL中,您可以使用单个glut命令渲染茶壶。因此glut是内置在茶壶中的,而不是openGL。向我们展示您到目前为止的代码。