Jump to content
  • Advertisement
Sign in to follow this  

Vertex Shader - verts to NDC

This topic is 4040 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 am trying to transform the vertices of a polygon into NDC coordinates (0 to 1) to be used as UV texture coordinates. This is being done in a vertex shader. It seems simple enough. (You can imagine a small rectangle polygon on the screen. It should then be as if I am using scissoring or clip planes to ONLY render the part of the scene that falls within those polygon boundaries). Here is what I am doing:
void main(void)
	vec4 pos = ftransform();
	gl_Position = pos;

        // This turns the position into NDC coordinates, ranging -1 to 1.
	vec2 ndcPos = vec2(pos.x / pos.w, pos.y / pos.w);

        // Now turn that into a 0 to 1.0 range.
	ndcPos = vec2((pos.x + 1.0) / 2.0, (pos.y + 1.0) / 2.0);

        // Use that as the texture coordinate.
	gl_TexCoord[0].s = ndcPos.x;
	gl_TexCoord[0].t = ndcPos.y;
The fragment shader is simply:
gl_FragColor = texture2D(tex, gl_TexCoord[0].st);
Something is clearly not right, because when I debug (painting red in pixels where the U/V are 0-1 and green when outside the range), the window is mostly green with a much-too-small red box that enlarges as I move very close. When the polygon is on-screen, it should always been red, right? Any ideas? This seems like it should be easy to do.
edit: dumb typo :((( [Edited by - Doggan on October 2, 2007 5:30:32 AM]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!