Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void drawSpriteTransparent(Sprite sprite, int x, int y, float transparency) {
- Texture t = sprite.getTexture();
- double tx = sprite.getTexutreX();
- double ty = sprite.getTextureY();
- double tw = sprite.getWidthInTexture();
- double th = sprite.getHeightInTexture();
- // System.out.println(vboiID);
- // System.out.println(vaoID);
- gl.glBindTexture(GL2.GL_TEXTURE_2D, t.getTextureID());
- gl.glUseProgram(spriteProgramID);
- gl.glUniform2i(this.positionLoc, x, y); // Update X and Y position of the Sprite's Location
- gl.glUniform2i(this.sizeLoc, sprite.getWidth(), sprite.getHeight()); // Update Width and Height information of the Sprite's Dimension
- gl.glUniform4f(this.colorLoc, 1.0f, 1.0f, 1.0f, 0.0f); // Setup the tint color (currently ignored)
- gl.glUniform2f(this.texCoordLoc, (float) tx, (float) ty); // Update the X and Y of the Sprite within it's texture
- gl.glBindVertexArray(this.vaoID);
- gl.glEnableVertexAttribArray(0);
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, this.vboiID);
- gl.glDrawElements(GL2.GL_TRIANGLES, 6, GL2.GL_UNSIGNED_BYTE, 0);
- // gl.glColor4f(1f, 0f, 0f, 1f - transparency);
- // gl.glBegin(GL2.GL_QUADS);
- // {
- // gl.glTexCoord2d(tx, ty);
- // gl.glVertex3d(x, y, 0);
- // gl.glTexCoord2d(tx, ty + th);
- // gl.glVertex3d(x, y + sprite.getHeight(), 0);
- // gl.glTexCoord2d(tx + tw, ty + th);
- // gl.glVertex3d(x + sprite.getWidth(), y + sprite.getHeight(), 0);
- // gl.glTexCoord2d(tx + tw, ty);
- // gl.glVertex3d(x + sprite.getWidth(), y, 0);
- // }
- // gl.glEnd();
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
- gl.glDisableVertexAttribArray(0);
- gl.glBindVertexArray(0);
- gl.glBindTexture(GL2.GL_TEXTURE_2D, 0);
- gl.glUseProgram(0);
- }
- void init(GL2 gl){
- this.gl = gl;
- /* Load Shader */
- System.out.println("Load Shader");
- char sep = File.separatorChar;
- this.spriteVertexShaderID = loadShader("res" + sep + "shader" + sep + "sprite_vs.glsl" , GL2.GL_VERTEX_SHADER, gl);
- this.spriteHueFragmentShaderID = loadShader("res" + sep + "shader" + sep + "sprite_hue_fs.glsl", GL2.GL_FRAGMENT_SHADER, gl);
- this.spriteProgramID = gl.glCreateProgram();
- gl.glAttachShader(this.spriteProgramID, this.spriteVertexShaderID);
- gl.glAttachShader(this.spriteProgramID, this.spriteHueFragmentShaderID);
- gl.glLinkProgram(this.spriteProgramID);
- gl.glValidateProgram(this.spriteProgramID);
- System.out.println("Error after validation: "+gl.glGetError());
- ByteBuffer cbuffer = ByteBuffer.allocate(512);
- IntBuffer ibuffer = IntBuffer.allocate(16);
- gl.glGetProgramInfoLog(this.spriteProgramID, 512, ibuffer, cbuffer);
- int c = 0;
- boolean written = false;
- for(int i = 0 ; i < 16 ; i++){
- for(int j = ibuffer.get(i) ; j > 0 ; j--){
- char l = (char) cbuffer.get(c);
- c++;
- System.out.print(l);
- written = true;
- }
- if(written){
- System.out.println();
- written = false;
- }
- }
- gl.glGetProgramiv(this.spriteProgramID, GL2.GL_VALIDATE_STATUS, VBO_VAO_ID_BUFFER);
- System.out.println("Validate Status: " + VBO_VAO_ID_BUFFER.get(0));
- gl.glUseProgram(this.spriteProgramID);
- // System.out.println("Error: "+ gl.glGetError());
- this.positionLoc = gl.glGetUniformLocation(spriteProgramID, "position");
- this.sizeLoc = gl.glGetUniformLocation(spriteProgramID, "size");
- this.colorLoc = gl.glGetUniformLocation(spriteProgramID, "color");
- this.texCoordLoc = gl.glGetUniformLocation(spriteProgramID, "texture_coordinate");
- System.out.println("Error after getting Uniform Locations: "+ gl.glGetError());
- int vertexLoc = gl.glGetAttribLocation(this.spriteProgramID, "vertex");
- System.out.println("Error after getting attrib location: "+ gl.glGetError());
- gl.glBindAttribLocation(this.spriteProgramID, 0, "vertex");
- System.out.println("Created Program");
- this.vaoID = createVertexArray(gl);
- System.out.println("New VAO: " + this.vaoID);
- this.vboID = createBufferObject(gl);
- System.out.println("New VBO: " + this.vboID);
- float[] vertices = {
- 0f, 0f,
- 0f, 1f,
- 1f, 1f,
- 1f, 0f,
- };
- // FloatBuffer vBuffer = ByteBuffer.allocate(vertices.length << 2).order(ByteOrder.nativeOrder()).asFloatBuffer();
- FloatBuffer vBuffer = FloatBuffer.allocate(vertices.length);
- vBuffer.put(vertices);
- vBuffer.flip();
- byte[] indices = {
- 0, 1, 2,
- 2, 3, 0
- };
- ByteBuffer iBuffer = ByteBuffer.allocate(indices.length).order(ByteOrder.nativeOrder());
- iBuffer.put(indices);
- iBuffer.flip();
- gl.glBindVertexArray(this.vaoID);
- gl.glEnableVertexAttribArray(vertexLoc);
- gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, this.vboID);
- gl.glBufferData(GL2.GL_ARRAY_BUFFER, vertices.length * 4, vBuffer, GL2.GL_STATIC_DRAW);
- gl.glVertexAttribPointer(vertexLoc, 2, GL2.GL_FLOAT, false, 0, 0L);
- gl.glBindVertexArray(0);
- System.out.println("Error after setting up Vertex Buffer Array: "+ gl.glGetError());
- this.vboiID = createBufferObject(gl);
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, vboiID);
- gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, indices.length, iBuffer, GL2.GL_STATIC_DRAW);
- gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, 0);
- System.out.println("Error after setting up Vertes Buffer Object for Indices: "+ gl.glGetError());
- System.out.println("Renderer initialized!");
- }
- private int loadShader(String file, int shaderType, GL2 gl){
- String string = "";
- StringBuilder builder = new StringBuilder();
- int shaderID = 0;
- try {
- BufferedReader reader = new BufferedReader(new FileReader(SpritesUtility.loadFile(file)));
- String line;
- while((line = reader.readLine()) != null){
- // System.out.println(line);
- string += line + "\n";
- }
- } catch (IOException e){
- e.printStackTrace();
- System.exit(-1);
- }
- shaderID = gl.glCreateShader(shaderType);
- System.out.println("New Shader ID: "+ shaderID);
- gl.glShaderSource(shaderID, 1, new String[] {string}, null);
- gl.glCompileShader(shaderID);
- ByteBuffer cbuffer = ByteBuffer.allocate(512);
- IntBuffer ibuffer = IntBuffer.allocate(16);
- gl.glGetShaderInfoLog(shaderID, 512, ibuffer, cbuffer);
- int c = 0;
- boolean written = false;
- for(int i = 0 ; i < 16 ; i++){
- for(int j = ibuffer.get(i) ; j > 0 ; j--){
- char l = (char) cbuffer.get(c);
- c++;
- System.out.print(l);
- written = true;
- }
- if(written){
- System.out.println();
- written = false;
- }
- }
- return shaderID;
- }
- private int createVertexArray(GL2 gl){
- gl.glGenVertexArrays(1, VBO_VAO_ID_BUFFER);
- // System.out.println("Create Vertex Array Object: "+ gl.glGetError());
- return VBO_VAO_ID_BUFFER.get(0);
- }
- private int createBufferObject(GL2 gl){
- gl.glGenBuffers(1, VBO_VAO_ID_BUFFER);
- // System.out.println("Create Vertex Buffer Object: "+ gl.glGetError());
- return VBO_VAO_ID_BUFFER.get(0);
- }
Advertisement
Add Comment
Please, Sign In to add comment