Trouble with drawing a texture

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

Trouble with drawing a texture

wcobalt
This post was updated on .
down vote
favorite
I have had copy and past code from here: http://forum.jogamp.org/Modern-JOGL-simple-texture-example-td4029964.html Then I have changed all paths on custom and I have changed rgb at fragment shader on rgba and change GLProfile.getDefault() (at begin of the code) on GLProfile.get(GLProfile.GL3). Without it the code did not run. Then I have tried to run it. But all that I have seen it was black window. What I am doing wrong?

The code:

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 com.jogamp.opengl.GL3;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.GLException;
import com.jogamp.opengl.GLProfile;
import com.jogamp.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 = "/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.get(GLProfile.GL3);

        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);



        texture = initializeTexture(gl3);
        initializeVertexBuffer(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 + "vertexshader.glsl");
        programObject.attachFragmentShader(gl3, shadersFilepath + "fragmentshader.glsl");
        programObject.initializeProgram(gl3, true);

        textureUnLoc = gl3.glGetUniformLocation(programObject.getProgramId(), "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("/gamedata/texture.png"), false, ".png");

            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).rgba;
}
P.S. My proccessor is Intel Core i5-7500 (Video Adapter is Intel HD Graphics 630).
P.S.S On my notebook this code has worked, but on my desktop computer has not
Reply | Threaded
Open this post in threaded view
|

Re: Trouble with drawing a texture

gouessej
Administrator
wcobalt wrote
P.S. My processor is Intel Core i5-7500 (Video Adapter is Intel HD Graphics 630).
P.S.S On my notebook this code has worked, but on my desktop computer has not
Are you sure that you have a good OpenGL driver on both machines? Maybe your desktop computer doesn't support OpenGL 3.
Julien Gouesse | Personal blog | Website