opengl笔记—— glMultMatrixf() 区别 glLoadMatrixf()
时间: 2023-11-23 10:07:24 浏览: 30
在OpenGL中,glMultMatrixf()和glLoadMatrixf()都是用来设置矩阵的函数,但它们有一些区别。
glLoadMatrixf()函数用于设置当前矩阵为指定矩阵,这意味着当前矩阵将完全替换为指定矩阵。例如,如果我们想在模型视图矩阵中设置一个新的矩阵,则可以使用该函数将其完全替换为指定矩阵。
glMultMatrixf()函数用于将指定矩阵乘以当前矩阵,并将结果设置为当前矩阵。这意味着,我们可以使用该函数将指定矩阵与当前矩阵组合在一起。例如,如果我们想将一个旋转矩阵应用于模型视图矩阵,则可以使用该函数将旋转矩阵与当前矩阵相乘,从而将其应用于模型视图矩阵。
总之,glLoadMatrixf()用于替换当前矩阵,而glMultMatrixf()用于组合当前矩阵和指定矩阵。
相关问题
opengl绘制动画效果茶壶
要使用OpenGL绘制动画效果茶壶,可以按照以下步骤进行:
1. 加载茶壶模型
使用OpenGL的模型加载库,例如Assimp,加载茶壶模型。
2. 设置视图和投影矩阵
使用OpenGL的矩阵库,例如glm,设置视图和投影矩阵。
3. 设置光照
使用OpenGL的光照库,例如glLight,设置光照。
4. 绘制茶壶
使用OpenGL的绘制函数,例如glBegin和glEnd,绘制茶壶。
5. 实现动画效果
使用OpenGL的定时器,例如glutTimerFunc,实现动画效果。可以通过改变茶壶的位置,旋转角度或者大小来实现动画效果。
下面是一个使用OpenGL绘制动画效果茶壶的示例代码:
```c++
#include <GL/glut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <vector>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
using namespace std;
// 定义茶壶模型
struct Model {
vector<GLfloat> vertices;
vector<GLfloat> normals;
};
Model teapotModel;
// 定义茶壶的位置和旋转角度
GLfloat teapotX = 0.0f;
GLfloat teapotY = 0.0f;
GLfloat teapotZ = -5.0f;
GLfloat teapotAngle = 0.0f;
// 加载茶壶模型
void loadTeapotModel() {
Assimp::Importer importer;
const aiScene* scene = importer.ReadFile("teapot.obj", aiProcess_Triangulate | aiProcess_GenSmoothNormals | aiProcess_FlipUVs);
if (!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
cout << "Error: Failed to load teapot model!" << endl;
return;
}
aiMesh* mesh = scene->mMeshes[0];
for (int i = 0; i < mesh->mNumFaces; i++) {
aiFace face = mesh->mFaces[i];
for (int j = 0; j < 3; j++) {
aiVector3D vertex = mesh->mVertices[face.mIndices[j]];
aiVector3D normal = mesh->mNormals[face.mIndices[j]];
teapotModel.vertices.push_back(vertex.x);
teapotModel.vertices.push_back(vertex.y);
teapotModel.vertices.push_back(vertex.z);
teapotModel.normals.push_back(normal.x);
teapotModel.normals.push_back(normal.y);
teapotModel.normals.push_back(normal.z);
}
}
}
// 初始化OpenGL
void init() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_DEPTH_TEST);
loadTeapotModel();
}
// 绘制茶壶
void drawTeapot() {
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, &teapotModel.vertices[0]);
glNormalPointer(GL_FLOAT, 0, &teapotModel.normals[0]);
glDrawArrays(GL_TRIANGLES, 0, teapotModel.vertices.size() / 3);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
// 绘制场景
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 5.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));
glMultMatrixf(glm::value_ptr(view));
glm::mat4 projection = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
glMultMatrixf(glm::value_ptr(projection));
glTranslatef(teapotX, teapotY, teapotZ);
glRotatef(teapotAngle, 0.0f, 1.0f, 0.0f);
drawTeapot();
glutSwapBuffers();
}
// 更新动画
void update(int value) {
teapotAngle += 1.0f;
if (teapotAngle > 360.0f) {
teapotAngle = 0.0f;
}
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}
// 处理键盘事件
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'w':
teapotY += 0.1f;
break;
case 's':
teapotY -= 0.1f;
break;
case 'a':
teapotX -= 0.1f;
break;
case 'd':
teapotX += 0.1f;
break;
case 'q':
teapotZ += 0.1f;
break;
case 'e':
teapotZ -= 0.1f;
break;
}
glutPostRedisplay();
}
// 主函数
int main(int argc, char* argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutCreateWindow("OpenGL Teapot Animation");
init();
glutDisplayFunc(display);
glutTimerFunc(0, update, 0);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
```
在该示例代码中,我们使用了Assimp库加载茶壶模型,使用了glm库设置视图和投影矩阵,使用了glut库实现定时器和键盘事件处理。在display函数中,我们使用glTranslatef和glRotatef函数分别改变茶壶的位置和旋转角度。在update函数中,我们使用glutTimerFunc函数实现动画效果。在keyboard函数中,我们处理键盘事件,改变茶壶的位置。
opengl利用矩阵绕某点旋转
要将一个物体绕某个点旋转,需要使用以下步骤:
1. 将物体从其原始位置平移到以该点为中心的原点位置。
2. 对物体进行旋转。
3. 将物体从原点位置平移到其旋转后的位置。
在OpenGL中,可以使用矩阵来实现这些步骤。以下是一个基本的旋转矩阵:
```
| cosθ -sinθ 0 |
| sinθ cosθ 0 |
| 0 0 1 |
```
其中,θ是旋转角度,单位是弧度。这个矩阵可以将一个物体绕z轴旋转θ度。为了让物体绕某个点旋转,需要进行一些额外的计算。
以下是一个将物体绕某个点旋转的示例代码:
```c++
// 定义旋转角度和旋转点坐标
float angle = 45.0f;
float rotatePointX = 0.0f;
float rotatePointY = 0.0f;
float rotatePointZ = 0.0f;
// 将物体从其原始位置平移到以旋转点为中心的原点位置
glTranslatef(-rotatePointX, -rotatePointY, -rotatePointZ);
// 计算旋转矩阵
float radian = angle * 3.1415926f / 180.0f;
float c = cos(radian);
float s = sin(radian);
float matrix[16] = {
c, -s, 0, 0,
s, c, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1
};
glMultMatrixf(matrix);
// 将物体从原点位置平移到其旋转后的位置
glTranslatef(rotatePointX, rotatePointY, rotatePointZ);
```
这段代码将会将物体绕(0, 0, 0)点绕z轴旋转45度。如果要绕其他轴旋转,需要修改旋转矩阵。如果要使用其他角度或旋转点,需要修改angle、rotatePointX、rotatePointY和rotatePointZ的值。