Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (491)
Games in Android Showcase (112)
games submitted by our members
Games in WIP (556)
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-09 23:57:43, viewed 38 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   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335  
package dw.superbible6.tutorials.ex501spinningcube;

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 org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

import dw.game.GameUtils;
import dw.math.MathUtils;

public class SpinningCube
{

   private static final String WINDOW_TITLE = "SpinningCube";
   private static final int WIDTH = 512;
   private static final int HEIGHT = 512;

   private int vaoId;
   private int buffer; // vbo

   // program ids
   private int programId;
   private int fragmentShaderId;
   private int vertexShaderId;

   private int mv_location; // model view location
   private int proj_location; // projection location

   // projection matrix.
   private Matrix4f proj_matrix;
   // modelview matrix
   private Matrix4f mv_matrix;

   private FloatBuffer matrix44Buffer;

   private static final String vsSource = // vertex shader
   "        #version 410 core                                                  \n" + //
         "                                                                   \n" + //
         "in vec4 position;                                                  \n" + //
         "                                                                   \n" + //
         "out VS_OUT                                                         \n" + //
         "{                                                                  \n" + //
         "    vec4 color;                                                    \n" + //
         "} vs_out;                                                          \n" + //
         "                                                                   \n" + //
         "uniform mat4 mv_matrix;                                            \n" + //
         "uniform mat4 proj_matrix;                                          \n" + //
         "                                                                   \n" + //
         "void main(void)                                                    \n" + //
         "{                                                                  \n" + //
         "    gl_Position = proj_matrix * mv_matrix * position;              \n" + //
         "    vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);      \n" + //
         "}                                                                  \n";

   private static final String fsSource = // fragment shader
   "        #version 410 core                                                  \n" + //
         "                                                                   \n" + //
         "out vec4 color;                                                    \n" + //
         "                                                                   \n" + //
         "in VS_OUT                                                          \n" + //
         "{                                                                  \n" + //
         "    vec4 color;                                                    \n" + //
         "} fs_in;                                                           \n" + //
         "                                                                   \n" + //
         "void main(void)                                                    \n" + //
         "{                                                                  \n" + //
         "    color = fs_in.color;                                           \n" + //
         "}                                                                  \n";

   public SpinningCube()
   {
      this.setupDisplay();
      this.startup();

      // this.init();
      this.render();

      this.shutdown();
   }

