12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package me.hammerle.snuviengine.api;
- import java.nio.FloatBuffer;
- import org.lwjgl.BufferUtils;
- import static org.lwjgl.opengl.GL11.*;
- import static org.lwjgl.opengl.GL15.*;
- import static org.lwjgl.opengl.GL20.*;
- import static org.lwjgl.opengl.GL30.*;
- public class TextureRenderer {
- private final int vertexArray;
- private final int vertexBuffer;
- private int triangles = 0;
- private FloatBuffer buffer;
- private boolean built = false;
- public TextureRenderer(int triangles) {
- buffer = BufferUtils.createFloatBuffer(triangles * 12);
- vertexArray = glGenVertexArrays();
- vertexBuffer = glGenBuffers();
- GLHelper.glBindVertexArray(vertexArray);
- GLHelper.glBindBuffer(vertexBuffer);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, false, 16, 0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(1, 2, GL_FLOAT, false, 16, 8);
- }
- public void addTriangle(float x1, float y1, float x2, float y2, float x3, float y3, float tx1, float ty1, float tx2, float ty2, float tx3, float ty3) {
- if(buffer.position() + 12 > buffer.capacity()) {
- FloatBuffer newBuffer = BufferUtils.createFloatBuffer(buffer.capacity() * 2);
- buffer.flip();
- newBuffer.put(buffer);
- buffer = newBuffer;
- }
- addToBuffer(x1, y1, tx1, ty1);
- addToBuffer(x2, y2, tx2, ty2);
- addToBuffer(x3, y3, tx3, ty3);
- triangles++;
- }
- private void addToBuffer(float x, float y, float tx, float ty) {
- buffer.put(x);
- buffer.put(y);
- buffer.put(tx);
- buffer.put(ty);
- }
- public void addRectangle(float minX, float minY, float maxX, float maxY, float tMinX, float tMinY, float tMaxX, float tMaxY) {
- addTriangle(minX, maxY, minX, minY, maxX, maxY, tMinX, tMaxY, tMinX, tMinY, tMaxX, tMaxY);
- addTriangle(maxX, maxY, minX, minY, maxX, minY, tMaxX, tMaxY, tMinX, tMinY, tMaxX, tMinY);
- }
- public boolean isBuilt() {
- return built;
- }
- public void build() {
- if(triangles == 0 || built) {
- return;
- }
- buffer.flip();
- GLHelper.glBindVertexArray(vertexArray);
- GLHelper.glBindBuffer(vertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, buffer, GL_STATIC_DRAW);
- buffer.limit(buffer.capacity());
- built = true;
- }
- public void clear() {
- triangles = 0;
- buffer.clear();
- built = false;
- }
- public void draw() {
- if(triangles == 0 || !built) {
- return;
- }
- GLHelper.glBindVertexArray(vertexArray);
- GLHelper.glBindBuffer(vertexBuffer);
- glDrawArrays(GL_TRIANGLES, 0, triangles * 3);
- }
- public void delete() {
- buffer = null;
- glDeleteVertexArrays(vertexArray);
- glDeleteBuffers(vertexBuffer);
- built = false;
- }
- }
|