Passing normal, tex coord of vertex from vertex shader to fragment shader

Started by
7 comments, last by _WeirdCat_ 9 years ago

As far as i understand the usage of the 'shaders'

when defining output in the vertex shader (that i can later use in fragment shader)

i do:

void main()

{

gl_Position = position_of_the_vertex;

gl_FragColor = color_of the vertex; //or maybe i cant write gl_FragColor in vertex shader then how do i do that?

now how do i pass texture coord and normal coordianet to the fragment shader

}

in old ARB vertex program / fragment program

i wrote (for vertex program)

MOV result.position, vertexClip;
MOV result.color, vertex.color;
MOV result.texcoord[0], vertex.texcoord;
that passed position of the vertex, color of the vertex and texture coordinate. but i cant find any equivalents in glsl.
Advertisement

You can set variables in the vertex and fragment shader above main() and write to them. Look up varying glsl.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

The principle is that you define both an input and an output structure for the vertex shader (output of the VS == input for the PS/fragment shader). In pseudo:

Vs_input
- position
- texcoord
- normal

Vs_output
- position
- texcoord

VS:
- do stuff with input and return output, i.e. Output.Tex = input.tex (pass through unmodified) and output.pos = input.pos mul some matrix

Fragment shader:
- do stuff with input.tex and input.pos etc.
- return final pixel color

Crealysm game & engine development: http://www.crealysm.com

Looking for a passionate, disciplined and structured producer? PM me

Which version of GLSL (and OpenGL) are you targetting? The answer is different depending on the version.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

well opengl es 2.0 for android, and opengl 3.3 for pc (i dont know the glsl versions of them)

Last question: are you using glVertexPointer/glTexCoordPointer or glVertexAttribPointer calls?

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

No i am doing such thing:


void SendToGPU()
{

	 glGenBuffers(1, &m_Buffer);
	 glBindBuffer(GL_ARRAY_BUFFER, m_Buffer);
	 glBufferData(GL_ARRAY_BUFFER, sizeof(TTGLVertex<T,float,float>) * header.LENGTH, AOS,
	                    GL_STATIC_DRAW);


    glBindBuffer(GL_ARRAY_BUFFER, m_Buffer);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,	sizeof(TTGLVertex<T,float,float>),	reinterpret_cast<void*>(offsetof(struct TFloatVertex, v)));
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TTGLVertex<T,float,float>),  reinterpret_cast<void*>(offsetof(struct TFloatVertex, t)));
    glVertexAttribPointer(3, 3, GL_FLOAT, GL_TRUE, 	sizeof(TTGLVertex<T,float,float>),  reinterpret_cast<void*>(offsetof(struct TFloatVertex, n)));
    glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(TTGLVertex<T,float,float>),  reinterpret_cast<void*>(offsetof(struct TFloatVertex, c)));
    glBindBuffer(GL_ARRAY_BUFFER, 0);

}

now to draw something i do


 void  DrawSimpleModel()
{
//if (is_same_type<T, double>::value == true)


	    glBindBuffer(GL_ARRAY_BUFFER, m_Buffer);
	    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,	sizeof(TTGLVertex<T,float,float>),	reinterpret_cast<void*>(offsetof(struct TFloatVertex, v)));
	    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(TTGLVertex<T,float,float>),  reinterpret_cast<void*>(offsetof(struct TFloatVertex, t)));
	    glVertexAttribPointer(3, 3, GL_FLOAT, GL_TRUE, 	sizeof(TTGLVertex<T,float,float>),  reinterpret_cast<void*>(offsetof(struct TFloatVertex, n)));
	    glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(TTGLVertex<T,float,float>),  reinterpret_cast<void*>(offsetof(struct TFloatVertex, c)));
//gl draw call
}

i dont really know what now i should even call to draw anything

yeah i need to know how to draw that since gl es 2.0 doesnt support VAOs i need to somehow draw that buffer i dont know how

i asked on the chat how to send data from vertex to fragment and i came with such shaders (dont mind reading them but i will post them anyway)


#version 100
#define GLES2

layout(location = 0) in vec3 pos;
layout(location = 1) in vec2 texcoord;
layout(location = 2) in vec3 normal;
layout(location = 3) in vec4 color;

//world(model) * view * projection matrix
in vec4 MVP1;
in vec4 MVP2;
in vec4 MVP3;
in vec4 MVP4;

//world matrix
in vec4 WM1;
in vec4 WM2;
in vec4 WM3;
in vec4 WM4;

//light position
in vec3 LPOS;
//light diffuse color
in vec3 LDIFF;
//light ambient color
in vec3 LAMB;



out vec4 fragColor;
out vec2 TexCoord;

float dp4(vec4 matrow, vec4 p)
{
return ( (matrow.x*p.x) + (matrow.y*p.y) + (matrow.z*p.z) + (matrow.w*p.w) );
}

float dp3(vec4 matrow, vec3 p)
{
return ( (matrow.x*p.x) + (matrow.y*p.y) + (matrow.z*p.z) );
}

float dot3(vec3 p1, vec3 p2)
{
return ( (p1.x*p2.x) + (p1.y*p2.y) + (p1.z*p2.z) );
}


vec4 vecMultipleComponents(vec3 A, vec3 B)
{
return vec3(A.x*B.x, A.y*B.y, A.z*B.z);
}


vec3 Normalize3(vec3 n)
{
float len = dot3(n,n);
len = sqrt(len);
vec3 res;

res.x = n.x / len;
res.y = n.y / len;
res.z = n.z / len;

return res;
}

vec3 vectorAB3(vec3 A, vec3 B)
{
return vec3(B.x-A.x, B.y-A.y, B.z-A.z);
}

