Sign in to follow this  
WiredCat

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

Recommended Posts

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.
Edited by WiredCat

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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);
//}

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this