我正在尝试在屏幕上放置一个立方体并点亮它。我想要立方体上的 phong 阴影。
当我运行我的代码时,我可以看到背景图像,但看不到立方体。
我相当确定立方体本身是正确的,因为我已经设法用纯色着色器显示它。
我已经设法编译着色器程序,但我根本看不到立方体。我不知道 GLES/LibGdx 是否有运行时异常机制,但我在日志中看不到任何内容。
我假设
- 模型不在视野范围内
- 模型被渲染为透明
- 两个都
我试图将我的代码削减到我认为问题所在的位置。如果您需要查看其他内容,请询问。
设置
shader = new ShaderProgram(
Gdx.files.internal("shaders/phongVertexShader.glsl"),
Gdx.files.internal("shaders/phongFragmentShader.glsl"));
if (!shader.isCompiled()) {
throw new IllegalStateException(shader.getLog());
}
mesh = Shapes.genCube();
mesh.getVertexAttribute(Usage.Position).alias = "a_position";
mesh.getVertexAttribute(Usage.Normal).alias = "a_normal";
使成为
public void onRender() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
batch.setProjectionMatrix(camera.combined);
angle += Gdx.graphics.getDeltaTime() * 40.0f;
float aspect = Gdx.graphics.getWidth()
/ (float) Gdx.graphics.getHeight();
projection.setToProjection(1.0f, 20.0f, 60.0f, aspect);
view.idt().trn(0, 0, -2.0f);
model.setToRotation(axis, angle);
combined.set(projection).mul(view).mul(model);
Gdx.gl20.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
shader.begin();
float[] light = {10, 10, 10};
shader.setUniformMatrix("mvpMatrix", combined);
shader.setUniformMatrix("mvMatrix", new Matrix4().translate(0, 0, -10));
shader.setUniform3fv("vLightPosition", light, 0, 3);
mesh.render(shader, GL20.GL_TRIANGLES);
shader.end();
}
顶点着色器
#version 330
in vec4 vVertex;
in vec3 vNormal;
uniform mat4 mvpMatrix; // mvp = ModelViewProjection
uniform mat4 mvMatrix; // mv = ModelView
uniform mat3 normalMatrix;
uniform vec3 vLightPosition;
smooth out vec3 vVaryingNormal;
smooth out vec3 vVaryingLightDir;
void main(void) {
vVaryingNormal = normalMatrix * vNormal;
vec4 vPosition4 = mvMatrix * vVertex;
vec3 vPosition3 = vPosition4.xyz / vPosition4.w;
vVaryingLightDir = normalize(vLightPosition - vPosition3);
gl_Position = mvpMatrix * vVertex;
}
片段着色器
#version 330
out vec4 vFragColor;
uniform vec4 ambientColor;
uniform vec4 diffuseColor;
uniform vec4 specularColor;
smooth in vec3 vVaryingNormal;
smooth in vec3 vVaryingLightDir;
void main(void) {
float diff = max(0.0, dot(normalize(vVaryingNormal), normalize(vVaryingLightDir)));
vFragColor = diff * diffuseColor;
vFragColor += ambientColor;
vec3 vReflection = normalize(reflect(-normalize(vVaryingLightDir),normalize(vVaryingNormal)));
float spec = max(0.0, dot(normalize(vVaryingNormal), vReflection));
if(diff != 0) {
float fSpec = pow(spec, 32.0);
vFragColor.rgb += vec3(fSpec, fSpec, fSpec);
}
}
有人可以指出我正确的方向吗?