首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络游戏 >

求教各位,关于DX9顶点颜色的有关问题

2013-07-08 
求教各位,关于DX9顶点颜色的问题如图,我给顶点加了颜色之后,就变成这个样子了。为什么只有线有颜色? 还需要

求教各位,关于DX9顶点颜色的问题

如图,我给顶点加了颜色之后,就变成这个样子了。
为什么只有线有颜色? 还需要设置什么东西?我想在没个面上面都弄颜色,该怎么弄啊?求教各位了。

代码如下,就是龙书第3章cube的代码,我只是加了颜色


//////////////////////////////////////////////////////////////////////////////////////////////////
// 
// File: cube.cpp
// 
// Author: Frank Luna (C) All Rights Reserved
//
// System: AMD Athlon 1800+ XP, 512 DDR, Geforce 3, Windows XP, MSVC++ 7.0 
//
// Desc: Renders a spinning cube in wireframe mode.  Demonstrates vertex and 
//       index buffers, world and view transformations, render states and
//       drawing commands.
//          
//////////////////////////////////////////////////////////////////////////////////////////////////

#include "d3dUtility.h"

//
// Globals
//

HRESULT hr = 0;

IDirect3DDevice9* Device = 0; 

const int Width  = 640;
const int Height = 480;

IDirect3DVertexBuffer9* VB = 0;
IDirect3DIndexBuffer9*  IB = 0;
ID3DXMesh *ppmesh = 0;

D3DXMATRIX WorldMatrix;
D3DXMATRIX CenterPoint;
D3DXMATRIX ObjectWorldMatrix;




//
// Classes and Structures
//

struct Vertex
{
Vertex(){}
Vertex(float x, float y, float z)
{
_x = x;  _y = y;  _z = z;
_color = 0;
}
Vertex(float x,float y,float z,D3DCOLOR color)
{
Vertex(x,y,z);
_color = color;
}
float _x, _y, _z;
D3DCOLOR _color;
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_DIFFUSE;

//
// Framework Functions
//
bool Setup()
{
//
// Create vertex and index buffers.
// 创建缓存
//

//顶点缓存
hr = Device->CreateVertexBuffer(
8 * sizeof(Vertex), 
D3DUSAGE_WRITEONLY,
Vertex::FVF,
D3DPOOL_MANAGED,
&VB,
0);

if(hr){
MessageBox(0,"Create Vertex buffers -> FAILED",0,0);


return false;
}

//索引缓存
hr = Device->CreateIndexBuffer(
36 * sizeof(WORD),
D3DUSAGE_WRITEONLY,
D3DFMT_INDEX16,
D3DPOOL_MANAGED,
&IB,
0);

if(hr){
MessageBox(0,"Create index buffers -> FAILED",0,0);
return false;
}

//
// Fill the buffers with the cube data. 
// 绘制立方体
//

// define unique vertices:
//顶点
Vertex* vertices;
VB->Lock(0, 0, (void**)&vertices, 0);

vertices[0] = Vertex(-1.0f, -1.0f, -1.0f);
vertices[1] = Vertex(-1.0f,  1.0f, -1.0f);
vertices[2] = Vertex( 1.0f,  1.0f, -1.0f);
vertices[3] = Vertex( 1.0f, -1.0f, -1.0f);
vertices[4] = Vertex(-1.0f, -1.0f,  1.0f);
vertices[5] = Vertex(-1.0f,  1.0f,  1.0f);
vertices[6] = Vertex( 1.0f,  1.0f,  1.0f);
vertices[7] = Vertex( 1.0f, -1.0f,  1.0f);

int i = 0;
for(;i<8;i++){
vertices[i]._color = D3DCOLOR_XRGB(rand()%255,rand()%255,rand()%255);
}

VB->Unlock();

// define the triangles of the cube:
// 定义构造立方体的三角形
WORD* indices = 0;
IB->Lock(0, 0, (void**)&indices, 0);

// front side
indices[0]  = 0; indices[1]  = 1; indices[2]  = 2;
indices[3]  = 0; indices[4]  = 2; indices[5]  = 3;

// back side
indices[6]  = 4; indices[7]  = 6; indices[8]  = 5;
indices[9]  = 4; indices[10] = 7; indices[11] = 6;

// left side
indices[12] = 4; indices[13] = 5; indices[14] = 1;
indices[15] = 4; indices[16] = 1; indices[17] = 0;

// right side
indices[18] = 3; indices[19] = 2; indices[20] = 6;
indices[21] = 3; indices[22] = 6; indices[23] = 7;

// top
indices[24] = 1; indices[25] = 5; indices[26] = 6;
indices[27] = 1; indices[28] = 6; indices[29] = 2;

// bottom
indices[30] = 4; indices[31] = 0; indices[32] = 3;
indices[33] = 4; indices[34] = 3; indices[35] = 7;



IB->Unlock();

//D3DXMatrixTranslation(&WorldMatrix,-3.0f,0.0f,0.0f);
//D3DXMatrixTranslation(&CenterPoint,3.0f,0.0f,0.0f);
//D3DXCreateTeapot(Device,&ppmesh,0);

//
// Position and aim the camera.
//

D3DXVECTOR3 position(0.0f, 0.0f, -5.0f);
D3DXVECTOR3 target(0.0f, 0.0f, 0.0f);
D3DXVECTOR3 up(0.0f, 1.0f, 0.0f);
    D3DXMATRIX V;
D3DXMatrixLookAtLH(&V, &position, &target, &up);

    Device->SetTransform(D3DTS_VIEW, &V);

//
// Set the projection matrix.
//

D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(
&proj,
D3DX_PI * 0.5f, // 90 - degree
(float)Width / (float)Height,
1.0f,
1000.0f);
Device->SetTransform(D3DTS_PROJECTION, &proj);


Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
//Device->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE);
Device->SetRenderState(D3DRS_LIGHTING, false);


return true;
}

void Cleanup()
{
d3d::Release<IDirect3DVertexBuffer9*>(VB);
d3d::Release<IDirect3DIndexBuffer9*>(IB);
}

bool Display(float timeDelta)
{
if( Device )
{
Device->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffff, 1.0f, 0);
Device->BeginScene();
Device->SetFVF(Vertex::FVF);
Device->SetStreamSource(0, VB, 0, sizeof(Vertex));
Device->SetIndices(IB);
Device->SetRenderState(D3DRS_SHADEMODE,D3DSHADE_GOURAUD);
Device->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12);
Device->EndScene();
Device->Present(0, 0, 0, 0);
}
return true;
}


