Sign in to follow this  
absriram

OpenGL Texture Access in Shader

Recommended Posts

I want to paint a chess board texture onto a quad using Cg. Can someone tell me how to do the openGL part of it. I know I have to use sampler2D in the shader. Thanks, Sriram.

Share this post


Link to post
Share on other sites
wait... why the heck arent you just using textures? theres absolutely no use for fragment shaders here... unless your asking how you would write a shader to show textured crap... i could show you that (but only in ARB fragment programs)

in summation... huh?
-Dan

Share this post


Link to post
Share on other sites
It was just an example, so that I could learn how to use the sampler2D function along with the OGL part. I have lot more things to do in Cg using shaders. I am going to do some comparisons with previously rendered textures to perform blending, collision detection etc. I want to do those stuff in the GPU instead of in CPU.

Thanks,

Sriram

Share this post


Link to post
Share on other sites
I'm assuming you've already got the cgcontex, profiles and your program compiled, loaded and an parameter added.

Than you need something like this:




cgGLEnableProfile(cgFragmentProfile);
cgGLBindProgram(program);
glEnable(GL_TEXTURE_2D);
cgGLSetTextureParameter(parameter, texID);
cgGLEnableTextureParameter(parameter);


glBegin( GL_QUADS );
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 0);
glVertex2i(0, 0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 0);
glVertex2i(w, 0);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 1, 1);
glVertex2i(w, h);
glMultiTexCoord2fARB(GL_TEXTURE0_ARB, 0, 1);
glVertex2i(0, h);
glEnd();

cgGLUnbindProgram(cgFragmentProfile);
cgGLDisableProfile(cgFragmentProfile);
cgGLDisableTextureParameter(parameter);





That draws a 2D quad to the screen.

Is that what you needed, or you do you need help writing the cg program and loading it?

Share this post


Link to post
Share on other sites
You even don't have to enable GL_TEXTURE_2D.
OpenGL "knows" from the fragment program that the texture is used.

Alternatively, you can also use the binding semantics to tell the FP which texture to use. For example, if you use
uniform sampler2D texture : TEXUNIT0

in the FP, the sampler2D parameter will be automatically bound to the texture which you set by
glBindTexture(GL_TEXTURE_2D, myTextureID);

in the OpenGL-Code. You don't need the
cgGLSetTextureParameter(parameter, texID);
cgGLEnableTextureParameter(parameter);

calls any more, and you still don't need
glEnable(GL_TEXTURE_2D);


Share this post


Link to post
Share on other sites
Hold On guys, I think I lost u in the beginning. Here's what I understood from the Cg Tutorial Handbook,

When you bind a texture to the sampler2D handle, and use
OUT.color.xyz = tex2D(sampler2D, texCoord);

inside the fragment shader, the texel values from the texture at the coordinate specified by texCoord will be set as the color of the outgoing fragment. The texCoord values will be passed on from the vertex shader to the fragment shader.

Am I right? If so, what is the need for the specifying the texture coordinates in the OGL code using glMultiTexCoord2f?

Also, please have a look at

http://www.gamedev.net/community/forums/topic.asp?topic_id=272311

I couldn't get my program to work when I followed the steps mentioned there. My polygons are all black, no texturing is done.

Thanks,

Sriram

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