vec3 vectors_add(vec3 A, vec3 B)
{
return vec3(A.x+B.x, A.y+B.y, A.z+B.z);
}


vec3 Clamp01(vec3 vert)
{
vec3 tmp;
tmp = vert;

if (tmp.x < 0.0) tmp.x = 0.0;
if (tmp.y < 0.0) tmp.y = 0.0;
if (tmp.z < 0.0) tmp.z = 0.0;


if (tmp.x > 1.0) tmp.x = 1.0;
if (tmp.y > 1.0) tmp.y = 1.0;
if (tmp.z > 1.0) tmp.z = 1.0;
return tmp;
}

 
void main()
{
//jakby cos sie pierdolilo to trzeba bedzie pos.w ustawic na 1.0
vec4 vertexClip;
vertexClip.x = dp4(MVP1, pos);
vertexClip.y = dp4(MVP2, pos);
vertexClip.z = dp4(MVP3, pos);
vertexClip.w = dp4(MVP4, pos);



//transform vertex to actual world position this is the most true position of all
vec4 vertexWorld;
vertexWorld.x = dp4(WM1, pos);
vertexWorld.y = dp4(WM2, pos);
vertexWorld.z = dp4(WM3, pos);
vertexWorld.w = dp4(WM4, pos);


vec4 TRANSFORMED_NORMAL;
vec4 TRANS_NORMAL_LEN;

//transform normal
TRANSFORMED_NORMAL.x = dp3(WM1, normal);
TRANSFORMED_NORMAL.y = dp3(WM2, normal);
TRANSFORMED_NORMAL.z = dp3(WM3, normal);
TRANSFORMED_NORMAL = Normalize3(TRANSFORMED_NORMAL); //since i rotate normalized vector this should be used


//vector from light to vertex

vec3 LIGHT_TO_VERTEX_VECTOR;
TEMP InvSqrLen;
//get direction from Light pos to transformed vertex
LIGHT_TO_VERTEX_VECTOR = vectorAB3(LPOS, vertexWorld); 

vec3 LIGHT_TO_VERTEX_NORMAL = Normalize3( LIGHT_TO_VERTEX_VECTOR );
//dot product of normalized vertex normal and light to vertex direction


float NORMAL_DOT = dot3(LIGHT_TO_VERTEX_NORMAL, TRANSFORMED_NORMAL);
 
vec4 NEW_VERTEX_COLOR;

NEW_VERTEX_COLOR.x = 0.0 - NORMAL_DOT;
NEW_VERTEX_COLOR.y = 0.0 - NORMAL_DOT;
NEW_VERTEX_COLOR.z = 0.0 - NORMAL_DOT;
NEW_VERTEX_COLOR = Clamp01( NEW_VERTEX_COLOR );

NEW_VERTEX_COLOR = vectors_add(NEW_VERTEX_COLOR, LAMB); //add ambient lighting color

NEW_VERTEX_COLOR = Clamp01( NEW_VERTEX_COLOR ); //clamp again

NEW_VERTEX_COLOR = vecMultipleComponents(NEW_VERTEX_COLOR, color);



        gl_Position = vertexClip;

        fragColor = NEW_VERTEX_COLOR;
        
		TexCoord = texcoord;

}



//ADD NEW_VERTEX_COLOR.x, 0.12, NEW_VERTEX_COLOR.x;
//ADD NEW_VERTEX_COLOR.y, 0.12, NEW_VERTEX_COLOR.y;
//ADD NEW_VERTEX_COLOR.z, 0.26, NEW_VERTEX_COLOR.z;


//#dodatkowe
//MUL NEW_VERTEX_COLOR.x, 0.5, NEW_VERTEX_COLOR.x;
//MUL NEW_VERTEX_COLOR.y, 0.5, NEW_VERTEX_COLOR.y;
//MUL NEW_VERTEX_COLOR.z, 0.5, NEW_VERTEX_COLOR.z;



frag


#version 100
#define GLES2

in vec4 fragColor;
in vec2 TexCoord;
uniform sampler2D tex;


int Floor(float k)
{
return int(k);
}

void main()
{

TEMP texcol;

vec2 FLOOR_TEX_COORD;

FLOOR_TEX_COORD.x = TexCoord.x - float(Floor(TexCoord.x));
FLOOR_TEX_COORD.y = TexCoord.y - float(Floor(TexCoord.y));

vec4 texcol = texture(tex, FLOOR_TEX_COORD);


gl_FragColor = fragColor * texcol;



}



//#version 150
//smooth in vec4 Color;
//smooth in vec2 UVf;
//uniform sampler2D tex;
//out vec4 outputColor;
//void main(){
//outputColor = Color * texture(tex, UVf);
//}

OK, brutal honesty time.

I can't help you here. OK, so I could just give you the code you need, but I don't think that's going to be in any way genuinely helpful. You're over-reaching your current ability here, you're trying to be too ambitious.

The fact that you don't appear to know about glDrawArrays or glDrawElements is pretty fundamental here. But yet you're trying to deal with cross-platform issues, you're trying to deal with porting and compatibility issues between OpenGL and GL ES, you're in at the deep end and you seem in over your head.

You don't need to be given code, you need some basic tutorial material, and you need to focus on a single platform while you're working through it so that you've minimal distractions from what you're learning. One thing at a time.

Sorry, and best of luck.

Direct3D has need of instancing, but we do not. We have plenty of glVertexAttrib calls.

so after calling glbindbuffer i call gldrawarrays huh? thats what i wanted to know LOL : d

This topic is closed to new replies.

Advertisement