Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (475)
Games in Android Showcase (105)
games submitted by our members
Games in WIP (524)
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: BrassApparatus (posted 2013-06-27 22:27:39, viewed 142 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  
package com.brassapparatus.dust.utils;

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL32.*;

import java.util.Map;

/* Copyright (c) 2012-2013, Roi Atalla
 * All rights reserved.
 */

public class ShaderProgram {
        private int program;
        
        public ShaderProgram(String vertexShader, String fragmentShader) {
                this(vertexShader, null, fragmentShader, null);
        }
        
        public ShaderProgram(String vertexShader, String fragmentShader, Map<Integer,String> attributes) {
                this(vertexShader, null, fragmentShader, attributes);
        }
        
        public ShaderProgram(String vertexShader, String geometryShader, String fragmentShader) {
                this(vertexShader, geometryShader, fragmentShader, null);
        }
        
        public ShaderProgram(String vertexShader, String geometryShader, String fragmentShader, Map<Integer,String> attributes) {
                int vs = compileShader(vertexShader, GL_VERTEX_SHADER);
                int gs = compileShader(geometryShader, GL_GEOMETRY_SHADER);
                int fs = compileShader(fragmentShader, GL_FRAGMENT_SHADER);
                
                program = glCreateProgram();
                glAttachShader(program, vs);
                if(gs != -1)
                        glAttachShader(program, gs);
                glAttachShader(program, fs);
                
                if(attributes != null)
                        for(int i : attributes.keySet())
                                glBindAttribLocation(program, i, attributes.get(i));
                
                glLinkProgram(program);
                
                String infoLog = glGetProgramInfoLog(program, glGetProgrami(program, GL_INFO_LOG_LENGTH));
                
                if(glGetProgrami(program, GL_LINK_STATUS) == GL_FALSE)
                        throw new RuntimeException("Failure in linking program. Error log:\n" + infoLog);
                else {
                        System.out.print("Linking program successful.");
                        if(infoLog != null && !(infoLog = infoLog.trim()).isEmpty())
                                System.out.println(" Log:\n" + infoLog);
                        else
                                System.out.println();
                }
                
                glDetachShader(program, vs);
                if(gs != -1)
                        glDetachShader(program, gs);
                glDetachShader(program, fs);
                
                glDeleteShader(vs);
                if(gs != -1)
                        glDeleteShader(gs);
                glDeleteShader(fs);
        }
        
        private int compileShader(String source, int type) {
                if(source == null)
                        return -1;
                
                int shader = glCreateShader(type);
                glShaderSource(shader, source);
                
                glCompileShader(shader);
                
                String infoLog = glGetShaderInfoLog(shader, glGetShaderi(shader, GL_INFO_LOG_LENGTH));
                
                if(glGetShaderi(shader, GL_COMPILE_STATUS) == GL_FALSE)
                        throw new RuntimeException("Failure in compiling " + getName(type) + " shader. Error log:\n" + infoLog);
                else {
                        System.out.print("Compiling " + getName(type) + " shader successful.");
                        if(infoLog != null && !(infoLog = infoLog.trim()).isEmpty())
                                System.out.println(" Log:\n" + infoLog);
                        else
                                System.out.println();
                }
                
                return shader;
        }
        
        private String getName(int shaderType) {
                if(shaderType == GL_VERTEX_SHADER)
                        return "vertex";
                if(shaderType == GL_GEOMETRY_SHADER)
                        return "geometry";
                if(shaderType == GL_FRAGMENT_SHADER)
                        return "fragment";
                
                throw new IllegalArgumentException("Invalid shaderType, must be either GL_VERTEX_SHADER, GL_GEOMETRY_SHADER, or GL_FRAGMENT_SHADER");
        }
        
        public int getProgram() {
                return program;
        }
        
        public void begin() {
                glUseProgram(program);
        }
        
        public void end() {
                glUseProgram(0);
        }
        
        public void destroy() {
                glDeleteProgram(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.

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

SHC (35 views)
2014-07-12 17:50:04

Riven (32 views)
2014-07-10 20:20:18

CopyableCougar4 (32 views)
2014-07-10 02:26:14

CopyableCougar4 (34 views)
2014-07-09 02:55:38

Code Mage (34 views)
2014-07-08 23:57:00

Code Mage (23 views)
2014-07-08 23:49:08

AppleSauce (28 views)
2014-07-08 19:25:32

CopyableCougar4 (30 views)
2014-07-06 01:51:26

ipe369 (36 views)
2014-07-05 14:18:25
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!