Skip to main content
GameDev.net gamedev.net
🔒 Locked

Small Vertex Shader Problem

Started by dave Apr 20, 2004 at 7:30 PM 0 replies 200+ views
Original Post
dave
dave
edit:supposed to be entitled Small Vertex Shader Problem Hi, i'm now mostly aware of hour Vertex Shaders work but im having a problem: What i am aiming to do is using a separate singleton class, create and render the contents of the a Vertex Buffer with a simple square in it and colour it with a single colour using a Vertex Shader. The method i have used is to create the following class: // VSTest.cpp: implementation of the CVSTest class. // ////////////////////////////////////////////////////////////////////// #include "VSTest.h" #include <d3dx9.h> ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// struct CUSTOMVERTEX { FLOAT x, y, z; }; CUSTOMVERTEX g_Vertices[]= { // x y z { -0.0f, -1.0f, 0.0f }, { +1.0f, -1.0f, 0.0f }, { +1.0f, +1.0f, 0.0f }, { -1.0f, +1.0f, 0.0f }, }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ) LPDIRECT3DVERTEXBUFFER9 g_pVB; LPDIRECT3DVERTEXSHADER9 m_pVertexShader; LPD3DXBUFFER pCode; // Buffer with the assembled shader code LPD3DXBUFFER pErrorMsgs; // Buffer with error messages CVSTest::CVSTest() { } CVSTest::~CVSTest() { } HRESULT CVSTest::CreateVertexShader(HWND hWnd, LPDIRECT3DDEVICE9 g_pD3DDevice) { CUSTOMVERTEX g_Vertices[]= { // x y z { -0.0f, -1.0f, 0.0f }, { +1.0f, -1.0f, 0.0f }, { +1.0f, +1.0f, 0.0f }, { -1.0f, +1.0f, 0.0f }, }; // Declare the vertex position and diffuse color data. g_pD3DDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ); // Fill the vertex buffer. VOID* pVertices; if( FAILED( g_pVB->Lock( 0, sizeof(g_Vertices), (void**)&pVertices, 0 ) ) ) { return E_FAIL; } memcpy( pVertices, g_Vertices, sizeof(g_Vertices) ); g_pVB->Unlock(); // Create the shader declaration. D3DVERTEXELEMENT9 decl[] = { { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, D3DDECL_END() }; char strShaderPath[12] = "VSTest.vsh"; D3DXAssembleShaderFromFile( strShaderPath, NULL, NULL, 0, &pCode, &pErrorMsgs ); g_pD3DDevice->CreateVertexShader((DWORD*)pCode->GetBufferPointer(), &m_pVertexShader); pCode->Release(); //pErrorMsgs->Release(); return S_OK; } void CVSTest::RenderVertexShader(HWND hWnd, LPDIRECT3DDEVICE9 g_pD3DDevice) { // Update vertex shader constants from view projection matrix data. D3DXMATRIX mat, matView, matProj; D3DXMatrixMultiply( &mat, &matView, &matProj ); D3DXMatrixTranspose( &mat, &mat ); g_pD3DDevice->SetVertexShaderConstantF( 0, (float*)&mat, 4 ); // Declare and define the constant vertex color. float color[4] = {0,1.0,0,0}; g_pD3DDevice->SetVertexShaderConstantF( 4, (float*)&color, 1 ); // Turn off specular light contribution because the specular value // is not written in the vertex shader. g_pD3DDevice->SetRenderState( D3DRS_SPECULARENABLE, FALSE ); // Render the output. g_pD3DDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pD3DDevice->SetVertexShader( m_pVertexShader ); g_pD3DDevice->DrawPrimitive( D3DPT_TRIANGLEFAN, 0, 4 ); } The CreateVertexShader function is called after every other creation/setup functions for everything else in the engine and the RenderVertexShader function is called every frame after all other things have been drawn. The problem i am getting is a completely blank screen of the colour that it is cleared to. Another thing i noticed is that for a split second as soon as the app runs the scene that i am aiming to run along side this simple square flicks up for like a single frame then it goes blank. I have a feeling that i havent restored some matrices that i'm supposed to have but i cant figure out what. Help would me greatly appreciated. regards, ace [edited by - ace_lovegrove on April 20, 2004 8:31:05 PM] [edited by - ace_lovegrove on April 20, 2004 8:31:53 PM] [edited by - ace_lovegrove on April 20, 2004 8:32:28 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.