Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 8 of 8

Thread: Encoding id picking into a 16b red component texture

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Intern Contributor
    Join Date
    Dec 2011
    Posts
    85

    Encoding id picking into a 16b red component texture

    So, I am trying to perform picking through id, that is before each drawArray a specific uniform variable will be set and written in the red component

    IDs are signed short

    But this is not working, and I don't know why

    Code :
    private void initRenderingTargets(GL3 gl3, int width, int height) {
     
            textures = new int[2];
            fbo = new int[1];
     
            gl3.glGenTextures(2, textures, 0);
            gl3.glGenFramebuffers(1, fbo, 0);
            /**
             * Depth.
             */
            gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, textures[depth]);
     
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_WRAP_S, GL3.GL_CLAMP_TO_EDGE);
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_WRAP_T, GL3.GL_CLAMP_TO_EDGE);
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_NEAREST);
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_NEAREST);
     
            gl3.glTexImage2D(GL3.GL_TEXTURE_RECTANGLE, 0, GL3.GL_DEPTH_COMPONENT32F, width, height, 0, GL3.GL_DEPTH_COMPONENT, GL3.GL_FLOAT, null);
            /**
             * IDs.
             */
            gl3.glBindTexture(GL3.GL_TEXTURE_RECTANGLE, textures[id]);
     
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_WRAP_S, GL3.GL_CLAMP_TO_EDGE);
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_WRAP_T, GL3.GL_CLAMP_TO_EDGE);
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_MIN_FILTER, GL3.GL_NEAREST);
            gl3.glTexParameteri(GL3.GL_TEXTURE_RECTANGLE, GL3.GL_TEXTURE_MAG_FILTER, GL3.GL_NEAREST);
     
            gl3.glTexImage2D(GL3.GL_TEXTURE_RECTANGLE, 0, GL3.GL_R16I, width, height, 0, GL3.GL_RED_INTEGER, GL3.GL_SHORT, null);
            /**
             * FBO.
             */
            gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, fbo[0]);
            gl3.glFramebufferTexture2D(GL3.GL_FRAMEBUFFER, GL3.GL_DEPTH_ATTACHMENT, GL3.GL_TEXTURE_RECTANGLE, textures[depth], 0);
            gl3.glFramebufferTexture2D(GL3.GL_FRAMEBUFFER, GL3.GL_COLOR_ATTACHMENT0, GL3.GL_TEXTURE_RECTANGLE, textures[id], 0);
     
            gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, 0);
        }
     
     public void pick(GL3 gl3) {
            System.out.println("pick");
     
            gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, fbo[0]);
            Vec4 clear = EC_GUI.main.getGlViewer().getClearColor();
            gl3.glClearColor(clear.x, clear.y, clear.z, clear.w);
            gl3.glClear(GL3.GL_COLOR_BUFFER_BIT | GL3.GL_DEPTH_BUFFER_BIT);
            gl3.glDrawBuffer(GL3.GL_COLOR_ATTACHMENT0);
            {
                pickModel.bind(gl3);
                {
                    for (EC_Mesh mesh : EC_GUI.main.getList().getMeshes()) {
                        System.out.println("id " + mesh.getId());
                        gl3.glUniform1i(pickModel.getIdUL(), (int) mesh.getId());
    //                    gl3.glUniform1i(pickModel.getIdUL(), 1);
     
                        mesh.render(gl3);
                    }
                }
                pickModel.unbind(gl3);
            }
            gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, 0);
        }
     
        public void read(GL3 gl3) {
     
            gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, fbo[0]);
            {
                gl3.glReadBuffer(GL3.GL_COLOR_ATTACHMENT0);
     
                FloatBuffer pixel = FloatBuffer.allocate(4);
                ShortBuffer p = ShortBuffer.allocate(2);
                Vec2i point = EC_GUI.main.getGlViewer().getViewPole().getStartingPoint();
     
                gl3.glReadPixels(point.x, point.y, 1, 1, GL3.GL_RED_INTEGER, GL3.GL_SHORT, p);
     
                System.out.println("pixel id "+p.get(0));
                System.out.println("pixel id "+(short)p.get(0));
            }
            gl3.glBindFramebuffer(GL3.GL_FRAMEBUFFER, 0);
        }

    Important lines

    gl3.glTexImage2D(GL3.GL_TEXTURE_RECTANGLE, 0, GL3.GL_R16I, width, height, 0, GL3.GL_RED_INTEGER, GL3.GL_SHORT, null);

    and

    gl3.glReadPixels(point.x, point.y, 1, 1, GL3.GL_RED_INTEGER, GL3.GL_SHORT, p);



    VS:

    Code :
    #version 330
     
    layout (location = 0) in vec3 position;
     
    uniform mat4 modelToWorldMatrix;
     
    layout(std140) uniform GlobalMatrices {
        mat4 worldToCameraMatrix;
        mat4 cameraToClipMatrix;
    };
     
    void main() {
     
        gl_Position = cameraToClipMatrix * (worldToCameraMatrix * vec4(position, 1.0));
    }

    FS:

    Code :
    #version 330
     
    uniform int id;
     
    out vec4 outputColor;
     
    void main() {
     
        outputColor = vec4(id, 0, 0, 1);
    }

    Im reading always the same value (32767)

    What is wrong, guys?
    Last edited by elect; 02-10-2014 at 08:09 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •