4
votes

I'm working on a small game engine. One of the features of it is that it should support DirectX and OpenGL rendering.

I am using Vertex Buffer Objects and I have a structure to define the format of my vertices. The problem is that I would like to be able to use the same structure for both DirectX and OpenGL so that I could switch from my DirectX rendering component to the OpenGL one without changing the vertices of my objects.

Is this possible?

Currently, I am using the following structure for DirectX:

struct Vertex{
float position[3];      // x, y, z
float normal[3];        // nx, ny, nz
DWORD colour;           // The vertex color
float texture[2];       // u, v
};

along with:

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_DIFFUSE | D3DFVF_TEX1 ) 

as flexible vertex format.

My understanding of OpenGL tells me that when I want to draw my object, I can tell the colour with this command:

glColorPointer(4, GL_FLOAT, sizeof(Vertex), BUFFER_OFFSET24);

in the drawing routine, assuming the colour has 4 components. And indeed, this works. However I do not believe I can tell OpenGL to use an unsigned integer for this task, therefore I am using:

struct Vertex{
float position[3];      // x, y, z
float normal[3];        // nx, ny, nz
float colour[4];        // r, g, b, a
float texture[2];       // u, v
};

that struct for my OpenGL code.

2

2 Answers

4
votes

What you need to do is employ the ARB_vertex_array_bgra extension. It's specifically designed for D3D interop.

So your glColorPointer call would look like this:

glColorPointer(GL_BGRA, GL_UNSIGNED_BYTE, sizeof(Vertex), BUFFER_OFFSET12);
1
votes

A DWORD is a 32-bit unsigned integer. As far as I know, directx reads it as 4 sets, of 8-bits, each representing a color channel.

You can accomplish the same thing in OpenGL by telling it to read unsigned bytes instead of floats:

glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), BUFFER_OFFSET12);

You will have to change your OpenGL struct to

struct Vertex{
float position[3];      // x, y, z
float normal[3];        // nx, ny, nz
unsigned int colour;
float texture[2];       // u, v
};