Search in sources :

Example 1 with GL_TEXTURE_2D

use of org.lwjgl.opengl.GL11.GL_TEXTURE_2D in project chunkstories by Hugobros3.

the class GLFWGameWindow method displaySplashScreen.

@SuppressWarnings("unused")
private void displaySplashScreen() throws IOException {
    int texture = glGenTextures();
    InputStream is = getClass().getResourceAsStream("/splash.png");
    PNGDecoder decoder = new PNGDecoder(is);
    int width = decoder.getWidth();
    int height = decoder.getHeight();
    ByteBuffer temp = ByteBuffer.allocateDirect(4 * width * height);
    decoder.decode(temp, width * 4, Format.RGBA);
    is.close();
    // ChunkStoriesLogger.getInstance().log("decoded " + width + " by " + height + " pixels (" + name + ")", ChunkStoriesLogger.LogType.RENDERING, ChunkStoriesLogger.LogLevel.DEBUG);
    temp.flip();
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (ByteBuffer) temp);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    int shaderProgramId = glCreateProgram();
    int vertexShaderId = glCreateShader(GL_VERTEX_SHADER);
    int fragShaderId = glCreateShader(GL_FRAGMENT_SHADER);
    String vertexSource = "#version 330\n\n\nin vec3 vertexIn;\nout vec2 texCoord;\nuniform float ratio;\n\nvoid main()\n{\ngl_Position = vec4(vertexIn.x*ratio, vertexIn.y, 0.0, 1.0);\ntexCoord = vertexIn.xy*0.5+0.5;\n}";
    String fragSource = "#version 330\nuniform sampler2D diffuseTexture;\n\nin vec2 texCoord;\nout vec4 fragColor;\n\nvoid main()\n{\nfragColor = texture(diffuseTexture, vec2(texCoord.x, 1.0-texCoord.y));\n}\n";
    // System.out.println(vertexSource);
    // System.out.println(fragSource);
    glShaderSource(vertexShaderId, vertexSource);
    glCompileShader(vertexShaderId);
    glBindFragDataLocation(shaderProgramId, 0, "fragColor");
    glShaderSource(fragShaderId, fragSource);
    glCompileShader(fragShaderId);
    glAttachShader(shaderProgramId, vertexShaderId);
    glAttachShader(shaderProgramId, fragShaderId);
    glLinkProgram(shaderProgramId);
    glUseProgram(shaderProgramId);
    int uniformLocation = glGetUniformLocation(shaderProgramId, "diffuseTexture");
    // glUniform2f(uniformLocation, ((Vector2fc)uniformData).x(), ((Vector2fc)uniformData).y());
    glUniform1i(uniformLocation, (Integer) 0);
    float ratio = (float) windowHeight / windowWidth;
    uniformLocation = glGetUniformLocation(shaderProgramId, "ratio");
    glUniform1f(uniformLocation, ratio);
    glValidateProgram(shaderProgramId);
    FloatBuffer fsQuadBuffer = BufferUtils.createFloatBuffer(6 * 2);
    fsQuadBuffer.put(new float[] { 1f, 1f, -1f, -1f, 1f, -1f, 1f, 1f, -1f, 1f, -1f, -1f });
    fsQuadBuffer.flip();
    int vertexBuffer = glGenBuffers();
    glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
    glBufferData(GL_ARRAY_BUFFER, (FloatBuffer) fsQuadBuffer, GL_STATIC_DRAW);
    int location = glGetAttribLocation(shaderProgramId, "vertexIn");
    glEnableVertexAttribArray(location);
    glVertexAttribPointer(location, 2, GL_FLOAT, false, 0, 0L);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // while(1 - Math.floor(1) == 0 && !glfwWindowShouldClose(glfwWindowHandle))
    {
        glClearColor(0.25f, 0.25f, 0.25f, 1f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        // Draw happens here
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glfwSwapBuffers(glfwWindowHandle);
        glfwPollEvents();
    }
    glDisable(GL_BLEND);
    glDisableVertexAttribArray(location);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDeleteBuffers(vertexBuffer);
    glBindTexture(GL_TEXTURE_2D, 0);
    glDeleteTextures(texture);
    glUseProgram(0);
    glDeleteProgram(shaderProgramId);
    glDeleteShader(vertexShaderId);
    glDeleteShader(fragShaderId);
    glClearColor(0.0f, 0.0f, 0.0f, 1f);
}
Also used : PNGDecoder(de.matthiasmann.twl.utils.PNGDecoder) InputStream(java.io.InputStream) FloatBuffer(java.nio.FloatBuffer) GL11.glGetString(org.lwjgl.opengl.GL11.glGetString) ByteBuffer(java.nio.ByteBuffer) GLFW.glfwWindowHint(org.lwjgl.glfw.GLFW.glfwWindowHint)

Aggregations

PNGDecoder (de.matthiasmann.twl.utils.PNGDecoder)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 FloatBuffer (java.nio.FloatBuffer)1 GLFW.glfwWindowHint (org.lwjgl.glfw.GLFW.glfwWindowHint)1 GL11.glGetString (org.lwjgl.opengl.GL11.glGetString)1