I follow tutorial http://www.mbsoftworks.sk/tutorials/opengl3/ and try to compile 10th example.
Everything work fine besides place I send (projection matrix mul modelview matrix) to the shader.
There is place where I send matrix:
//...
// render.cpp
glm::mat4 projectionMatrix = *(oglControl->getProjectionMatrix());
glm::mat4 cam = glm::translate(mModelView, cCamera.vEye);
auto newM = projectionMatrix * cam;
spDirectionalLight.setUniform("projectionMatrixMulModelViewMatrix",&newM);
//...
//...
// setUniform implementation
void CShaderProgram::setUniform(string sName, glm::mat4* mMatrices, int iCount)
{
int iLoc = glGetUniformLocation(uiProgram, sName.c_str());
glUniformMatrix4fv(iLoc, iCount, FALSE, (GLfloat*)mMatrices);
}
//...
and at the end mMatrices contain ] .
Shader code
#version 330 core
uniform mat4 projectionMatrixMulModelViewMatrix;
uniform mat4 normalMatrix;
layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec2 inCoord;
layout (location = 2) in vec3 inNormal;
out vec2 texCoord;
smooth out vec3 vNormal;
void main()
{
gl_Position = projectionMatrixMulModelViewMatrix*vec4(inPosition, 1.0);
texCoord = inCoord;
vec4 vRes = normalMatrix*vec4(inNormal, 0.0);
vNormal = vRes.xyz;
}
The result is blank screen. Renderdoc debugger tells me that gl_position matrix completely NaN. Renderdoc screeenshot
When I send glm::mat4(1) I got valid result.
Why after multiplication shader got NaN vector?
好像 VS2017 骗了我。我还没有在代码内部深处初始化矩阵:
glm::mat4 mModelView = cCamera.look();
glm::mat4 CFlyingCamera::look()
{
glm::mat4 result = glm::mat4(1.0f);
result = glm::lookAt(vEye, vView, vUp);
return result;
}
这是我的错
void CFlyingCamera::update()
{
// Change camera view direction
rotateWithMouse();
// Get view direction
glm::vec3 vMove = vView-vEye;
vMove = glm::normalize(vMove);
vMove *= fSpeed;
// Get normal to view direction vector
glm::vec3 vStrafe = glm::cross(vView-vEye, vUp);
vStrafe = glm::normalize(vStrafe);
vStrafe *= fSpeed;
int iMove = 0;
////
////
///// ERROR HERE, vMoveBy isn't initialized
glm::vec3 vMoveBy;
/////
////
////
// Get vector of move
if(Keys::key(iForw))vMoveBy += vMove*appMain.sof(1.0f);
if(Keys::key(iBack))vMoveBy -= vMove*appMain.sof(1.0f);
if(Keys::key(iLeft))vMoveBy -= vStrafe*appMain.sof(1.0f);
if(Keys::key(iRight))vMoveBy += vStrafe*appMain.sof(1.0f);
vEye += vMoveBy; vView += vMoveBy;
}
但是调试器还是给了我有效的矩阵。你知道为什么会这样吗?
你必须初始化mModelView = glm::mat4(1.0f)
实际上,mModelView 之前已经初始化了。这不是原因。glm::mat4 mModelView = cCamera.look();`glm::mat4 CFlyingCamera::look() { return glm::lookAt(vEye, vView, vUp); }
正如我之前提到的 mMatrix 是有效的 ,你可以在这里看到它
@Rabbid76:似乎不是重复的,因为 glm 矩阵没问题。问题可能是上传到着色器。
glUseProgram@DiliDup:你之前打过电话setUniform吗?的价值是多少iLoc?为什么调用不setUniform包含第三个参数