Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


Problem with d3d

This topic is 5774 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

Hey all.. i really need to get to work so i''m just gonna throw this out there. I have this code (see below) that is suppost to show a shape of somesort.. i don''t really understand how the vertex stuff works yet (mainly how you can visualize non-transformed vertex''s), but i was able to make a triangle on the screen a few other times, following tutorials.. but i never really understood the difference between all the different ways. I know i have done it with DrawPrimitiveUP or something similar, but i''m not exactly sure. Anyway, this is using a vertex buffer and is from jim''s book (which i have found very helpful.. but i think if i just got the basics down it would go much more smoothly). Here''s the code:
// D3D Function prototypes

#include "includes.h"

IDirect3D8 * lpD3D = NULL;
IDirect3DDevice8 * lpD3DDevice = NULL;
IDirect3DVertexBuffer8 * lpD3DVB = NULL;

typedef struct{

	FLOAT x, y, z;
	D3DCOLOR diffuse;
} sVertex;

int initD3D(){

	// vertex information

	sVertex Verts[4] = {
		{-100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA(255, 255, 255, 255)},
		{100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA(255, 0, 0, 255)},
		{100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA(0, 255, 0, 255)},
		{-100.0f, -100.0f, 100.0f, D3DCOLOR_RGBA(0, 0, 255, 255)},

	// create the D3D object

	if((lpD3D = Direct3DCreate8(D3D_SDK_VERSION)) == NULL){
		fError("Failed on Direct3DCreate8(D3D_SDK_VERSION))");
		return 0;

	ZeroMemory(&D3DPP, sizeof(D3DPRESENT_PARAMETERS));

	if(screenStatus == SS_FULL){		
		D3DDM.Width	        = SWIDTH;				// fullscreen width

		D3DDM.Height        = SHEIGHT;				// fullscreen height

		D3DDM.RefreshRate	= SRFR;					// fullscreen refresh rate

		D3DDM.Format		= SCF;					// fullscreen color format

		D3DPP.Windowed		= FALSE;				// not in windowed mode

		D3DPP.SwapEffect	= D3DSWAPEFFECT_FLIP;	// flip the back buffer

		D3DPP.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;				// default refresh rate

		D3DPP.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;	// default refresh interval

		D3DPP.BackBufferFormat = D3DDM.Format;		// use color format from D3DDM

		D3DPP.BackBufferWidth  = D3DDM.Width;
		D3DPP.BackBufferHeight = D3DDM.Height;


		// get current display info

		if(FAILED(lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &D3DDM))){ 

			fError("Failed on lpD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &D3DDM)");
			return 0;

		D3DPP.Windowed = TRUE;						// in windowed mode

		D3DPP.BackBufferFormat = D3DDM.Format;		// use color format from D3DDM


	// create the D3D device


		return 0;

	// create the Vertex Buffer

	if(FAILED(lpD3DDevice->CreateVertexBuffer(sizeof(sVertex) * 4, 0, VertexFVF, D3DPOOL_DEFAULT, &lpD3DVB))){

		fError("Failed on lpD3DDevice->CreateVertexBuffer(sizeof(sVertex) * 4, 0, VertexFVF, D3DPOOL_MANAGED, &lpD3DVB)");
		return 0;

	BYTE * Ptr;

	// lock the vertex buffer

	if(SUCCEEDED(lpD3DVB->Lock(0, 0, (BYTE**)&Ptr, 0))){
		// copy vertex data over

		memcpy(Ptr, Verts, sizeof(Verts));

		// unlock the vertex buffer



		fError("Failed on lpD3DVB->Lock(0, 0, (BYTE**)&Ptr, 0)");
		return 0;

	return 1;

int mainD3D(){
	lpD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_RGBA(0, 0, 0, 255), 1.0f, 0);


		// set the vertex stream and shader

		if(FAILED(lpD3DDevice->SetStreamSource(0, lpD3DVB, sizeof(sVertex)))){
			fError("Failed on lpD3DDevice->SetStreamSource(0, lpD3DVB, sizeof(sVertex))");
			return 0;


			fError("Failed on lpD3DDevice->SetVertexShader(VertexFVF))");
			return 0;

		if(FAILED(lpD3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2))){

			fError("Failed on lpD3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2)");
			return 0;


	lpD3DDevice->Present(NULL, NULL, NULL, NULL);

	return 1;

int releaseD3D(){

	if(lpD3DVB != NULL){

		lpD3DVB = NULL;

	if(lpD3DDevice != NULL){
		lpD3DDevice = NULL;

	if(lpD3D != NULL){
		lpD3D = NULL;

	return 1;

If someone could just fill in the gaps for me.. or, tell me what ever they don''t think i know that would be great. I realize i''m asking a lot, i''m in no hurry to get anything on the screen, i just want to understand it. ANY advice is greatly appriciated. Thanks in advance!! - Fuzztrek ¬_¬

Share this post

Link to post
Share on other sites

lets start simple - you are stuck with thinking in 3d :-)

The FvF that you describe is defining a point in space (three coordinates) and a color (diffuse).

Do you have all your initialisation in place? Basically you need to

(a) create a device and
(b) a vertex buffer on it.

You then need to set your matrices - basically saying how you want the projection to happen (zoom factor, near/far plane) and the camera position and direction.

Then you move the vertices into the vertex buffer (locking/unlocking it) and render it.

Now, lets start with some bad saying: did you even care to read up n the material that the API producer provides? There are pretty easy samples out there for DirectX, coming as part of the DirectX SDK - easy to see how to send out vertex information.

Then, the first resource for you should be the manual. I frankly do not really understan the need to books to eplain me how to use DirectX (API) - good, the formulas are not in the documentation, but the documentation is extremely good in explaining how to use the API and basically how to optimise performance. The basics you ask for are there.

Now, as a side note - who gave you the ridiculous stupid advice to use DrawPrimitiveUP? Basically, if you want performance, dont use the UP methods- there are nice papers on the NVidia website on how to optimise performance using Dynamic Vertex Buffers.

I started with DirectX about a month ago (hm, ok - less) and by now I feel abosolutly comfortable with the API. Ok, it might help that I had some exposure to 3d work 15 years ago when I was making my own wireframe graphics (old times), s I know most of the maths involved and have a feeling for this.

But I got the API by readong through all the documentation and the Articles on the MSDN website, then following the links given here. Maybe starting with the tutorials is definitly the wrong way to go, and some old fashioned reading would be the next step? I found the MS documentation to really not be too bad (ok, as I said, you need to know the maths - they dont really have a lot of diagrams).

For me, the hard part of DirectX is coming up with some NICE output - combining various techniques to get an optimal effect with as little rsesources as possible.


Thomas Tomiczek
THONA Consulting Ltd.
(Microsoft MVP C#/.NET)

Share this post

Link to post
Share on other sites
Thanks thona!

I have read the sdk docs, however most of the stuff i didn''t find very clear. specifically, I didn''t (up until your post) understand the order of things with direct3D. I did not know that you ALWAYS had to initalize your world matrix and stuff (though it makes sense to me now). I think i just needed a simple step by step guide to get me started. I''ll try going over my code again adding in the things that I left out and see what happens. Thanks again!!


Share this post

Link to post
Share on other sites
I Think you Need a better understanding of the whole aspect of 3d
instead of learning directx or opengl you need to learn the math
of how everything works really well. you can make directx or opengl program without knowing any of this but you well never be able to make anything that stands out or is to complicated. you need to start learning allot of math. i would recomend buying the book
"mathamatics for 3d game programming & computer Graphics"
by eric lengyel a very good book on the subject
however it is rather complicated so , besides learning about directx learn everything you can about math at the same time

Share this post

Link to post
Share on other sites
hheh.. i''ll learn that later =P i just want to get something on the screen (don''t we all) and while i understand it''s importance, i shall read it when i need it. perhaps that is a bad motto, but i''m in no rush.


Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!