Shader & texture - weird display

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

Shader & texture - weird display

greatdonking
Hi,

I'm following the the tutorial from LearnOpenGlTK and i'm trying to display a simple square with a png texture.
I'm using SWT in order to create a window with a GLCanvas to display OpenGL.

I'm able to render a correct square but it turns to brown (wood texture) or red (coke texture) when i'm trying to display the texture. Could you please assist and tell me where I'm wrong ?

Here is my code

public class ShaderDebug {
	
	private static final Logger logger = LogManager.getLogger(ShaderProgram.class.getName());
	
	private GL2 gl = GLContextManager.getInstance().getOpenGLContext().getGL();
	
	private ShaderProgram shader;
	
	private int vbo;
	private int vao;
	private int ebo;
	
	private Texture texture;
	private FloatBuffer dataV;
	private IntBuffer dataI;	
	
	float[] vertices = { 
		    //Position          Texture coordinates
		     0.5f,  0.5f, 0.0f, 1.0f, 1.0f, // top right
		     0.5f, -0.5f, 0.0f, 1.0f, 0.0f, // bottom right
		    -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, // bottom left
		    -0.5f,  0.5f, 0.0f, 0.0f, 1.0f  // top left
		};
	
	private int[] indices = {  // note that we start from 0!
		    0, 1, 3,   // first triangle
		    1, 2, 3    // second triangle
		};
	

	
	public ShaderDebug() {
		_addDataV(vertices);
		_addDataI(indices);
	}
	
	public void init() {			
		try {
			// VBO
			// 1. bind Vertex Array Object	
			vbo = GLContextManager.getInstance().getOpenGLContext().generateID();
	    	gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo);    	
	    	// 2. copy our vertices array in a buffer for OpenGL to use		
			gl.glBufferData(GL2.GL_ARRAY_BUFFER, dataV.capacity() * GLBuffers.SIZEOF_FLOAT, dataV, GL2.GL_STATIC_DRAW);
			
			// EBO 	
			// 1. bind Vertex Array Object	
	    	ebo = GLContextManager.getInstance().getOpenGLContext().generateID();
	    	gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, ebo);    	
	    	// 2. copy our vertices array in a buffer for OpenGL to use		
			gl.glBufferData(GL2.GL_ELEMENT_ARRAY_BUFFER, dataI.capacity() * GLBuffers.SIZEOF_INT, dataI, GL2.GL_STATIC_DRAW);
			
			
			// Shader
			this.shader = ShaderProgramManager.getShaderProgram();
			this.shader.use();

			
			// Texture
	        ClassLoader classLoader = ClassLoader.getSystemClassLoader();
	        URL resource = classLoader.getResource("images/container.png");
	        if (resource == null) {
	            throw new IllegalArgumentException("file not found!");
	        } 
	        
	        File file = new File(resource.getFile());
			texture = TextureIO.newTexture(file, true);
            texture.setMustFlipVertically(true);
			texture.enable(gl);
	        texture.bind(gl);

	        texture.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, (GL2.GL_LINEAR));
	        texture.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, (GL2.GL_LINEAR));
			
			// VAO
			// 1. bind VAO, VBO, EBO
			gl.glBindVertexArray(vao);			
			gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, vbo);
			gl.glBindBuffer(GL2.GL_ELEMENT_ARRAY_BUFFER, ebo);			
			
			// vertexLocation
			int vertexLocation = shader.GetAttribLocation("aPosition");
            gl.glEnableVertexAttribArray(vertexLocation);
            gl.glVertexAttribPointer(vertexLocation, 3, GL2.GL_FLOAT, false, 5 * GLBuffers.SIZEOF_FLOAT, 0);
						
			// texCoordLocation
            int texCoordLocation = shader.GetAttribLocation("aTexCoord");
            gl.glEnableVertexAttribArray(texCoordLocation);
            gl.glVertexAttribPointer(texCoordLocation, 2, GL2.GL_FLOAT, false, 5 * GL2.GL_FLOAT, 3 * GL2.GL_FLOAT);
            
		} catch (Exception e) {
			logger.error("Error while creating shader", e);
		}
	}
	
	public void render() {		
		 //gl.glClear(GL2.GL_COLOR_BUFFER_BIT);
		// bind VAO
		 gl.glBindVertexArray(vao);
		
		// texture
		 texture.enable(gl);
		 texture.bind(gl);
		 
		// shader
		 shader.use();
		
		// draw elements
		 gl.glDrawElements(GL2.GL_TRIANGLES, indices.length, GL2.GL_UNSIGNED_INT, 0);
		 
		// gl.glFlush();
	}	

    
    public void _addDataV(float[] data) {		
    	this.dataV = FloatBuffer.wrap(data);
    	this.dataV.rewind();
    }
    
	public void _addDataI(int[] data) {		
    	this.dataI = IntBuffer.wrap(data);
		this.dataI.rewind();
	}

}
Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

greatdonking
The Png Image
Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

greatdonking
In reply to this post by greatdonking
The render
Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

gouessej
Administrator
In reply to this post by greatdonking
Hello

We're not responsible for unofficial tutorials, especially when they infringe the most basic guidelines that I have repeated for about 14 years. Rather look at our rudimentary examples in our wiki:
https://jogamp.org/wiki/index.php?title=Jogl_Tutorial

The line "private GL2 gl = GLContextManager.getInstance().getOpenGLContext().getGL();" illustrates my warning, you mustn't store a GL instance that way, you mustn't store it into a field. You must get it from the GLEventListener or from GLContext.getCurrentGL(). When you store a GL instance into a field, you take the risk of using it when it's no longer valid, on the wrong thread or on the good thread but not when the OpenGL context is current. I don't want to make you believe that this source code is correct.

By the way, don't use indirect NIO buffers, rather create direct NIO buffers when you want to pass them to JOGL (otherwise, it will have to make the conversion under the hood anyway but it will increase the memory footprint because the indirect NIO buffers will still occupy some memory on the Java heap while the direct NIO buffers will occupy some memory on the native heap).
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

greatdonking
Thanks for your answer.
Sorry for the misunderstood, I'm not requesting assistance related to the unofficial tutorials, but just some help/suggestions in order to learn and fix my issue.

So followings your suggestions, I have removed the reference to the GL2 and now retrieve it thanks to the GLContext created in my main Window. In addition, IndirectBuffres switch to DirectBuffer.

Finally, I raised the Following Excpetion :
com.jogamp.opengl.GLException: Thread[main,5,main] glGetError() returned the following error codes after a call to glVertexAttribPointer(<int> 0x1, <int> 0x2, <int> 0x1406, <boolean> false, <int> 0x641E, <long> 15378): GL_INVALID_VALUE ( 1281 0x501), 
	at com.jogamp.opengl.DebugGL4bc.writeGLError(DebugGL4bc.java:31803) ~[jogl-all.jar:2.3.2]
	at com.jogamp.opengl.DebugGL4bc.glVertexAttribPointer(DebugGL4bc.java:30516) ~[jogl-all.jar:2.3.2]

Related Code is call in init()
// texCoordLocation
            int texCoordLocation = shader.GetAttribLocation(gl, "aTexCoord");
            logger.info(String.format("texCoordLocation [%s]", texCoordLocation));
            gl.glEnableVertexAttribArray(texCoordLocation);
            gl.glVertexAttribPointer(texCoordLocation, 2, GL2.GL_FLOAT, false, 5 * GL2.GL_FLOAT, 3 * GL2.GL_FLOAT);
            
 The vertexShader define a var layout(location = 1) in vec2 aTexCoord.

Any ideas where I'm wrong ?

Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

greatdonking
Finally, I realized that the parameters was incorrect
GL2.GL_FLOAT instead of GLBuffers.SIZEOF_FLOAT.
			// texCoordLocation
            int texCoordLocation = shader.GetAttribLocation(gl, "aTexCoord");
            logger.info(String.format("texCoordLocation [%s]", texCoordLocation));
            gl.glEnableVertexAttribArray(texCoordLocation);
            gl.glVertexAttribPointer(texCoordLocation, 
            		2, 
            		GL2.GL_FLOAT, 		// ok
            		false, 				// normalized
            		5 * GLBuffers.SIZEOF_FLOAT, 	// vertices composed by 5 vertex
            		3 * GLBuffers.SIZEOF_FLOAT);  // start index vertices
Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

gouessej
Administrator
Yes, that's what Xerxes does in our official examples:
https://jogamp.org/cgit/jogl-demos.git/tree/src/demos/es2/RawGL2ES2demo.java?id=HEAD#n520
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

greatdonking
Do you have any suggestions regarding all vector calculations : create mine or resued existing framework, If so which one ?

Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

gouessej
Administrator
JOML is worth a look, it's very actively maintained and I provided a few examples using JOGL with it (I don't remember where they are).
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Shader & texture - weird display

greatdonking
Merci julien.