Modern JOGL, simple texture example

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Modern JOGL, simple texture example

elect
This post was updated on .
Hi,

I didnt managed to find a simple and clear example regarding using texture with modern JOGL

This it what I think it should be the right way, but it's not working and I dont know why

package test;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import glsl.GLSLProgramObject;
import com.jogamp.opengl.util.GLBuffers;
import com.jogamp.opengl.util.glsl.ShaderUtil;
import com.jogamp.opengl.util.texture.Texture;
import com.jogamp.opengl.util.texture.TextureIO;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.media.opengl.GL3;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLEventListener;
import javax.media.opengl.GLException;
import javax.media.opengl.GLProfile;
import javax.media.opengl.awt.GLCanvas;

/**
 *
 * @author gbarbieri
 */
public class Test implements GLEventListener {

    private int imageWidth = 800;
    private int imageHeight = 600;
    private GLCanvas canvas;
    private GLSLProgramObject programObject;
    private int[] vertexBufferObject = new int[1];
    private int[] vertexArrayObject = new int[1];
    private float[] vertexData = new float[]{
        0.25f, 0.25f, 0.75f, 1.0f,
        0.25f, -0.25f, 0.75f, 1.0f,
        -0.25f, -0.25f, 0.75f, 1.0f,
        -0.25f, 0.25f, 0.75f, 1.0f,
        1.0f, 1.0f,
        1.0f, 0.0f,
        0.0f, 0.0f,
        0.0f, 1.0f};
    private String shadersFilepath = "/tut04/shaders/";
    private Texture texture;
    private int textureUnLoc;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Test test = new Test();

        Frame frame = new Frame("Test");

        frame.add(test.getCanvas());

        frame.setSize(test.getCanvas().getWidth(), test.getCanvas().getHeight());

        frame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent windowEvent) {
                System.exit(0);
            }
        });

        frame.setVisible(true);
    }

    public Test() {
        initGL();
    }

    private void initGL() {
        GLProfile profile = GLProfile.getDefault();

        GLCapabilities capabilities = new GLCapabilities(profile);

        canvas = new GLCanvas(capabilities);

        canvas.setSize(imageWidth, imageHeight);

        canvas.addGLEventListener(this);
    }

    @Override
    public void init(GLAutoDrawable glad) {
        System.out.println("init");

        canvas.setAutoSwapBufferMode(false);

        GL3 gl3 = glad.getGL().getGL3();

        buildShaders(gl3);

        initializeVertexBuffer(gl3);

        texture = initializeTexture(gl3);

        gl3.glGenVertexArrays(1, IntBuffer.wrap(vertexArrayObject));
        gl3.glBindVertexArray(vertexArrayObject[0]);

        gl3.glEnable(GL3.GL_CULL_FACE);
        gl3.glCullFace(GL3.GL_BACK);
        gl3.glFrontFace(GL3.GL_CW);
    }

    @Override
    public void dispose(GLAutoDrawable glad) {
        System.out.println("dispose");
    }

    @Override
    public void display(GLAutoDrawable glad) {
        System.out.println("display");

        GL3 gl3 = glad.getGL().getGL3();

        gl3.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        gl3.glClear(GL3.GL_COLOR_BUFFER_BIT);

        programObject.bind(gl3);
        {
            gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);

            gl3.glEnableVertexAttribArray(0);
            gl3.glEnableVertexAttribArray(1);
            {
                gl3.glActiveTexture(GL3.GL_TEXTURE0);
                texture.enable(gl3);
                texture.bind(gl3);
                gl3.glUniform1i(textureUnLoc, 0);
               
                gl3.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
                gl3.glVertexAttribPointer(1, 2, GL3.GL_FLOAT, false, 0, 4 * 4 * 4);

                gl3.glDrawArrays(GL3.GL_QUADS, 0, 4);
               
                texture.disable(gl3);
            }
            gl3.glDisableVertexAttribArray(0);
            gl3.glDisableVertexAttribArray(1);
        }
        programObject.unbind(gl3);

        glad.swapBuffers();
    }

    @Override
    public void reshape(GLAutoDrawable glad, int x, int y, int w, int h) {
        System.out.println("reshape() x: " + x + " y: " + y + " width: " + w + " height: " + h);

        GL3 gl3 = glad.getGL().getGL3();

        gl3.glViewport(x, y, w, h);
    }

    private void buildShaders(GL3 gl3) {
        System.out.print("Building shaders...");

        programObject = new GLSLProgramObject(gl3);
        programObject.attachVertexShader(gl3, shadersFilepath + "OrthoWithOffset_VS.glsl");
        programObject.attachFragmentShader(gl3, shadersFilepath + "StandardColor_FS.glsl");
        programObject.initializeProgram(gl3, true);

        textureUnLoc = gl3.glGetUniformLocation(programObject.getProgId(), "myTexture");

        System.out.println("ok");
    }

    private void initializeVertexBuffer(GL3 gl3) {
        gl3.glGenBuffers(1, IntBuffer.wrap(vertexBufferObject));

        gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, vertexBufferObject[0]);
        {
            FloatBuffer buffer = GLBuffers.newDirectFloatBuffer(vertexData);

            gl3.glBufferData(GL3.GL_ARRAY_BUFFER, vertexData.length * 4, buffer, GL3.GL_STATIC_DRAW);
        }
        gl3.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
    }

    private Texture initializeTexture(GL3 gl3) {

        Texture t = null;

        try {
            t = TextureIO.newTexture(this.getClass().getResource("data/Texture.jpg"), false, ".jpg");

            t.setTexParameteri(gl3, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_LINEAR);
            t.setTexParameteri(gl3, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_LINEAR);
            t.setTexParameteri(gl3, GL3.GL_TEXTURE_WRAP_S, GL3.GL_CLAMP_TO_EDGE);
            t.setTexParameteri(gl3, GL3.GL_TEXTURE_WRAP_T, GL3.GL_CLAMP_TO_EDGE);

        } catch (IOException | GLException ex) {
            Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
        }

        return t;
    }

    public GLCanvas getCanvas() {
        return canvas;
    }

    public void setCanvas(GLCanvas canvas) {
        this.canvas = canvas;
    }
}


