-
-
Save elect86/55cfe88b81a71bdcb4a3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* To change this license header, choose License Headers in Project Properties. | |
* To change this template file, choose Tools | Templates | |
* and open the template in the editor. | |
*/ | |
package ec.debug.giuseppe; | |
import com.jogamp.opengl.util.GLBuffers; | |
import com.jogamp.opengl.util.texture.TextureData; | |
import com.jogamp.opengl.util.texture.TextureIO; | |
import ec.rendering.EC_Samplers; | |
import ec.rendering.GLViewer; | |
import glsl.GLSLProgramObject; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.FloatBuffer; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.media.opengl.GL3; | |
import jglm.Jglm; | |
import jglm.Mat4; | |
/** | |
* | |
* @author djanssen | |
*/ | |
public class Debug { | |
private static int debugProgram; | |
private static int textureProgram; | |
private static int[] texture; | |
private static int[] debugVao; | |
private static int[] debugVbo; | |
public static void init(GL3 gl3) { | |
GLSLProgramObject tuamadre = new GLSLProgramObject(gl3, "/ec/graph/node/rendering/hud/glsl/shaders/", "VS_1.glsl", "FS_1.glsl"); | |
textureProgram = tuamadre.getProgramId(); | |
int modelToCameraMatrixUL = gl3.glGetUniformLocation(tuamadre.getProgramId(), "modelToCameraMatrix"); | |
int cameraToClipMatrixUL = gl3.glGetUniformLocation(tuamadre.getProgramId(), "cameraToClipMatrix"); | |
int textureUL = gl3.glGetUniformLocation(tuamadre.getProgramId(), "textureNode"); | |
if (modelToCameraMatrixUL == -1 || cameraToClipMatrixUL == -1 || textureUL == -1) { | |
throw new Error("cazzo"); | |
} | |
Mat4 modelToCamera = new Mat4(10f); | |
modelToCamera.c3.w = 1f; | |
Mat4 cameraToClipMatrix = Jglm.orthographic2D(0, GLViewer.instance.getGlWindow().getWidth(), 0, GLViewer.instance.getGlWindow().getHeight()); | |
gl3.glUseProgram(textureProgram); | |
{ | |
gl3.glUniformMatrix4fv(cameraToClipMatrixUL, 1, false, cameraToClipMatrix.toFloatArray(), 0); | |
gl3.glUniformMatrix4fv(modelToCameraMatrixUL, 1, false, modelToCamera.toFloatArray(), 0); | |
gl3.glUniform1i(textureUL, 0); | |
} | |
gl3.glUseProgram(0); | |
initTexture(gl3); | |
/** | |
* Init Vbo/vao. | |
*/ | |
float[] vertexData = new float[]{ | |
0, 0, | |
1, 0, | |
1, 1}; | |
debugVbo = new int[1]; | |
gl3.glGenBuffers(1, debugVbo, 0); | |
gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, debugVbo[0]); | |
{ | |
FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(vertexData); | |
gl3.glBufferData(GL3.GL_ARRAY_BUFFER, vertexData.length * Float.BYTES, buffer, GL3.GL_STATIC_DRAW); | |
} | |
gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0); | |
debugVao = new int[1]; | |
gl3.glGenVertexArrays(1, debugVao, 0); | |
gl3.glBindVertexArray(debugVao[0]); | |
{ | |
gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, debugVbo[0]); | |
{ | |
gl3.glEnableVertexAttribArray(0); | |
{ | |
gl3.glVertexAttribPointer(0, 2, GL3.GL_FLOAT, false, 0, 0); | |
} | |
} | |
gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0); | |
} | |
gl3.glBindVertexArray(0); | |
} | |
/** | |
* Init texture. | |
*/ | |
private static void initTexture(GL3 gl3) { | |
try { | |
File textureFile = new File("src/ec/debug/giuseppe/asset/texture.png"); | |
/** | |
* Texture data is an object containing all the relevant information | |
* about texture. | |
*/ | |
TextureData textureData = TextureIO.newTextureData(gl3.getGLProfile(), textureFile, false, TextureIO.PNG); | |
/** | |
* We don't use multiple levels (mipmaps) here, then our maximum | |
* level is zero. | |
*/ | |
int level = 0; | |
texture = new int[1]; | |
gl3.glGenTextures(1, texture, 0); | |
gl3.glBindTexture(GL3.GL_TEXTURE_2D, texture[0]); | |
{ | |
/** | |
* In this example internal format is GL_RGB8, dimensions are | |
* 512 x 512, border should always be zero, pixelFormat GL_RGB, | |
* pixelType GL_UNSIGNED_BYTE. | |
*/ | |
gl3.glTexImage2D(GL3.GL_TEXTURE_2D, level, textureData.getInternalFormat(), | |
textureData.getWidth(), textureData.getHeight(), textureData.getBorder(), | |
textureData.getPixelFormat(), textureData.getPixelType(), textureData.getBuffer()); | |
/** | |
* We set the base and max level. | |
*/ | |
gl3.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_BASE_LEVEL, 0); | |
gl3.glTexParameteri(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_MAX_LEVEL, level); | |
/** | |
* We set the swizzling. Since it is an RGB texture, we can | |
* choose to make the missing component alpha equal to one. | |
*/ | |
int[] swizzle = new int[]{GL3.GL_RED, GL3.GL_GREEN, GL3.GL_BLUE, GL3.GL_ONE}; | |
gl3.glTexParameterIiv(GL3.GL_TEXTURE_2D, GL3.GL_TEXTURE_SWIZZLE_RGBA, swizzle, 0); | |
} | |
gl3.glBindTexture(GL3.GL_TEXTURE_2D, 0); | |
} catch (IOException ex) { | |
Logger.getLogger(Debug.class.getName()).log(Level.SEVERE, null, ex); | |
} | |
} | |
public static void render(GL3 gl3) { | |
gl3.glClear(GL3.GL_DEPTH_BUFFER_BIT | GL3.GL_COLOR_BUFFER_BIT); | |
gl3.glUseProgram(textureProgram); | |
{ | |
gl3.glBindVertexArray(debugVao[0]); | |
{ | |
gl3.glActiveTexture(GL3.GL_TEXTURE0); | |
gl3.glBindTexture(GL3.GL_TEXTURE_2D, texture[0]); | |
gl3.glBindSampler(0, EC_Samplers.pool[EC_Samplers.Id.clampToEdge_nearest_0maxAn.ordinal()]); | |
{ | |
gl3.glDrawArrays(GL3.GL_TRIANGLES, 0, 3); | |
} | |
gl3.glBindTexture(GL3.GL_TEXTURE_2D, 0); | |
gl3.glBindSampler(0, 0); | |
} | |
gl3.glBindVertexArray(0); | |
} | |
gl3.glUseProgram(0); | |
} | |
} | |
VS: | |
#version 330 | |
layout (location = 0) in vec2 position; | |
uniform mat4 modelToCameraMatrix; | |
uniform mat4 cameraToClipMatrix; | |
out vec2 fragmentUV; | |
void main() | |
{ | |
gl_Position = cameraToClipMatrix * modelToCameraMatrix * vec4(position, 0, 1); | |
fragmentUV = position; | |
} | |
FS: | |
#version 330 | |
in vec2 fragmentUV; | |
out vec4 outputColor; | |
uniform sampler2D textureNode; | |
void main() | |
{ | |
outputColor = texture(textureNode, fragmentUV); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment