Login  Register

Shader & texture - weird display

Posted by greatdonking on Apr 20, 2020; 6:16pm
URL: https://forum.jogamp.org/Shader-texture-weird-display-tp4040540.html

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

}