Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
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: BlackHamm3rJack (posted 2014-06-20 13:02:06, viewed 43 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  
package org.altervista.blackhamm3rjack.learning_opengl;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
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;

public class Main {
   public static final int WIDTH = 500;
   public static final int HEIGHT = 500;

   public static final String TITLE = "Learning OpenGL";

   public static final boolean FULLSCREEN = false;
   public static final boolean RESIZABLE = false;
   public static final boolean VSYNC = false;

   public static final int TARGET_FPS = 60;

   private int program;

   private int vertexBuffer;
   private int vertexArray;

   private float[] vertexPositions = new float[] { 0.75f, 0.75f, 0.0f, 1.0f,
         0.75f, -0.75f, 0.0f, 1.0f, -0.75f, -0.75f, 0.0f, 1.0f };

   private String vertexShader = "#version 330\n"
         + "layout(location = 0) in vec4 position;\n" + "void main()\n"
         + "{\n" + "   gl_Position = position;\n" + "}\n";
   private String fragmentShader = "#version 330\n"
         + "out vec4 outputColor;\n" + "void main()\n" + "{\n"
         + "   outputColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);\n" + "}\n";

   private int createShader(int type, String code) {
      int shader;

      shader = GL20.glCreateShader(type);

      GL20.glShaderSource(shader, code);
      GL20.glCompileShader(shader);

      if (GL20.glGetShaderi(shader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) {
         System.out.println("Failed to compile a shader.");
         System.exit(1);
      }

      return shader;
   }

   private int createShaderProgram(int[] shaders) {
      int program;

      program = GL20.glCreateProgram();

      for (int shader : shaders)
         GL20.glAttachShader(program, shader);

      GL20.glLinkProgram(program);

      if (GL20.glGetProgrami(program, GL20.GL_LINK_STATUS) == GL11.GL_FALSE) {
         System.out.println("Failed to link a program.");
         System.exit(1);
      }

      for (int shader : shaders)
         GL20.glDetachShader(program, shader);

      return program;
   }

   /*
    * Initialize the vertex buffer, that reads the vertex positions from the
    * compiled shaders
    */
   public void initializeBuffer() {
      vertexBuffer = GL15.glGenBuffers();

      FloatBuffer vertexPositionsBuffer = BufferUtils
            .createFloatBuffer(vertexPositions.length);
      vertexPositionsBuffer.put(vertexPositions);

      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
      GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexPositionsBuffer,
            GL15.GL_STATIC_DRAW);
      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
   }

   /*
    * Initialize the program by compiling shaders and linking the shader
    * program
    */
   public void initializeProgram() {
      int[] shaderList = new int[] {
            createShader(GL20.GL_VERTEX_SHADER, vertexShader),
            createShader(GL20.GL_FRAGMENT_SHADER, fragmentShader) };

      program = createShaderProgram(shaderList);

      vertexArray = GL30.glGenVertexArrays();
      GL30.glBindVertexArray(vertexArray);
   }

   /*
    * Display the triangle in the window
    */
   public void displayTriangle() {
      GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);

      GL20.glUseProgram(program);

      GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vertexBuffer);
      GL20.glEnableVertexAttribArray(0);
      GL20.glVertexAttribPointer(0, 4, GL11.GL_FLOAT, false, 0, 0);

      GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, 3);

      GL20.glDisableVertexAttribArray(0);
      GL20.glUseProgram(0);
   }

   public Main() {
      try {
         Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
         Display.setTitle(TITLE);
         Display.setFullscreen(FULLSCREEN);
         Display.setResizable(RESIZABLE);
         Display.setVSyncEnabled(VSYNC);

         Display.create();
      } catch (LWJGLException e) {
         System.out.println("Display initialization error.");
         System.exit(0);
      }

      while (!Display.isCloseRequested()) {

         Display.update();
         Display.sync(TARGET_FPS);
      }
   }

   public static void main(String[] args) {
      new Main();
   }
}





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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (34 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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!