Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6683881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:52:27+00:00 2026-05-26T04:52:27+00:00

I find this annoying but is there a better way to retrieve a struct

  • 0

I find this annoying but is there a better way to retrieve a struct parameter from a Cg shader?

In the 11_two_light_with_structs example (OpenGL), there’s a struct Material in the shader:

Cg shader:

// From page 128
struct Material {
  float3 Ke;
  float3 Ka;
  float3 Kd;
  float3 Ks;
  float shininess;
};

Then in the code they do this:

C code:

myCgVertexParam_material_Ke = cgGetNamedParameter( myCgVertexProgram, "material.Ke" );
myCgVertexParam_material_Ka = cgGetNamedParameter( myCgVertexProgram, "material.Ka");
myCgVertexParam_material_Kd = cgGetNamedParameter( myCgVertexProgram, "material.Kd");
myCgVertexParam_material_Ks = cgGetNamedParameter( myCgVertexProgram, "material.Ks");

Seems tedious, can’t you just do

myCgVertexParam_materialALL = cgGetNamedParameter( myCgVertexProgram, "material" ) ;

Then something like:

cgSetParameterValuefr( myCgVertexParam_materialALL, 13, brassMat ) ;//! no.

Here I’m trying to treat the struct as an array, but that last command doesn’t work though, with the error "The parameter is not of a numeric type."

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:52:27+00:00Added an answer on May 26, 2026 at 4:52 am

    Cg just does it this way, and it is what it is.

    HLSL/D3D11 does improve this with the concept of CBUFFERS. What you end up being able to do is create a C-style struct in your C++ code that mirrors all the parameters in your shader, and flushing that all at once to the gpu, without having to do it by silly variable name:

    C++ code:

    struct GPUCDATA
    {
      // the concatenated modelview-projection matrix
      float modelViewProj[16] ;  // column major
      float diffuseLightPos[4] ;
      float diffuseLightColor[4] ;
      float specularLightPos[4] ;
      float specularLightColor[4] ;
      float eyePos[4] ;
    } ;
    

    hlslShader.vsh

    cbuffer Globals : register( b0 )
    {
      // the concatenated modelview-projection matrix
      float4x4 modelViewProj ;
      float4 diffuseLightPos ;
      float4 diffuseLightColor ;
      float4 specularLightPos ;
      float4 specularLightColor ;
      float4 eyePos ;
    } ;
    

    Then from C++:

    // gpu is the ID3D11DeviceContext
    D3D11_MAPPED_SUBRESOURCE mappedRes ;
    CHECK_HRESULT( gpu->Map( gpuCDataBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedRes ), "Map gpucdatabuffer" );
    GPUCDATA* gpuMems = (GPUCDATA*)mappedRes.pData ;
    memcpy( gpuMems, &gpuCData, sizeof( GPUCDATA ) ) ; // write to the gpu mems.
    gpu->Unmap( gpuCDataBuffer, 0 ) ;
    gpu->VSSetConstantBuffers( 0, 1, &gpuCDataBuffer );
    gpu->PSSetConstantBuffers( 0, 1, &gpuCDataBuffer );
    

    And that’s it!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way I can do this? I find it annoying having to
I find this syntax astoundingly annoying. Every time I rename my class, I have
Alright, this specific layout is just annoying me. And can't seem to find a
i find this question tricky but what files store method signature info for xml
I was trying to find this answer on Google, but I guess the symbol
I tried to find this in the relevant RFC, IETF RFC 3986 , but
somehow couldn't find this with a google search, but I feel like it has
I guess this was asked before, but I could not find any similar question.
I've been using MessageFormat.format() for a while, but there's one thing that I find
I don't think this is possible, but if it is, I'd find it very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.