在C语言中为OpenGL实现Look At矩阵遇渲染问题求助
自定义LookAt矩阵导致3D渲染无画面问题
编辑说明
问题出在未展示的代码部分,我修改了代码,仅在渲染循环前分配一次投影矩阵uniform。感谢@Rabbid76的热心帮助!另外,我不知道如何关闭这个问题...
问题描述
我正在跟随LearnOpenGL教程,用GLFW和C语言重写GLM库的函数。在学习相机部分之前,3D渲染一直正常,现在自定义了LookAt矩阵后屏幕上没有任何图形渲染,希望能得到错误点的指点。
自定义的makeLookAtMatrix函数
void makeLookAtMatrix(struct v3 eye, struct v3 target, struct v3 up, struct mat4* viewMat) { struct mat4 dirMat; struct v3 X, Y, Z; subtractVectors(eye, target, &Z); normalize3d(&Z); Y = up; crossProduct(Y, Z, &X); crossProduct(Z, X, &Y); normalize3d(&X); normalize3d(&Y); float dotX; float dotY; float dotZ; dotProduct(X, eye, &dotX); dotProduct(Y, eye, &dotY); dotProduct(Z, eye, &dotZ); dirMat.m[0][0] = X.x; dirMat.m[0][1] = X.y; dirMat.m[0][2] = X.z; dirMat.m[1][0] = Y.x; dirMat.m[1][1] = Y.y; dirMat.m[1][2] = Y.z; dirMat.m[2][0] = Z.x; dirMat.m[2][1] = Z.y; dirMat.m[2][2] = Z.z; dirMat.m[0][3] = -1.0f * dotX; dirMat.m[1][3] = -1.0f * dotY; dirMat.m[2][3] = -1.0f * dotZ; dirMat.m[3][0] = 0; dirMat.m[3][1] = 0; dirMat.m[3][2] = 0; dirMat.m[3][3] = 1.0f; *viewMat = dirMa }
相关辅助函数
void crossProduct(struct v3 vec1, struct v3 vec2, struct v3* returnVec) { returnVec->x = (vec1.y * vec2.z) - (vec1.z * vec2.y); returnVec->y = (vec1.z * vec2.x) - (vec1.x * vec2.z); returnVec->z = (vec1.x * vec2.y) - (vec1.y * vec2.x); } void dotProduct(struct v3 vec1, struct v3 vec2, float *result) { *result = (vec1.x * vec2.x) + (vec1.y * vec2.y) + (vec1.z * vec2.z); } void normalize3d(struct v3* vec) { float dotProduct = vec->x * vec->x + vec->y * vec->y + vec->z * vec->z; float magnitude = sqrt(dotProduct); if (magnitude != 0.0) { vec->x /= magnitude; vec->y /= magnitude; vec->z /= magnitude; } }
渲染循环中的使用方式
struct v3 yAxis = {0.0f, 1.0f, 0.0f}; struct mat4 view; struct v3 cameraVec = {0.0f, 0.0f, 3.0f}; struct v3 cameraTarget = {0.0f, 0.0f, 0.0f}; makeLookAtMatrix(cameraVec, cameraTarget, yAxis, &view);
此前正常工作的视图矩阵用法
struct mat4 view = makeIdentityMatrix(); struct v3 cameraVec = {0.0f, -2.0f, -10.0f}; translate(cameraVec, &view);
我尝试了几种不同的LookAt矩阵实现方式,这是其中最简单的一种,但找不到代码问题,可能是对相关数学原理理解有误。现在屏幕上没有任何图形被渲染,而之前用上述视图矩阵时一切正常。
内容的提问来源于stack exchange,提问作者Ryan Muncy
相关产品推荐
相关产品推荐

