gpt4 book ai didi

c - 如何正确初始化 C 中的模型矩阵?

转载 作者:太空宇宙 更新时间:2023-11-04 06:52:11 25 4
gpt4 key购买 nike

我试图转换我在 here 上提出的问题的代码.

但是当我尝试运行它时,我遇到了一个异常,它指向 this 的 mat4(这是一个 4x4 float 的二维数组)标题。 (CGLM,用于 C 中的 OpenGL 数学,因为 GLM 是仅 C++ 库)库。

C++ 代码工作正常并且运行完美,但为什么它在 C 中崩溃。

这是使用“CGLM”的代码的 C 片段(这是崩溃的代码):-

glUseProgram(shaderProgram);
mat4 model =
{
{ 1.0f, 0.0f, 0.0f, 0.0f },
{ 0.0f, 1.0f, 0.0f, 0.0f },
{ 0.0f, 0.0f, 1.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
};
mat4 projection;
glm_ortho(0.0f, (GLfloat)(WIDTH), (GLfloat)(HEIGHT), 0.0f, -1.0f, 1.0f, projection);

vect2 scale = { 2.0f, 2.0f };
vect2 position = { 50.0f, 0.0f };
vec4 color = { 1.0f, 1.0f, 1.0f, 0.5f };
GLfloat rotation = 0.0f;

glm_translate(model, (vec3){ position[0], position[1], 0.0f });

glm_translate(model, (vec3){ 0.5f * scale[0], 0.5f * scale[1], 0.0f });
glm_rotate(model, rotation, (vec3){ 0.0f, 0.0f, 1.0f }); // Why is this function crashing?
glm_translate(model, (vec3){ -0.5f * scale[0], -0.5f * scale[1], 0.0f });

glm_scale(model, (vec3){ scale[0] * width, scale[1] * height, 1.0f });

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, (GLfloat *)projection);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, (GLfloat *)model);
glUniform4f(glGetUniformLocation(shaderProgram, "spriteColor"), color[0], color[1], color[2], color[3]);

这是使用 GLM 的代码的 C++ 片段:-

glUseProgram(shaderProgram);
glm::mat4 model(1.0f);
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(WIDTH), static_cast<GLfloat>(HEIGHT), 0.0f, -1.0f, 1.0f);

glm::vec2 scale = glm::vec2(2.0f, 2.0f);
glm::vec2 position = glm::vec2(50.0f, 0.0f);
glm::vec4 color = glm::vec4(1.0f, 1.0f, 1.0f, 0.5f);
GLfloat rotation = 0.0f;

model = glm::translate(model, glm::vec3(position, 0.0f));

model = glm::translate(model, glm::vec3(0.5f * scale.x, 0.5f * scale.y, 0.0f));
model = glm::rotate(model, rotation, glm::vec3(0.0f, 0.0f, 1.0f));
model = glm::translate(model, glm::vec3(-0.5f * scale.x, -0.5f * scale.y, 0.0f));

model = glm::scale(model, glm::vec3(scale * glm::vec2(width, height), 1.0f));

glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, glm::value_ptr(model));
glUniform4f(glGetUniformLocation(shaderProgram, "spriteColor"), color.x, color.y, color.z, color.w);

最佳答案

参见 CGLM documentation

根据getting started

Aligment is Required: vec4 and mat4 requires 16 byte aligment because vec4 and mat4 operations are vectorized by SIMD instructions (SSE/AVX).

所以你需要像这样初始化单位矩阵:

mat4 model;
glm_mat4_identity(model);
....
mat4 projection;
glm_mat4_identity(projection);
glm_ortho(0.0f, (GLfloat)(WIDTH), (GLfloat)(HEIGHT), 0.0f, -1.0f, 1.0f, projection);

关于c - 如何正确初始化 C 中的模型矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49653978/

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