How to make multiple lights non-additive in opengl - c++

I'm trying to get a model working which has 8 different light sources. I would like to make it so that a surface reaches its "maximum" value when near one light source, but the way the code works now if multiple lights are near a surface, it just keeps increasing in how lit up it is until it is almost pure-white.
For surfaces, I use:
float white[] = {1,1,1,1};
float black[] = {0,0,0,1};
float medium[] = {.5,.5,.5,.5};
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,white);
glMaterialfv(GL_FRONT_AND_BACK,GL_EMISSION,black);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,medium);
And for the eight lights:
int glLightConstant = GL_LIGHT0 + i;
float Diffuse[] = {(float)0.01*50 ,(float)0.01*50 ,(float)0.01*50 ,1.0f};
float Specular[] = {(float)0.01*0,(float)0.01*0,(float)0.01*0,1.0f};
float Position[] = {(float)positions[i].x,(float)positions[i].y,(float)positions[i].z,(float)1.0};
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER,0.0);
glColorMaterial(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(glLightConstant);
if(i == 0) {
float Ambient[] = {(float)0.01*0 ,(float)0.01*0 ,(float)0.01*0 ,1.0f};
glLightfv(glLightConstant,GL_AMBIENT ,Ambient);
}
glLightfv(glLightConstant,GL_DIFFUSE ,Diffuse);
glLightfv(glLightConstant,GL_SPECULAR,Specular);
glLightfv(glLightConstant,GL_POSITION,Position);
I realize that the ambient light is 0, I might change it later.

Related

RayTracing, Phong model/specular lighting not working?

I've been trying to produce a glossy shading using the Phong model but for some reason instead of glossy appearance, all I get is big white blotch on the front of the sphere. Initially the model worked for a single sphere, but now I've updated the code so I can draw multiple spheres and the model has started to fail despite applying the same logic and I don't know why.
single sphere: diffuse and specular
diffuse, multiple
diffuse+specular, multiple
main part
vec color(const ray& r)
{
vector <sphere> objects;
vector <Light> lighting;
objects.push_back(sphere(vec(0,-100.5,-3), 100, vec(0, 1, 0)));
objects.push_back(sphere(vec(0, 0, -1), 0.5, vec(1, 0, 0)));
objects.push_back(sphere(vec(0, 1 ,-1), 0.5, vec(1, 0, 1)));
lighting.push_back(Light(vec(0, 0, -1), vec(0, -1, 0)));
float infinity = 2000.0;
sphere* closest = NULL;
vec background_color( .678, .847, .902);
vec totalLight(0.0, 0.0, 0.0);
int pos = 0;
for(int j = 0; j < objects.size(); j++)
{
float t = objects[j].intersect(r);
if(t > 0.0)
{
if(t < 2000.0)
{
infinity = t;
closest = &objects[j];
pos = j;
}
}
}
if(infinity == 2000.0)
return background_color;
else
{
float a = objects[pos].intersect(r);
vec view_dir = vec(-2, 2, 10) - r.p_at_par(a);
vec normal = unit_vector((r.p_at_par(a) - closest->centre)/closest->radius);
vec light = unit_vector(vec(-2, 0, 0) - r.p_at_par(a));
vec reflection = 2.0*dot(light, normal)*normal - light;
vec specular = vec(1, 1, 1)*pow(max(0.f, dot(reflection, view_dir)), 256);
vec diffuse = (closest->color)*max(0.f, dot(normal, light));
vec total = diffuse + specular;
return total;
}
}
as I understand, specular = white * dot(view_dir, L_dir)^n * ks and the total lighting is = specular + diffuse + ambient.
You are indeed right on your specular contribution. You can see it as how much light is reflected into my viewing direction.
First of all I don't see you normalising view_dir. Make sure all vectors are normalised. If a and b have length 1 the next is true
Also to help debugging in the future you may want to generate false color images. This images can help you see what's going on. e.g you can just render your flat color, surface normals (xyz to rgb), the number of light sources affecting a certain pixel, ... . This may help you spotting unexpected behaviours.
Hope this helps.

Bad lighting behaviour (fixed pipeline)

I'm trying to render an object (say cube) with OpenGL 1.1 (I know that doesn't makes sense nowadays, but I've to use this). Everything works fine until I try some lighting.
Here's the problem:
The Global variable set are:
static GLfloat light_position[] = {1.0, 1.0, 2*cZ.x , 0.0};
// cZ.x is the minimum z of the mesh. I know
// this is at infinity, but don't work also with w=1.0
In the main function:
...
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
....
Drawing a mesh k
// While drawing mesh k
GLfloat light_ambient[] = {COLOUR[k][0], COLOUR[k][1], COLOUR[k][2], 1.0};
GLfloat light_diffuse[] = {COLOUR[k][0], COLOUR[k][1], COLOUR[k][2], 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
....
//This is a mesh, so will be drawn using triangles
glBegin(GL_TRIANGLES);
//Triangles will be defined by vertex indices in faces
for (unsigned int i = 0; i<mesh->faces.size(); i++){
int index1 = mesh->faces.at(i).x;
int index2 = mesh->faces.at(i).y;
int index3 = mesh->faces.at(i).z;
glNormal3f(mesh->normals.at(i).x,mesh->normals.at(i).y,mesh->normals.at(i).z);
glVertex3f(mesh->vertices.at(index1).x, mesh->vertices.at(index1).y, mesh->vertices.at(index1).z);
glVertex3f(mesh->vertices.at(index2).x, mesh->vertices.at(index2).y, mesh->vertices.at(index2).z);
glVertex3f(mesh->vertices.at(index3).x, mesh->vertices.at(index3).y, mesh->vertices.at(index3).z);
}
glEnd();
....
Whereas the normal are computed as:
glm::vec3 currFace = m->faces.at(faceIndex);
glm::vec3 vert1 = m->vertices.at(currFace.x);
glm::vec3 vert2 = m->vertices.at(currFace.y);
glm::vec3 vert3 = m->vertices.at(currFace.z);
glm::vec3 side1 = (vert2 - vert1);
glm::vec3 side2 = (vert3 - vert1);
glm::vec3 normal = glm::cross(side1, side2);
normal = glm::normalize(normal);
I'm really struggling to understand what's wrong, can you point me in the right direction?
EDIT: This happens similarly with the standford bunny (taken from standford repo, so it's well formed)
http://imgur.com/Z6225QG
Looking at your normals picture it looks like that more than not being shaded, some of your object faces are transparent.
I used to have a similar problem while learning OpenGL, in my case I forgot to enable DEPTH_TEST. You can do it simply adding this line to your GL init function function:
glEnable(GL_DEPTH_TEST);
Give it a try

how to get a spotlight in opengl?

reference
http://www.chai3d.org/doc/classc_light.html
code
light2->setPos(cVector3d( 0, 0,0.0)); // position the light source
light2->m_ambient.set(0.8, 0.8, 0.8);
light2->m_diffuse.set(0.8, 0.8, 0.8);
light2->m_specular.set(0.8, 0.8, 0.8);
light2->setDirectionalLight(false);//make a positional light
the rendering code which uses opengl is
void cLight::renderLightSource()
{
// check if light source enabled
if (m_enabled == false)
{
// disable OpenGL light source
glDisable(m_glLightNumber);
return;
}
computeGlobalCurrentObjectOnly();
// enable this light in OpenGL
glEnable(m_glLightNumber);
// set lighting components
glLightfv(m_glLightNumber, GL_AMBIENT, m_ambient.pColor());
glLightfv(m_glLightNumber, GL_DIFFUSE, m_diffuse.pColor() );
glLightfv(m_glLightNumber, GL_SPECULAR, m_specular.pColor());
// position the light source in (global) space (because we're not
// _rendered_ as part of the scene graph)
float position[4];
position[0] = (float)m_globalPos.x;
position[1] = (float)m_globalPos.y;
position[2] = (float)m_globalPos.z;
//position[0] = (float)m_localPos.x;
//position[1] = (float)m_localPos.y;
//position[2] = (float)m_localPos.z;
// Directional light source...
if (m_directionalLight) position[3] = 0.0f;
// Positional light source...
else position[3] = 1.0f;
glLightfv(m_glLightNumber, GL_POSITION, (const float *)&position);
// set cutoff angle
glLightf(m_glLightNumber, GL_SPOT_CUTOFF, m_cutOffAngle);
// set the direction of my light beam, if I'm a _positional_ spotlight
if (m_directionalLight == false)
{
cVector3d dir = m_globalRot.getCol0();
float direction[4];
direction[0] = (float)dir.x;
direction[1] = (float)dir.y;
direction[2] = (float)dir.z;
direction[3] = 0.0f;
glLightfv(m_glLightNumber, GL_SPOT_DIRECTION, (const float *)&direction);
}
// set attenuation factors
glLightf(m_glLightNumber, GL_CONSTANT_ATTENUATION, m_attConstant);
glLightf(m_glLightNumber, GL_LINEAR_ATTENUATION, m_attLinear);
glLightf(m_glLightNumber, GL_QUADRATIC_ATTENUATION, m_attQuadratic);
// set exponent factor
glLightf(m_glLightNumber, GL_SPOT_EXPONENT, m_spotExponent);
}
why do I get my whole environment uniformly lighted?
how do I get a concetrated light around the origin 0,0,0, which fades away after 1 or 2 unit distance? My origin is the middle cube in the grid.
caveat emptor: This is "from the top of my head" from way-back-when i used to fumble with OpenGL.
I think the OpenGL concept of a "directional light" is sort of like a point-lightsource at infinity, meaning the light-vector is invariant across the entire scene.
In order to do the spotlight effect, you need to form the dot-product of the light-direction and the light-vector (vector from vertex to light) and attenuate the light exponentially as the angle increases.
I remember reading a tutorial about this once, will search...
Right, its described here
Scroll down to the description of "spotlights".
In the code you've listed, I believe you need to ensure that m_spotExponent is greater-than zero to get a "cone" effect. Higher values yield a "sharper" transition from "lit" to "dark" parts of the cone (I think).
Hope that helps

how to realize z-fail algorithm in opengl?

I code as the NeHe tutors Lesson27 told me,but it's a z-pass algorithm.when i'm in the shadow,the shadow is gone.somebody told me I can use z-fail algorithm to solve this problem.
so I spend two days to research z-fail algorithm.At last ,I can't figure it out.My program never run as what i think.
The z-fail algorithm as the wiki listed:
Depth fail
Around the year 2000, several people discovered that Heidmann's method can be made to work for all camera positions by reversing the depth. Instead of counting the shadow surfaces in front of the object's surface, the surfaces behind it can be counted just as easily, with the same end result. This solves the problem of the eye being in shadow, since shadow volumes between the eye and the object are not counted, but introduces the condition that the rear end of the shadow volume must be capped, or shadows will end up missing where the volume points backward to infinity.
Disable writes to the depth and color buffers.
Use front-face culling.
Set the stencil operation to increment on depth fail (only count shadows behind the object).
Render the shadow volumes.
Use back-face culling.
Set the stencil operation to decrement on depth fail.
Render the shadow volumes.
The Main question I think is the depth test. At step 3 and 6,the stencil operation is based on depth fail.Although it can show out the shadow,but it maybe shadowed on the object before it(i.e:the object which depth buffer value is less than it).so all the shadow effect looks mess.
But in z-pass algorithm,the stencil operation is based on depth pass,that means it not only can show out the shadow,but also shadowed only on the object behind it,that accords with eye system.
so how to solve this problem to make my depth fail algorithm show out the shadow on the right objects.
here is my z-fail algorithm code(somewhere may be where,please help me find out,the shadow effect is awful)
VECTOR vec;
void shadowvolume(SECTOR &sec,float *lp)
{
unsigned int p1, p2;
VECTOR v1, v2;
int i, j, k, jj;
for (i=0; i<sec.numplanes;i++)
{
if (sec.planes[i].visible)
{
for (j=0;j<3;j++)
{
k = sec.planes[i].neigh[j];
if ((!k) || (!sec.planes[k-1].visible))//如果以第k个点开始的邻边没有相邻平面或者相邻平面不可见
{
// here we have an edge, we must draw a polygon
p1 = sec.planes[i].p[j]-1;//邻边的起点
jj = (j+1)%3;
p2 = sec.planes[i].p[jj]-1;//邻边的终点
//calculate the length of the vector
v1.x = (sec.points[p1].vec.x - lp[0])*100;
v1.y = (sec.points[p1].vec.y - lp[1])*100;
v1.z = (sec.points[p1].vec.z - lp[2])*100;
v2.x = (sec.points[p2].vec.x - lp[0])*100;
v2.y = (sec.points[p2].vec.y - lp[1])*100;
v2.z = (sec.points[p2].vec.z - lp[2])*100;
glBegin(GL_TRIANGLE_STRIP);//将光源连到邻边的起点并延长,将光源连到邻边的终点的并延长,最后延长出来的梯形,画了过后模板缓冲区的值加1
glVertex3f(sec.points[p1].vec.x,sec.points[p1].vec.y,sec.points[p1].vec.z);
glVertex3f(sec.points[p1].vec.x + v1.x,sec.points[p1].vec.y + v1.y,sec.points[p1].vec.z + v1.z);
glVertex3f(sec.points[p2].vec.x,sec.points[p2].vec.y,sec.points[p2].vec.z);
glVertex3f(sec.points[p2].vec.x + v2.x,sec.points[p2].vec.y + v2.y,sec.points[p2].vec.z + v2.z);
glEnd();
}
}
// caps
glBegin(GL_TRIANGLES);
for(k=0;k<3;k++)
glVertex3fv((float*)&sec.points[sec.planes[i].p[k]-1].vec);
glEnd();
glBegin(GL_TRIANGLES);
for(k=2;k>=0;k--)
{
vec.x=sec.points[sec.planes[i].p[k]-1].vec.x+(sec.points[sec.planes[i].p[k]-1].vec.x-lp[0])*100;
vec.y=sec.points[sec.planes[i].p[k]-1].vec.y+(sec.points[sec.planes[i].p[k]-1].vec.y-lp[1])*100;
vec.z=sec.points[sec.planes[i].p[k]-1].vec.z+(sec.points[sec.planes[i].p[k]-1].vec.z-lp[2])*100;
glVertex3fv((float*)&vec);
}
glEnd();
}
}
}
void CastShadow(SECTOR &sec, float *lp)
{//lp是光源相对于物体的位置
float side;
glEnable(GL_CULL_FACE);
int i;
for (i=0;i<sec.numplanes;i++)
{
side =sec.planes[i].planeeq.a*lp[0]+sec.planes[i].planeeq.b*lp[1]+sec.planes[i].planeeq.c*lp[2]+sec.planes[i].planeeq.d*lp[3];
if (side>0)
sec.planes[i].visible = TRUE;
else
sec.planes[i].visible = FALSE;
}
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_STENCIL_TEST);
glColorMask(0, 0, 0, 0);
glStencilFunc(GL_ALWAYS, 0, 0xffffffff);
glCullFace(GL_FRONT);
glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
//glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
shadowvolume(sec,lp);
glCullFace(GL_BACK);
glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
//glStencilOp(GL_KEEP,GL_KEEP, GL_INCR);
shadowvolume(sec,lp);
glColorMask(1, 1, 1, 1);
//draw a shadowing rectangle covering the entire screen
glColor4f(0.0f, 0.0f, 0.0f,0.4f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStencilFunc(GL_NOTEQUAL, 0, 0xffffffff);
//glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glPushMatrix();
glLoadIdentity();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(-0.1f, 0.1f,-0.0010f);
glVertex3f(-0.1f,-0.1f,-0.0010f);
glVertex3f( 0.1f, 0.1f,-0.0010f);
glVertex3f( 0.1f,-0.1f,-0.0010f);
glEnd();
glPopMatrix();
glDisable(GL_BLEND);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);
glDisable(GL_STENCIL_TEST);
glShadeModel(GL_SMOOTH);
glDisable(GL_CULL_FACE);
}
the VECTOR class is like this:
class VECTOR
{
public:
float x,y,z;
bool operator==(VECTOR vec)
{
if(x==vec.x && y==vec.y && z==vec.z)
return true;
return false;
}
};
the SECTOR class and others is like this:
class PLANEEQ
{
public:
float a,b,c,d;
};
class PLANE
{
public:
unsigned int p[3];//点的序号
VECTOR normal[3];
unsigned int neigh[3];//平面3个相依平面的序号
PLANEEQ planeeq;
bool visible;
PLANE()
{
neigh[0]=0;
neigh[1]=0;
neigh[2]=0;
planeeq.a=0;
planeeq.b=0;
planeeq.c=0;
planeeq.d=0;
visible=false;
}
};
class SECTOR
{
public:
int numpoints;
int numplanes;
vector<VERTEX> points;
vector<PLANE> planes;
MATERIAL material;
bool read();
bool loadtexture();
bool build();
bool plane_calc();
void SetConnectivity();
SECTOR& SECTOR::subdivide(long depth);
SECTOR(string str1,string str2):modelfilename(str1),texturefilename(str2)
{
numpoints=0;
numplanes=0;
}
SECTOR()
{
numpoints=0;
numplanes=0;
}
private:
FILE *modelfilein,*texturefilein;
string modelfilename,texturefilename;
char oneline[255];
UINT texturename;
AUX_RGBImageRec *TextureImage;
};
class POSITION
{
public:
float x,y,z,w;
};
the DrawGLScene function in my main.cpp is like this:
int DrawGLScene(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT |GL_STENCIL_BUFFER_BIT);
glLoadIdentity();
DrawGLRoom();
glLoadIdentity();
GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = -ypos-1.2f;
GLfloat sceneroty = 360.0f - yrot;
glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);
glTranslatef(xtrans, ytrans, ztrans);
brick_sec.build();
floor_sec.build();
//wall_sec.build();
//CastShadow(wall_sec,(float *)&lightgroup.lights[0].pos);
CastShadow(brick_sec,(float*)&lightgroup.lights[0].pos);
CastShadow(floor_sec,(float*)&lightgroup.lights[0].pos);
lightgroup.build();
glColor4f(0.7f, 0.4f, 0.0f, 1.0f);
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glTranslatef(lightgroup.lights[0].pos.x, lightgroup.lights[0].pos.y, lightgroup.lights[0].pos.z);
gluSphere(q, 0.2f, 16, 8);
glEnable(GL_LIGHTING);
glDepthMask(GL_TRUE);
if(space_time>0)
{
ypos=sin(space_time*3.1415926/180);
space_time-=4;
}
else
{
sp=false;
}
//glFlush();
return TRUE; // Everything Went OK
}
Since my reputation is under 10,I can't capture the shadow effect to show u how badly it looks like! pls help me,I would thx u for ur attention and ur time!!!
thx Najzero for giving me 5 reputation,now i can capture the screen to show the effect.I will append a detail description follow.
the z-pass algorithm effect:
when i'm not in the effect,it's ok!(the orange pot represent the light)
but when i'm in the wall_shadow,it's not ok!the wall_shadow is gone,although the brick_shadow is still there.
so I need z-fail algorithm to solve this problem.but the last effect my code realized is like this:
the tick represent the shadow effect is right,the cross represent the shadow shouldn't appear on the object.
another screenshot,
a ha,At last,I find the problem in my code.I am so happy ,lol!!!!!!!!!
the problem is gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.001f,100.0f);
as the GuentherKrass said in the http://www.opengl.org/discussion_boards/showthread.php/146157-Z-Fail-Stencil-Shadow-Volumes
If you do it this way, be sure to use a perspective projection matrix with an infinite far plane or use GL_DEPTH_CLAMP to avoid the back cap being culled by the far clipping plane.
so I just change the code above to
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.001f,1000000.0f);
alright,It looks like perfect!!!!!!!!!!!!!!!!!111 hahahahaaa
two days, stay up, instant noodles..it's god damn so worth!!
ok,, I will put the last effct picture out.If anyone want my code just email me(nomorefancy#gmail.com)
attention:
the brick shadow is independent of the wall shadow.

why D3DXCreateCylinder made mesh coloring/material is not working?

This question is in continuation of "why D3DXCreateCylinder is not creating a cylinder?". I m able to draw the cylinder but it is only drawing it as .
The code is as follows
void draw_Cylinder(void){
D3DXMATRIX rot_matrix;
D3DXMATRIX trans_matrix;
D3DXMATRIX world_matrix;
static float rot_triangle=0.0f;
static float rot_triangle2=0.0f;
D3DXMatrixRotationY(&rot_matrix,rot_triangle); //Rotate the cylinder
D3DXMatrixRotationX(&rot_matrix,rot_triangle2); //Rotate the cylinder
D3DXMatrixTranslation(&trans_matrix,2.0f,0,20.0f); //Shift it 2 units to the left
D3DXMatrixMultiply(&world_matrix,&rot_matrix,&trans_matrix);
D3DMATERIAL9 material;// = new D3DMATERIAL9();
ZeroMemory( &material, sizeof(D3DMATERIAL9) );
// Set the RGBA for diffuse reflection.
material.Diffuse.r = 0.5f;
material.Diffuse.g = 0.0f;
material.Diffuse.b = 0.5f;
material.Diffuse.a = 1.0f;
// Set the RGBA for ambient reflection.
material.Ambient.r = 0.5f;
material.Ambient.g = 0.0f;
material.Ambient.b = 0.5f;
material.Ambient.a = 1.0f;
// Set the color and sharpness of specular highlights.
material.Specular.r = 1.0f;
material.Specular.g = 1.0f;
material.Specular.b = 1.0f;
material.Specular.a = 1.0f;
material.Power = 2.0f;
// Set the RGBA for emissive color.
material.Emissive.r = 0.0f;
material.Emissive.g = 0.0f;
material.Emissive.b = 0.0f;
material.Emissive.a = 0.0f;
g_d3d_device->SetMaterial(&material);
g_d3d_device->SetTexture(0,NULL);
g_d3d_device->SetTransform(D3DTS_WORLD,&world_matrix);
m_ppMeshCylinder->DrawSubset(0);
////Render from our Vertex Buffer
//g_d3d_device->DrawPrimitive(D3DPT_TRIANGLELIST, //PrimitiveType
// 0, //StartVertex
// g_pyramid_count); //PrimitiveCount
rot_triangle+=0.0007f;
if(rot_triangle > D3DX_PI*2)
{
rot_triangle-=D3DX_PI*2;
}
rot_triangle2+=0.0007f;
if(rot_triangle2 > D3DX_PI*2)
{
rot_triangle2-=D3DX_PI*2;
}
}
or download the project.
I have attracted my codes here "project code"
I want to draw it having 3D shades what generally any 3D mesh has if rendered by default.
I am not nicely aware of materials. Or is it the problem with graphics card (I just thought :D ).
In addition where can I get information and samples abt SetRenderState
Try
g_d3d_device->SetRenderState( D3DRS_DIFFUSEMATERIALSOURCE, D3DMCS_MATERIAL );
At the moment it is defaulting to using "Color 1" which is the first of the 2 possible vertex colours.