Texture not properly rendering when passed into shader

So I finally am getting some of the texture to show into my shader, however it gets all sorts of garbled and as the the texture rotates within the shader.

Screenshot of what’s happening to my texture:
http://imgur.com/J3qo3F8

The top left quadrant is what the texture is supposed to look like

My code:

void ofApp::setup(){

ofDisableArbTex();

img.loadImage("tex09.jpg");




ofSetCircleResolution(80);

backgroundMusic.loadSound("Lumahai.mp3", true);
backgroundMusic.setLoop(true);
backgroundMusic.play();
}

My main app is:

void ofApp::draw(){

shader.load("", "redGiant.frag");



shader.begin();
img.getTextureReference();
shader.setUniform1f("u_time", ofGetElapsedTimef());
shader.setUniform2f("u_resolution", ofGetWidth(), ofGetHeight());
shader.setUniform2f("u_mouse", mouseX, mouseY);
shader.setUniformTexture("u_text1", img.getTextureReference(), 1);
img.bind();
ofEnableDepthTest();
ofDisableAlphaBlending();
ofRect(0,0, ofGetWindowWidth(), ofGetWindowHeight());
shader.end();

}

I know my shader code is working properly as when I run the shader code on shadertoy.com it runs perfectly with the texture. Textures are 1024x1024 to keep within the power of 2.

I’ve tried moving around the disablealphablending and depthtest to no avail, same with bind and unbind calls to the image

