Texture Access in Shader

Started by
4 comments, last by absriram 19 years, 5 months ago
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.
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
When General Patton died after World War 2 he went to the gates of Heaven to talk to St. Peter. The first thing he asked is if there were any Marines in heaven. St. Peter told him no, Marines are too rowdy for heaven. He then asked why Patton wanted to know. Patton told him he was sick of the Marines overshadowing the Army because they did more with less and were all hard-core sons of bitches. St. Peter reassured him there were no Marines so Patton went into Heaven. As he was checking out his new home he rounded a corner and saw someone in Marine Dress Blues. He ran back to St. Peter and yelled "You lied to me! There are Marines in heaven!" St. Peter said "Who him? That's just God. He wishes he were a Marine."
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
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?
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);


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

This topic is closed to new replies.

Advertisement