//
// WndProc
//
LRESULT CALLBACK d3d::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
D3DXMATRIX Ry;
D3DXMATRIX Rx;
D3DXMATRIX p;
D3DXMATRIX translation;
static float y = 0.0f;
static float x = 0.0f;
static float z_axis = 0.0f;
switch( msg )
{
case WM_DESTROY:
::PostQuitMessage(0);
break;

case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
DestroyWindow(hwnd);
break;
case VK_RIGHT:
if(Device){
y += 0.1f;
if(y > 6.28f)


y = 0.0f;
D3DXMatrixRotationY(&Ry, y);
D3DXMatrixRotationX(&Rx,x);
D3DXMatrixTranslation(&translation,0,0,z_axis);
p = Ry * Rx * translation;
Device->SetTransform(D3DTS_WORLD,&p);
break;
}
case VK_LEFT:
if(Device){
y -= 0.1f;
D3DXMatrixRotationY(&Ry, y);
D3DXMatrixRotationX(&Rx,x);
D3DXMatrixTranslation(&translation,0,0,z_axis);
p = Ry * Rx * translation;
Device->SetTransform(D3DTS_WORLD,&p);
break;
}
case VK_DOWN:
if(Device){
x -= 0.1f;
if(x < 0)
x = 6.28f;
D3DXMatrixRotationY(&Ry, y);
D3DXMatrixRotationX(&Rx,x);
D3DXMatrixTranslation(&translation,0,0,z_axis);
p = Ry * Rx * translation;
Device->SetTransform(D3DTS_WORLD,&p);
break;
}
case VK_UP:
if(Device){
x += 0.1f;
if(x > 6.28f)
x = 0.0f;
D3DXMatrixRotationY(&Ry, y);
D3DXMatrixRotationX(&Rx,x);
D3DXMatrixTranslation(&translation,0,0,z_axis);
p = Ry * Rx * translation;
//ObjectWorldMatrix = Rx * Ry;
Device->SetTransform(D3DTS_WORLD,&p);
break;
}
case 87:
if(Device){
z_axis += 0.1f;
D3DXMatrixRotationY(&Ry, y);
D3DXMatrixRotationX(&Rx,x);
D3DXMatrixTranslation(&translation,0,0,z_axis);
p = Ry * Rx * translation;
Device->SetTransform(D3DTS_WORLD,&p);
break;
}
case 83:
if(Device){
z_axis-=0.1f;
D3DXMatrixRotationY(&Ry, y);
D3DXMatrixRotationX(&Rx,x);
D3DXMatrixTranslation(&translation,0,0,z_axis);
p = Ry * Rx * translation;
Device->SetTransform(D3DTS_WORLD,&p);
break;
}
}
if( wParam == VK_ESCAPE )
::DestroyWindow(hwnd);
break;
}
return ::DefWindowProc(hwnd, msg, wParam, lParam);
}


D3DXMATRIX getMatrix(float* x,const float xIncrement, float* y, const float yIncrement){
D3DXMATRIX Ry;
D3DXMATRIX Rx;
*x += xIncrement;
*y += yIncrement;
if(*x<0){
*x = 6.28f;
} else if(*x > 6.28f){
*x = 0.0f;
}
if( *y < 0){


*y = 6.28f;
} else if(*y > 6.28f){
*y = 0.0f;
}
D3DXMatrixRotationX(&Rx,*x);
D3DXMatrixRotationY(&Ry,*y);
return Rx * Ry;



int WINAPI WinMain(HINSTANCE hinstance,
   HINSTANCE prevInstance, 
   PSTR cmdLine,
   int showCmd)
{
if(!d3d::InitD3D(hinstance,
Width, Height, true, D3DDEVTYPE_HAL, &Device))
{
::MessageBox(0, "InitD3D() - FAILED", 0, 0);
return 0;
}

if(!Setup())
{
::MessageBox(0, "Setup() - FAILED", 0, 0);
return 0;
}

d3d::EnterMsgLoop( Display );

Cleanup();

Device->Release();

return 0;
}


[解决办法]
注释掉

Device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);

看看。

热点排行