Java-Gaming.org Hi !
Featured games (87)
games approved by the League of Dukes
Games in Showcase (649)
Games in Android Showcase (181)
games submitted by our members
Games in WIP (700)
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 210 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
 
KaiHH (150 views)
2016-01-31 23:15:29

sci4me (155 views)
2016-01-23 21:47:05

sci4me (139 views)
2016-01-23 21:46:58

KaiHH (173 views)
2016-01-19 13:26:42

theagentd (258 views)
2016-01-05 17:10:00

ClaasJG (272 views)
2016-01-03 16:58:36

chrisdalke (263 views)
2015-12-28 06:31:21

Guerra2442 (270 views)
2015-12-25 03:42:55

Guerra2442 (271 views)
2015-12-25 03:27:21

theagentd (297 views)
2015-12-21 14:43:24
List of Learning Resources
by SilverTiger
2016-02-05 09:39:47

List of Learning Resources
by SilverTiger
2016-02-05 09:38:38

List of Learning Resources
by SilverTiger
2016-02-05 09:35:50

Rendering resources
by Roquen
2015-11-13 14:37:59

Rendering resources
by Roquen
2015-11-13 14:36:58

Math: Resources
by Roquen
2015-10-22 07:46:10

Networking Resources
by Roquen
2015-10-16 07:12:30

Rendering resources
by Roquen
2015-10-15 07:40:48
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!