Vertex Shader

#version 330

layout (location = 0) in vec4 position;
layout (location = 1) in vec2 vertexUV;

out vec2 fragmentUV;

void main()
{
    gl_Position = position;
   
    fragmentUV = vertexUV;
}


Fragment Shader
#version 330

in vec2 fragmentUV;

out vec4 outputColor;

uniform sampler2D myTexture;

void main()
{
    //outputColor = new vec4(1.0f, 0.0f, 0.0f, 1.0f);

    outputColor = texture(myTexture, fragmentUV).rgb;
}


This is what I see



and this is the texture I should see applied on the quad



Reply | Threaded
Open this post in threaded view
|

Re: Modern JOGL, simple texture example

Xerxes Rånby
elect wrote
Hi,

I didnt managed to find a simple and clear example regarding using texture with modern JOGL

This it what I think it should be the right way, but it's not working and I dont know why
...
Fragment Shader
#version 330

in vec2 fragmentUV;

out vec4 outputColor;

uniform sampler2D myTexture;

void main()
{
    //outputColor = new vec4(1.0f, 0.0f, 0.0f, 1.0f);

    outputColor = texture(myTexture, fragmentUV).rgb;
}
You have a faulty assignment of different type in the fragment shader.
outputColor is a vec4 while you tried to pass it a "vec3" (.rgb)

Use:
    outputColor = texture(myTexture, fragmentUV).rgba;

It worked for me:
Reply | Threaded
Open this post in threaded view
|

Re: Modern JOGL, simple texture example

Xerxes Rånby
Reply | Threaded
Open this post in threaded view
|

Re: Modern JOGL, simple texture example

elect
In reply to this post by Xerxes Rånby
Xerxes Rånby wrote
You have a faulty assignment of different type in the fragment shader.
outputColor is a vec4 while you tried to pass it a "vec3" (.rgb)

Use:
    outputColor = texture(myTexture, fragmentUV).rgba;

It worked for me:
O.o ... I fixed as you said but on mine its still not working..

Are you running the same code?
Reply | Threaded
Open this post in threaded view
|

Re: Modern JOGL, simple texture example

Xerxes Rånby
<quote author="elect">
Xerxes Rånby wrote
You have a faulty assignment of different type in the fragment shader.
outputColor is a vec4 while you tried to pass it a "vec3" (.rgb)

Use:
    outputColor = texture(myTexture, fragmentUV).rgba;

...
O.o ... I fixed as you said but on mine its still not working..

Are you running the same code?
I am running exactly this, Linux 12.04 32bit using NVIDIA 4.3 drivers.

git clone https://github.com/xranby/modern-jogl-examples
cd modern-jogl-examples/modern-jogl-examples
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386/ mvn install
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386/ mvn exec:java -Dexec.mainClass=tut14.Test


Reply | Threaded
Open this post in threaded view
|

Re: Modern JOGL, simple texture example

elect
Xerxes Rånby wrote
I am running exactly this, Linux 12.04 32bit using NVIDIA 4.3 drivers.

git clone https://github.com/xranby/modern-jogl-examples
cd modern-jogl-examples/modern-jogl-examples
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386/ mvn install
JAVA_HOME=/usr/lib/jvm/java-7-openjdk-i386/ mvn exec:java -Dexec.mainClass=tut14.Test
I am an idiot, I was using the shaders from tut04 :D

private String shadersFilepath = "/tut04/shaders/";

Thanks Xerxes! ;)
Reply | Threaded
Open this post in threaded view
|

Re: Modern JOGL, simple texture example

elect
Enabling the debug I see this

GLDebugEvent[ id 0x20092
        type Warning: implementation dependent performance
        severity Medium: Severe performance/deprecation/other warnings
        source GL API
        msg Program/shader state performance warning: Fragment Shader is going to be recompiled because the shader key based on GL state mismatches.
        when 1379504391541
        source 3.3 (Compat profile, arb, debug, ES2 compat, FBO, hardware) - 3.3.0 - hash 0x35161c07]
java.lang.Exception: Stack trace
display
        at java.lang.Thread.dumpStack(Thread.java:1342)
        at jogamp.opengl.GLDebugMessageHandler$StdErrGLDebugListener.messageSent(GLDebugMessageHandler.java:306)
        at jogamp.opengl.GLDebugMessageHandler.sendMessage(GLDebugMessageHandler.java:292)
        at jogamp.opengl.GLDebugMessageHandler.glDebugMessageARB(GLDebugMessageHandler.java:317)
        at jogamp.opengl.gl4.GL4bcImpl.dispatch_glDrawArrays1(Native Method)
        at jogamp.opengl.gl4.GL4bcImpl.glDrawArrays(GL4bcImpl.java:4554)
        at test.Test.display(Test.java:141)
        at jogamp.opengl.GLDrawableHelper.displayImpl(GLDrawableHelper.java:588)
        at jogamp.opengl.GLDrawableHelper.display(GLDrawableHelper.java:572)
        at javax.media.opengl.awt.GLCanvas$7.run(GLCanvas.java:1054)
        at jogamp.opengl.GLDrawableHelper.invokeGLImpl(GLDrawableHelper.java:1034)
        at jogamp.opengl.GLDrawableHelper.invokeGL(GLDrawableHelper.java:909)
        at javax.media.opengl.awt.GLCanvas$8.run(GLCanvas.java:1065)
        at javax.media.opengl.Threading.invoke(Threading.java:193)
        at javax.media.opengl.awt.GLCanvas.display(GLCanvas.java:483)
        at javax.media.opengl.awt.GLCanvas.paint(GLCanvas.java:537)
        at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
        at sun.awt.RepaintArea.paint(RepaintArea.java:240)
        at sun.awt.windows.WComponentPeer.handleEvent(WComponentPeer.java:347)
        at java.awt.Component.dispatchEventImpl(Component.java:4937)
        at java.awt.Component.dispatchEvent(Component.java:4687)
        at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:729)
        at java.awt.EventQueue.access$200(EventQueue.java:103)
        at java.awt.EventQueue$3.run(EventQueue.java:688)
        at java.awt.EventQueue$3.run(EventQueue.java:686)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
        at java.awt.EventQueue$4.run(EventQueue.java:702)
        at java.awt.EventQueue$4.run(EventQueue.java:700)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
        at java.awt.EventQueue.dispatchEvent(EventQueue.java:699)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
        at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
        at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
        at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)


Why? What has being changed and then the shader compiled again?