java无法渲染简单顶点数组
我正在从legacy迁移2D游戏->;现代OpenGL。对我来说,这意味着使用着色器和顶点数组而不是glProjection
、glOrtho
和glBegin
/glEnd
我已经将相关代码精简为一个非常基本的示例。相机在(0,0,-1)处,看着(0,0,0),我正试图用点绘制一个三角形:
(0, 0, 0)
X
| '
| '
| '
X-------X
(0, 1, 0) (1, 1, 0)
即使使用一个非常简单的着色器将所有碎片渲染为白色,我也看不到这个三角形,只看到闪烁的背景色
代码
package client.render.opengl;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.joml.Matrix4f;
import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import client.res.gfx.ScreenShader;
public class RenderingTest {
private static final int NUM_VERTICES = 3;
private static long window;
private static int shaderProgram;
private static int vao;
private static int uniformLoc;
private static Matrix4f viewProjMatrix = new Matrix4f();
private static FloatBuffer fb16 = BufferUtils.createFloatBuffer(16);
public static void main(String[] args) throws IOException {
init();
while (!GLFW.glfwWindowShouldClose(window)) {
GLFW.glfwPollEvents();
render();
GLFW.glfwSwapBuffers(window);
}
}
private static void init() throws IOException {
// Setup an error callback
GLFW.glfwSetErrorCallback(
GLFWErrorCallback.createPrint(System.err)
);
// Initialise GLFW
if (!GLFW.glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
// Create window
window = GLFW.glfwCreateWindow(800, 600, "test", 0, 0);
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
GLFW.glfwShowWindow(window);
// Load shader
// Assume this code is good for now, it's too long to post here
ScreenShader.initialise();
shaderProgram = ScreenShader.shader.getProgram();
uniformLoc = ScreenShader.shader.getUniformLoc(ScreenShader.UNIFORM_VIEW_PROJ_MATRIX);
// Define our vertices
ByteBuffer vertexBuffer = ByteBuffer.allocateDirect(NUM_VERTICES * 2 * Float.BYTES);
vertexBuffer.putFloat(0).putFloat(0); // Vertex 1
vertexBuffer.putFloat(0).putFloat(1); // Vertex 2
vertexBuffer.putFloat(1).putFloat(1); // Vertex 3
vertexBuffer.flip();
// Create VAO
vao = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vao);
// Create VBO and fill it with vertex positions
int vboIdPositions = GL15.glGenBuffers();
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboIdPositions);
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW);
GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); // Deselect
// Unbind the VAO
GL30.glBindVertexArray(0);
}
public static void render() {
// Render a random background to prove the loop is working
GL11.glClearColor(
(float) (Math.random() * 0.2f),
(float) (Math.random() * 0.2f),
(float) (Math.random() * 0.2f),
1.0f);
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
// Use shader
GL20.glUseProgram(shaderProgram);
// Get camera position
float cameraX = 0;
float cameraY = 0;
// Set view and projection
viewProjMatrix
.setOrtho(
0,
800,
600,
0,
0.01f,
10
).lookAt(
cameraX, cameraY, -1, // Camera position
cameraX, cameraY, 0, // Camera "look" vector
0, 1, 0 // Camera "up" vector
);
// Pass this matrix to our shader
GL20.glUniformMatrix4fv(uniformLoc, false, viewProjMatrix.get(fb16));
// Bind our vertex array
GL30.glBindVertexArray(vao);
// Enable vertex attributes
GL20.glEnableVertexAttribArray(0);
// Draw the vertices
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, NUM_VERTICES);
// Disable vertex attributes
GL20.glDisableVertexAttribArray(0);
// Unbind vertex array
GL30.glBindVertexArray(0);
// Release shader
GL20.glUseProgram(0);
}
}
顶点着色器
#version 330
uniform mat4 view_proj_matrix;
layout(location = 0) in vec2 in_vertex;
void main(void) {
gl_Position = view_proj_matrix * vec4(in_vertex, 0, 1);
}
片段着色器
#version 330
out vec4 frag_colour;
void main(void) {
frag_colour = vec4(1, 1, 1, 1);
}
我不知道如何进步,因为我甚至不能让这个玩具的例子工作。有人能看到我做错了什么,或者给我举一个使用JOML和LWJGL2以及着色器的工作示例吗
# 1 楼答案
使用视图和投影矩阵,三角形的大小为1像素,位于屏幕左侧
如果三角形具有坐标(0,0,0),(0,1,0),(1,1,0),则窗口的大小为(800600),并设置正交投影链接,如下所示:
三角形在屏幕上的大小为1像素
您必须更改正交投影:
注意,投影矩阵描述了从场景的三维点到视口的二维点的映射。在正交投影时,视图空间中的坐标将线性映射到剪辑空间坐标
如果仅将三角形缩放为(0,0,0),(0,100,0),(100,100,0),也不会看到三角形,因为视图矩阵:
使用该视图矩阵,x轴在Right-handed坐标系中反转。视图坐标系描述了查看场景的方向和位置。视图矩阵从世界空间变换到视图(眼睛)空间。在视图空间中,X轴向左,Y轴向上,Z轴在视图外(注意,在右手系统中,Z轴是X轴和Y轴的叉积)
您的视图定义如下
y轴和z轴为:
x轴是y轴和z轴的叉积:
这意味着,你看三角形的后面。这会导致三角形“翻转”出视口。它在屏幕的左侧绘制,因此您无法看到它:
反转视线以解决问题:
# 2 楼答案
您的着色器需要OpenGL 3.3版。要使用
GLFW
创建此版本的核心配置文件,您可以调用:此外,似乎您按逆时针顺序传递顶点。因为
glCullFace
的默认值是back
,这意味着您想要看到的一面没有被渲染。更改顶点顺序或使用glCullFace
播放