Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This code has been "de-Java-fyed", though if readability suffers too much, you can ask me to simply reupload the whole java class
- int error;
- print("Load Shader:");
- /* Request two Shader IDs, one typed with GL_VERTEX_SHADER, one with GL_FRAGMENT_SHADER
- * The vertex shader calculates the position of each vertex onscreen, the fragment shader, or pixel shader,
- * calculates the color of each pixel
- */
- vertexShaderID = loadShader("res/shader/vs.glsl" , GL_VERTEX_SHADER);
- fragmentShaderID = loadShader("res/shader/fs.glsl", GL_FRAGMENT_SHADER);
- /* Request a ProgramID that links the vertex and fragment shader together */
- spriteProgramID = glCreateProgram();
- /* Attach the shaders to the program */
- glAttachShader(spriteProgramID, spriteVertexShaderID);
- glAttachShader(spriteProgramID, spriteHueFragmentShaderID);
- /* Compile/Link and validate the program */
- glLinkProgram(this.spriteProgramID);
- glValidateProgram(this.spriteProgramID);
- if((error = glGetError()) != 0){
- print("Error after validation: " + error);
- } else {
- print("Validation completed without errors");
- }
- /* Create two buffers, one for character, one for integers: after calling glGetProgramInfoLog, within the character buffer,
- * 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
- * The allocation functions depend on your programming language - either way, the glGetProgramInfoLog requests pointers
- */
- cbuffer = allocateByteBuffer(512);
- ibuffer = allocateIntBuffer(16);
- glGetProgramInfoLog(spriteProgramID, 512, ibuffer, cbuffer);
- /* Write the error message on screen */
- int c = 0;
- boolean written = false;
- for(int i = 0 ; i < 16 ; i++){
- for(int j = ibuffer[i] ; j > 0 ; j--){
- char l = (char) cbuffer[c];
- c++;
- print(l);
- written = true;
- }
- if(written){
- newLine();
- written = false;
- }
- }
- /* Get the validation status of the program - the integer representing that will be written into an integer buffer (again, the function
- * requests a pointer) */
- glGetProgramiv(spriteProgramID, GL_VALIDATE_STATUS, ID_BUFFER);
- error = ID_BUFFER[0];
- if(error == 0){
- print("Validation: Error");
- } else if(error == 1){
- print("Validation: OK!");
- } else {
- print("Validation: Unknown Error");
- }
- /* Now that our program is usable, we bind it to our OpenGL context */
- glUseProgram(spriteProgramID);
- /* If your program uses Uniform variables, we can request a pointer ID (also called "location") from the program, that links to the
- * value of the variable */
- uniformLoc = glGetUniformLocation(spriteProgramID, "uniformVariableName");
- if((error = glGetError()) != 0){
- print("Error after getting Uniform Locations: "+ error);
- } else {
- print("Uniform Locations received");
- }
- /* To get the pointer to an input variable, we use glGetAttribLocation to get a pointer ID to that parameter */
- attributeLoc = glGetAttribLocation(spriteProgramID, "attributeVariableName");
- if((error = glGetError()) != 0){
- print("Error after getting attribute locations: "+ error);
- } else {
- print("Attribute Locations received");
- }
- /* I have actually forgotten why I do this */
- glBindAttribLocation(spriteProgramID, 0, "attributeVariableName");
- print("Created Program");
- /* Now we set up the vertices of our object, in this example: a rectangle */
- float[] vertices = {
- 0f, 0f,
- 0f, 1f,
- 1f, 1f,
- 1f, 0f,
- };
- /* OpenGL wants it's data input in reverse, so we need to flip the buffer after it is initialzed */
- vBuffer = allocateFloats(vertices.length);
- vBuffer.put(vertices);
- vBuffer.flip();
- /* Now we create another buffer that contains the information in what order we want to use the given vertices to draw triangles - a
- * rectangle contains two triangles, so we set up this index buffer*/
- byte[] indices = {
- 0, 1, 2
- 2, 3, 0
- };
- iBuffer = allocateByteBuffer(indices.length);
- iBuffer.put(indices);
- iBuffer.flip();
- /* Now we request a VertexArrayObject and as always, we get an ID*/
- vaoID = createVertexArray(gl);
- print("New VAO: " + vaoID);
- /* Into this VertexArrayObject we want to put two VertexBufferObjects, one for the vertices, one for the indices */
- vboID = createBufferObject();
- System.out.println("New VBO: " + vboID);
- vboiID = createBufferObject();
- System.out.println("New VBO: " + vboiID);
- if((error = glGetError()) != 0){
- print("Error after setting up VAO or VBA: "+ error);
- } else {
- print("VBA and VBOs created");
- }
- /* Bind VAO */
- glBindVertexArray(this.vaoID);
- /* Bind VBO - this adds the VBO to the VAO */
- glBindBuffer(GL_ARRAY_BUFFER, this.vboID);
- /* 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
- * to the shader program */
- glEnableVertexAttribArray(attributeLoc);
- /* glVertexAttribPointer(idOfVariable, size of each element, type of each element, normalizing, stride, offset*/
- glVertexAttribPointer(attributeLoc, 2, GL_FLOAT, false, 0, 0L);
- /* Buffer Data into the Buffer - glBufferData(whereToBufferTo, lengthOfDataTypeInBytes, pointerToBuffer,
- * whenToUse) (1 Float = 4 Bytes)
- */
- glBufferData(GL_ARRAY_BUFFER, vertices.length * 4, vBuffer, GL_STATIC_DRAW);
- /* Bind EBO - this adds the EBO to the VAO (The EBO is the Index Buffer)*/
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboiID);
- /* Buffer Data into the Buffer */
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.length, iBuffer, GL_STATIC_DRAW);
- /* UnBind for safety reasons ... */
- glBindVertexArray(0);
- if((error = glGetError()) != 0){
- print("Error after setting up the VAO: "+ error);
- } else {
- print("VAO successfully set up");
- }
- print("Renderer initialized!");
- /* Subroutine to load a shader */
- int loadShader(String file, int shaderType){
- /* depending on your programming language, simply read out the source code of your .glsl file and save it within a string */
- string = readFile(file);
- shaderID = glCreateShader(shaderType);
- print("New Shader ID: "+ shaderID);
- /* attach the source code to the shader - it strangely needs a string array, though each line of a shader's code is simply
- * seperated with \n within the string itself
- */
- glShaderSource(shaderID, 1, new string[] {string}, null);
- glCompileShader(shaderID);
- cbuffer = allocateCharacterBuffer(512);
- ibuffer = allocateIntegerBuffer(16);
- glGetShaderInfoLog(shaderID, 512, ibuffer, cbuffer);
- int c = 0;
- boolean written = false;
- for(int i = 0 ; i < 16 ; i++){
- for(int j = ibuffer[i] ; j > 0 ; j--){
- char l = cbuffer[c];
- c++;
- print(l);
- written = true;
- }
- if(written){
- newLine();
- written = false;
- }
- }
- return shaderID;
- }
- private int createVertexArray(){
- glGenVertexArrays(1, ID_BUFFER);
- return ID_BUFFER[0];
- }
- private int createBufferObject(GL2 gl){
- glGenBuffers(1, ID_BUFFER);
- return ID_BUFFER[0];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement