我在 1 到 -1 范围(z 范围)之外渲染的任何内容都不会出现在屏幕上。我一直在尝试一切,包括使用不同的矩阵来尝试将 1 到 -1 之外的顶点转换到这个范围内,但似乎没有任何效果。
我将把我的代码放在下面。它由一个存储数据的模型类、一个着色器程序(我不会包含它 - 它非常简单)和一个主类组成。
顶点着色器
#version 330 core
in vec4 in_Position;
in vec4 in_Color;
out vec4 pass_Color;
void main(void) {
gl_Position = in_Position;
pass_Color = in_Color;
}
片段着色器
#version 330 core
in vec4 pass_Color;
out vec4 out_Color;
void main(void) {
out_Color = pass_Color;
}
模型类
package util;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL20.*;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
public class Model {
// Vertex Array ID
int vaoID;
// VBO ID's
int vboVertexID, vboColorID, vboIndexID;
// Vertex Count
int numVertices, numIndices;
public Model(FloatBuffer vertexData, int vertexCount, FloatBuffer colorData, int colorCount, ByteBuffer indexData, int indexCount) {
// Create the vertex array
vaoID = glGenVertexArrays();
// Select the vertex array
bind();
// Attach vertex data
attachVertexData(vertexData, vertexCount);
// Attach Color data
attachColorData(colorData, colorCount);
// Deselect the vertex array
unbind();
// Indice attachment
attachIndexArray(indexData);
// Set the vertex count
numVertices = vertexCount;
numIndices = indexCount;
}
/**
* Attach some vertex data
*/
public void attachVertexData(FloatBuffer vertexData, int vertexCount) {
// Create the buffer
vboVertexID = glGenBuffers();
// Bind the new buffer
glBindBuffer(GL_ARRAY_BUFFER, vboVertexID);
// Give the data to the GPU
glBufferData(GL_ARRAY_BUFFER, vertexData, GL_STATIC_DRAW);
// Set the location of the data within the vertex array
glVertexAttribPointer(0, vertexCount, GL_FLOAT, false, 0, 0);
// Deselect this buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
/**
* Attach some color data
*/
public void attachColorData(FloatBuffer colorData, int colorCount) {
// Create the buffer
vboColorID = glGenBuffers();
// Bind the new buffer
glBindBuffer(GL_ARRAY_BUFFER, vboColorID);
// Give the data to the GPU
glBufferData(GL_ARRAY_BUFFER, colorData, GL_STATIC_DRAW);
// Set the location of the data within the vertex array
glVertexAttribPointer(1, colorCount, GL_FLOAT, false, 0, 0);
// Deselect this buffer
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
/**
* Attach the index data
*/
public void attachIndexArray(ByteBuffer indexData) {
// Create the buffer
vboIndexID = glGenBuffers();
// Bind it
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexID);
// Put the data in
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexData, GL_STATIC_DRAW);
// Unbind the buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexID);
}
/**
* Enable the buffers
*/
public void enableAttribArrays() {
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
}
/**
* Disable buffers
*/
public void disableAttribArrays() {
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
/**
* Bind the Model
*/
public void bind() {
glBindVertexArray(vaoID);
}
/**
* Unbind the Model
*/
public void unbind() {
glBindVertexArray(0);
}
/**
* Bind the indices
*/
public void bindIndexBuffer() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndexID);
}
/**
* Unbind the indices buffer
*/
public void unbindIndexBuffer() {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
/**
* Draw the vertex array
*/
public void drawArray() {
glDrawElements(GL_TRIANGLE_STRIP, numIndices, GL_UNSIGNED_BYTE, 0);
}
}
主类
package d3;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.PixelFormat;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL30.*;
import static org.lwjgl.opengl.GL20.*;
import util.Game;
import util.Model;
import util.ShaderProgram;
public class Pyramidion {
ShaderProgram shader;
Model model;
public Pyramidion() {
try {
Display.setDisplayMode(new DisplayMode(800, 600));
Display.create(new PixelFormat(), new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true));
Display.setVSyncEnabled(true);
} catch (LWJGLException e) {
e.printStackTrace();
}
init();
while(!Display.isCloseRequested()) {
render();
Display.update();
}
Display.destroy();
}
public void init() {
Display.setTitle("Pyramid");
glViewport(0, 0, Display.getWidth(), Display.getHeight());
// Shader Initialization
setupShader();
// Model Init
setupModel();
}
private void setupModel() {
int verticesCount = 4;
float[] verticesData = {
-0.5f, +0.5f, -10f, 1f, // Top Left
-0.5f, -0.5f, 0f, 1f, // Bottom Left
+0.5f, -0.5f, 0f, 1f, // Bottom Right
+0.5f, +0.5f, 0f, 1f // Top Right
};
FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(verticesData.length);
verticesBuffer.put(verticesData); verticesBuffer.flip();
int colorCount = 4;
float[] colorData = {
0f, 1f, 0f, 1f, // Green
1f, 0f, 0f, 1f, // Red
0f, 0f, 1f, 1f, // Blue
1f, 1f, 1f, 1f // White
};
FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(colorData.length);
colorBuffer.put(colorData); colorBuffer.flip();
int indicesCount = 6;
byte[] indicesData = {
0, 1, 2,
2, 3, 0
};
ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesData.length);
indicesBuffer.put(indicesData); indicesBuffer.flip();
// Create Model
model = new Model(verticesBuffer, verticesCount, colorBuffer, colorCount, indicesBuffer, indicesCount);
}
private void setupShader() {
shader = new ShaderProgram();
shader.attachVertexShader("src/d3/vertex.vert");
shader.attachFragmentShader("src/d3/fragment.frag");
shader.link();
shader.bindAtrribLocation(0, "in_Position");
shader.bindAtrribLocation(1, "in_Color");
}
public void render() {
glClear(GL_COLOR_BUFFER_BIT);
shader.bind();
model.bind();
model.enableAttribArrays();
model.bindIndexBuffer();
model.drawArray();
model.unbindIndexBuffer();
model.disableAttribArrays();
model.unbind();
ShaderProgram.unbind();
}
public static void main(String[] args) {
new Pyramidion();
}
}
编辑:添加了我的矩阵设置
顶点着色器
#version 330 core
uniform mat4 model_Matrix;
uniform mat4 view_Matrix;
uniform mat4 projection_Matrix;
in vec4 in_Position;
in vec4 in_Color;
out vec4 pass_Color;
void main(void) {
gl_Position = projection_Matrix * view_Matrix * model_Matrix * in_Position;
//gl_Position = in_Position;
pass_Color = in_Color;
}
设置矩阵的代码
private void setupMatrices() {
// Model - Identity Matrix
model_Matrix = new Mat4(1.0f);
shader.setUniformMat4("model_Matrix", model_Matrix);
// View - translate it forward
view_Matrix = new Mat4(1f);
shader.setUniformMat4("view_Matrix", view_Matrix);
// Projection - simple perspective
projection_Matrix = Matrices.perspective(60, Display.getWidth() / Display.getHeight(), 0.1f, 100f);
projection_Matrix = Matrices.ortho(-1, 1, 1, -1, 0.1f, 100f);
projection_Matrix = new Mat4(1);
shader.setUniformMat4("projection_Matrix", projection_Matrix);
}
最佳答案
仔细查看应用了矩阵乘法的顶点着色器。特别是这两行:
gl_Position = projection_Matrix * view_Matrix * model_Matrix * in_Position;
gl_Position = in_Position;
所以你看到了吗?好吧,看第二行?它有什么作用?它用未转换的 in_Position 覆盖 gl_Position 变量。摆脱它,你应该会看到矩阵发挥作用。
更新
下一个问题在这里
// Projection - simple perspective
projection_Matrix = Matrices.perspective(60, Display.getWidth() / Display.getHeight(), 0.1f, 100f);
projection_Matrix = Matrices.ortho(-1, 1, 1, -1, 0.1f, 100f);
projection_Matrix = new Mat4(1);
你真的应该满足于一个矩阵并坚持下去。现在您只需用单位矩阵覆盖projecting_Matrix。
在计算透视矩阵时,显示宽度和高度的除法可能是向下舍入的整数除法。您必须先将 getWidth 和 getHeight 的结果转换为 float 。
projection_Matrix =
Matrices.perspective(
60,
(float)Display.getWidth() / (float)Display.getHeight(),
0.1f, 100.f );
但是我怀疑你真的想要那里的显示尺寸。您更有可能希望将视口(viewport)大小用于长宽比计算。另外,近处的 0.1 和远处的 100 都不是最佳值。您应该选择尽可能大的。
关于java - Opengl 渲染 - z 范围 -1 到 1 之外的任何内容都不会出现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23233510/