Skip to content

Commit

Permalink
adding more lessons
Browse files Browse the repository at this point in the history
  • Loading branch information
mattdesl committed Dec 28, 2012
1 parent 3feb67b commit c04719b
Show file tree
Hide file tree
Showing 10 changed files with 522 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
*
* @author davedes
*/
public class Framebuffer implements ITexture {
public class FrameBuffer implements ITexture {

public static boolean isSupported() {
return GLContext.getCapabilities().GL_EXT_framebuffer_object;
Expand All @@ -63,7 +63,7 @@ public static boolean isSupported() {
protected Texture texture;
protected boolean ownsTexture;

Framebuffer(Texture texture, boolean ownsTexture) throws LWJGLException {
FrameBuffer(Texture texture, boolean ownsTexture) throws LWJGLException {
this.texture = texture;
this.ownsTexture = ownsTexture;
if (!isSupported()) {
Expand Down Expand Up @@ -91,7 +91,7 @@ public static boolean isSupported() {
* @param texture the texture to use
* @throws LWJGLException if the framebuffer was not initialized correctly
*/
public Framebuffer(Texture texture) throws LWJGLException {
public FrameBuffer(Texture texture) throws LWJGLException {
this(texture, false);
}

Expand All @@ -103,15 +103,15 @@ public Framebuffer(Texture texture) throws LWJGLException {
* @param wrap
* @throws LWJGLException
*/
public Framebuffer(int width, int height, int filter, int wrap) throws LWJGLException {
public FrameBuffer(int width, int height, int filter, int wrap) throws LWJGLException {
this(new Texture(width, height, filter, wrap), true);
}

public Framebuffer(int width, int height, int filter) throws LWJGLException {
public FrameBuffer(int width, int height, int filter) throws LWJGLException {
this(width, height, filter, Texture.DEFAULT_WRAP);
}

public Framebuffer(int width, int height) throws LWJGLException {
public FrameBuffer(int width, int height) throws LWJGLException {
this(width, height, Texture.DEFAULT_FILTER, Texture.DEFAULT_WRAP);
}

Expand Down
43 changes: 25 additions & 18 deletions src/mdesl/graphics/SpriteBatch.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ public class SpriteBatch {
public static int renderCalls = 0;

protected FloatBuffer buf16;
protected Matrix4f projMatrix;
protected Matrix4f viewMatrix;
protected Matrix4f projViewMatrix;
protected Matrix4f transpositionPool;

protected Matrix4f projMatrix = new Matrix4f();
protected Matrix4f viewMatrix = new Matrix4f();
protected Matrix4f transpositionPool = new Matrix4f();
private Matrix4f projViewMatrix = new Matrix4f(); //only for re-using Matrix4f objects
protected Texture texture;
protected ShaderProgram program;

Expand All @@ -100,8 +100,12 @@ public static ShaderProgram getDefaultShader() throws LWJGLException {
public SpriteBatch(ShaderProgram program) {
this(program, 1000);
}

public SpriteBatch(ShaderProgram program, int size) {
this(program, 1000, true);
}

public SpriteBatch(ShaderProgram program, int size) {
public SpriteBatch(ShaderProgram program, int size, boolean updateUniforms) {
this.program = program;

// later we can do some abstraction to replace this with VBOs...
Expand All @@ -110,8 +114,6 @@ public SpriteBatch(ShaderProgram program, int size) {
// max indices before we need to flush the renderer
maxIndex = size * 6;

viewMatrix = new Matrix4f();

// default size
resize(Display.getWidth(), Display.getHeight());
}
Expand All @@ -137,6 +139,12 @@ public Matrix4f getProjectionMatrix() {
return projMatrix;
}

public Matrix4f getCombinedMatrix() {
Matrix4f.mul(Matrix4f.transpose(projMatrix, transpositionPool),
viewMatrix, projViewMatrix);
return projViewMatrix;
}

/** A convenience method to resize the projection matrix to the given
* dimensions, using y-down ortho 2D. This will invoke a call to
* updateMatrices.
Expand Down Expand Up @@ -165,7 +173,7 @@ public void setColor(Color color) {
public void setColor(float r, float g, float b, float a) {
color.set(r, g, b, a);
}

/** Call to multiply the the projection with the view matrix and save the
* result in the uniform mat4 {@value #U_PROJ_VIEW}, as well as update the
* {@value #U_TEXTURE} uniform. */
Expand All @@ -177,9 +185,7 @@ public void updateUniforms() {
* result in the uniform mat4 {@value #U_PROJ_VIEW}, as well as update the
* {@value #U_TEXTURE} uniform. */
public void updateUniforms(ShaderProgram program) {
// Multiply the transposed projection matrix with the view matrix:
projViewMatrix = Matrix4f.mul(Matrix4f.transpose(projMatrix, transpositionPool),
viewMatrix, projViewMatrix);
projViewMatrix = getCombinedMatrix();

// bind the program before sending uniforms
program.use();
Expand All @@ -189,7 +195,8 @@ public void updateUniforms(ShaderProgram program) {
//disable strict mode so we don't run into any problems
ShaderProgram.setStrictMode(false);

//we can now utilize ShaderProgram's hash map which may be better than glGetUniformLocation
// we can now utilize ShaderProgram's hash map which may be better than
// glGetUniformLocation

// Store the the multiplied matrix in the "projViewMatrix"-uniform:
program.setUniformMatrix(U_PROJ_VIEW, false, projViewMatrix);
Expand All @@ -210,13 +217,13 @@ public void updateUniforms(ShaderProgram program) {
public void setShader(ShaderProgram program, boolean updateUniforms) {
if (program==null)
throw new NullPointerException("shader cannot be null; use getDefaultShader instead");
if (drawing) {
if (drawing) //if we are already drawing, flush the batch before switching shaders
flush();
this.program.use();
}
this.program = program;
if (updateUniforms)
this.program = program; //now switch the shader
if (updateUniforms) //send uniform data to shader
updateUniforms();
else if (drawing) //if we don't want to update, then just start the program if we are drawing
program.use();
}

/** Changes the shader and updates it with the current texture and projView
Expand Down
49 changes: 41 additions & 8 deletions src/mdesl/graphics/Texture.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@

import static org.lwjgl.opengl.GL11.GL_CLAMP;
import static org.lwjgl.opengl.GL11.GL_LINEAR;
import static org.lwjgl.opengl.GL11.GL_LINEAR_MIPMAP_LINEAR;
import static org.lwjgl.opengl.GL11.GL_LINEAR_MIPMAP_NEAREST;
import static org.lwjgl.opengl.GL11.GL_NEAREST;
import static org.lwjgl.opengl.GL11.GL_NEAREST_MIPMAP_LINEAR;
import static org.lwjgl.opengl.GL11.GL_NEAREST_MIPMAP_NEAREST;
import static org.lwjgl.opengl.GL11.GL_PACK_ALIGNMENT;
import static org.lwjgl.opengl.GL11.GL_REPEAT;
import static org.lwjgl.opengl.GL11.GL_RGBA;
Expand All @@ -59,6 +63,8 @@
import java.nio.ByteBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.EXTFramebufferObject;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GLContext;

import de.matthiasmann.twl.utils.PNGDecoder;
Expand Down Expand Up @@ -91,6 +97,10 @@ public static boolean isNPOTSupported() {
// Some filters, included here for convenience
public static final int LINEAR = GL_LINEAR;
public static final int NEAREST = GL_NEAREST;
public static final int LINEAR_MIPMAP_LINEAR = GL_LINEAR_MIPMAP_LINEAR;
public static final int LINEAR_MIPMAP_NEAREST = GL_LINEAR_MIPMAP_NEAREST;
public static final int NEAREST_MIPMAP_NEAREST = GL_NEAREST_MIPMAP_NEAREST;
public static final int NEAREST_MIPMAP_LINEAR = GL_NEAREST_MIPMAP_LINEAR;

// Some wrap modes, included here for convenience
public static final int CLAMP = GL_CLAMP;
Expand Down Expand Up @@ -124,22 +134,27 @@ public Texture(int width, int height) {
public Texture(int width, int height, int filter) {
this(width, height, filter, DEFAULT_WRAP);
}

/** Creates an empty OpenGL texture with the given width and height, where
* each pixel is transparent black (0, 0, 0, 0).
*
* @param width the width of the texture
* @param height the height of the texture
* @param filter the filter to use
* @param wrap the wrap mode to use */
* @param minFilter the minification filter to use
* @param magFilter the magnification filter to use
* @param wrap the wrap mode to use
* @param genMipmaps - whether to generate mipmaps, which requires
* GL_EXT_framebuffer_object (or GL3+) */
public Texture(int width, int height, int filter, int wrap) {
glEnable(getTarget());
id = glGenTextures();
this.width = width;
this.height = height;
bind();

setFilter(filter);
setWrap(wrap);

ByteBuffer buf = BufferUtils.createByteBuffer(width * height * 4);
upload(GL_RGBA, buf);
}
Expand All @@ -151,8 +166,17 @@ public Texture(URL pngRef) throws IOException {
public Texture(URL pngRef, int filter) throws IOException {
this(pngRef, filter, DEFAULT_WRAP);
}

public Texture(URL pngRef, int filter, int wrap) throws IOException {
this(pngRef, filter, filter, wrap, false);
}

public Texture(URL pngRef, int filter, boolean genMipmap) throws IOException {
this(pngRef, filter, filter, DEFAULT_WRAP, genMipmap);
}

public Texture(URL pngRef, int minFilter, int magFilter, int wrap,
boolean genMipmap) throws IOException {
//TODO: npot check
InputStream input = null;
try {
Expand All @@ -169,9 +193,14 @@ public Texture(URL pngRef, int filter, int wrap) throws IOException {
id = glGenTextures();

bind();
setFilter(filter);
setFilter(minFilter, magFilter);
setWrap(wrap);
upload(GL_RGBA, buf);

//use EXT since we are targeting 2.0+
if (genMipmap) {
EXTFramebufferObject.glGenerateMipmapEXT(getTarget());
}
} finally {
if (input != null) {
try {
Expand Down Expand Up @@ -220,11 +249,15 @@ public void upload(int x, int y, int width, int height, int dataFormat, ByteBuff
}

public void setFilter(int filter) {
setFilter(filter, filter);
}

public void setFilter(int minFilter, int magFilter) {
bind();
glTexParameteri(getTarget(), GL_TEXTURE_MIN_FILTER, filter);
glTexParameteri(getTarget(), GL_TEXTURE_MAG_FILTER, filter);
glTexParameteri(getTarget(), GL_TEXTURE_MIN_FILTER, minFilter);
glTexParameteri(getTarget(), GL_TEXTURE_MAG_FILTER, magFilter);
}

public void setWrap(int wrap) {
bind();
glTexParameteri(getTarget(), GL_TEXTURE_WRAP_S, wrap);
Expand Down
4 changes: 4 additions & 0 deletions src/mdesl/graphics/text/BitmapFont.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public int getLineHeight() {
return lineHeight;
}

public TextureRegion[] getTexturePages() {
return texturePages;
}

public void drawText(SpriteBatch batch, CharSequence text, int x, int y) {
drawText(batch, text, x, y, 0, text.length());
}
Expand Down
8 changes: 4 additions & 4 deletions test/mdesl/test/FBOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import javax.imageio.ImageIO;

import mdesl.graphics.Color;
import mdesl.graphics.Framebuffer;
import mdesl.graphics.FrameBuffer;
import mdesl.graphics.SpriteBatch;
import mdesl.graphics.Texture;
import mdesl.graphics.TextureRegion;
Expand All @@ -45,7 +45,7 @@ public static void main(String[] args) throws LWJGLException {
Texture atlas;
TextureRegion track, thumb;

Framebuffer fbo;
FrameBuffer fbo;
TextureRegion fboRegion;

protected void create() throws LWJGLException {
Expand All @@ -63,12 +63,12 @@ protected void create() throws LWJGLException {

//create a new FBO with the width and height of our track
if (Texture.isNPOTSupported()) {
fbo = new Framebuffer(width, height);
fbo = new FrameBuffer(width, height);
fboRegion = new TextureRegion(fbo.getTexture());
} else {
int texWidth = Texture.toPowerOfTwo(width);
int texHeight = Texture.toPowerOfTwo(height);
fbo = new Framebuffer(texWidth, texHeight);
fbo = new FrameBuffer(texWidth, texHeight);
fboRegion = new TextureRegion(fbo.getTexture(), 0, texHeight-height, width, height);
}
fboRegion.flip(false, true);
Expand Down
2 changes: 1 addition & 1 deletion test/mdesl/test/RectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.nio.ByteBuffer;

import mdesl.graphics.Color;
import mdesl.graphics.Framebuffer;
import mdesl.graphics.FrameBuffer;
import mdesl.graphics.SpriteBatch;
import mdesl.graphics.Texture;
import mdesl.graphics.TextureRegion;
Expand Down
23 changes: 0 additions & 23 deletions test/mdesl/test/SimpleTest.java

This file was deleted.

Loading

0 comments on commit c04719b

Please sign in to comment.