自从我上次与VBO合作以来已经有一段时间了,现在我正试图让它再次运作。但是,无论我做什么,除了使用glClearColor,我都无法渲染任何东西。
这是我的代码:
初始化GL
...
vmml::vec3f eye = vmml::vec3f(0.0, 0.0, 0.0);
vmml::vec3f tar = vmml::vec3f(0.0, 0.0, 1.0);
vmml::vec3f up = vmml::vec3f(0.0, 1.0, 0.0);
viewMatrix = lookAt(eye, tar, up);
创建方框
glGenBuffers(1, &vertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 72 * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
glGenBuffers(1, &uvBuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glBufferData(GL_ARRAY_BUFFER, 48 * sizeof(GLfloat), &uvs[0], GL_STATIC_DRAW);
glGenBuffers(1, &normalBuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glBufferData(GL_ARRAY_BUFFER, 72 * sizeof(GLfloat), &normals[0], GL_STATIC_DRAW);
[code for adding material and texture to box...]
PaintGL
[code for shadows...]
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
shadeManager.bindShader(SHADOWSHADER);
vmml::vec4f b1(0.5, 0.0, 0.0, 0.5);
vmml::vec4f b2(0.0, 0.5, 0.0, 0.5);
vmml::vec4f b3(0.0, 0.0, 0.5, 0.5);
vmml::vec4f b4(0.0, 0.0, 0.0, 1.0);
vmml::mat4f bias = vmml::mat4f::ZERO;
bias.set_column(0, b1);
bias.set_column(1, b2);
bias.set_column(2, b3);
bias.set_column(3, b4);
vmml::mat4f depthBiasVP = bias * depthVP;
GLuint depthBiasID = glGetUniformLocation(shadowShaderID, "depthBiasVP");
GLuint lightDirID = glGetUniformLocation(shadowShaderID, "lightInvDir");
GLuint shadowMapID = glGetUniformLocation(shadowShaderID, "shadowMap");
GLuint viewID = glGetUniformLocation(shadowShaderID, "V");
GLuint projectionID = glGetUniformLocation(shadowShaderID, "P");
glUniformMatrix4fv(depthBiasID, 1, GL_FALSE, &depthBiasVP[0][0]);
glUniform3fv(lightDirID, 1, &lightPos[0]);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, depthTextureID);
glUniform1i(shadowMapID, 1);
[calculate eye, target and up...]
viewMatrix = lookAt(eye, target, up);
glUniformMatrix4fv(viewID, 1, GL_FALSE, &viewMatrix[0][0]);
glUniformMatrix4fv(projectionID, 1, GL_FALSE, &projectionMatrix[0][0]);
currentFrustum->setActive(true);
currentFrustum->extractFrustum(projectionMatrix, viewMatrix);
scenegraph.render(false);
渲染框
GLuint id = ShaderManager::getInstance().getShader(SHADOWSHADER);
if (depthPass)
id = ShaderManager::getInstance().getShader(DEPTHSHADER);
GLuint mID = glGetUniformLocation(id, "M");
GLuint texID = glGetUniformLocation(id, "tex");
GLuint diffID = glGetUniformLocation(id, "diffMaterial");
GLuint ambiID = glGetUniformLocation(id, "ambiMaterial");
GLuint specID = glGetUniformLocation(id, "specMaterial");
GLuint shinID = glGetUniformLocation(id, "shininess");
glUniformMatrix4fv(mID, 1, GL_FALSE, &mod[0][0]);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1i(texID, 0);
glUniform3fv(diffID, 1, &values.diffuseMaterial[0]);
glUniform3fv(ambiID, 1, &values.ambientMaterial[0]);
glUniform3fv(specID, 1, &values.specularMaterial[0]);
glUniform1f(shinID, values.shinyMaterial[0]);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glVertexAttribPointer(
0,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, uvBuffer);
glVertexAttribPointer(
1,
2,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, normalBuffer);
glVertexAttribPointer(
2,
3,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
glEnableVertexAttribArray(2);
glDrawArrays(GL_QUADS, 0, 6 * 4 );
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(2);
着色器..
#version 420 core
layout(location = 0) in vec3 vertexPos;
layout(location = 1) in vec2 vertexUV;
layout(location = 2) in vec3 vertexNorm;
out vec2 UV;
out vec3 position;
out vec3 normal;
out vec3 viewDirection;
out vec3 lightDirection;
out vec4 shadow;
uniform mat4 M;
uniform mat4 V;
uniform mat4 P;
uniform vec3 lightInvDir;
uniform mat4 depthBiasVP;
void main() {
gl_Position = P * V * M * vec4(vertexPos, 1);
UV = vertexUV;
position = (M * vec4(vertexPos, 1)).xyz;
normal = (V * M * vec4(vertexNorm, 0)).xyz;
viewDirection = vec3(0,0,0) - (V * M * vec4(vertexPos, 1)).xyz;
lightDirection = (V * vec4(lightInvDir, 0)).xyz;
shadow = depthBiasVP * M * vec4(vertexPos, 1);
}
#version 420 core
in vec2 UV;
in vec3 position;
in vec3 normal;
in vec3 viewDirection;
in vec3 lightDirection;
in vec4 shadow;
layout(location = 0) out vec3 color;
uniform sampler2D tex;
uniform sampler2D shadowMap;
uniform vec3 diffLight;
uniform vec3 ambiLight;
uniform vec3 specLight;
uniform vec3 diffMaterial;
uniform vec3 ambiMaterial;
uniform vec3 specMaterial;
uniform float shininess;
vec2 disk[16] = vec2[] (
vec2( -0.94201624, -0.39906216 ),
vec2( 0.94558609, -0.76890725 ),
vec2( -0.094184101, -0.92938870 ),
vec2( 0.34495938, 0.29387760 ),
vec2( -0.91588581, 0.45771432 ),
vec2( -0.81544232, -0.87912464 ),
vec2( -0.38277543, 0.27676845 ),
vec2( 0.97484398, 0.75648379 ),
vec2( 0.44323325, -0.97511554 ),
vec2( 0.53742981, -0.47373420 ),
vec2( -0.26496911, -0.41893023 ),
vec2( 0.79197514, 0.19090188 ),
vec2( -0.24188840, 0.99706507 ),
vec2( -0.81409955, 0.91437590 ),
vec2( 0.19984126, 0.78641367 ),
vec2( 0.14383161, -0.14100790 )
);
float random(vec3 seed, int i) {
vec4 s = vec4(seed, i);
float dotProduct = dot(s, vec4(12.9898, 78.233, 45.164, 94.673));
return fract(sin(dotProduct) * 43758.5453);
}
void main() {
vec3 materialDiffuseColor = diffMaterial * texture2D( tex, UV ).rgb;
vec3 materialAmbientColor = ambiMaterial * materialDiffuseColor;
vec3 materialSpecularColor = specMaterial;
vec3 l = normalize(lightDirection);
vec3 n = normalize(normal);
vec3 v = normalize(viewDirection);
vec3 ref = reflect(-l, n);
float diff = clamp(dot(n, l), 0,1);
float spec = clamp(dot(l, ref), 0,1);
float visibility = 1.0;
float bias = 0.005*tan(acos(diff));
bias = clamp(bias, 0.0, 0.01);
vec4 shadowCoordDivide = shadow / shadow.w;
if (texture2D( shadowMap, shadowCoordDivide.xy ).z < shadowCoordDivide.z-bias) {
//visibility = 0.5;
diff = 0;
spec = 0;
}
color = 2 * ambiLight * materialAmbientColor;
if (diff != 0) {
color += visibility * diffLight * materialDiffuseColor * diff;
float iSpec = pow(spec, shininess);
color += visibility * specLight * materialSpecularColor * iSpec;
}
}
我确保着色器正确加载,并且模型/视图/投影矩阵是正确的,仍然没有。谁可以指出我正确的方向?
Edit1:添加了代码部分我忘了并删除了一些不重要的代码.. Edit2:将代码删除到可能的错误区域
答案 0 :(得分:0)
仅仅从查看渲染代码开始,看起来就像是将View和Perspective矩阵传递给顶点着色器。如果您引用尚未传入的制服,会发生什么情况取决于您的GPU驱动程序有多好。有些会崩溃,有些人会愉快地提供一些默认的回退矩阵,只是为了让着色器运行。可能是您的着色器看到了此默认矩阵,并且没有正确转换您的框。
如果您在代码中的其他位置正确传递矩阵,请忽略此答案。它只是基于您粘贴的代码。