`

1 Like

Hi, can you post the the shader you are using?

1 Like
#ifdef GL_ES
precision mediump float;
#endif

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define PI 3.14159265359
uniform sampler2D u_text1;

float snoise(vec3 uv, float res)	// by trisomie21
{
const vec3 s = vec3(1e0, 1e2, 3e4);

uv *= res;

vec3 uv0 = floor(mod(uv, res))*s;
vec3 uv1 = floor(mod(uv+vec3(1.), res))*s;

vec3 f = fract(uv); f = f*f*(3.-2.*f);

vec4 v = vec4(uv0.x+uv0.y+uv0.z, uv1.x+uv0.y+uv0.z,
	      	  uv0.x+uv1.y+uv0.z, uv1.x+uv1.y+uv0.z);

vec4 r = fract(sin(v*1e-3)*1e5);
float r0 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);

r = fract(sin((v + uv1.z - uv0.z)*1e-3)*1e5);
float r1 = mix(mix(r.x, r.y, f.x), mix(r.z, r.w, f.x), f.y);

return mix(r0, r1, f.z)*2.-1.;
 }



void main()
{

float brightness = .001;
float radius		= 0.25 +brightness * 0.1;
float invRadius 	= 1.09/radius;

vec3 red			= vec3( 0.9, 0.2, 0.1 );
vec3 orangeRed		= vec3( 0.9, 0.1, 0.1 );
float time		= u_time * 0.1;
float aspect	= u_resolution.x/u_resolution.y;
vec2 uv			= gl_FragCoord.xy / u_resolution.xy;
vec2 p 			= -0.5 + uv;
p.x *= aspect;

float fade		= pow( length( 2.7 * p ), 0.5 );
float fVal1		= 2.2 - fade;
float fVal2		= 1.1 - fade;

float angle		= atan( p.x, p.y )/6.2832;
float dist		= length(p);
vec3 coord		= vec3( angle, dist, time * 0.2 );

float newTime1	= abs( snoise( coord + vec3( 0.0, -time * ( 0.35 + brightness * 0.001 ), time * 0.015 ), 15.0 ) );
float newTime2	= abs( snoise( coord + vec3( 0.0, -time * ( 0.15 + brightness * 0.001 ), time * 0.015 ), 45.0 ) );	
for( int i=1; i<=9; i++ ){
	float power = pow( 2.0, float(i + 1) );
	fVal1 += ( 0.5 / power ) * snoise( coord + vec3( 0.0, -time, time * 0.2 ), ( power * ( 10.0 ) * ( newTime1 + 1.0 ) ) );
	fVal2 += ( 0.5 / power ) * snoise( coord + vec3( 0.0, -time, time * 0.2 ), ( power * ( 25.0 ) * ( newTime2 + 1.0 ) ) );
}

float corona		= pow( fVal1 * max( 1.1 - fade, 0.0 ), 2.0 ) * 50.0;
corona				+= pow( fVal2 * max( 1.1 - fade, 0.0 ), 2.0 ) * 50.0;
corona				*= 1.2 - newTime1;
vec3 sphereNormal 	= vec3( 0.0, 0.0, 1.0 );
vec3 dir 			= vec3( 0.0 );
vec3 center			= vec3( 0.5, 0.5, 1.0 );
vec3 starSphere		= vec3( 0.0 );

vec2 sp = -1.0 + 2.0 * uv;
sp.x *= aspect;
sp *= ( 2.0 - brightness );
float r = dot(sp,sp);
float f = (1.0-sqrt(abs(1.0-r)))/(r) + brightness * 0.5;
if( dist < radius ){
	corona			*= pow( dist * invRadius, 24.0 );
	vec2 newUv;
	newUv.x = sp.x*f;
	newUv.y = sp.y*f;
	newUv += vec2( time, 0.0 );
	
	vec3 texSample 	= texture2D( u_text1, newUv ).rgb;
	float uOff		= ( texSample.g * brightness * 4.5 + time );
	vec2 starUV		= newUv + vec2( uOff, 0.0 );
	starSphere		= texture2D( u_text1, starUV ).rgb;
}

float starGlow	= min( max( 1.0 - dist * ( 1.0 - brightness ), 0.0 ), 1.0 );
//fragColor.rgb	= vec3( r );
gl_FragColor.rgb	= vec3( f * ( 0.2 + brightness * 0.3 ) * red ) + starSphere + corona * red + starGlow * orangeRed;
gl_FragColor.a		= 1.0;
}

Do I have to use fbo’s for this?

Hi,

I would start by simplifying the shader a bit so that you can test one thing at a time and sanity check along the way.

You’re deriving texture coordinates from screen location there, which is all good, but you’ll want to make sure these are still sane (0…1) now that you moved from one environment to another.

One thing that comes to mind is that perhaps shadertoy expects a different wrapping mode for the textures? The code might generate coordinates outside of 0…1 but expect the texture to be set to wrapping rather than clamping.

http://openframeworks.cc/documentation/gl/ofTexture/#show_setTextureWrap

Not sure how you mean regarding FBOs, I’m going to guess this is a single pass effect as it came from shadertoy?

1 Like

The fbo thing I just threw out there as most texture questions I see here
involves them so was wondering if they have to be used to properly pass a
texture into a shader. I’m pretty sure shadertoy runs on the 0,1.

I will follow your advice and start simple again and get a texture working
on a circle first then build up from there.

I had this problem before on an old project where I was wrapping textures
to sphere prims and the alphablending and depth test methods fixed it was
hoping it would do that here too hah

Is it OK to message you with further questions if I still am stuck later?

Hi, yeah definitely.

1 Like

Like @hahakid said, Shadertoy uses REPEAT as it’s wrapping mode, as OF uses CLAMP_TO_EDGE by default. That’s why you get those vertical and horizontal lines: it’s just repeating the edge.

So, after you load your image, you can do:

img.getTexture().setTextureWrap(GL_REPEAT, GL_REPEAT);
2 Likes

this solved it! although i had to use getTextureReference() instead of getTexture() a fyi if anyone runs into this same problem!

@hubris and @hahakid thank you both very much for taking the time to help me with this, you don’t know how much frustration of mine you’ve cured!!

It’s funny how you can beat your head against a wall for hours and all you’re missing is one little method call :S

This gave me quite a bit more insight into how exactly textures are getting mapped, thanks for the tips!

You’re welcome! :slightly_smiling:

Just to leave a note, getTextureReference is from the older versions: pre 0.9. It was been deprecated.

oh good to note! I may need an update hah

1 Like