Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.main;
- import com.main.Main;
- import com.main.ShaderProgram;
- import org.lwjgl.BufferUtils;
- import java.nio.FloatBuffer;
- 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 Tutorial1 extends Main {
- int vaoID;
- int vboID;
- int vboColID;
- ShaderProgram shaderProgram = new ShaderProgram();
- float x1,y1,x2,y2,x3,y3;
- public Tutorial1() {
- }
- public void initGame() {
- x1= +0f;
- x2= -1f;
- x3= +1f;
- y1= +1f;
- y2= -1f;
- y3= -1f;
- shaderProgram.attachVertexShader("com/utility/simply2D.vs");
- shaderProgram.attachFragmentShader("com/utility/blank.fs");
- shaderProgram.link();
- }
- int lastaction = 2;
- public void render(float delta)
- {
- // Clear the screen
- glClear(GL_COLOR_BUFFER_BIT);
- // Use our program
- shaderProgram.bind();
- // Bind the vertex array and enable our location
- glBindVertexArray(vaoID);
- glEnableVertexAttribArray(0);
- // Draw a triangle of 3 vertices
- glDrawArrays(GL_TRIANGLES, 0, 3);
- // Disable our location
- glDisableVertexAttribArray(0);
- glBindVertexArray(0);
- // Un-bind our program
- shaderProgram.unbind();
- if (lastaction==2 && y1>-1) {
- x2 += +0.004f;
- x3 += -0.004f;
- y1 += -0.004f;
- y2 += +0.004f;
- y3 += +0.004f;
- if (y1<-1 || y1==-1) {lastaction = 1;}
- }
- if (lastaction==1 && y1<1) {
- x2 += -0.004f;
- x3 += +0.004f;
- y1 += +0.004f;
- y2 += -0.004f;
- y3 += -0.004f;
- if (y1>1 || y1==1) {lastaction = 2;}
- }
- verticeSpecify(x1, y1, x2, y2, x3, y3);
- colorSpecify();
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glBindVertexArray(0);
- }
- public void dispose()
- {
- // Dispose the program
- shaderProgram.dispose();
- // Dispose the vertex array
- glBindVertexArray(0);
- glDeleteVertexArrays(vaoID);
- // Dispose the buffer object
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glBindBuffer(GL_ARRAY_BUFFER, 1);
- glDeleteBuffers(vboID);
- glDeleteBuffers(vboColID);
- }
- public void verticeSpecify(float x1, float y1, float x2, float y2, float x3, float y3) {
- // Generate and bind a Vertex Array
- vaoID = glGenVertexArrays();
- glBindVertexArray(vaoID);
- // The vertices of our Triangle
- float[] vertices = new float[]
- {
- x1, y1, // Top coordinate
- x2, y2, // Bottom-left coordinate
- x3, y3 // Bottom-right coordinate
- };
- // Create a FloatBuffer of vertices
- FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
- verticesBuffer.put(vertices).flip();
- // Create a Buffer Object and upload the vertices buffer
- vboID = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, vboID);
- glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
- // Point the buffer at location 0, the location we set
- // inside the vertex shader. You can use any location
- // but the locations should match
- glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
- }
- public void colorSpecify() {
- // The vertices of our Triangle
- float[] colors = new float[]
- {
- 1, 0, 0, 1, // Red color, for the first vertex
- 0, 1, 0, 1, // Green color, for the second vertex
- 0, 0, 1, 1 // Blue color, for the third vertex
- };
- // Create a FloatBuffer of vertices
- FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length);
- colorsBuffer.put(colors).flip();
- // Create a Buffer Object and upload the vertices buffer
- vboColID = glGenBuffers();
- glBindBuffer(GL_ARRAY_BUFFER, vboColID);
- glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
- // Point the buffer at location 0, the location we set
- // inside the vertex shader. You can use any location
- // but the locations should match
- glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, 0);
- }
- public static void main(String[] args) {
- new Tutorial1().start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement