0
votes

i am trying to send sphere data to my fragment shader in order to test my glsl raycaster. A sphere consists of four float values for x,y,z and r. I created a texture and set the internal format to GL_RGBA32F to say OpenGL i want four floats per texture coordinate.

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0,  GL_RGBA32F, 1, 3, 0, GL_RGBA, GL_FLOAT, texData);

and in the fragment shader i do this to read the x value for every sphere:

for (int i = 0; i < 3; i++) {
    Sphere s   = {vec4(texture2D(sphereData,vec2(0,i)).r, 0.0, -3.3, 1.0), 0.1, scolor};}

for testing i only read the x coordinate of every sphere. Unfortunately it did not work. It draws one or two spheres depending on the settings of glTexImage2D, but the values are not right. If i hardcode the coordinates like

    Sphere s   = {vec4(0.5, 0.0, -3.3, 1.0), 

then it draws the spheres correctly. So i assume theres something wrong with the format of the texture.

I tested every combination of internal formats for glTexImage2D.

1

1 Answers

2
votes

You are not using useful texture coordinates. Since you have to use normalized texture coordinates in the [0,1], you end iup with only the very first or the very last row of the texture to be acessed. You also did set GL_LINEAR as texture filter, but I don't see that you actually want to blend the spehre parameters between different texels. That is, to get useful results, besides normalizing the texcoords, you would also have to take care to sample exactly at texel centers to avoid any mixing.

Howerver, since you clearly want to access the texture as a simple 2D array, I recommend you use the texelFetch() GLSL function which takes the unnormalized texel coords as an ivec2 (in the 2D case) and retrieves exactly that texel value, without any filtering at all. So

texelFetch(sphereData,ivec2(0,i))

should provide you exaclty with the values you are seeking.