Sign in to follow this  

GLSL frag shader position not set

This topic is 813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I created a shader in shader maker it works great when I go to add it into my program it wasnt working and I believe to have narrowed it down to the position is not being set I use this to set my position

vec4 position = gl_TexCoord[0];

and I just copied shader makers vertex

// simple vertex shader

void main()
{
	gl_Position    = gl_ModelViewProjectionMatrix * gl_Vertex;
	gl_FrontColor  = gl_Color;
	gl_TexCoord[0] = gl_MultiTexCoord0;
}

I can manually set the gl_FragColor and that works just fine the rectangles color will change

I dont know if there is something that I have to enable or initialize for gl_Texcoord does someone know what I am missing or doing wrong?

I am using c on linux glx window gl 3.3 Thanks

Share this post


Link to post
Share on other sites

I created this simpler frag shader to help visualize and narrow down the issue

// fragment shader

vec4 position = gl_TexCoord[0];


void main(){

	// Calculate distance to center
	float d = distance(position.xy, vec2(0.5, 0.5));

	vec3 color = vec3(0.0,0.0,0.0);

	color += d;
	gl_FragColor = vec4(color, 1.0);

}

Heres how shader maker looks versus my program the shader maker one is the circle

 

Share this post


Link to post
Share on other sites

why dont you remove


vec4 position = gl_TexCoord[0];

and don't use directly your coordinates? anyway I would move the above line in the main function?

I am just guessing never used shader maker etc, but maybe shader maker is doing some extra step for you that in your stand alone case doesn't happen?

I have a feeling that your position get set once and never computed again, thus the uniform  color

Share this post


Link to post
Share on other sites

I set a position because its different on gles so I have a if gles do this else do what its doing now

 

I have changed it to not include that but it doesnt change the output :(

// fragment shader


void main(){

	// Calculate distance to center
	float d = distance(gl_TexCoord[0].xy, vec2(0.5, 0.5));

	vec3 color = vec3(0.0,0.0,0.0);

	color += d;
	gl_FragColor = vec4(color, 1.0);

}

Its still the same uniform grey

Share this post


Link to post
Share on other sites

My vertex shader has gl_position
gl_Position    = gl_ModelViewProjectionMatrix * gl_Vertex;
Is that the same thing I am looking for can I use it instead of texture coordinates?


I'm going to assume that you are sending vertices to the shader GPU and that those vertices include UV coordinates. Am I correct?

Edited by MarkS

Share this post


Link to post
Share on other sites

Your assuming a bit too much lol

This is my first shader program my only otheer opengl experience has been 1.5 fixed function and even that was limited experience

 

I was following this tutorial

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-2-the-first-triangle/

 

got it working got a shader working with colors and took my shader from shader maker and must have jumped some steps I figured since I am not using a texture I didnt need all that

 

I will look into the textured tutorial and learn some more and see about uv coordinates

Share this post


Link to post
Share on other sites

Shaders are not magical bits of code that make things appear on screen. You still must send geometry to the GPU. If you are not sending geometry, what does the shader have to work on? UV (texture) coordinates are required for the shader code you posted, along with standard x/y/z coordinates.

Share this post


Link to post
Share on other sites

ok sorry, I don't want to throw you off road, I need to understand better your setup.

By doing a quick googling looks like shader maker is a shader editor, which in your case I am guessing provides you a "plane" (2 triangles) on which you can draw with your fragment shader, in a similar fashion shader toy does correct?

What is your "program" ?

Are you handling everything yourself? If so I would have to have a buffer with your mesh and a buffer with the uv coordinates for each vertex. Do you have this kind of setup?

 

did not see @MarkS, but yeah that s basically what I am trying to figure out if you sent properly data on the gpu

Edited by giordi

Share this post


Link to post
Share on other sites

@MarkS yea I didnt know where those things like gl_MultiTexCoord0, gl_TexCoord[0], gl_Position came from and that apparently the issue I need to send them somehow which I am trying to figure out how to do

 

@giordi Yea it looks like it works the same way as shader toy but for the desktop so its regular opengl not es/webgl

My current "program" is just a splash screen then after that I will venture into showing a single texture that will update via webkit and go from there

I am handling everything myself x11 opengl in c not glut no glew nothing else

my buffer is just a rectangle

static const GLfloat g_vertex_buffer_data[] = { 
    -0.5f, -0.5f, 0.0f,
    -0.5f, 0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    0.5f, 0.5f, 0.0f
};

I am reading this tutorial and trying to figure out the uv coordinates now

http://www.opengl-tutorial.org/beginners-tutorials/tutorial-5-a-textured-cube/

Share this post


Link to post
Share on other sites

I am having a problem understanding how  gl_TexCoord[0] gets set I think that tutorial uses a newer shader type than what I have been using

gl_TexCoord[0] is in both the vertex and the frag shaders but no where am I setting it and that tutorial doesnt seem to set it

I am sending uv coordinates now but I think I am just sending them blindly

 

I create the uv array then send it like this

// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(
	1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
	2,                                // size : U+V => 2
	GL_FLOAT,                         // type
	GL_FALSE,                         // normalized?
	0,                                // stride
	(void*)0                          // array buffer offset
);

but nowhere do I see how to set something useful like gl_TexCoord[0]

Share this post


Link to post
Share on other sites

It seems like I need something like this

glBegin(GL_QUADS);
    glTexCoord2f(0, 0);glVertex2f(0, 0);
    glTexCoord2f(1, 0);glVertex2f(100, 0);
    glTexCoord2f(1, 1);glVertex2f(100, 100);
    glTexCoord2f(0, 1);glVertex2f(0, 100);
glEnd();

but not fixed pipeline

Share this post


Link to post
Share on other sites

naa you don't need fixed pipeline, it s legacy, buffers are perfectly fine, here check my sprite renderer component

CmpSprite::CmpSprite(const string path,
					 const uint32_t u_reps,
					 const uint32_t v_reps) : 
					 m_path(path), 
				     m_texture(0, path.c_str()),
					 m_u_reps(u_reps),
				     m_v_reps(v_reps)
{
	

    ANIM_RATE = Constants::TICKS_PER_SEC/12;
    action_counter =0;    
    
    int w = m_texture.get_width();
	int h = m_texture.get_height();
	w /= m_u_reps;
	h /= m_v_reps;
	float hh = float(h / 2);
	float wh = float(w / 2);
	//TODO (giordi) : remove magic number and give a default from constants
	float  z = -13;

	//generating the quad data
    float data[18]{ -wh, -hh, z,
					-wh,  hh, z,
					wh ,  hh, z,
					-wh, -hh, z,
					wh,  hh,  z,
					wh, -hh,  z};
    //generating the uv data
	float max_u = 1.0f / m_u_reps;
	float max_v = 1.0f / m_v_reps;
	float uvs[12] = { 0.0f, 0.0f, 
					  0.0f,max_v,
					  max_u,max_v,
					  0.0f,0.0f, 
					  max_u,max_v,
					  max_u,0.0f};
//uploading data on the buffer	
    m_vtx.set_data(data, 18);
	m_uvs.set_data(uvs, 12);

}

so  as you can see I generate the rectangly in a similar way as you do, but I also generate  values for the uv map where basically the bottom left corner is 0,0 uv coordinates and the top right is 1,1.

Then you can upload the data in the same way you normally upload buffers, of course you will need a different attribute for texture coordinates in the shaders.

Share this post


Link to post
Share on other sites

Thank you guys

 

I was having the hardest time figuring out why the newer shaders with in and outs wasnt working at all then I found a few lines I missed that I didnt think were important

GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

Once I got that and set the uvcoordinates I got it working better but my coordinates are off shown in picture

How do I get the correct coordinates?

Share this post


Link to post
Share on other sites

that s how i did,

I generated two triangles (in yellow), i did it in clock wise order starting from bottom left. so using vertices (0,1,2), (3,4,5).

You will need to repeat the matching vertices since you are not using an index buffer. 

Anyway then you build a similar data structure but for the uv (in red). where i start again from bottom left, and I follow the exact same order  i did for the vertex data, after all you need to specify a uv coordinate for each vertex so make sense to follow the same order so opengl will know how to loop the data.

Share this post


Link to post
Share on other sites

This topic is 813 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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