Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. #version 400 core
  2.  
  3. in vec2 pass_textureCoords
  4.  
  5. out vec4 out_Color;
  6.  
  7. uniform sampler2D textureSampler;
  8.  
  9. void main(void) {
  10.  
  11. out_Color = texture(textureSampler,pass_textureCoords);
  12.  
  13. }
  14.  
  15. #version 400 core
  16.  
  17. in vec2 pass_textureCoords
  18.  
  19. out vec4 out_Color;
  20.  
  21. uniform sampler2D textureSampler;
  22.  
  23. void main(void) {
  24.  
  25. out_Color = texture(textureSampler,pass_textureCoords);
  26.  
  27. }
  28.  
  29. package shaders;
  30.  
  31. import java.io.BufferedReader;
  32. import java.io.FileReader;
  33. import java.io.IOException;
  34.  
  35. import org.lwjgl.opengl.GL11;
  36. import org.lwjgl.opengl.GL20;
  37.  
  38. public abstract class ShaderProgram {
  39.  
  40. private int programID;
  41. private int vertexShaderID;
  42. private int fragmentShaderID;
  43.  
  44. public ShaderProgram(String vertexFile,String fragmentFile){
  45.  
  46. vertexShaderID = loadShader(vertexFile,GL20.GL_VERTEX_SHADER);
  47. fragmentShaderID = loadShader(fragmentFile,GL20.GL_FRAGMENT_SHADER);
  48. programID = GL20.glCreateProgram();
  49. GL20.glAttachShader(programID,vertexShaderID);
  50. GL20.glAttachShader(programID, fragmentShaderID);
  51. GL20.glLinkProgram(programID);
  52. GL20.glValidateProgram(programID);
  53. bindAttributes();
  54. }
  55.  
  56. public void start(){
  57. GL20.glUseProgram(programID);
  58. }
  59.  
  60. public void stop(){
  61. GL20.glUseProgram(0);
  62. }
  63.  
  64. public void cleanUp(){
  65. stop();
  66. GL20.glDetachShader(programID, vertexShaderID);
  67. GL20.glDetachShader(programID, fragmentShaderID);
  68. GL20.glDeleteShader(vertexShaderID);
  69. GL20.glDeleteShader(fragmentShaderID);
  70. GL20.glDeleteProgram(programID);
  71. }
  72.  
  73. protected abstract void bindAttributes();
  74.  
  75. protected void bindAttribute(int attribute, String variableName){
  76. GL20.glBindAttribLocation(programID,attribute,variableName);
  77. }
  78.  
  79.  
  80. private static int loadShader(String file, int type){
  81. StringBuilder shaderSource = new StringBuilder();
  82. try{
  83. BufferedReader reader = new BufferedReader(new FileReader(file));
  84. String line;
  85. while((line = reader.readLine())!=null){
  86. shaderSource.append(line).append("//n");
  87. }
  88. reader.close();
  89. }catch(IOException e){
  90. e.printStackTrace();
  91. System.exit(-1);
  92. }
  93. int shaderID = GL20.glCreateShader(type);
  94. GL20.glShaderSource(shaderID, shaderSource);
  95. GL20.glCompileShader(shaderID);
  96. if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS )== GL11.GL_FALSE){
  97. System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
  98. System.err.println("Could not compile shader!");
  99. System.exit(-1);
  100. }
  101. return shaderID;
  102. }
  103.  
  104. }
  105.  
  106. package shaders;
  107.  
  108. public class StaticShader extends ShaderProgram{
  109.  
  110. private static final String VERTEX_FILE = "src/shaders/vertexShader.txt";
  111. private static final String FRAGMENT_FILE = "src/shaders/fragmentShader.txt";
  112.  
  113. public StaticShader() {
  114. super(VERTEX_FILE, FRAGMENT_FILE);
  115. }
  116.  
  117. @Override
  118. protected void bindAttributes() {
  119.  
  120. super.bindAttribute(0, "position");
  121. super.bindAttribute(1, "textureCoords");
  122. }
  123.  
  124.  
  125.  
  126. }
  127.  
  128. package models;
  129.  
  130. import textures.ModelTexture;
  131.  
  132. public class TexturedModel {
  133.  
  134. private RawModel rawModel;
  135. private ModelTexture texture;
  136.  
  137. public TexturedModel(RawModel model, ModelTexture texture){
  138. this.rawModel = model;
  139. this.texture = texture;
  140. }
  141.  
  142. public RawModel getRawModel() {
  143. return rawModel;
  144. }
  145.  
  146. public ModelTexture getTexture() {
  147. return texture;
  148. }
  149.  
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement