【发布时间】:2018-07-27 19:18:49
【问题描述】:
我正在从传统 -> 现代 OpenGL 迁移 2D 游戏。对我来说,这意味着使用着色器和顶点数组而不是 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 个像素大。
-
把它放大了 100 倍,仍然没有任何迹象!
标签: java opengl shader lwjgl coordinate-transformation