Jump to content
  • Advertisement
Sign in to follow this  
absriram

OpenGL Texture Access in Shader

This topic is 5058 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 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
Advertisement
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
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!