Jump to content
  • Advertisement
Sign in to follow this  
leonard2012

How to change color of graphics objects at runtime.

This topic is 2378 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'm new to D3D programming and now I have trouble in changing the color of graphics objects at runtime. The color of graphics objects should be changed when they are selected or highlighted, or user changes the visual properties of these graphics objects.
In my implementation, the graphics object is described by a C++ class CGraphics. There are two member variables of this class that are related to graphics rendering:

Vertex *mVertices; // object vertices
ID3D10Buffer* mVB; // vertex buffer

where data type Vertex is defined as

struct Vertex
{
D3DXVECTOR3 pos;
D3DXCOLOR color;
};

I know in D3D all graphics objects should be initialized before the draw routine. However, once the vertex values are assigned and vertex buffers are created, how can I change vertex color at runtime?
Here is the code I write to create vertex buffer:

D3D10_BUFFER_DESC vbd;
vbd.Usage = D3D10_USAGE_IMMUTABLE;
vbd.ByteWidth = sizeof(Vertex) * numVertices;
vbd.BindFlags = D3D10_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = 0;
vbd.MiscFlags = 0;
D3D10_SUBRESOURCE_DATA vinitData;
vinitData.pSysMem = mVertices;
HR(md3dDevice->CreateBuffer(&vbd, &vinitData, &mVB));

Share this post


Link to post
Share on other sites
Advertisement
Generally it's done by setting a vertex shader (or pixel shader) parameter.

Specifically, you want to provide a colour to multiply the result by (and / or a colour to add to the result).

Share this post


Link to post
Share on other sites

Generally it's done by setting a vertex shader (or pixel shader) parameter.

Specifically, you want to provide a colour to multiply the result by (and / or a colour to add to the result).


Thank you very much, I'll try your method.

Share this post


Link to post
Share on other sites

Generally it's done by setting a vertex shader (or pixel shader) parameter.

Specifically, you want to provide a colour to multiply the result by (and / or a colour to add to the result).

I tried your method and it works. In detail, I add an effect variable '

float4 gColor' to the effect file and update this variable whenever the object color is changed. However, in order to make change of effect variable taking into effect, the effect techniques have to re-applied.

// Change effect variable mfxPenColorVar and reapply the techniques.
mfxPenColorVar->SetFloatVector((float *)&penColor_.r);
D3D10_TECHNIQUE_DESC techDesc;
mTech->GetDesc( &techDesc );
for(UINT p = 0; p < techDesc.Passes; ++p) {
mTech->GetPassByIndex(p)->Apply(0);
}


I wonder if too many applications of effect techniques lead to performance problem.

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!