Skip to main content
deleted 266 characters in body
Source Link
BlueMonkMN
  • 279
  • 4
  • 12

In addition to switchingTo allow the fragment shader to glVertexAttribIPointerinterpolate texture coordinates between the vertices, it was necessary to figure out how to remove the flatness of the vectors being communicated between the vector shader and the fragment shader. So it appears I hadOne way to do this is to make vec2 copies of the ivec2 structures to allow interpolation between the shaders, thethen type-cast back to ivec2 in the fragment shader. WhatBut why bother casting to float and then back to int when all I'm really trying to accomplish is to use non-normalized coordinates. I can eliminate the need to use VertexAttribIPointer altogether and just postpone the casting to ivec2 until the fragment shader. I end up with is the original vertex shader that was dealing only with vec2 structures, and the following fragment shader code:

Shader vshader = new Shader(ShaderType.VertexShader,
    @"#version 130

    // a projection transformation to apply to the vertex' position
    uniform mat4 projectionMatrix;

    // attributes of our vertex
    in vec2 vPosition;
    in ivec2 vTexCoord;

    out vec2 vTex; // must match name in fragment shader

    void main()
    {
        // gl_Position is a special variable of OpenGL that must be set
        gl_Position = projectionMatrix * vec4(vPosition, -2.0, 1.0);
        vTex = vTexCoord;
    }");

Shader fshader = new Shader(ShaderType.FragmentShader,
    @"#version 130

    in vec2 vTex; // must match name in vertex shader
    ivec2 ivTex;

    out vec4 fragColor; // first out variable is automatically written to the screen

    uniform sampler2D tex;

    void main()
    {
        ivTex = ivec2(vTex);
        fragColor = texelFetch(tex, ivTexivec2(vTex), 0);
    }");

In addition to switching to glVertexAttribIPointer it was necessary to figure out how to remove the flatness of the vectors being communicated between the vector shader and the fragment shader. So it appears I had to make vec2 copies of the ivec2 structures to allow interpolation between the shaders, the type-cast back to ivec2 in the fragment shader. What I end up with is the following shader code:

Shader vshader = new Shader(ShaderType.VertexShader,
    @"#version 130

    // a projection transformation to apply to the vertex' position
    uniform mat4 projectionMatrix;

    // attributes of our vertex
    in vec2 vPosition;
    in ivec2 vTexCoord;

    out vec2 vTex; // must match name in fragment shader

    void main()
    {
        // gl_Position is a special variable of OpenGL that must be set
        gl_Position = projectionMatrix * vec4(vPosition, -2.0, 1.0);
        vTex = vTexCoord;
    }");

Shader fshader = new Shader(ShaderType.FragmentShader,
    @"#version 130

    in vec2 vTex; // must match name in vertex shader
    ivec2 ivTex;

    out vec4 fragColor; // first out variable is automatically written to the screen

    uniform sampler2D tex;

    void main()
    {
        ivTex = ivec2(vTex);
        fragColor = texelFetch(tex, ivTex, 0);
    }");

To allow the fragment shader to interpolate texture coordinates between the vertices, it was necessary to figure out how to remove the flatness of the vectors being communicated between the vector shader and the fragment shader. One way to do this is to make vec2 copies of the ivec2 structures to allow interpolation between the shaders, then type-cast back to ivec2 in the fragment shader. But why bother casting to float and then back to int when all I'm really trying to accomplish is to use non-normalized coordinates. I can eliminate the need to use VertexAttribIPointer altogether and just postpone the casting to ivec2 until the fragment shader. I end up with the original vertex shader that was dealing only with vec2 structures, and the following fragment shader:

Shader fshader = new Shader(ShaderType.FragmentShader,
    @"#version 130

    in vec2 vTex; // must match name in vertex shader

    out vec4 fragColor; // first out variable is automatically written to the screen

    uniform sampler2D tex;

    void main()
    {
        fragColor = texelFetch(tex, ivec2(vTex), 0);
    }");
Source Link
BlueMonkMN
  • 279
  • 4
  • 12

In addition to switching to glVertexAttribIPointer it was necessary to figure out how to remove the flatness of the vectors being communicated between the vector shader and the fragment shader. So it appears I had to make vec2 copies of the ivec2 structures to allow interpolation between the shaders, the type-cast back to ivec2 in the fragment shader. What I end up with is the following shader code:

Shader vshader = new Shader(ShaderType.VertexShader,
    @"#version 130

    // a projection transformation to apply to the vertex' position
    uniform mat4 projectionMatrix;

    // attributes of our vertex
    in vec2 vPosition;
    in ivec2 vTexCoord;

    out vec2 vTex; // must match name in fragment shader

    void main()
    {
        // gl_Position is a special variable of OpenGL that must be set
        gl_Position = projectionMatrix * vec4(vPosition, -2.0, 1.0);
        vTex = vTexCoord;
    }");

Shader fshader = new Shader(ShaderType.FragmentShader,
    @"#version 130

    in vec2 vTex; // must match name in vertex shader
    ivec2 ivTex;

    out vec4 fragColor; // first out variable is automatically written to the screen

    uniform sampler2D tex;

    void main()
    {
        ivTex = ivec2(vTex);
        fragColor = texelFetch(tex, ivTex, 0);
    }");