gpt4 book ai didi

c++ - 在 OpenGl 中创建一个围绕其 y 轴连续旋转的三角形

转载 作者:行者123 更新时间:2023-11-28 03:07:10 24 4
gpt4 key购买 nike

我想知道如何在 OpenGL 中围绕其 Y 轴旋转 OpenGL 三角形,我已经能够将三角形从对象空间转换到剪辑空间。我想知道是否有人对这个问题有任何经验并且可以提供帮助。

main.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <Windows.h>

using namespace std;
using namespace glm;

const int windowWidth = 1024;
const int windowHeight = 768;

GLuint VBO;
const int NUMVERTS = 3;
GLuint gModelToWorldTransformLocation;
GLuint gWorldToViewTransformLocation;
GLuint gProjectionTransformLocation;
struct SimpleVertex
{
vec3 pos;
vec4 colour;
};

static void renderSceneCallBack()
{
static mat4 modelToWorldTransform = mat4(1.0f);
static mat4 worldToViewTransform = lookAt(
vec3(0.0f,0.0f,3.0f), // position of your camera, in world space
vec3(0.0f,0.0f,0.0f), // look at in world space
vec3(0.0f,1.0f,0.0f) // Camera up direction (set to 0,-1,0 to look upside-down)
);
static mat4 projectionTransform = perspective(45.0f, (float)windowWidth / (float)windowHeight, 1.0f, 100.0f);
glUniformMatrix4fv(gModelToWorldTransformLocation, 1, GL_FALSE, &modelToWorldTransform[0][0]);
glUniformMatrix4fv(gWorldToViewTransformLocation, 1, GL_FALSE, &worldToViewTransform[0][0]);
glUniformMatrix4fv(gProjectionTransformLocation, 1, GL_FALSE, &projectionTransform[0][0]);

glClear(GL_COLOR_BUFFER_BIT);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, VBO);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), 0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, sizeof(SimpleVertex), (const GLvoid*)12);
glDrawArrays(GL_TRIANGLES, 0, NUMVERTS);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glutSwapBuffers();
}

static void initializeGlutCallbacks()
{
glutDisplayFunc(renderSceneCallBack);
glutIdleFunc(renderSceneCallBack);
}

static void createVertexBuffer()
{
// Create some vertices to put in our VBO.
// Create vertex buffer
SimpleVertex vertices[] =
{
{vec3(-0.5f, -0.5f, 0.0f), vec4(1.0f, 0.0f, 0.0f, 1.0f)},
{vec3(0.5f, -0.5f, 0.0f), vec4(0.0f, 1.0f, 0.0f, 1.0f)},
{vec3( 0.0f, 0.5f, 0.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f)}
};

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(SimpleVertex) * 3, vertices, GL_STATIC_DRAW);
}

static void addShader(GLuint shaderProgram, const char* pShaderText, GLenum shaderType)
{
GLuint shaderObj = glCreateShader(shaderType);

if (shaderObj == 0)
{
cerr<<"Error creating shader type "<<shaderType<<endl;
exit(0);
}

const GLchar* p[1];
p[0] = pShaderText;
GLint Lengths[1];
Lengths[0]= strlen(pShaderText);
glShaderSource(shaderObj, 1, p, Lengths);
glCompileShader(shaderObj);
GLint success;
glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);
if (!success)
{
GLchar InfoLog[1024];
glGetShaderInfoLog(shaderObj, 1024, NULL, InfoLog);
cerr<<"Error compiling shader type "<<shaderType<<": "<<InfoLog<<endl;
exit(1);
}

glAttachShader(shaderProgram, shaderObj);
}

const string readFileToString(char* filename)
{
ifstream file (filename, ios::in);
if (file.is_open())
{
stringstream continut;
continut << file.rdbuf();
continut << '\0';
return continut.str();
}
return "";
}

static void buildShaders()
{
GLuint shaderProgram = glCreateProgram();

if (shaderProgram == 0)
{
cerr<<"Error creating shader program\n";
exit(1);
}

string VS = readFileToString("vertexShader.glsl");
string FS = readFileToString("fragmentShader.glsl");

addShader(shaderProgram, VS.c_str(), GL_VERTEX_SHADER);
addShader(shaderProgram, FS.c_str(), GL_FRAGMENT_SHADER);

GLint success = 0;
GLchar errorLog[1024] = { 0 };

glLinkProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (success == 0)
{
glGetProgramInfoLog(shaderProgram, sizeof(errorLog), NULL, errorLog);
cerr<<"Error linking shader program: "<<errorLog<<endl;
exit(1);
}

glValidateProgram(shaderProgram);
glGetProgramiv(shaderProgram, GL_VALIDATE_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(shaderProgram, sizeof(errorLog), NULL, errorLog);
cerr<<"Error linking shader program: "<<errorLog<<endl;
exit(1);
}

glUseProgram(shaderProgram);

gModelToWorldTransformLocation = glGetUniformLocation(shaderProgram, "gModelToWorldTransform");
//assert(gModelToWorldTransformLocation != 0xFFFFFFFF);
gWorldToViewTransformLocation = glGetUniformLocation(shaderProgram, "gWorldToViewTransform");
//assert(gWorldToViewTransformLocation != 0xFFFFFFFF);
gProjectionTransformLocation = glGetUniformLocation(shaderProgram, "gProjectionTransform");
//assert(gProjectionTransformLocation != 0xFFFFFFFF);

}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(100, 100);
glutCreateWindow("Transformations");

initializeGlutCallbacks();

// Must be done after glut is initialized!
GLenum res = glewInit();
if (res != GLEW_OK)
{
cerr<<"Error: "<<glewGetErrorString(res)<<"\n";
return 1;
}

buildShaders();

glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

// Create a vertex buffer
createVertexBuffer();

glutMainLoop();

return 0;
}

顶点着色器

#version 330

layout (location = 0) in vec3 Position;
layout (location = 1) in vec4 Colour;

out vec4 Colour0;

uniform mat4 gModelToWorldTransform;
uniform mat4 gWorldToViewTransform;
uniform mat4 gProjectionTransform;

void main()
{
vec4 vertexPositionInModelSpace = vec4(Position, 1);
vec4 vertexInWorldSpace = gModelToWorldTransform * vertexPositionInModelSpace;
vec4 vertexInViewSpace = gWorldToViewTransform * vertexInWorldSpace;
vec4 vertexInHomogeneousClipSpace = gProjectionTransform * vertexInViewSpace;
gl_Position = vertexInHomogeneousClipSpace;
Colour0 = Colour;
}

片段着色器

#version 330

in vec4 Colour0;

out vec4 FragColor;

void main()
{
FragColor = Colour0;
}

最佳答案

你可以有一个用于旋转的浮点变量,并从中创建一次旋转矩阵然后存储它。每一帧你都应该像这样更新世界矩阵:modelToWorldTransform = rotationTransform * modelToWorldTransform;请参阅维基百科以创建围绕 y 的旋转矩阵:http://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations

编辑:我想您在仿射变换方面遇到了麻烦。要移动(无论如何 - 平移,旋转)世界中的对象,您必须对其应用变换。目前,您正在应用一种转换,即转换到位置 0、0、3。到目前为止,一切都很好。但是你的三角形是静止的(它不会移动)。所以要旋转它,您需要应用另一个变换(再次由矩阵表示)。但是如果你总是应用一个相同的旋转,三角形将根据它的原始变换旋转,但它不会再次移动。所以你需要每帧应用一个旋转,将结果存储在 modeltoworldtransform 中,然后在下一帧重复该步骤。例如在 renderCallback 中:

static mat4 modelToWorldTransform = mat4(1.0f);
static mat4 rotationTransform = <rotation by the described in wiki way>;

modelToWorldTransform = rotationTransform * modelToWorldTransform;

此外,乘法矩阵的顺序很重要,因为它们的乘法(我们用它来获得从对象空间到相机空间的变换)是不可交换的,并且 A * B 不同于 B * A。我强烈建议您阅读有关线性的更多信息代数,它将帮助您了解“幕后”发生的事情。许多关于游戏开发者图形的书籍都以非常好且易于理解的方式解释了它。 Eric Lengyel 的“3D 游戏编程和计算机图形学数学”非常棒。它甚至在我大学学习数学时帮助了我 ;)。

关于c++ - 在 OpenGl 中创建一个围绕其 y 轴连续旋转的三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19408037/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com