Advertisement
Guest User

openGL Introduction

a guest
Jun 3rd, 2014
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This code has been "de-Java-fyed", though if readability suffers too much, you can ask me to simply reupload the whole java class
  2.  
  3. int error;
  4.  
  5. print("Load Shader:");
  6.  
  7. /* Request two Shader IDs, one typed with GL_VERTEX_SHADER, one with GL_FRAGMENT_SHADER
  8.  * The vertex shader calculates the position of each vertex onscreen, the fragment shader, or pixel shader,
  9.  * calculates the color of each pixel
  10.  */
  11.  
  12. vertexShaderID = loadShader("res/shader/vs.glsl" , GL_VERTEX_SHADER);
  13. fragmentShaderID = loadShader("res/shader/fs.glsl", GL_FRAGMENT_SHADER);
  14.  
  15. /* Request a ProgramID that links the vertex and fragment shader together */
  16.        
  17. spriteProgramID = glCreateProgram();
  18.  
  19. /* Attach the shaders to the program */
  20.  
  21. glAttachShader(spriteProgramID, spriteVertexShaderID);
  22. glAttachShader(spriteProgramID, spriteHueFragmentShaderID);
  23.  
  24. /* Compile/Link and validate the program */
  25.        
  26. glLinkProgram(this.spriteProgramID);
  27. glValidateProgram(this.spriteProgramID);
  28.  
  29. if((error = glGetError()) != 0){
  30.     print("Error after validation: " + error);
  31. } else {
  32.     print("Validation completed without errors");
  33. }
  34.  
  35. /* Create two buffers, one for character, one for integers: after calling glGetProgramInfoLog, within the character buffer,
  36.  * you will find the text of the error message, within the integer buffer, you will find the length of each line of the error message
  37.  * The allocation functions depend on your programming language - either way, the glGetProgramInfoLog requests pointers
  38.  */    
  39. cbuffer = allocateByteBuffer(512);
  40. ibuffer = allocateIntBuffer(16);
  41.  
  42. glGetProgramInfoLog(spriteProgramID, 512, ibuffer, cbuffer);
  43.  
  44. /* Write the error message on screen */
  45.  
  46. int c = 0;
  47. boolean written = false;
  48. for(int i = 0 ; i < 16 ; i++){
  49.     for(int j = ibuffer[i] ; j > 0 ; j--){
  50.         char l = (char) cbuffer[c];
  51.         c++;
  52.         print(l);
  53.         written = true;
  54.     }
  55.     if(written){
  56.         newLine(); 
  57.         written = false;
  58.     }
  59. }
  60.  
  61. /* Get the validation status of the program - the integer representing that will be written into an integer buffer (again, the function
  62.  * requests a pointer) */
  63.  
  64. glGetProgramiv(spriteProgramID, GL_VALIDATE_STATUS, ID_BUFFER);
  65.  
  66. error = ID_BUFFER[0];
  67.  
  68. if(error == 0){
  69.     print("Validation: Error");
  70. } else if(error == 1){
  71.     print("Validation: OK!");
  72. } else {
  73.     print("Validation: Unknown Error");
  74. }
  75.  
  76. /* Now that our program is usable, we bind it to our OpenGL context */
  77.  
  78. glUseProgram(spriteProgramID);
  79.  
  80. /* If your program uses Uniform variables, we can request a pointer ID (also called "location") from the program, that links to the
  81.  * value of the variable */
  82.  
  83. uniformLoc = glGetUniformLocation(spriteProgramID, "uniformVariableName");
  84.        
  85. if((error = glGetError()) != 0){
  86.     print("Error after getting Uniform Locations: "+ error);
  87. } else {
  88.     print("Uniform Locations received");
  89. }
  90.  
  91. /* To get the pointer to an input variable, we use glGetAttribLocation to get a pointer ID to that parameter */
  92.  
  93. attributeLoc = glGetAttribLocation(spriteProgramID, "attributeVariableName");
  94.  
  95. if((error = glGetError()) != 0){
  96.     print("Error after getting attribute locations: "+ error);
  97. } else {
  98.     print("Attribute Locations received");
  99. }
  100.  
  101. /* I have actually forgotten why I do this */
  102.  
  103. glBindAttribLocation(spriteProgramID, 0, "attributeVariableName");
  104.  
  105. print("Created Program");
  106.        
  107. /* Now we set up the vertices of our object, in this example: a rectangle */
  108.  
  109. float[] vertices = {
  110.         0f, 0f,
  111.         0f, 1f,
  112.         1f, 1f,
  113.         1f, 0f,
  114.         };
  115.  
  116. /* OpenGL wants it's data input in reverse, so we need to flip the buffer after it is initialzed */
  117.  
  118. vBuffer = allocateFloats(vertices.length);
  119. vBuffer.put(vertices);
  120. vBuffer.flip();
  121.        
  122. /* Now we create another buffer that contains the information in what order we want to use the given vertices to draw triangles - a
  123.  * rectangle contains two triangles, so we set up this index buffer*/
  124.  
  125. byte[] indices = {
  126.         0, 1, 2
  127.         2, 3, 0
  128.         };
  129.        
  130. iBuffer = allocateByteBuffer(indices.length);
  131. iBuffer.put(indices);
  132. iBuffer.flip();
  133.  
  134. /* Now we request a VertexArrayObject and as always, we get an ID*/
  135.  
  136. vaoID = createVertexArray(gl);
  137. print("New VAO: " + vaoID);
  138.  
  139. /* Into this VertexArrayObject we want to put two VertexBufferObjects, one for the vertices, one for the indices */
  140.  
  141. vboID = createBufferObject();
  142. System.out.println("New VBO: " + vboID);
  143. vboiID = createBufferObject();
  144. System.out.println("New VBO: " + vboiID);
  145.  
  146. if((error = glGetError()) != 0){
  147.     print("Error after setting up VAO or VBA: "+ error);
  148. } else {
  149.     print("VBA and VBOs created");
  150. }
  151.  
  152. /* Bind VAO */
  153. glBindVertexArray(this.vaoID);
  154. /* Bind VBO - this adds the VBO to the VAO */
  155. glBindBuffer(GL_ARRAY_BUFFER, this.vboID);
  156. /* Enable linking the just bound VBO to the input of a shader - that means whatever is within this buffer is used as an input value
  157.  * to the shader program */
  158. glEnableVertexAttribArray(attributeLoc);
  159. /* glVertexAttribPointer(idOfVariable, size of each element, type of each element, normalizing, stride, offset*/
  160. glVertexAttribPointer(attributeLoc, 2, GL_FLOAT, false, 0, 0L);
  161. /* Buffer Data into the Buffer - glBufferData(whereToBufferTo, lengthOfDataTypeInBytes, pointerToBuffer,
  162.  * whenToUse) (1 Float = 4 Bytes)
  163.  */
  164. glBufferData(GL_ARRAY_BUFFER, vertices.length * 4, vBuffer, GL_STATIC_DRAW);
  165. /* Bind EBO - this adds the EBO to the VAO (The EBO is the Index Buffer)*/
  166. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboiID);
  167. /* Buffer Data into the Buffer */
  168. glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.length, iBuffer, GL_STATIC_DRAW);
  169. /* UnBind for safety reasons ... */
  170. glBindVertexArray(0);
  171.  
  172. if((error = glGetError()) != 0){
  173.     print("Error after setting up the VAO: "+ error);
  174. } else {
  175.     print("VAO successfully set up");
  176. }
  177.  
  178. print("Renderer initialized!");
  179.  
  180. /* Subroutine to load a shader */
  181. int loadShader(String file, int shaderType){
  182.         /* depending on your programming language, simply read out the source code of your .glsl file and save it within a string */
  183.     string = readFile(file);
  184.  
  185.     shaderID = glCreateShader(shaderType);
  186.     print("New Shader ID: "+ shaderID);
  187.         /* attach the source code to the shader - it strangely needs a string array, though each line of a shader's code is simply
  188.          * seperated with \n within the string itself
  189.          */
  190.     glShaderSource(shaderID, 1, new string[] {string}, null);
  191.     glCompileShader(shaderID);
  192.    
  193.     cbuffer = allocateCharacterBuffer(512);
  194.     ibuffer = allocateIntegerBuffer(16);
  195.     glGetShaderInfoLog(shaderID, 512, ibuffer, cbuffer);
  196.     int c = 0;
  197.     boolean written = false;
  198.     for(int i = 0 ; i < 16 ; i++){
  199.         for(int j = ibuffer[i] ; j > 0 ; j--){
  200.             char l = cbuffer[c];
  201.             c++;
  202.             print(l);
  203.             written = true;
  204.         }
  205.         if(written){
  206.             newLine(); 
  207.             written = false;
  208.         }
  209.     }  
  210.     return shaderID;
  211. }
  212.    
  213.     private int createVertexArray(){
  214.         glGenVertexArrays(1, ID_BUFFER);
  215.         return ID_BUFFER[0];
  216.     }
  217.    
  218.     private int createBufferObject(GL2 gl){
  219.         glGenBuffers(1, ID_BUFFER);
  220.         return ID_BUFFER[0];
  221.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement