因此,当我将变换矩阵加载到使用LWJGL 3渲染的四边形上时,该四边形看起来很好,并且可以拉伸和移动。但是,当我在顶点着色器中将变换矩阵乘以投影矩阵(即使用它)时,我得到的只是背景颜色(橙色)。
我尝试了所有操作(我认为),我尝试将GL20.glGetUniformLocation更改为其他内容,教程说我尝试将z值更改为负值和正值,我曾尝试将不同的东西与matrixbuffer一起使用,但无济于事。我此刻真的很沮丧。 (对不起,如果代码看起来不好)
public class Main {
public static Window window = new Window(900, 700, 60, "name", "version");
static float[] verts = {
// Left bottom triangle
-0.5f, 0.5f, 0f,
-0.5f, -0.5f, 0f,
0.5f, -0.5f, 0f,
0.5f, 0.5f, 0f,
};
static byte[] indices = {
// Left bottom triangle
0, 1, 2,
// Right top triangle
2, 3, 0
};
static float[] colors = {
1f, 0f, 0f, 1f,
0f, 1f, 0f, 1f,
0f, 0f, 1f, 1f,
1f, 1f, 1f, 1f,
};
static float[] uv = {
0,0,
0,1,
1,1,
1,0,
};
public static void main(String[] args) {
window.create();
Vector3f pos = new Vector3f(0, 0, 0);
Vector3f rot = new Vector3f(0, 0, 0);
Vector3f scale = new Vector3f(1, 1, 1);
Model model = new Model(verts, indices, uv);
Material mat = new Material(model.loadTexture("cat"));
MaterialModel blob = new MaterialModel(model, mat);
PosedModel mod = new PosedModel(blob, pos, scale, rot);
Shader shader = new Shader();
Renderer renderer = new Renderer(shader);
model.create();
shader.create();
while (!window.isclosed()) {
if (window.isupdating()) {
window.update();
renderer.render(mod, shader);
window.swapbuffers();
}
}
model.cleanup();
shader.cleanup();
window.stop();
}
}
public class Shader {
private int verid, fragid, progid, transmatloc, progmatloc;
private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
public void create() {
verid = loadShader("src/shading/vert.glsl", GL20.GL_VERTEX_SHADER);
fragid = loadShader("src/shading/frag.glsl", GL20.GL_FRAGMENT_SHADER);
progid = GL20.glCreateProgram();
GL20.glAttachShader(progid, verid);
GL20.glAttachShader(progid, fragid);
GL20.glLinkProgram(progid);
GL20.glValidateProgram(progid);
transmatloc = GL20.glGetUniformLocation(progid, "transmatrix");
progmatloc = GL20.glGetUniformLocation(progid, "progmatrix");
}
public void activate() {
GL20.glUseProgram(progid);
}
public void deactivate() {
GL20.glUseProgram(0);
}
public void cleanup() {
deactivate();
GL20.glDetachShader(progid, verid);
GL20.glDetachShader(progid, fragid);
GL20.glDeleteShader(verid);
GL20.glDeleteShader(fragid);
GL20.glDeleteProgram(progid);
}
public void loadTransformationMatrix(Matrix4f matrix) {
matrix.store(matrixBuffer);
matrixBuffer.flip();
GL20.glUniformMatrix4fv(transmatloc, false, matrixBuffer);
}
public void loadProjectionMatrix(Matrix4f projection) {
projection.store(matrixBuffer);
matrixBuffer.flip();
GL20.glUniformMatrix4fv(progmatloc, false, matrixBuffer);
}
public int loadShader(String filename, int type) {
StringBuilder shaderSource = new StringBuilder();
int shaderID = 0;
try {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
shaderSource.append(line).append("\n");
}
reader.close();
} catch (IOException e) {
System.err.println("Could not read file.");
e.printStackTrace();
System.exit(-1);
}
shaderID = GL20.glCreateShader(type);
GL20.glShaderSource(shaderID, shaderSource);
GL20.glCompileShader(shaderID);
return shaderID;
}
}
public class Renderer {
public Renderer(Shader shader) {
createProjectionMatrix();
shader.loadProjectionMatrix(projectionMatrix);
}
private static final float FOV = 70;
private static final float NEAR_PLANE = 0.1f;
private static final float FAR_PLANE = 1000f;
private Matrix4f projectionMatrix;
public void render(PosedModel nodel, Shader shader) {
MaterialModel model = nodel.getModel();
Model mod = model.getModel();
shader.activate();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
GL30.glBindVertexArray(mod.get_vaoid());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
Matrix4f tansmat = Matrixstuff.createTransformationMatrix(nodel.getPos(), nodel.getRot(), nodel.getScale());
shader.loadTransformationMatrix(tansmat);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, mod.get_vboiid());
// Draw the vertices
GL11.glDrawElements(GL11.GL_TRIANGLES, mod.get_indscount(), GL11.GL_UNSIGNED_BYTE, 0);
// Put everything back to default (deselect)
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL30.glBindVertexArray(0);
shader.deactivate();
}
private void createProjectionMatrix(){
float width = 900;
float height = 700;
float aspectRatio = (float)width / (float)height;
float yScale = (float) ((1f / Math.tan(Math.toRadians(FOV / 2f))) * aspectRatio);
float xScale = yScale / aspectRatio;
float frustumLength = FAR_PLANE - NEAR_PLANE;
projectionMatrix = new Matrix4f();
projectionMatrix.m00 = xScale;
projectionMatrix.m11 = yScale;
projectionMatrix.m22 = -((FAR_PLANE + NEAR_PLANE) / frustumLength);
projectionMatrix.m23 = -1;
projectionMatrix.m32 = -((2 * NEAR_PLANE * FAR_PLANE) / frustumLength);
projectionMatrix.m33 = 0;
}
}
public class Model {
public Model(float[] verts, byte[] inds, float[] texcords) {
this.verts = verts;
vcount = verts.length/3;
this.inds = inds;
this.texcords = texcords;
}
private int vaoid, vboid, vboiid, vcount, indscount, texid;
private float[] verts, texcords;
private byte[] inds;
public void create() {
FloatBuffer buffer = BufferUtils.createFloatBuffer(verts.length);
buffer.put(verts);
buffer.flip();
indscount = inds.length;
ByteBuffer indbuf = BufferUtils.createByteBuffer(indscount);
indbuf.put(inds);
indbuf.flip();
vaoid = GL30.glGenVertexArrays();
GL30.glBindVertexArray(vaoid);
texid = GL15.glGenBuffers();
FloatBuffer texbuf = BufferUtils.createFloatBuffer(texcords.length);
texbuf.put(texcords);
texbuf.flip();
vbd(texid, texbuf);
GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
vboid = GL15.glGenBuffers();
vbd(vboid, buffer);
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL30.glBindVertexArray(0);
vboiid = GL15.glGenBuffers();
vbd(vboiid, indbuf);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
}
public int loadTexture(String fileName) {
int textureID = 0;
try {
textureID = TextureLoader.getTexture("PNG", new FileInputStream("res/"+fileName+".png")).getTextureID();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return textureID;
}
public void cleanup() {
GL20.glDisableVertexAttribArray(0);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glDeleteBuffers(vboid);
GL30.glBindVertexArray(0);
GL30.glDeleteVertexArrays(vaoid);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
GL15.glDeleteBuffers(vboiid);
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
GL15.glDeleteBuffers(vboiid);
}
public static void vbd(int id, FloatBuffer buffer) { //Not restricted to FloatBuffer
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, id); //Bind buffer (also specifies type of buffer)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); //Send up the data and specify usage hint.
}
public static void vbd(int id, ByteBuffer buffer) { //Not restricted to FloatBuffer
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, id); //Bind buffer (also specifies type of buffer)
GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, buffer, GL15.GL_STATIC_DRAW); //Send up the data and specify usage hint.
}
public int get_vaoid() {
return vaoid;
}
public int get_vcount() {
return vcount;
}
public int get_vboiid() {
return vboiid;
}
public int get_indscount() {
return indscount;
}
}
#version 400 core
in vec3 position;
in vec2 textcoords;
out vec2 texcoords;
uniform mat4 transmatrix;
uniform mat4 progmatrix;
void main(void) {
gl_Position = transmatrix * progmatrix * vec4(position,1.0);
texcoords = textcoords;
}
#version 400 core
in vec2 texcoords;
out vec4 out_Color;
uniform sampler2D text;
void main(void) {
out_Color = texture(text, texcoords);
}