Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (530)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Gremolin (posted 2014-05-03 15:04:47, viewed 36 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214  
package dw.superbible6.tutorials.ex301movetriangle;

import static org.lwjgl.opengl.GL20.glValidateProgram;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL15;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL30;
import org.lwjgl.opengl.PixelFormat;

import dw.game.GameUtils;

public class MoveTriangleB
{
   private static final String WINDOW_TITLE = "Example";
   private static final int WIDTH = 512;
   private static final int HEIGHT = 512;

   // Shader ids
   private int vertex_shader;
   private int fragment_shader;

   // Vertex Array Object
   private int vaoId;

   private int programId;

   private final String vertex_shader_source = //
   "         #version 430 core                                                       \n" + //
         " // 'offset' is an input vertex attribute                         \n" + //
         " layout (location = 0) in vec4 offset;                            \n" + //
         " void main(void)                                                  \n" + //
         " {                                                                \n" + //
         " const vec4 vertices[3] = vec4[3](vec4( 0.25, -0.25, 0.5, 1.0),   \n" + //
         " vec4(-0.25, -0.25, 0.5, 1.0),                                    \n" + //
         " vec4( 0.25, 0.25, 0.5, 1.0));                                    \n" + //
         " // Add 'offset' to our hard-coded vertex position                \n" + //
         " gl_Position = vertices[gl_VertexID] + offset;                    \n" + //
         " }                                                                \n";

   private final String fragment_shader_source = //

   "         #version 430 core                         \n" + //
         "                                           \n" + //
         "out vec4 color;                            \n" + //
         "                                           \n" + //
         "void main(void)                            \n" + //
         "{                                          \n" + //
         " color = vec4(0.0, 0.8, 1.0, 1.0);         \n" + //
         "}                                          \n";
   private int positionBufferHandle;

   public MoveTriangleB()
   {
      this.setupDisplay();

      programId = this.compileShaders();
      this.init();

      this.render();
   }

   private void init()
   {
      vaoId = GL30.glGenVertexArrays();
   }

   private int compileShaders()
   {
      // Create and compile vertex shader
      vertex_shader = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
      // vertex_shader = ShaderUtils.loadShader("src/dw/superbible6/tutorials/ex301movetriangle/vertex.glsl", GL20.GL_VERTEX_SHADER);
      GL20.glShaderSource(vertex_shader, vertex_shader_source);
      GL20.glCompileShader(vertex_shader);

      // Create and compile fragment shader
      fragment_shader = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
      GL20.glShaderSource(fragment_shader, fragment_shader_source);
      GL20.glCompileShader(fragment_shader);

      // Create program, attach shaders to it, and link it
      programId = GL20.glCreateProgram();
      GL20.glAttachShader(programId, vertex_shader);
      GL20.glAttachShader(programId, fragment_shader);
      GL20.glLinkProgram(programId);

      glValidateProgram(programId);

      vaoId = GL30.glGenVertexArrays();
      GL30.glBindVertexArray(vaoId);
      GL20.glEnableVertexAttribArray(0);

      // create vertex data
      float[] positionData = new float[] { 0f, 0f, 0f, 0f };
      // convert vertex array to buffer
      FloatBuffer positionBuffer = BufferUtils.createFloatBuffer(positionData.length);
      positionBuffer.put(positionData);
      positionBuffer.flip();

      // create vertex byffer object (VBO) for vertices
      positionBufferHandle = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, positionBufferHandle);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, positionBuffer, GL15.GL_STATIC_DRAW);

      // assign vertex VBO to slot 0 of VAO
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, positionBufferHandle);
      GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);

      // Delete the shaders as the program has them now
      GL20.glDeleteShader(vertex_shader);
      GL20.glDeleteShader(fragment_shader);
      GameUtils.exitOnGLError("compile shaders");
      return programId;
   }

   private void render()
   {
      while (!Display.isCloseRequested())
      {
         FloatBuffer green = BufferUtils.createFloatBuffer(4);
         green.put(new float[] { 0.0f, 0.25f, 0.0f, 1.0f });
         green.flip();

         GL30.glClearBuffer(GL11.GL_COLOR, 0, green);
         GameUtils.exitOnGLError("set background colour");

         // Use the program object we created earlier for rendering
         System.out.println(programId);
         GL20.glUseProgram(programId);
         GameUtils.exitOnGLError("use program");

         // bind vertex
         GL30.glBindVertexArray(vaoId);
         GL20.glEnableVertexAttribArray(0);

         FloatBuffer attrib = BufferUtils.createFloatBuffer(4);
         float[] move = new float[] { //
         (float) Math.sin(getCurrentTime()) * 0.5f, //
               (float) (Math.cos(getCurrentTime()) * 0.6f), 0.0f,//
               0.0f };
         attrib.put(move);
         attrib.flip();
         GameUtils.exitOnGLError("set vertex");

         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, positionBufferHandle);
         GL15.glBufferData(GL15.GL_ARRAY_BUFFER, attrib, GL15.GL_STATIC_DRAW);

         // assign vertex VBO to slot 0 of VAO
         GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
         // GL20.glVertexAttrib4f(0, a * 100, b * 100, 0.0f, 0.0f);

         GameUtils.exitOnGLError("set shader variable");
         System.out.println(GL20.glGetActiveAttrib(programId, 0, 1000));

         // Draw one triangle
         GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);

         // Force a maximum FPS of about 60
         // Let the CPU synchronize with the GPU if GPU is tagging behind
         Display.sync(60);
         Display.update();
         GameUtils.exitOnGLError("End of render");
      }

      this.destroyOpenGL();
   }

   private long getCurrentTime()
   {
      return System.currentTimeMillis() / 100;
   }

   private void setupDisplay()
   {
      try
      {
         PixelFormat pixelFormat = new PixelFormat();
         ContextAttribs contextAtrributes = new ContextAttribs(3, 2).withForwardCompatible(true).withProfileCore(true);

         Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
         Display.setVSyncEnabled(true);
         Display.setTitle(WINDOW_TITLE);
         Display.create(pixelFormat, contextAtrributes);

      } catch (LWJGLException e)
      {
         e.printStackTrace();
         System.exit(-1);
      }
      GL11.glClearColor(0f, 0f, 0f, 0f);
      GL11.glViewport(0, 0, WIDTH, HEIGHT);
   }

   private void destroyOpenGL()
   {
      GL30.glDeleteVertexArrays(vaoId);
      GL20.glDeleteProgram(programId);

      Display.destroy();
   }

   public static final void main(String[] args)
   {
      System.out.println("Compile Shader");
      new MoveTriangleB();
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

ctomni231 (31 views)
2014-07-18 06:55:21

Zero Volt (27 views)
2014-07-17 23:47:54

danieldean (23 views)
2014-07-17 23:41:23

MustardPeter (24 views)
2014-07-16 23:30:00

Cero (39 views)
2014-07-16 00:42:17

Riven (41 views)
2014-07-14 18:02:53

OpenGLShaders (28 views)
2014-07-14 16:23:47

Riven (28 views)
2014-07-14 11:51:35

quew8 (25 views)
2014-07-13 13:57:52

SHC (62 views)
2014-07-12 17:50:04
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!