我正在用Java编写游戏,最近我尝试添加投影矩阵, 这样我就可以沿Z轴移动对象,并且实际上看到对象变小,具有FOV, 等等。
现在的问题是,当我引入此对象时,我的对象只是无法渲染。 我一直在关注一个不错的教程系列,相信我做的一切都正确,并仔细检查了两个小时,尝试了新的解决方案。 然后我放弃了,看了看LWJGL文档。 仍然无法渲染我的对象。
这是我的vertexShader代码。
#version 150 core
in vec3 position;
in vec2 textureCoords;
out vec3 color;
out vec2 pass_textureCoords;
uniform mat4 projectionMatrix;
uniform mat4 transformationMatrix;
void main(void) {
gl_Position = projectionMatrix * transformationMatrix * vec4(position, 1.0);
pass_textureCoords = textureCoords;
color = vec3(position.x+0.5,0.0,position.y+0.5);
}
这是我的fragmentShader代码
#version 150 core
in vec2 pass_textureCoords;
out vec4 out_Color;
uniform sampler2D textureSampler;
void main(void) {
out_Color = texture(textureSampler, pass_textureCoords);
}
这是我的渲染器类。
package me.purplex.jgame.renderer;
import me.purplex.jgame.entity.Entity;
import me.purplex.jgame.model.RawModel;
import me.purplex.jgame.model.TexturedModel;
import me.purplex.jgame.shaders.program.impl.StaticShader;
import me.purplex.jgame.utils.MathUtils;
import org.lwjgl.opengl.*;
import org.lwjgl.util.vector.Matrix4f;
public class Renderer {
private Matrix4f projectionMatrix;
public static final float FOV = 70;
public static final float NEAR_PLANE = 0.1f;
/**
* VIEW DISTANCE
*/
public static final float FAR_PLANE = 1000;
public Renderer(StaticShader shader) {
createProjectionMatrix();
shader.start();
shader.loadProjectionMatrix(projectionMatrix);
shader.stop();
}
public void prepare() {
GL11.glClearColor(1, 0, 0, 1);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
}
public void render(Entity entity, StaticShader shader) {
TexturedModel model = entity.getModel();
RawModel rawModel = model.getRawModel();
GL30.glBindVertexArray(rawModel.getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
Matrix4f transformationMatrix = MathUtils.createTransformationMatrix(entity.getPosition(),
entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale());
shader.loadTransformationMatrix(transformationMatrix);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getModelTexture().getTextureID());
GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
}
private void createProjectionMatrix() {
final float width = Display.getWidth();
final float height = Display.getHeight();
final float fieldOfView = Renderer.FOV;
final float aspectRatio = width / height;
final float nearPlane = Renderer.NEAR_PLANE;
final float farPlane = Renderer.FAR_PLANE;
final float yScale = (float) coTanget(Math.toRadians(fieldOfView / 2f));
final float xScale = yScale / aspectRatio;
final float frustumLength = farPlane - nearPlane;
projectionMatrix = new Matrix4f();
projectionMatrix.m00 =xScale;
projectionMatrix.m11 = yScale;
projectionMatrix.m22 = -((farPlane + nearPlane) / frustumLength);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = -((2 * nearPlane * farPlane) / frustumLength);
projectionMatrix.m33 = 0;
}
private double coTanget(double rads) {
return (1.0 / Math.tan(rads));
}
}
这是我的主循环
DisplayManager.createDisplay();
float[] vertices = {
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f
};
int[] indices = {
0, 1, 3,
3, 1, 2
};
float[] textureCoords = {
0, 0,
0, 1,
1, 1,
1, 0
};
ModelLoader modelLoader = new ModelLoader();
StaticShader shader = new StaticShader();
Renderer renderer = new Renderer(shader);
RawModel model = modelLoader.loadToVAO(vertices, textureCoords, indices);
TexturedModel staticModel = new TexturedModel(model,new ModelTexture(modelLoader.loadTexture("image")));
Entity entity = new Entity(staticModel, new Vector3f(0, 0, -1), 0, 0, 0, 1);
while (!Display.isCloseRequested()) {
renderer.prepare();
shader.start();
renderer.render(entity, shader);
shader.stop();
DisplayManager.updateDisplay();
}
shader.cleanUp();
modelLoader.cleanUp();
DisplayManager.closeDisplay();
为什么?我有点生气。
在我的vertexShader
中,当我移除projectionMatrix
部分时,一切正常,但是我没有投影矩阵!
当我添加它时,我的对象完全没有渲染,我看到的只是我的红色背景。
如果有人可以帮助,那就意味着世界!
谢谢。
我看到的只是我的红色背景,但没有图像。
答案 0 :(得分:0)
透视投影矩阵可以由frustum定义。
距离left
,right
,bottom
和top
是指从视图中心到视锥面在近平面上的距离。 near
和far
指定到截头锥体的近平面和远平面的距离。
r = right, l = left, b = bottom, t = top, n = near, f = far
x: 2*n/(r-l) 0 0 0
y: 0 2*n/(t-b) 0 0
z: (r+l)/(r-l) (t+b)/(t-b) -(f+n)/(f-n) -1
t: 0 0 -2*f*n/(f-n) 0
如果投影是对称的并且视线是视锥的对称轴,则可以简化矩阵:
a = w / h
ta = tan( fov_y / 2 );
2 * n / (r-l) = 1 / (ta * a)
2 * n / (t-b) = 1 / ta
(r+l)/(r-l) = 0
(t+b)/(t-b) = 0
对称透视投影矩阵为:
x: 1/(ta*a) 0 0 0
y: 0 1/ta 0 0
z: 0 0 -(f+n)/(f-n) -1
t: 0 0 -2*f*n/(f-n) 0
因此,您对yScale
的计算是错误的:
yScale = (float) coTanget(Math.toRadians(fieldOfView / 2f));
yScale = 1.0f / (float)coTanget(Math.toRadians(fieldOfView / 2f));
答案 1 :(得分:0)
我想澄清问题已经解决,我忘了调用一个初始化所有统一变量的方法了,我感到有些东西仍然起作用。 谢谢您的帮助!