Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Pull Request: The pre-multiplied projViewMatrix in SpriteBatch. #2

Merged
merged 1 commit into from

2 participants

@matheus23

I tried to improve the version of my last Pull Request, as you suggested:

I removed the "updateView()" method in SpriteBatch, and changed the "updateProjection()" method into "updateMatrices()".

I also added the utility method "storeUniformMat4(String uniform, Matrix4f mat, boolean transposed)" to ShaderProgram. This method stores a Matrix4f in the given uniform, either transposed or left as-is. This method also throws a "IllegalStateException" if "getUniformLocation()" returns -1.

Finally I changed the VertexArrayExample and everything worked perfectly.

Hope you take this one as an improvement, and I hope I was helpful :)

@matheus23 matheus23 commented on the diff
.classpath
@@ -6,7 +6,7 @@
<classpathentry kind="lib" path="lib/lwjgl_util.jar"/>
<classpathentry kind="lib" path="lib/lwjgl.jar">
<attributes>
- <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="lwjgl-basics/native/macosx"/>
+ <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="lwjgl-basics/native/linux"/>

I am sorry about this... I accedently added the .classpath file to the commit files... :(
I don't know how to change that back...
Propably you could re-patch that, or just leave it as is. Since you normally need to change the .classpath anyways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@mattdesl mattdesl merged commit fecadd2 into from
@matheus23 matheus23 Premultiplied the transposed projection matrix with the view matrix on
the cpu, which gets uploaded as one matrix to the shader.

Added a utility method in the ShaderProgram which enables simple pushing
of matrices to the shader and makes use of a FloatBuffer Pool.
49220d3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 10, 2012
  1. @matheus23

    Premultiplied the transposed projection matrix with the view matrix on

    matheus23 authored
    the cpu, which gets uploaded as one matrix to the shader.
    
    Added a utility method in the ShaderProgram which enables simple pushing
    of matrices to the shader and makes use of a FloatBuffer Pool.
This page is out of date. Refresh to see the latest.
View
2  .classpath
@@ -6,7 +6,7 @@
<classpathentry kind="lib" path="lib/lwjgl_util.jar"/>
<classpathentry kind="lib" path="lib/lwjgl.jar">
<attributes>
- <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="lwjgl-basics/native/macosx"/>
+ <attribute name="org.eclipse.jdt.launching.CLASSPATH_ATTR_LIBRARY_PATH_ENTRY" value="lwjgl-basics/native/linux"/>

I am sorry about this... I accedently added the .classpath file to the commit files... :(
I don't know how to change that back...
Propably you could re-patch that, or just leave it as is. Since you normally need to change the .classpath anyways.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
View
77 src/mdesl/graphics/SpriteBatch.java
@@ -32,7 +32,6 @@
import static org.lwjgl.opengl.GL11.GL_TRIANGLES;
import static org.lwjgl.opengl.GL20.glUniform1i;
-import static org.lwjgl.opengl.GL20.glUniformMatrix4;
import java.nio.FloatBuffer;
import java.util.Arrays;
@@ -44,7 +43,6 @@
import mdesl.graphics.glutils.VertexData;
import mdesl.util.MathUtil;
-import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.Display;
import org.lwjgl.util.vector.Matrix4f;
@@ -55,16 +53,14 @@
*/
public class SpriteBatch {
public static final String TEXCOORD_0 = "tex0";
- public static final String PROJECTION_MATRIX = "projMatrix";
- public static final String VIEW_MATRIX = "viewMatrix";
+ public static final String PROJ_VIEW_MATRIX = "projViewMatrix";
public static final String ATTR_COLOR = "Color";
public static final String ATTR_POSITION = "Position";
public static final String ATTR_TEXCOORD = "TexCoord";
public static final String DEFAULT_VERT_SHADER =
- "uniform mat4 "+PROJECTION_MATRIX+";\n" +
- "uniform mat4 "+VIEW_MATRIX+";\n" +
+ "uniform mat4 "+PROJ_VIEW_MATRIX+";\n" +
"attribute vec4 "+ATTR_COLOR+";\n" +
"attribute vec2 "+ATTR_TEXCOORD+";\n" +
"attribute vec2 "+ATTR_POSITION+";\n" +
@@ -73,7 +69,7 @@
"void main() {\n" +
" vColor = "+ATTR_COLOR+";\n" +
" vTexCoord = "+ATTR_TEXCOORD+";\n" +
- " gl_Position = "+PROJECTION_MATRIX+" * "+VIEW_MATRIX+" * vec4("+ATTR_POSITION+".xy, 0, 1);\n" +
+ " gl_Position = "+PROJ_VIEW_MATRIX+" * vec4("+ATTR_POSITION+".xy, 0, 1);\n" +
"}";
public static final String DEFAULT_FRAG_SHADER =
@@ -96,6 +92,8 @@
protected FloatBuffer buf16;
protected Matrix4f projMatrix;
protected Matrix4f viewMatrix;
+ protected Matrix4f projViewMatrix;
+ protected Matrix4f transpositionPool;
protected Texture texture;
protected ShaderProgram program;
@@ -123,9 +121,7 @@ public SpriteBatch(ShaderProgram program, int size) {
//max indices before we need to flush the renderer
maxIndex = size * 6;
- updateProjection();
-
- updateView();
+ updateMatrices();
}
public SpriteBatch(int size) {
@@ -148,24 +144,26 @@ public void setColor(float r, float g, float b, float a) {
}
/**
- * Call to update the projection matrix with the screen and send the uniform to the current shader.
+ * Call to multiply the the projection with the view matrix and save
+ * the result in the uniform mat4 {@value #PROJ_VIEW_MATRIX}.
*/
- public void updateProjection() {
+ public void updateMatrices() {
+ // Create projection matrix:
projMatrix = MathUtil.toOrtho2D(projMatrix, 0, 0, Display.getWidth(), Display.getHeight());
+ // Create view Matrix, if not present:
+ if (viewMatrix == null) {
+ viewMatrix = new Matrix4f();
+ }
+ // Multiply the transposed projection matrix with the view matrix:
+ projViewMatrix = Matrix4f.mul(
+ Matrix4f.transpose(projMatrix, transpositionPool),
+ viewMatrix,
+ projViewMatrix);
program.begin();
- //upload projection matrix
- int proj = program.getUniformLocation(PROJECTION_MATRIX);
- if (proj!=-1) { //if the uniform is active
- //ShaderProgram should probably have this as a convenience method
- if (buf16==null)
- buf16 = BufferUtils.createFloatBuffer(16);
- buf16.clear();
- projMatrix.store(buf16);
- buf16.flip();
- glUniformMatrix4(proj, true, buf16);
- }
+ // Store the the multiplied matrix in the "projViewMatrix"-uniform:
+ program.storeUniformMat4(PROJ_VIEW_MATRIX, projViewMatrix, false);
//upload texcoord 0
int tex0 = program.getUniformLocation(TEXCOORD_0);
@@ -175,39 +173,6 @@ public void updateProjection() {
program.end();
}
- /**
- * Used to upload the View matrix to VRAM, to be useable in the shaders.
- */
- public void updateView() {
- // In case we haven't got a matrix yet:
- if (viewMatrix == null) {
- viewMatrix = new Matrix4f();
- }
- // So we can update it's uniforms:
- program.begin();
- // Get the uniform:
- int view = program.getUniformLocation(VIEW_MATRIX);
- if (view != -1) {
- // Store the matrix in a buffer:
- if (buf16 == null) {
- buf16 = BufferUtils.createFloatBuffer(16);
- }
- buf16.clear();
- viewMatrix.storeTranspose(buf16);
- buf16.flip();
- // Upload the matrix:
- glUniformMatrix4(view, true, buf16);
- }
- // (I have only added this since it is in updateProjection() too...)
- int tex0 = program.getUniformLocation(TEXCOORD_0);
- if (tex0 != -1) {
- glUniform1i(tex0, 0);
- }
-
- program.end();
-
- }
-
public void begin() {
if (drawing) throw new IllegalStateException("must not be drawing before calling begin()");
drawing = true;
View
67 src/mdesl/graphics/glutils/ShaderProgram.java
@@ -2,15 +2,15 @@
* Copyright (c) 2012, Matt DesLauriers All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
+ * modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
+ * list of conditions and the following disclaimer.
*
* * Redistributions in binary
* form must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided
- * with the distribution.
+ * with the distribution.
*
* * Neither the name of the Matt DesLauriers nor the names
* of his contributors may be used to endorse or promote products derived from
@@ -51,22 +51,29 @@
import static org.lwjgl.opengl.GL20.glGetUniformLocation;
import static org.lwjgl.opengl.GL20.glLinkProgram;
import static org.lwjgl.opengl.GL20.glShaderSource;
+import static org.lwjgl.opengl.GL20.glUniformMatrix4;
import static org.lwjgl.opengl.GL20.glUseProgram;
+import java.nio.FloatBuffer;
import java.util.List;
+import org.lwjgl.BufferUtils;
+import org.lwjgl.util.vector.Matrix4f;
+
/**
- * A bare-bones ShaderProgram utility based on ra4king's ArcSynthesis Java ports.
- *
- * @author ra4king, modifications by davedes
+ * A bare-bones ShaderProgram utility based on ra4king's ArcSynthesis Java ports.
+ *
+ * @author ra4king, modifications by davedes and matheusdev
*/
public class ShaderProgram {
+ private static final FloatBuffer buf16Pool = BufferUtils.createFloatBuffer(16);
+
public final int program;
public final int vertex;
public final int fragment;
protected String log;
-
+
public ShaderProgram(String vertexSource, String fragmentSource) {
this(vertexSource, fragmentSource, null);
}
@@ -77,43 +84,43 @@ public ShaderProgram(String vertexShader, String fragmentShader, List<VertexAttr
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
-
+
if (attributes != null)
for (VertexAttrib a : attributes)
glBindAttribLocation(program, a.location, a.name);
-
+
glLinkProgram(program);
-
+
String infoLog = glGetProgramInfoLog(program, glGetProgram(program, GL_INFO_LOG_LENGTH));
-
+
if (glGetProgram(program, GL_LINK_STATUS) == GL_FALSE)
throw new RuntimeException(
"Failure in linking program. Error log:\n" + infoLog);
-
+
if (infoLog!=null && infoLog.trim().length()!=0)
log += infoLog;
-
+
glDetachShader(program, vertex);
glDetachShader(program, fragment);
glDeleteShader(vertex);
glDeleteShader(fragment);
}
-
+
private int compileShader(String source, int type) {
int shader = glCreateShader(type);
glShaderSource(shader, source);
glCompileShader(shader);
-
+
String infoLog = glGetShaderInfoLog(shader,
glGetShader(shader, GL_INFO_LOG_LENGTH));
if (glGetShader(shader, GL_COMPILE_STATUS) == GL_FALSE)
throw new RuntimeException("Failure in compiling " + getName(type)
+ ". Error log:\n" + infoLog);
-
+
if (infoLog!=null && infoLog.trim().length()!=0)
log += getName(type) +": "+infoLog + "\n";
-
+
return shader;
}
@@ -122,7 +129,7 @@ private String getName(int shaderType) {
return "GL_VERTEX_SHADER";
if (shaderType == GL_FRAGMENT_SHADER)
return "GL_FRAGMENT_SHADER";
- else
+ else
return "shader";
}
@@ -133,12 +140,30 @@ public void begin() {
public void end() {
glUseProgram(0);
}
-
- public void destroy() {
+
+ public void destroy() {
glDeleteProgram(program);
}
-
+
public int getUniformLocation(String str) {
return glGetUniformLocation(program, str);
}
+
+ public FloatBuffer getBuf16Pool() {
+ buf16Pool.clear();
+ return buf16Pool;
+ }
+
+ public void storeUniformMat4(String uniform, Matrix4f mat, boolean transposed) {
+ FloatBuffer buffer = getBuf16Pool();
+ mat.store(buffer);
+ buffer.flip();
+
+ int uniformId = getUniformLocation(uniform);
+ if (uniformId != -1) {
+ glUniformMatrix4(uniformId, transposed, buf16Pool);
+ } else {
+ throw new IllegalStateException("glGetUniformLocation for the uniform \"" + uniform + "\" returned -1.");
+ }
+ }
}
View
4 src/mdesl/test/VertexArrayExample.java
@@ -92,7 +92,7 @@ static URL getResource(String ref) {
protected void resize() {
glViewport(0, 0, Display.getWidth(), Display.getHeight());
- batch.updateProjection();
+ batch.updateMatrices();
}
protected void create() {
@@ -135,7 +135,7 @@ protected void render() {
translate.set(-rotationPointX, -rotationPointY, 0);
batch.getViewMatrix().translate(translate);
// Upload the view matrix to the GPU:
- batch.updateView();
+ batch.updateMatrices();
// Begin rendering:
batch.begin();
Something went wrong with that request. Please try again.