Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (533)
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 17:32:07, viewed 26 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  
package org.altervista.blackhamm3rjack.opengl;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;

public class Triangle extends Window {
   public static void main(String[] args) {
      new Triangle().start(640, 480);
   }

   private int pbo;
   private int vao;

   private int p;
   private final float[] v = { 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 final String vertexShader = "#version 330\n"
         + "layout(location = 0) in vec4 position;\n" + "void main()\n"
         + "{\n" + "   gl_Position = position;\n" + "}\n";
   private final 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";

   @Override
   protected void init() {
      initializeProgram();
      initializeVertexBuffer();

      vao = glGenVertexArrays();
      glBindVertexArray(vao);
   }

   private void initializeProgram() {
      int[] shaderList = new int[] {
            createShader(GL_VERTEX_SHADER, vertexShader),
            createShader(GL_FRAGMENT_SHADER, fragmentShader) };

      p = createProgram(shaderList);

      for (int shader : shaderList)
         glDeleteShader(shader);
   }

   private void initializeVertexBuffer() {
      FloatBuffer vpo = BufferUtils.createFloatBuffer(v.length);

      vpo.put(v);
      vpo.flip();

      pbo = glGenBuffers();

      glBindBuffer(GL_ARRAY_BUFFER, pbo);
      glBufferData(GL_ARRAY_BUFFER, vpo, GL_STATIC_DRAW);
      glBindBuffer(GL_ARRAY_BUFFER, 0);
   }

   @Override
   protected void display() {
      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
      glClear(GL_COLOR_BUFFER_BIT);

      glUseProgram(p);

      glBindBuffer(GL_ARRAY_BUFFER, pbo);
      glEnableVertexAttribArray(0);
      glVertexAttribPointer(0, 4, GL_FLOAT, false, 0, 0);

      glDrawArrays(GL_TRIANGLES, 0, 3);

      glDisableVertexAttribArray(0);
      glUseProgram(0);
   }

   private int createShader(int type, String source) {
      int shader = glCreateShader(type);

      glShaderSource(shader, source);
      glCompileShader(shader);

      if (glGetShaderi(shader, GL_COMPILE_STATUS) == GL_FALSE) {
         System.out.println("Can't compile a shader.");
         System.exit(1);
      }

      return shader;
   }

   private int createProgram(int[] shaderList) {
      int program = glCreateProgram();

      for (int shader : shaderList)
         glAttachShader(program, shader);
      glLinkProgram(program);

      if (glGetProgrami(program, GL_LINK_STATUS) == GL_FALSE) {
         System.out.println("Can't link a program.");
         System.exit(1);
      }

      for (int shader : shaderList)
         glDetachShader(program, shader);

      return program;
   }
}





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.

pw (22 views)
2014-07-24 01:59:36

Riven (20 views)
2014-07-23 21:16:32

Riven (17 views)
2014-07-23 21:07:15

Riven (20 views)
2014-07-23 20:56:16

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

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

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

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

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

Riven (52 views)
2014-07-14 18:02:53
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!