0

Vertex shader:

#version 130

in vec2 position;
in vec2 texcoord;

out vec3 Color;
out vec2 Texcoord;

void main() {
    Texcoord = texcoord;
    gl_Position = vec4(position, 0, 1);
}

Fragment shader:

#version 130

in vec2 Texcoord;
uniform sampler2D texImage;

void main()
{
  gl_FragColor = texture2D(texImage, Texcoord);
}

Where I initialize my image:

func initImage() {
    gl.UseProgram(texProgram)
    var vao uint32
    gl.GenVertexArrays(1, &vao)
    gl.BindVertexArray(vao)
    imgQuadData := []float32{
        -.5, .5, 0, 0, // Top Left
        .5, .5, 1, 0, // Top Right
        .5, -.5, 1, 1, // Bottom Right
        -.5, -.5, 0, 1, // Bottom Left
    }

    gl.GenBuffers(1, &imgQuadBuffer)
    gl.BindBuffer(gl.ARRAY_BUFFER, imgQuadBuffer)
    gl.BufferData(gl.ARRAY_BUFFER, len(imgQuadData)*4, gl.Ptr(imgQuadData), gl.STATIC_DRAW)

    posAttrib = gl.GetAttribLocation(texProgram, gl.Str("position\x00"))
    gl.EnableVertexAttribArray(uint32(posAttrib))
    gl.VertexAttribPointer(uint32(posAttrib), 2, gl.FLOAT, false, 4*4, gl.Ptr(nil))

    texAttrib = gl.GetAttribLocation(texProgram, gl.Str("texcoord\x00"))
    gl.EnableVertexAttribArray(uint32(texAttrib))
    ptr := 2 * 4
    gl.VertexAttribPointer(uint32(texAttrib), 2, gl.FLOAT, false, 4*4, gl.Ptr(&ptr))

    pixels := []float32{
        1, 0, 0, 0, 1, 0, // Red / Green
        0, 0, 1, 1, 1, 1, // Blue / White
    }
    gl.ActiveTexture(gl.TEXTURE0)
    gl.GenTextures(1, &bcTexture)
    gl.BindTexture(gl.TEXTURE_2D, bcTexture)
    gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGB, 2, 2, 0, gl.RGB, gl.FLOAT, gl.Ptr(&pixels[0]))
    gl.Uniform1i(gl.GetUniformLocation(texProgram, gl.Str("texImage\x00")), 0)

    gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
    gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
    gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
    gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
}

Where I draw to screen:

gl.ClearColor(1, 1, 0, 1)
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.UseProgram(texProgram)
gl.BindBuffer(gl.ARRAY_BUFFER, imgQuadBuffer)
gl.DrawArrays(gl.QUADS, 0, 4)

On the laptop I'm running on 1.30 is the latest GLSL language version supported so I can't move up to a newer version.

imgQuadData holds the X/Y position of where to draw a corner of the quad as well as what the S/T values for texture coordinates at that corner should be. I've followed this tutorial to get to what I have. Although I'm working in go, the go-gl library makes it pretty trivial to convert C++ GL calls to go GL calls.

red square on yellow background

It's behaving as if Texcoord in the fragment shader is always vec2(0, 0). If I change that line in the fragment shader to use vec2(1, 1) instead of Texcoord then the square changes colors properly. The problem seems to be that imgQuadData isn't being parsed properly for the vertex shader to get the S and T values as non-0 values and I don't understand why.

How can my position attributes be read successfully but my texcoord attributes always equal 0?

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188

1 Answers1

0

It turns out that when specifying the last parameter in gl.VertexAttribPointer you should use gl.PtrOffset instead of gl.Ptr. I guess what GL was seeing with gl.Ptr was some large address when it should have been 8 (skip the first 2 floats that are of size 4).

proper color blur

Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188