Skip to content

Instantly share code, notes, and snippets.

@Longor1996
Created May 30, 2014 21:35
Show Gist options
  • Save Longor1996/8daa93ebd5fd791f4d94 to your computer and use it in GitHub Desktop.
Save Longor1996/8daa93ebd5fd791f4d94 to your computer and use it in GitHub Desktop.
The VertexArrayDrawer that is used in my Engine.
package de.jailong.engine.graphics;
import java.nio.FloatBuffer;
import java.util.Arrays;
import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL13;
public class VertexArrayDrawer implements IDrawer
{
private static final int[] defaultActiveTextureUnits = new int[]{0};
private static final int MAX_TEXTURE_UNITS = 8;
FloatBuffer vertexBuffer;
FloatBuffer normalBuffer;
FloatBuffer colorBuffer;
FloatBuffer[] textureBuffer;
int vertexCount = 0;
int drawMode = -1;
int[] activeTextureUnits = defaultActiveTextureUnits;
float r = 1;
float g = 1;
float b = 1;
float a = 1;
float nx = 0;
float ny = 1;
float nz = 0;
float offX = 0;
float offY = 0;
float offZ = 0;
float[] s = new float[MAX_TEXTURE_UNITS];
float[] t = new float[MAX_TEXTURE_UNITS];
boolean useColor = false;
boolean useNormals = false;
boolean useUV = false;
boolean blockadeDrawing = false;
boolean isDrawing = false;
public static final VertexArrayDrawer createInstance()
{
return new VertexArrayDrawer(1024 * 1024 * 2);
}
public static final VertexArrayDrawer createInstance(int size)
{
return new VertexArrayDrawer(size);
}
private VertexArrayDrawer(int amount)
{
this.vertexBuffer = BufferUtils.createFloatBuffer(amount);
this.normalBuffer = BufferUtils.createFloatBuffer(amount);
this.colorBuffer = BufferUtils.createFloatBuffer(amount);
this.textureBuffer = new FloatBuffer[MAX_TEXTURE_UNITS];
for(int i = 0; i < MAX_TEXTURE_UNITS; i++)
{
this.textureBuffer[i] = BufferUtils.createFloatBuffer(amount);
}
this.reset();
}
@Override
public void translation(float x, float y, float z)
{
this.offX = x;
this.offY = y;
this.offZ = z;
}
@Override
public void color(float r, float g, float b, float a)
{
this.r = r;
this.g = g;
this.b = b;
this.a = a;
}
@Override
public void normal(float x, float y, float z)
{
this.nx = x;
this.ny = y;
this.nz = z;
}
@Override
public void texture(int texUnit, float s, float t)
{
this.s[texUnit] = s;
this.t[texUnit] = t;
}
@Override
public void vertex(float x, float y, float z, float s, float t)
{
this.texture(0, s, t);
this.vertex(x,y,z);
}
@Override
public void vertex2D(float x, float y, float s, float t)
{
this.texture(0, s, t);
this.vertex(x,y,0);
}
@Override
public void vertex(float x, float y, float z)
{
if(this.blockadeDrawing)
{
return;
}
this.vertexBuffer.put(x + this.offX);
this.vertexBuffer.put(y + this.offY);
this.vertexBuffer.put(z + this.offZ);
if(this.useNormals)
{
this.normalBuffer.put(this.nx);
this.normalBuffer.put(this.ny);
this.normalBuffer.put(this.nz);
}
if(this.useColor)
{
this.colorBuffer.put(this.r);
this.colorBuffer.put(this.g);
this.colorBuffer.put(this.b);
this.colorBuffer.put(this.a);
}
if(this.useUV)
{
for(int i = 0; i < MAX_TEXTURE_UNITS; i++)
{
this.textureBuffer[i].put(this.s[i]);
this.textureBuffer[i].put(this.t[i]);
}
}
this.vertexCount++;
}
private void reset()
{
this.r =
this.g =
this.b =
this.a =
this.nx =
this.ny =
this.nz =
this.offX =
this.offY =
this.offZ =
this.drawMode = 0;
Arrays.fill(this.s, 0);
Arrays.fill(this.t, 0);
this.isDrawing = false;
}
@Override
public void startDrawing(int mode, boolean color, boolean texture, boolean normals)
{
if (this.isDrawing)
{
throw new RuntimeException("VertexDrawerVA-instance is already drawing!");
}
this.reset();
this.vertexCount = 0;
this.drawMode = mode;
this.useColor = color;
this.useNormals = normals;
this.useUV = texture;
this.isDrawing = true;
this.blockadeDrawing = false;
this.activeTextureUnits = defaultActiveTextureUnits;
}
public void postStart(int[] activeTextureUnits)
{
this.activeTextureUnits = activeTextureUnits;
}
@Override
public void stopDrawing()
{
if(this.vertexCount == 0)
{
this.vertexBuffer.rewind();
this.colorBuffer.rewind();
this.normalBuffer.rewind();
for(int i = 0; i < MAX_TEXTURE_UNITS; i++)
{
this.textureBuffer[i].rewind();
}
this.vertexCount = 0;
this.reset();
return;
}
this.vertexBuffer.rewind();
this.colorBuffer.rewind();
this.normalBuffer.rewind();
for(int i = 0; i < MAX_TEXTURE_UNITS; i++)
{
this.textureBuffer[i].rewind();
}
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glVertexPointer(3, /* stride */3 << 2, this.vertexBuffer);
if (this.useColor)
{
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
GL11.glColorPointer(4, /* stride */4 << 2, this.colorBuffer);
}
if (this.useNormals)
{
GL11.glEnableClientState(GL11.GL_NORMAL_ARRAY);
GL11.glNormalPointer(3 << 2, this.normalBuffer);
}
if (this.useUV)
{
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
for(int i = 0; i < this.activeTextureUnits.length; i++)
{
int unitID = this.activeTextureUnits[i];
GL13.glActiveTexture(GL13.GL_TEXTURE0 + unitID);
GL11.glTexCoordPointer(2, /* stride */2 << 2, this.textureBuffer[unitID]);
}
GL13.glActiveTexture(GL13.GL_TEXTURE0);
}
GL11.glDrawArrays(this.drawMode, 0, this.vertexCount);
GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
if (this.useColor)
{
GL11.glDisableClientState(GL11.GL_COLOR_ARRAY);
}
if (this.useNormals)
{
GL11.glDisableClientState(GL11.GL_NORMAL_ARRAY);
}
if (this.useUV)
{
GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
}
this.reset();
}
@Override
public boolean isDrawing()
{
return this.isDrawing;
}
@Override
public boolean hasDrawenAnyVertices()
{
return this.vertexCount != 0;
}
@Override
public int getCurrentVertexCount()
{
return this.vertexCount;
}
@Override
public void blockDrawing(boolean c)
{
this.blockadeDrawing = c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment