Advertisement
Giuseppe499

Tutorial1.java

Oct 27th, 2015
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.35 KB | None | 0 0
  1. package com.main;
  2.  
  3. import com.main.Main;
  4. import com.main.ShaderProgram;
  5. import org.lwjgl.BufferUtils;
  6.  
  7. import java.nio.FloatBuffer;
  8.  
  9. import static org.lwjgl.opengl.GL11.*;
  10. import static org.lwjgl.opengl.GL15.*;
  11. import static org.lwjgl.opengl.GL20.*;
  12. import static org.lwjgl.opengl.GL30.*;
  13.  
  14.  
  15. public class Tutorial1 extends Main {
  16.    
  17.     int vaoID;
  18.     int vboID;
  19.     int vboColID;
  20.     ShaderProgram shaderProgram = new ShaderProgram();
  21.     float x1,y1,x2,y2,x3,y3;
  22.    
  23.     public Tutorial1() {
  24.        
  25.     }
  26.    
  27.     public void initGame() {
  28.        
  29.         x1= +0f;
  30.         x2= -1f;
  31.         x3= +1f;
  32.         y1= +1f;
  33.         y2= -1f;
  34.         y3= -1f;
  35.        
  36.         shaderProgram.attachVertexShader("com/utility/simply2D.vs");
  37.         shaderProgram.attachFragmentShader("com/utility/blank.fs");
  38.         shaderProgram.link();
  39.        
  40.        
  41.     }
  42.    
  43.     int lastaction = 2;
  44.    
  45.     public void render(float delta)
  46.     {
  47.         // Clear the screen
  48.         glClear(GL_COLOR_BUFFER_BIT);
  49.  
  50.         // Use our program
  51.         shaderProgram.bind();
  52.  
  53.         // Bind the vertex array and enable our location
  54.         glBindVertexArray(vaoID);
  55.         glEnableVertexAttribArray(0);
  56.  
  57.         // Draw a triangle of 3 vertices
  58.         glDrawArrays(GL_TRIANGLES, 0, 3);
  59.  
  60.         // Disable our location
  61.         glDisableVertexAttribArray(0);
  62.         glBindVertexArray(0);
  63.  
  64.         // Un-bind our program
  65.         shaderProgram.unbind();
  66.        
  67.        
  68.        
  69.         if (lastaction==2 && y1>-1) {
  70.             x2 += +0.004f;     
  71.             x3 += -0.004f;
  72.             y1 += -0.004f;
  73.             y2 += +0.004f;
  74.             y3 += +0.004f;         
  75.             if (y1<-1 || y1==-1) {lastaction = 1;}         
  76.             }
  77.        
  78.         if (lastaction==1  && y1<1) {
  79.             x2 += -0.004f;     
  80.             x3 += +0.004f;
  81.             y1 += +0.004f;
  82.             y2 += -0.004f;
  83.             y3 += -0.004f;
  84.             if (y1>1 || y1==1) {lastaction = 2;}
  85.             }
  86.        
  87.        
  88.         verticeSpecify(x1, y1, x2, y2, x3, y3);
  89.         colorSpecify();
  90.        
  91.         glEnableVertexAttribArray(0);
  92.         glEnableVertexAttribArray(1);
  93.         glBindVertexArray(0);
  94.        
  95.        
  96.     }
  97.    
  98.    
  99.     public void dispose()
  100.     {
  101.         // Dispose the program
  102.         shaderProgram.dispose();
  103.  
  104.         // Dispose the vertex array
  105.         glBindVertexArray(0);
  106.         glDeleteVertexArrays(vaoID);
  107.  
  108.         // Dispose the buffer object
  109.         glBindBuffer(GL_ARRAY_BUFFER, 0);
  110.         glBindBuffer(GL_ARRAY_BUFFER, 1);
  111.         glDeleteBuffers(vboID);
  112.         glDeleteBuffers(vboColID);
  113.     }
  114.    
  115.     public void verticeSpecify(float x1, float y1, float x2, float y2, float x3, float y3) {
  116.        
  117.         // Generate and bind a Vertex Array
  118.         vaoID = glGenVertexArrays();
  119.         glBindVertexArray(vaoID);
  120.  
  121.         // The vertices of our Triangle
  122.         float[] vertices = new float[]
  123.         {
  124.             x1, y1,    // Top coordinate
  125.             x2, y2,    // Bottom-left coordinate
  126.             x3, y3     // Bottom-right coordinate
  127.         };
  128.  
  129.         // Create a FloatBuffer of vertices
  130.         FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(vertices.length);
  131.         verticesBuffer.put(vertices).flip();
  132.  
  133.         // Create a Buffer Object and upload the vertices buffer
  134.         vboID = glGenBuffers();
  135.         glBindBuffer(GL_ARRAY_BUFFER, vboID);
  136.         glBufferData(GL_ARRAY_BUFFER, verticesBuffer, GL_STATIC_DRAW);
  137.  
  138.         // Point the buffer at location 0, the location we set
  139.         // inside the vertex shader. You can use any location
  140.         // but the locations should match
  141.         glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0);
  142.        
  143.     }
  144.    
  145.       public void colorSpecify() {
  146.        
  147.  
  148.         // The vertices of our Triangle
  149.         float[] colors = new float[]
  150.                 {
  151.                     1, 0, 0, 1,  // Red color, for the first vertex
  152.                     0, 1, 0, 1,  // Green color, for the second vertex
  153.                     0, 0, 1, 1   // Blue color, for the third vertex
  154.                 };
  155.  
  156.         // Create a FloatBuffer of vertices
  157.         FloatBuffer colorsBuffer = BufferUtils.createFloatBuffer(colors.length);
  158.         colorsBuffer.put(colors).flip();
  159.  
  160.         // Create a Buffer Object and upload the vertices buffer
  161.         vboColID = glGenBuffers();
  162.         glBindBuffer(GL_ARRAY_BUFFER, vboColID);
  163.         glBufferData(GL_ARRAY_BUFFER, colorsBuffer, GL_STATIC_DRAW);
  164.  
  165.         // Point the buffer at location 0, the location we set
  166.         // inside the vertex shader. You can use any location
  167.         // but the locations should match
  168.         glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, 0);
  169.        
  170.     }
  171.    
  172.     public static void main(String[] args) {
  173.         new Tutorial1().start();
  174.     }
  175.    
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement