Sphere Mapping texture coordinate problem


I’m playing around with texture mapping using GLSL in OpenSceneGraph. At the moment, I’m using the orange book’s earth sphere map example to test.

Anyway, I’m pretty sure my application-level code is right (OSG’s function calls are relatively transparent to the OpenGL’s, so translating between them is usually easy).
The significant application side code sets TEXTURE_WRAP_S & T to GL_REPEAT, and MAG / MIN_FILTER to GL_LINEAR.

The problem, I’m pretty sure, is in the shader code…

Vertex Shader:

#version 150

in vec4 osg_Vertex;
in vec3 osg_Normal;

out vec2 texCoord;

uniform mat4 osg_ModelViewProjectionMatrix;
uniform mat4 osg_ModelViewMatrix;

vec2 sphereMap( const in vec4 ecPos, const in vec3 normal )
	float m;
	vec3 r, u;
	u = normalize( vec3( ecPos ) / ecPos.w );
	r = reflect( u, normal );
	r.z += 1.0;
	m = 0.5 * inversesqrt( dot( r, r ));
	return r.xy * m + 0.5;

void main()
	vec4 mvm_Vertex = osg_ModelViewMatrix * osg_Vertex;
	texCoord = sphereMap( osg_Vertex, osg_Normal );
	gl_Position = osg_ModelViewProjectionMatrix * osg_Vertex;

Fragment Shader:

#version 150

in vec2 texCoord;

uniform sampler2D texture0;

out vec4 fragColor;

void main()
	fragColor = texture( texture0, texCoord);

Anyway, since I’m using GLSL 150, I can’t rely on built-ins’ and I’m trying to test my own coordinate generation rather than use texGen.

The other thing that’s worth noting is OSG tells me that OpenGL has an “invalid operation” error, and it’s demonstrably linked to the texture() call in my fragment shader. What appears onscreen is close to waht it should be - probably closer than I think it is…

Any thoughts or suggestions? I can post images of the effect if it’d be of any value…



Well, I solved the vague “invalid operation” error - had to do with using an unsigned int to set the texture unit value in the application code…

The rest of it remains a mystery. I’ve tried passing the vertex transformed to eye space (osg_ModelViewMatrix * osg_Vertex) and the transformed normal (osg_NormalMatrix * osg_Normal) to the sphereMap function, and the texture ends up fixed w.r.t the camera rather than the sphere…

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.