• 10
• 12
• 12
• 14
• 17

# Beginners direct3d vertex buffer problem

This topic is 4339 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all, how are we all? I hope you guys might be able to help me out with this, I'm just starting out with direct3d programming and so I'm editing a bit of code from toymaker.info, I'm just trying to get a simple triangle showing on the screen splitting who bits of code so i have a dedicated direct3d initialisation sequence. Starting in the main.cpp:

#include <windows.h>
#include "vis_setup.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

// The main entry point of our Windows program
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// Create and fill out a window class structure
WNDCLASS wcex;
ZeroMemory(&wcex,sizeof(WNDCLASS));

[... more windows stuff going on here]

So thats the initial start up, moving on the vis_seup.h:
#include <d3dx9.h>		// D3DX helper functions

LPDIRECT3D9 gD3dObject=NULL;
LPDIRECT3DDEVICE9 gD3dDevice=NULL;
LPDIRECT3DVERTEXBUFFER9	gVertexBuffer=NULL;

struct TMyVertex
{
D3DXVECTOR3 p;	// Vertex position
D3DCOLOR c;	// Vertex colour
};

#define MYVERTEX_FVF (D3DFVF_XYZ | D3DFVF_DIFFUSE)
class vis_setup
{
public:

vis_setup(void);
bool InitialiseDirect3D(HWND hWnd);
bool CreateTriangle();

And when i want to use MYVERTEX_FVF i do it like this:
bool vis_setup::CreateTriangle()
{

HRESULT hr=gD3dDevice->CreateVertexBuffer(3*sizeof(TMyVertex),D3DUSAGE_WRITEONLY, MYVERTEX_FVF,
D3DPOOL_MANAGED, &gVertexBuffer, NULL );
if (FAILED(hr))
return false;

// Lock and fill
TMyVertex* pVertices=NULL;

This all looks alright to me but when i compile I get errors like this:
Triangle fatal error LNK1169: one or more multiply defined symbols found
Triangle error LNK2005: "struct IDirect3DVertexBuffer9 * gVertexBuffer" (?gVertexBuffer@@3PAUIDirect3DVertexBuffer9@@A) already defined in main.obj

I dunno whats up with it, I've never being exactly confident with #defines (My c++ skills need ALOT of work) but I can't really see whats wrong with this. Anyone know? (sorry for cross posting)

##### Share on other sites
Why don't make your code between '[' + "source" + ']' and
'[' + "/source" + ']'? that will looks clear.

##### Share on other sites
Have you tried #include <d3d9.h> before #include <d3dx9.h>?
Do you have a vis_seup.cpp file that implement the class? or you put the implement all in vis_seup.h?

It seem like a multiply include.

##### Share on other sites
A few things:

A) Put include guards around each header file. That is:

#ifndef VIS_SETUP_H
#define VIS_SETUP_H

// all your code in here

#endif

B) You're defining a variable in a header file. That means that if you have more than one source file including that header file, there'll be two definitions (or more) of that variable, in this case gVertexBuffer, for the linker. Linkers only like one definition of each thing. To get around this, either use C++ properly (ask how, we'll be happy to tell you), or declare the variable gVertexBuffer, and define it in one source file. To do this, you have to declare gVertexBuffer extern:

// in the header fileextern LPDIRECT3DVERTEXBUFFER9 gVertexBuffer; // in the source filegVertexBuffer = NULL;

But seriously (seriously), I'd ask/read up on how to structure your code properly - you shouldn't be using global variables almost ever in C++. There are far better ways (for example, some sort of buffer class, so you can have many instances!).

Hope that helps.

##### Share on other sites
Yes goat, i think as well thats the point. I made the same mistakes when i wrote my first header files.

##### Share on other sites
Quote:
 Original post by _goat- you shouldn't be using global variables almost ever in C++.

That is a matter of personal preference my friend.

##### Share on other sites
Global variables ISN'T a matter of personal taste but a sign of good OO design. If you take the trouble to design your code and follow the standard Object Oriented Programming rules, then you will never use globals.

Just my 2 cents.