Jump to content
  • Advertisement
Sign in to follow this  
mikeman

Small Cg question

This topic is 5472 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

Ok,I've recently switched from GLSL to Cg,because GLSL is still very buggy.But I have a small question:I see that Cg,like GLSL,uses sampler2D uniform variables for texturing. Using GLSL,you load the textures using glBindTexture into the desired texture unit,set the sampler2D variable to that texture unit(=0,1,3,4...),and the fragment program samples whatever texture object is currently bound in that texture unit. From what I understand,Cg is different.It requires you to load the sampler2D variable with the texture name,not the texture unit.Am I correct?Does that mean that I don't need glBindTexture() calls while rendering,and I need to replace them with cgGLSetTextureParameter()?

Share this post


Link to post
Share on other sites
Advertisement
I don't know if I'm understanding you completely, but here's a rundown on how to bind a texture to a geometry chunk:

Quote:
glActiveTextureARB( GL_TEXTURE0_ARB );
glBindTexture(GL_TEXTURE_2D, GLid);

cgGLSetTextureParameter( Handle, GLid);
cgGLEnableTextureParameter( Handle );


Handle: This is the habdle to the texture parameter on the Cg Script.
GLid: This is the texture Id you get when you load up and bind the texture.

(tip: use more paragraphs in your posts, makes them easier to read, and reply [wink]).

Share this post


Link to post
Share on other sites
Quote:
Original post by Prozak
I don't know if I'm understanding you completely, but here's a rundown on how to bind a texture to a geometry chunk:

Quote:
glActiveTextureARB( GL_TEXTURE0_ARB );
glBindTexture(GL_TEXTURE_2D, GLid);

cgGLSetTextureParameter( Handle, GLid);
cgGLEnableTextureParameter( Handle );


Handle: This is the habdle to the texture parameter on the Cg Script.
GLid: This is the texture Id you get when you load up and bind the texture.

(tip: use more paragraphs in your posts, makes them easier to read, and reply [wink]).


That's what I'm wondering about.Do I really need to use glBindTexture()?I mean,cgGLSetTextureParameter seems to do the same thing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster

glActiveTextureARB( GL_TEXTURE0_ARB );
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, GLid1);


glActiveTextureARB( GL_TEXTURE1_ARB );
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, GLid2);


Does the trick for me. I dont think you actually need:


cgGLSetTextureParameter( Handle, GLid);
cgGLEnableTextureParameter( Handle );

Share this post


Link to post
Share on other sites
The only Cg knows about Handle is that it is a handle to a variable of type "texture" (or "map" if you prefer).

You need to tell OpenGL 3 things, under a multi-texture render context:
Which Texture Unit: glActiveTextureARB( GL_TEXTURE0_ARB );
Texture Type: glBindTexture(GL_TEXTURE_2D, GLid);
Handle to Texture: glBindTexture(GL_TEXTURE_2D, GLid);

Then you need to tell Cg some information too:
Handle is "glued" to which texture id?: cgGLSetTextureParameter( Handle, GLid);
Enable the Handle/Texture: cgGLEnableTextureParameter( Handle );

So in short, yes, yes you need to tell OpenGL and Cg all of that information, each step acomplishes something necessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by Prozak
The only Cg knows about Handle is that it is a handle to a variable of type "texture" (or "map" if you prefer).


Not true.Handle is a uniform parameter declared inside the fragment program,and it can be type of sampler2D,sampler3D,samplerCUBE...so Cg knows what kind of texture it is.So,a sampler variable holds:

1)The type of texture(based on the type of the sampler).
2)The texture object(by value).
But there is no way to associate a sampler with a texture unit.You can retrieve it with cgGLGetTextureEnum,but there is no function to set it.I guess you can use the return value to get the texture unit,and then use that to bind the texture with glBindTexture().

[Edited by - mikeman on September 24, 2004 9:09:58 AM]

Share this post


Link to post
Share on other sites
To set a texture for a cg program you have to query the handle of the texture parameter via its name (CGparameter cgGetNamedParameter(CGprogram prog, const char * name)).
Then you can use your texture created with OGL and attach it to the shader with cgGLSetTextureParameter(CGparameter handle_of_the_cg_texture_parameter, GLuint opengl_texture_id).
To activate your texture that it is used with the program you have to call cgGLEnableTextureParameter(CGparameter handle_of_the_cg_texture_parameter). You can disable the texture with cgGLDisableTextureParameter(CGparameter handle_of_the_cg_texture_parameter).

If you don't want to manage cgGLEnableTextureParameter/cgGLDisableTextureParameter calls and want them to be set automatically you can tell this cg with a call to cgGLSetManageTextureParameters(CGcontext ctx, CGbool flag) and flag set to CG_TRUE (it's disabled by default).
CG will the enable all textures attached to a certain program when you bind the program. To reset the texture state you should unbind the program again.

If you want to manage texture states all by yourself (via OpenGL calls) you can query for the texture stage you have to bind your texture to with a call to cgGLGetTextureEnum(CGparameter handle_of_the_cg_texture_parameter). It returns the proper GLenum that can be used to activate the correct texture stage with glActiveTexture.

Instead of querying the texture unit you can use a TEXUNITn semantic when you declare your uniform texture sampler in your cg program but there are some limitations when you want to compile to basic profiles.

Share this post


Link to post
Share on other sites
Quote:
Original post by now
To set a texture for a cg program you have to query the handle of the texture parameter via its name (CGparameter cgGetNamedParameter(CGprogram prog, const char * name)).
Then you can use your texture created with OGL and attach it to the shader with cgGLSetTextureParameter(CGparameter handle_of_the_cg_texture_parameter, GLuint opengl_texture_id).
To activate your texture that it is used with the program you have to call cgGLEnableTextureParameter(CGparameter handle_of_the_cg_texture_parameter). You can disable the texture with cgGLDisableTextureParameter(CGparameter handle_of_the_cg_texture_parameter).

If you don't want to manage cgGLEnableTextureParameter/cgGLDisableTextureParameter calls and want them to be set automatically you can tell this cg with a call to cgGLSetManageTextureParameters(CGcontext ctx, CGbool flag) and flag set to CG_TRUE (it's disabled by default).
CG will the enable all textures attached to a certain program when you bind the program. To reset the texture state you should unbind the program again.

If you want to manage texture states all by yourself (via OpenGL calls) you can query for the texture stage you have to bind your texture to with a call to cgGLGetTextureEnum(CGparameter handle_of_the_cg_texture_parameter). It returns the proper GLenum that can be used to activate the correct texture stage with glActiveTexture.

Instead of querying the texture unit you can use a TEXUNITn semantic when you declare your uniform texture sampler in your cg program but there are some limitations when you want to compile to basic profiles.


Yes,that's what I thought too.Thanks!

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!