Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Quad {
- float a = 0, b = 0;
- // Quad variables
- private int vaoId = 0;
- private int vboId = 0;
- private int vboiId = 0;
- private int indicesCount = 0;
- // Shader variables
- private int vsId = 0;
- private int fsId = 0;
- private int pId = 0;
- // Texture variables
- private int[] texIds = new int[] {0, 0};
- private int textureSelector = 0;
- // Update VBO variables
- private TexturedVertex[] vertices = null;
- private ByteBuffer vertexByteBuffer = null;
- private ByteBuffer verticesByteBuffer = null;
- boolean move;
- public Quad(boolean move, int aa) {
- this.move = move;
- this.vboId = aa;
- init();
- shaders();
- textures();
- }
- public void update() {
- // Texture selection
- while(Keyboard.next()) {
- if (!Keyboard.getEventKeyState() && move) continue;
- // Switch textures depending on the key released
- switch (Keyboard.getEventKey()) {
- case Keyboard.KEY_1:
- textureSelector = 0;
- break;
- case Keyboard.KEY_2:
- textureSelector = 1;
- break;
- case Keyboard.KEY_LEFT:
- a-= 0.2f;
- break;
- case Keyboard.KEY_RIGHT:
- a+= 0.2f;
- break;
- case Keyboard.KEY_UP:
- b+= 0.2f;
- break;
- case Keyboard.KEY_DOWN:
- b-= 0.2f;
- break;
- }
- }
- // Update vertices in the VBO, first bind the VBO
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
- // Apply and update vertex data
- for (int i = 0; i < vertices.length; i++) {
- TexturedVertex vertex = vertices[i];
- // Define offset
- float offsetX = (float) (Math.cos(Math.PI * Math.random()) * 0.1);
- float offsetY = (float) (Math.sin(Math.PI * Math.random()) * 0.1);
- // Offset the vertex position
- float[] xyz = vertex.getXYZ();
- vertex.setXYZ(xyz[0] + a, xyz[1] + offsetY, xyz[2]);
- // Put the new data in a ByteBuffer (in the view of a FloatBuffer)
- FloatBuffer vertexFloatBuffer = vertexByteBuffer.asFloatBuffer();
- vertexFloatBuffer.rewind();
- vertexFloatBuffer.put(vertex.getElements());
- vertexFloatBuffer.flip();
- GL15.glBufferSubData(GL15.GL_ARRAY_BUFFER, i * TexturedVertex.stride,
- vertexByteBuffer);
- // Restore the vertex data
- vertex.setXYZ(xyz[0], xyz[1], xyz[2]);
- }
- // And of course unbind
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- this.exitOnGLError("logicCycle");
- }
- public void render() {
- GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
- GL20.glUseProgram(pId);
- // Bind the texture
- GL13.glActiveTexture(GL13.GL_TEXTURE0);
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, texIds[textureSelector]);
- // Bind to the VAO that has all the information about the vertices
- GL30.glBindVertexArray(vaoId);
- GL20.glEnableVertexAttribArray(0);
- GL20.glEnableVertexAttribArray(1);
- GL20.glEnableVertexAttribArray(2);
- // Bind to the index VBO that has all the information about the order of the vertices
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
- // Draw the vertices
- GL11.glDrawElements(GL11.GL_TRIANGLES, indicesCount, 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);
- GL20.glDisableVertexAttribArray(2);
- GL30.glBindVertexArray(0);
- GL20.glUseProgram(0);
- this.exitOnGLError("renderCycle");
- }
- private void init() {
- // We'll define our quad using 4 vertices of the custom 'TexturedVertex' class
- TexturedVertex v0 = new TexturedVertex();
- v0.setXYZ(-0.5f, 0.5f, 0); v0.setRGB(1, 0, 0); v0.setST(0, 0);
- TexturedVertex v1 = new TexturedVertex();
- v1.setXYZ(-0.5f, -0.5f, 0); v1.setRGB(0, 1, 0); v1.setST(0, 1);
- TexturedVertex v2 = new TexturedVertex();
- v2.setXYZ(0.5f, -0.5f, 0); v2.setRGB(0, 0, 1); v2.setST(1, 1);
- TexturedVertex v3 = new TexturedVertex();
- v3.setXYZ(0.5f, 0.5f, 0); v3.setRGB(1, 1, 1); v3.setST(1, 0);
- vertices = new TexturedVertex[] {v0, v1, v2, v3};
- // Create a FloatBufer of the appropriate size for one vertex
- vertexByteBuffer = BufferUtils.createByteBuffer(TexturedVertex.stride);
- // Put each 'Vertex' in one FloatBuffer
- verticesByteBuffer = BufferUtils.createByteBuffer(vertices.length *
- TexturedVertex.stride);
- FloatBuffer verticesFloatBuffer = verticesByteBuffer.asFloatBuffer();
- for (int i = 0; i < vertices.length; i++) {
- // Add position, color and texture floats to the buffer
- verticesFloatBuffer.put(vertices[i].getElements());
- }
- verticesFloatBuffer.flip();
- // OpenGL expects to draw vertices in counter clockwise order by default
- byte[] indices = {
- 0, 1, 2,
- 2, 3, 0
- };
- indicesCount = indices.length;
- ByteBuffer indicesBuffer = BufferUtils.createByteBuffer(indicesCount);
- indicesBuffer.put(indices);
- indicesBuffer.flip();
- // Create a new Vertex Array Object in memory and select it (bind)
- vaoId = GL30.glGenVertexArrays();
- GL30.glBindVertexArray(vaoId);
- // Create a new Vertex Buffer Object in memory and select it (bind)
- vboId = GL15.glGenBuffers();
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vboId);
- GL15.glBufferData(GL15.GL_ARRAY_BUFFER, verticesFloatBuffer, GL15.GL_STREAM_DRAW);
- // Put the position coordinates in attribute list 0
- GL20.glVertexAttribPointer(0, TexturedVertex.positionElementCount, GL11.GL_FLOAT,
- false, TexturedVertex.stride, TexturedVertex.positionByteOffset);
- // Put the color components in attribute list 1
- GL20.glVertexAttribPointer(1, TexturedVertex.colorElementCount, GL11.GL_FLOAT,
- false, TexturedVertex.stride, TexturedVertex.colorByteOffset);
- // Put the texture coordinates in attribute list 2
- GL20.glVertexAttribPointer(2, TexturedVertex.textureElementCount, GL11.GL_FLOAT,
- false, TexturedVertex.stride, TexturedVertex.textureByteOffset);
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- // Deselect (bind to 0) the VAO
- GL30.glBindVertexArray(0);
- // Create a new VBO for the indices and select it (bind) - INDICES
- vboiId = GL15.glGenBuffers();
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, vboiId);
- GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER, indicesBuffer, GL15.GL_STATIC_DRAW);
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
- this.exitOnGLError("setupQuad");
- }
- private void shaders() {
- // Load the vertex shader
- vsId = this.loadShader("shaders/vert.shdr", GL20.GL_VERTEX_SHADER);
- // Load the fragment shader
- fsId = this.loadShader("shaders/frag.shdr", GL20.GL_FRAGMENT_SHADER);
- // Create a new shader program that links both shaders
- pId = GL20.glCreateProgram();
- GL20.glAttachShader(pId, vsId);
- GL20.glAttachShader(pId, fsId);
- // Position information will be attribute 0
- GL20.glBindAttribLocation(pId, 0, "in_Position");
- // Color information will be attribute 1
- GL20.glBindAttribLocation(pId, 1, "in_Color");
- // Textute information will be attribute 2
- GL20.glBindAttribLocation(pId, 2, "in_TextureCoord");
- GL20.glLinkProgram(pId);
- GL20.glValidateProgram(pId);
- this.exitOnGLError("setupShaders");
- }
- private void textures() {
- texIds[0] = this.loadPNGTexture("res/a.png", GL13.GL_TEXTURE0);
- texIds[1] = this.loadPNGTexture("res/erza.png", GL13.GL_TEXTURE0);
- this.exitOnGLError("setupTexture");
- }
- private 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);
- if (GL20.glGetShader(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
- System.err.println("Could not compile shader.");
- System.exit(-1);
- }
- this.exitOnGLError("loadShader");
- return shaderID;
- }
- private int loadPNGTexture(String filename, int textureUnit) {
- ByteBuffer buf = null;
- int tWidth = 0;
- int tHeight = 0;
- try {
- // Open the PNG file as an InputStream
- InputStream in = new FileInputStream(filename);
- // Link the PNG decoder to this stream
- PNGDecoder decoder = new PNGDecoder(in);
- // Get the width and height of the texture
- tWidth = decoder.getWidth();
- tHeight = decoder.getHeight();
- // Decode the PNG file in a ByteBuffer
- buf = ByteBuffer.allocateDirect(
- 4 * decoder.getWidth() * decoder.getHeight());
- decoder.decode(buf, decoder.getWidth() * 4, Format.RGBA);
- buf.flip();
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- System.exit(-1);
- }
- // Create a new texture object in memory and bind it
- int texId = GL11.glGenTextures();
- GL13.glActiveTexture(textureUnit);
- GL11.glBindTexture(GL11.GL_TEXTURE_2D, texId);
- // All RGB bytes are aligned to each other and each component is 1 byte
- GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, 1);
- // Upload the texture data and generate mip maps (for scaling)
- GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, tWidth, tHeight, 0,
- GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, buf);
- GL30.glGenerateMipmap(GL11.GL_TEXTURE_2D);
- // Setup the ST coordinate system
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
- // Setup what to do when the texture has to be scaled
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER,
- GL11.GL_LINEAR);
- GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER,
- GL11.GL_LINEAR_MIPMAP_LINEAR);
- this.exitOnGLError("loadPNGTexture");
- return texId;
- }
- private void exitOnGLError(String errorMessage) {
- int errorValue = GL11.glGetError();
- if (errorValue != GL11.GL_NO_ERROR) {
- String errorString = GLU.gluErrorString(errorValue);
- System.err.println("ERROR - " + errorMessage + ": " + errorString);
- if (Display.isCreated()) Display.destroy();
- System.exit(-1);
- }
- }
- public void destroy() {
- // Delete the texture
- GL11.glDeleteTextures(texIds[0]);
- GL11.glDeleteTextures(texIds[1]);
- // Delete the shaders
- GL20.glUseProgram(0);
- GL20.glDetachShader(pId, vsId);
- GL20.glDetachShader(pId, fsId);
- GL20.glDeleteShader(vsId);
- GL20.glDeleteShader(fsId);
- GL20.glDeleteProgram(pId);
- // Select the VAO
- GL30.glBindVertexArray(vaoId);
- // Disable the VBO index from the VAO attributes list
- GL20.glDisableVertexAttribArray(0);
- GL20.glDisableVertexAttribArray(1);
- // Delete the vertex VBO
- GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
- GL15.glDeleteBuffers(vboId);
- // Delete the index VBO
- GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
- GL15.glDeleteBuffers(vboiId);
- // Delete the VAO
- GL30.glBindVertexArray(0);
- GL30.glDeleteVertexArrays(vaoId);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement