Simple shading problem

Hi,
I’m trying to do a simple cloth simulation based on the Opengl SuperBible.
We consider that we have vertices that are connected to each other.
For each vertice, we only consider perturbation due to 4 adjacents points.
In my code below I first try to find the index of each vertex being processed then its 4 neighbours that I put in connection array.

Than I simply apply physics laws.
The problem is that I have warnings :

0(31) : warning C7050: “connection[0]” might be used before being initialized
0(31) : warning C7050: “connection[1]” might be used before being initialized
0(31) : warning C7050: “connection[2]” might be used before being initialized
0(31) : warning C7050: “connection[3]” might be used before being initialized

I don’t understand why the neighbours are not being initialized ?
And obviously the rendering result is not what I expected.

#version 400
in vec3 position;
in vec3 velocity;

//Outs
out vec3 position_out;
out vec3 velocity_out;

//Uniforms
uniform float t;
uniform samplerBuffer tex_position;
uniform samplerBuffer tex_velocity;

//consts
const float c = 0.04;
const vec3 gravity = vec3(0.0,-0.03,0.0);
const float k = 1.0;

void main(void)
{
	int N = 64;
	vec3 F; 		//Force on the mass
	vec3 v = velocity; 	// Final velocity;
	vec3 s = vec3(0.0); 	//Displacement step
	int index = gl_VertexID;
	int ix = index%N;
	int iy = index/N;
	int connection[4];

	if(ix == 0)
	{
		connection[0] = index;
	}
	else if(ix == N-1)
	{
		connection[2] = index;
	}
	else
	{
		connection[0] = index-1;
		connection[2] = index+1;
	}
	if(iy == 0)
	{
		connection[1] = index;
	}
	else if(iy == N-1)
	{
		connection[3] = index;
	}

	else
	{
		connection[1] = index-N+1;
		connection[3] = index+N-1;
	}
	
	if(index != 0 && index != N-1)
	{
		F = gravity- c*velocity;
			for(int i=0; i< 4; i++)
			{
				if(connection[i] != index)
				{
					vec3 q = texelFetch(tex_position,connection[i]).xyz;
					vec3 d = q-position;
					float x = length(d);
					F += -k*(1-x)*normalize(d);
				}
			}

		s = velocity*t+0.5*F*t*t;
		v = velocity + F*t;
	}





	position_out = position + s;
	velocity_out = v;
}

Stop Spam please.

Well, imagine in your shader is invoked on gl_VertexID == 0, where ix == 0 and iy ==0. Then only connection[0] and connection[1] will be assigned a value, and [2] and [3] will be uninitialized. Other vertex shader invocations will not fill in the other connection elements for you, nor do values you assigned in previous invocations affect the next invocation of the shader on a vertex.

Sorry I was really ridiculous, it works fine now.

Thank you so much.

Your warnings tell you your problem. If you look at your code for example when ix = 0; connection[1],connection[2] and connection[3] are undefined yet you try to use them.

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