[SOLVED] Mapping two (different types of) Textures on the same geometry

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

[SOLVED] Mapping two (different types of) Textures on the same geometry

elect
This post was updated on .
Hi,

I am rendering a cylinder and mapping two different textures to the top and to the bottom face (on two square, blending is enabled to cut the angles, the image to render is exactly a circle, all the rest is transparent).

Both the textures are loaded in the following way:


check3dFloorFile = new File(check3dFloorPath);
        
        try {
            check3dFloorTexture = TextureIO.newTexture(check3dFloorFile, true);
        } catch (IOException | GLException ex) {
            Logger.getLogger(Viewer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        check3dFloorTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER,
                                                    GL2.GL_LINEAR_MIPMAP_LINEAR);
        check3dFloorTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
                
        check3dFloorBackFile = new File(check3dFloorBackPath);
        
        try {
            check3dFloorBackTexture = TextureIO.newTexture(check3dFloorBackFile, true);
        } catch (IOException | GLException ex) {
            Logger.getLogger(Viewer.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        check3dFloorBackTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER,
                                                    GL2.GL_LINEAR_MIPMAP_LINEAR);
        check3dFloorBackTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);





The problem arises when I try to render to a texture (a blank one, that has nothing to do with this first two ones):


gl.glGenTextures(1, textureID, 0);
        gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
        
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_S, GL2.GL_REPEAT);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_WRAP_T, GL2.GL_REPEAT);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_NEAREST);
        gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_NEAREST);
        
        // null means reserve texture memory, but texels are undefined
        gl.glTexImage2D(GL2.GL_TEXTURE_2D, 0, GL2.GL_RGB, floorWidth, floorHeight,
                                                    0, GL2.GL_RGB, GL2.GL_FLOAT, null);
        
        gl.glGenFramebuffers(1, frameBufferID, 0);
        gl.glBindFramebuffer(GL2.GL_FRAMEBUFFER, frameBufferID[0]);
        
        //Attach 2D texture to this FBO
        gl.glFramebufferTexture2D(GL2.GL_FRAMEBUFFER, GL2.GL_COLOR_ATTACHMENT0,
                                        GL2.GL_TEXTURE_2D, textureID[0], 0);
        
        // depth buffer
        //int[] depthRenderBufferID = new int[1];
        gl.glGenRenderbuffers(1, depthRenderBufferID, 0);
        gl.glBindRenderbuffer(GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
        gl.glRenderbufferStorage(GL2.GL_RENDERBUFFER, GL2.GL_DEPTH_COMPONENT,
                                                floorWidth, floorHeight);
        gl.glFramebufferRenderbuffer(GL2.GL_FRAMEBUFFER, GL2.GL_DEPTH_ATTACHMENT,
                                            GL2.GL_RENDERBUFFER, depthRenderBufferID[0]);
        
        if(gl.glCheckFramebufferStatus(GL2.GL_FRAMEBUFFER) == GL2.GL_FRAMEBUFFER_COMPLETE)
            System.out.println("[Viewer] GL_FRAMEBUFFER_COMPLETE!!");
        else
            System.out.println("..cazzo ^^");

//gl.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
        gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
       
        gl.glColor3f(0.0f, 0.0f, 0.0f);
        gl.glBegin(GL2.GL_TRIANGLES);
            gl.glVertex3f( 0.0f,  1.0f, 0.0f);
            gl.glVertex3f(-1.0f, -1.0f, 0.0f);
            gl.glVertex3f( 1.0f, -1.0f, 0.0f);
        gl.glEnd();




As soon as this code is executed, the texture mapped on the top of the cylinder disappears and all the top turns to black (after some modifications actually I do not see anything)..

Why?

Ps: my task is to map this third rendered texture on the top of the cylinder, over the already existing one..

I would like to underline that the first texture (mapped on the top) is loaded by using TextureIO while the third (the one that I want to apply on the first one) is created on the fly by render to texture. This basically means that I dont have any idea how I could bind a texture created with TextureIO (like for example the "check3dFloorTexture" )
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

gouessej
Administrator
Why not using the bind() method of the Texture class? Maybe you use the wrong texture identifiers.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

Demoscene Passivist
Administrator
In reply to this post by elect
As far as I can see u don't unbind the FBO. So thats an explanation why u do not see anything :) If u don't unbind the FBO and rebind the other textures again in the next frame, but using the same texture unit, mapping will fail with multiple textures.

Sounds u have to dive deeper into the "texture unit"/"texture id" stuff. Maybe this thread helps ...
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

elect
Demoscene Passivist wrote
As far as I can see u don't unbind the FBO. So thats an explanation why u do not see anything :) If u don't unbind the FBO and rebind the other textures again in the next frame, but using the same texture unit, mapping will fail with multiple textures.

Sounds u have to dive deeper into the "texture unit"/"texture id" stuff. Maybe this thread helps ...

Is it possible to do that without using shaders?
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

gouessej
Administrator
You can use multiple texture units and FBO without shaders as far as I know.
Julien Gouesse | Personal blog | Website
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

elect
gouessej wrote
You can use multiple texture units and FBO without shaders as far as I know.
How can I bind a texture of Texture type?

Because my

public void glBindTexture(int i, int i1)

accepts only integers..
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

Demoscene Passivist
Administrator
>How can I bind a texture of Texture type?

U can do stg like this:

	inGL.glActiveTexture(GL_TEXTURE0);
	mTexture_Diffuse.enable();
	mTexture_Diffuse.bind();
	mTexture_Diffuse.setTexParameterf(GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	mTexture_Diffuse.setTexParameterf(GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	mTexture_Diffuse.setTexParameterf(GL_TEXTURE_WRAP_S,GL_REPEAT);
	mTexture_Diffuse.setTexParameterf(GL_TEXTURE_WRAP_T,GL_REPEAT);
	
        inGL.glActiveTexture(GL_TEXTURE1);
	mTexture_Specular.enable();
	mTexture_Specular.bind();
	mTexture_Specular.setTexParameterf(GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	mTexture_Specular.setTexParameterf(GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	mTexture_Specular.setTexParameterf(GL_TEXTURE_WRAP_S,GL_REPEAT);
	mTexture_Specular.setTexParameterf(GL_TEXTURE_WRAP_T,GL_REPEAT);

... to bind multiple textures to mutliple texture units like julien suggested ...
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

gouessej
Administrator
In reply to this post by elect
Reply | Threaded
Open this post in threaded view
|

Re: Mapping two (different types of) Textures on the same geometry

elect
Thanks all, I solved it in the following way :)

            gl.glActiveTexture(GL2.GL_TEXTURE0);
            check3dFloorTexture.enable(gl);
            check3dFloorTexture.bind(gl);
           
            check3dFloorTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
            check3dFloorTexture.setTexParameteri(gl, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
           
            gl.glActiveTexture(GL2.GL_TEXTURE1);
            gl.glEnable(GL2.GL_TEXTURE_2D);
            gl.glBindTexture(GL2.GL_TEXTURE_2D, textureID[0]);
           
            gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MIN_FILTER, GL2.GL_LINEAR);
            gl.glTexParameteri(GL2.GL_TEXTURE_2D, GL2.GL_TEXTURE_MAG_FILTER, GL2.GL_LINEAR);
           
            gl.glBegin(GL2.GL_QUADS);
                gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.0f, 0.0f);
                gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 0.0f, 0.0f);
                gl.glVertex3f(-a, -a, 0);
               
                gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 0.0f, 1.0f);
                gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 0.0f, 1.0f);
                gl.glVertex3f(-a, a, 0);

                gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1.0f, 1.0f);
                gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 1.0f, 1.0f);
                gl.glVertex3f(a, a, 0);
               
                gl.glMultiTexCoord2f(GL2.GL_TEXTURE0, 1.0f, 0.0f);
                gl.glMultiTexCoord2f(GL2.GL_TEXTURE1, 1.0f, 0.0f);
                gl.glVertex3f(a, -a, 0);
            gl.glEnd();  
                     
            //  GL_TEXTURE1
            gl.glDisable(GL2.GL_TEXTURE_2D);
            gl.glActiveTexture(GL2.GL_TEXTURE0);
            check3dFloorTexture.disable(gl);