   private void render()
   {
      while (!Display.isCloseRequested())
      {
                        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

         FloatBuffer green = BufferUtils.createFloatBuffer(4);
         green.put(new float[] { 0.0f, 0.25f, 0.0f, 1.0f });
         green.flip();

         GL11.glClearDepth(1.0f);
         // GL11.glViewport(0, 0, WIDTH, HEIGHT);
         GL30.glClearBuffer(GL11.GL_COLOR, 0, green);
         GameUtils.exitOnGLError("set background colour");

         GL20.glUseProgram(programId);

         proj_matrix.store(matrix44Buffer);
         matrix44Buffer.flip();
         GL20.glUniformMatrix4(proj_location, false, matrix44Buffer);

         boolean manyCubes = true;
         if (manyCubes)
         {
            int i;
            for (i = 0; i < 24; i++)
            {
               long currentTime = System.currentTimeMillis() * 100;
               float f = (float) currentTime * 0.3f;

               Matrix4f.translate(new Vector3f(0.0f, 0.0f, -0.06f), mv_matrix, mv_matrix);

               // Matrix4f.rotate((float) currentTime * 0.00450f, new Vector3f(0.0f, 1.0f, 0.0f), mv_matrix, mv_matrix);
               // Matrix4f.rotate((float) currentTime * 0.00210f, new Vector3f(1.0f, 0.0f, 0.0f), mv_matrix, mv_matrix);

               Vector3f vector3f = new Vector3f((float) Math.sin(2.1 * f) * 2.0f, (float) Math.cos(1.7f * f) * 2.0f, (float) (Math.sin(1.3f * f) * Math.cos(1.5f * f) * 2.0f));
               // Matrix4f.translate(vector3f, mv_matrix, mv_matrix);

               mv_matrix.store(matrix44Buffer);
               matrix44Buffer.flip();
               GL20.glUniformMatrix4(mv_location, false, matrix44Buffer);
               GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 36);
            }
         }
         else
         {
            long currentTime = System.currentTimeMillis() * 100;
            float f = (float) currentTime * 0.3f;

            Matrix4f.translate(new Vector3f(0.0f, 0.0f, -0.04f), mv_matrix, mv_matrix);
            // mv_matrix.translate(new Vector3f(0.0f, 0.0f, -4.0f), mv_matrix);

            // Vector3f vector3f = new Vector3f((float) Math.sin(2.1 * f) * 0.05f, (float) Math.cos(1.7f * f) * 0.05f, (float) (Math.sin(1.3f * f) * Math.cos(1.5f * f) * 0.20f));
            // Matrix4f.translate(vector3f, mv_matrix, mv_matrix);

            // mv_matrix = Matrix4f.translate(0.0f, 0.0f, -4.0f) *
            // vmath::translate(sinf(2.1f * f) * 0.5f,
            // cosf(1.7f * f) * 0.5f,
            // sinf(1.3f * f) * cosf(1.5f * f) * 2.0f) *

            // Matrix4f.rotate((float) currentTime * 0.00450f, new Vector3f(0.0f, 1.0f, 0.0f), mv_matrix, mv_matrix);
            // Matrix4f.rotate((float) currentTime * 0.00810f, new Vector3f(1.0f, 0.0f, 0.0f), mv_matrix, mv_matrix);
            // vmath::rotate((float)currentTime * 45.0f, 0.0f, 1.0f, 0.0f) *
            // vmath::rotate((float)currentTime * 81.0f, 1.0f, 0.0f, 0.0f);

            mv_matrix.store(matrix44Buffer);
            matrix44Buffer.flip();
            GL20.glUniformMatrix4(mv_location, false, matrix44Buffer);
            GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 36);

            // System.out.println(mv_matrix);
         }

         // 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("Render loop failed");
      }
   }

   // vmath::perspective
   private Matrix4f perspective(float fieldOfView, float aspectRatio, float nearPlane, float farPlane)
   {
      Matrix4f projectionMatrix = new Matrix4f();

      float yScale = MathUtils.coTangent(MathUtils.degreesToRadians(fieldOfView / 2f));
      float xScale = yScale / aspectRatio;
      float frustumLength = farPlane - nearPlane;

      projectionMatrix.m00 = xScale;
      projectionMatrix.m11 = yScale;
      projectionMatrix.m22 = -((farPlane + nearPlane) / frustumLength);
      projectionMatrix.m23 = -1;
      projectionMatrix.m32 = -((2 * nearPlane * farPlane) / frustumLength);
      projectionMatrix.m33 = 0;

      return projectionMatrix;
   }

   private void startup()
   {
      programId = GL20.glCreateProgram();

      // Create and compile fragment shader
      fragmentShaderId = GL20.glCreateShader(GL20.GL_FRAGMENT_SHADER);
      GL20.glShaderSource(fragmentShaderId, fsSource);
      GL20.glCompileShader(fragmentShaderId);
      GameUtils.exitOnGLError("Unable to compile fragment shader:=" + fragmentShaderId);

      // Create and compile vertex shader
      vertexShaderId = GL20.glCreateShader(GL20.GL_VERTEX_SHADER);
      GL20.glShaderSource(vertexShaderId, vsSource);
      GL20.glCompileShader(vertexShaderId);
      GameUtils.exitOnGLError("Unable to compile vertex shader:=" + vertexShaderId);

      // Attach shaders and link
      GL20.glAttachShader(programId, vertexShaderId);
      GL20.glAttachShader(programId, fragmentShaderId);

      GL20.glLinkProgram(programId);

      mv_location = GL20.glGetUniformLocation(programId, "mv_matrix");
      System.out.println("modelviewId:=" + mv_location);
      proj_location = GL20.glGetUniformLocation(programId, "proj_matrix");
      System.out.println("projectionId:=" + proj_location);

      vaoId = GL30.glGenVertexArrays();
      GL30.glBindVertexArray(vaoId);

      float[] vertex_positions = new float[] {//
      -0.25f, 0.25f, -0.25f,//
            -0.25f, -0.25f, -0.25f,//
            0.25f, -0.25f, -0.25f,//
            //
            0.25f, -0.25f, -0.25f,//
            0.25f, 0.25f, -0.25f,//
            -0.25f, 0.25f, -0.25f,//
            //
            0.25f, -0.25f, -0.25f,//
            0.25f, -0.25f, 0.25f,//
            0.25f, 0.25f, -0.25f,//
            //
            0.25f, -0.25f, 0.25f,//
            0.25f, 0.25f, 0.25f,//
            0.25f, 0.25f, -0.25f,//
            //
            0.25f, -0.25f, 0.25f,//
            -0.25f, -0.25f, 0.25f,//
            0.25f, 0.25f, 0.25f,//
            //
            -0.25f, -0.25f, 0.25f,//
            -0.25f, 0.25f, 0.25f,//
            0.25f, 0.25f, 0.25f,//
            //
            -0.25f, -0.25f, 0.25f,//
            -0.25f, -0.25f, -0.25f,//
            -0.25f, 0.25f, 0.25f,//
            //
            -0.25f, -0.25f, -0.25f,//
            -0.25f, 0.25f, -0.25f,//
            -0.25f, 0.25f, 0.25f,//
            //
            -0.25f, -0.25f, 0.25f,//
            0.25f, -0.25f, 0.25f,//
            0.25f, -0.25f, -0.25f,//
            //
            0.25f, -0.25f, -0.25f,//
            -0.25f, -0.25f, -0.25f,//
            -0.25f, -0.25f, 0.25f,//
            //
            -0.25f, 0.25f, -0.25f,//
            0.25f, 0.25f, -0.25f,//
            0.25f, 0.25f, 0.25f,//
            //
            0.25f, 0.25f, 0.25f,//
            -0.25f, 0.25f, 0.25f,//
            -0.25f, 0.25f, -0.25f //
      };

      FloatBuffer floatBuffer = BufferUtils.createFloatBuffer(vertex_positions.length);
      floatBuffer.put(vertex_positions);
      floatBuffer.flip();

      buffer = GL15.glGenBuffers();
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, buffer);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, floatBuffer, GL15.GL_STATIC_DRAW);

      GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0);
      GL20.glEnableVertexAttribArray(0);

      GL11.glEnable(GL11.GL_CULL_FACE);
      GL11.glFrontFace(GL11.GL_CW);

      GL11.glEnable(GL11.GL_DEPTH_TEST);
      GL11.glDepthFunc(GL11.GL_LEQUAL);

      float aspectRatio = (float) WIDTH / (float) HEIGHT;
      proj_matrix = perspective(50.0f, aspectRatio, 0.1f, 1000.0f);
      mv_matrix = new Matrix4f();

      // Create a FloatBuffer with the proper size to store our matrices later
      matrix44Buffer = BufferUtils.createFloatBuffer(16);
      GameUtils.exitOnGLError("Start Up Failed");
   }

   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);
      GameUtils.exitOnGLError("Set up display");
   }

   private void shutdown()
   {
      GL30.glDeleteVertexArrays(vaoId);
      GL15.glDeleteBuffers(buffer);
      GL20.glDeleteProgram(programId);

      Display.destroy();
   }

   public static final void main(String[] args)
   {
      System.out.println("Spinning Cube");
      new SpinningCube();
   }

}





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.

Nickropheliac (15 views)
2014-08-31 22:59:12

TehJavaDev (23 views)
2014-08-28 18:26:30

CopyableCougar4 (32 views)
2014-08-22 19:31:30

atombrot (41 views)
2014-08-19 09:29:53

Tekkerue (40 views)
2014-08-16 06:45:27

Tekkerue (35 views)
2014-08-16 06:22:17

Tekkerue (25 views)
2014-08-16 06:20:21

Tekkerue (37 views)
2014-08-16 06:12:11

Rayexar (72 views)
2014-08-11 02:49:23

BurntPizza (49 views)
2014-08-09 21:09:32
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!