This tutorial is part of a Collection: 01. DirectX 9 - Braynzar Soft Tutorials
rate up
0
rate down
1997
views
bookmark
06. Color

This is a short tutorial showing you how to add color to your polygons.

There are no files for this tutorial
Here we will learn how to add color. In Direct3D, Color is defined by three values, Red, Green, and Blue(RGB). By mixing Red, Green and Blue, we get tons of different colors. We can use three different types to hold our RGB data. One is the D3DCOLOR, which is a DWORD and is 32-bits. It is divided into four 8-bit sections, Red, Green, Blue, and Alpha. Each section gets a byte of memory, so that means the intensity of the section can range from 0 (low intensity) to 255(high intensity). Specifying colors in the D3DCOLOR type requires bit operations. Direct3D has a macro called D3DCOLOR_ARGB(A, R, G, B) that will do the math for us. It has four parameters, each value being 0-255, and one for apha(blending, which we will cover in a later lesson), one for red, one for green, and one for blue. There is another macro, D3DCOLOR_XRGB(R, G, B), which only takes 3 parameters, red, green and blue. It automatically sets the alpha value to 0xFF(or 255). Another type to store color is the D3DCOLORVALUE structure. It has the same four sections, but each section uses a float value between 0.0f and 1.0f to define the intensity. The other type is D3DXCOLOR. This has the same 4 data members as D3DCOLORVALUE so you can change between them without problems. The only difference is this type has extra constructors and overloaded operators, which make color manipulation easier. The color of primitives depends on the color of the vertices that make up the primitive. So the first thing we'll do is add a color member to our vertex structure. We cannot use D3DCOLORVALUE because Direct3D expects a 32-bit type to define the color of our vertex. In a later chapter, we will discuss vertex shaders, which will let us use a 128-bit type instead of a 32-bit type. Here is our new Vertex Structure... struct Vertex { FLOAT x, y, z; D3DCOLOR color; }; After we add color into our vertex structure, we need to let D3D know the new format of our vertex. We do this by adding in D3DFVF_DIFFUSE to our Flexible Vertex Format(FVF). const DWORD VertexFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE; Now that we have our new vertex format set, we need to add in color to our primitives. All we have to do is add in the color value for each vertex. TriangleVertexBuffer->Lock(0, 0, (void**)&vertices, 0); vertices[0].x =-0.5f; vertices[0].y = 0.0f; vertices[0].z = 0.0f; vertices[0].color = D3DCOLOR_XRGB(255, 0, 0); vertices[1].x = 0.0f; vertices[1].y = 0.5f; vertices[1].z = 0.0f; vertices[1].color = D3DCOLOR_XRGB(0, 255, 0); vertices[2].x = 0.5f; vertices[2].y = 0.0f; vertices[2].z = 0.0f; vertices[2].color = D3DCOLOR_XRGB(0, 0, 255); TriangleVertexBuffer->Unlock(); SquareVertexBuffer->Lock(0, 0, (void**)&vertices, 0); vertices[0].x =-0.5f; vertices[0].y = 0.5f; vertices[0].z = 0.0f; vertices[0].color = D3DCOLOR_XRGB(255, 0, 0); vertices[1].x = 0.5f; vertices[1].y = 0.5f; vertices[1].z = 0.0f; vertices[1].color = D3DCOLOR_XRGB(0, 255, 0); vertices[2].x =-0.5f; vertices[2].y =-0.5f; vertices[2].z = 0.0f; vertices[2].color = D3DCOLOR_XRGB(0, 0, 255); vertices[3].x = 0.5f; vertices[3].y =-0.5f; vertices[3].z = 0.0f; vertices[3].color = D3DCOLOR_XRGB(255, 0, 0); SquareVertexBuffer->Unlock(); Lets disable lighting because since we don't have lighting set up, it could cause some visual problems. Like showing no color and only black for our primitives. d3dDevice->SetRenderState(D3DRS_LIGHTING, false); Shading computes the vertex colors to determine what the pixel colors will be in the primitive. We have two to choose from, flat shading and Gouraud shading. Flat shading takes the first vertex color, and applies it to the rest of the vertex's in that primitive, so that primitive will be one solid color. Gouraud shading will take the colors at each vertex and smoothly translate them to the other colors accross the primitive. We can change the default shading using the render state method. Gouraud shading starts out as the default shading method. //Flat shading d3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_FLAT); //Gouraud shading d3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); d3dDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD); Our last tutorial rendered our polygons in WIREFRAME mode, so we should change this. Since D3DRS_FILLMODE is set to D3DFILL_SOLID by default, you can either delete, comment, or change the renderstate that says to render in wireframe mode. If you want to change it, change D3DFILL_WIREFREAME to D3DFILL_SOLID. d3dDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_SOLID); Run your program and hopefully your polygons or whatever will show a little more color than our last lessons. I hope you learned something from this! Our next lessons will be on 3D Rotations and the index buffer, or maybe the view and projection matrices. Whatever it is, see ya then! Here's the final code: main.cpp ... ... //Vertex structure struct Vertex { FLOAT x, y, z; D3DCOLOR color; //add color to vertex structure }; //Define the Flexible vertex format const DWORD VertexFVF = D3DFVF_XYZ | D3DFVF_DIFFUSE; ... ... bool SetupScene(){ //Set up our Scene ... ... //Lock vertex Buffer TriangleVertexBuffer->Lock(0, 0, (void**)&vertices, 0); vertices[0].x =-0.5f; //Left vertex vertices[0].y = 0.0f; vertices[0].z = 0.0f; vertices[0].color = D3DCOLOR_XRGB(255, 0, 0); vertices[1].x = 0.0f; //Top vertex vertices[1].y = 0.5f; vertices[1].z = 0.0f; vertices[1].color = D3DCOLOR_XRGB(0, 255, 0); vertices[2].x = 0.5f; //Right vertex vertices[2].y = 0.0f; vertices[2].z = 0.0f; vertices[2].color = D3DCOLOR_XRGB(0, 0, 255); TriangleVertexBuffer->Unlock(); //UnLock vertex buffer //Lock vertex buffer SquareVertexBuffer->Lock(0, 0, (void**)&vertices, 0); vertices[0].x =-0.5f; vertices[0].y = 0.5f; vertices[0].z = 0.0f; vertices[0].color = D3DCOLOR_XRGB(255, 0, 0); vertices[1].x = 0.5f; vertices[1].y = 0.5f; vertices[1].z = 0.0f; vertices[1].color = D3DCOLOR_XRGB(0, 255, 0); vertices[2].x =-0.5f; vertices[2].y =-0.5f; vertices[2].z = 0.0f; vertices[2].color = D3DCOLOR_XRGB(0, 0, 255); vertices[3].x = 0.5f; vertices[3].y =-0.5f; vertices[3].z = 0.0f; vertices[3].color = D3DCOLOR_XRGB(255, 0, 0); SquareVertexBuffer->Unlock(); //UnLock vertex buffer d3dDevice->SetRenderState(D3DRS_LIGHTING, false); return true; } ... ...