RAW Triangle Format: How to parse it properly
-
Yes. I used that tutorial from "drunkenhyena.com" to understand more about the direct 3d The code I'm working is huge. Here is the whole code. main.cpp
#include "common.h"
#pragma comment(lib, "d3d9")
#pragma comment(lib, "user32")
#pragma comment(lib, "gdi32")extern IDirect3D9 *g_d3d9;
extern IDirect3DDevice9 *g_device;
extern D3DPRESENT_PARAMETERS g_pparams;
extern const unsigned Width;
extern const unsigned Height;extern HRESULT Init_d3d9(IDirect3D9 **d3d9);
extern void Init_pparams(HWND hwnd, D3DFORMAT format, D3DFORMAT depth, D3DPRESENT_PARAMETERS *pparams);
extern HRESULT Init_device(IDirect3D9 *d3d9, HWND hwnd, D3DPRESENT_PARAMETERS *pparams, IDirect3DDevice9 **device);
extern void Kill_d3d9(IDirect3D9 **d3d9, IDirect3DDevice9 **device);HRESULT load_RAW(const char *fname, IDirect3DVertexBuffer9 **buffer, int *count)
{
HRESULT hr = E_FAIL;
FILE *f = fopen(fname, "r");
if(f)
{
(*count) = 0;
float tmp[3] = {0};
while(fscanf(f, "%g %g %g", &tmp[0], &tmp[1], &tmp[2]) == 3)(*count)++;rewind(f); HUD\_VERTEX vert\[(\*count)\]; memset(vert, 0, sizeof(vert)); for(int i = 0; i < (\*count); i++) { fscanf(f, "%g %g %g", &(vert\[i\].x), &(vert\[i\].y), &(vert\[i\].z)); if(ferror(f)) { fclose(f); return hr; } } fclose(f); int byteLen = sizeof(vert); hr = g\_device->CreateVertexBuffer(byteLen, D3DUSAGE\_WRITEONLY, HUD\_VERTEX\_fvf, D3DPOOL\_MANAGED, buffer, NULL); if(FAILED(hr))return hr; void \*ptr = NULL; hr = (\*buffer)->Lock(0, 0, &ptr, 0); if(FAILED(hr))return hr; memcpy(ptr, vert, byteLen); hr = (\*buffer)->Unlock(); if(FAILED(hr))return hr; } fclose(f); return hr;
}
// D3D related functions
void Resources_Init()
{
}void Resources_Free()
{
}HRESULT Device_Render(IDirect3DVertexBuffer9 *buffer, int count)
{
HRESULT hr = D3D_OK;
hr = g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0), 0.0, 0);
if(FAILED(hr))return hr;hr = g\_device->BeginScene(); if(FAILED(hr))return hr; g\_device->SetFVF(HUD\_VERTEX\_fvf); g\_device->SetStreamSource(0, buffer, 0, sizeof(HUD\_VERTEX)); g\_device->DrawPrimitive(D3DPT\_TRIANGLESTRIP, 0, count); g\_device->EndScene(); g\_device->Present(NULL, NULL, NULL, NULL);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_KEYDOWN:
case WM_CLOSE:
case WM_LBUTTONDOWN:
PostQuiI did kind-of ask for that didn't I?? Okay... That's gonna take me a bit to look through.... :doh:
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
-
I did kind-of ask for that didn't I?? Okay... That's gonna take me a bit to look through.... :doh:
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
By the way I'm using Blender 2.49a for exporting the geometry files Here
HRESULT load_RAW(const char *fname, IDirect3DVertexBuffer9 **buffer, int *count)
{
HRESULT hr = E_FAIL;
FILE *f = fopen(fname, "r");
if(f)
{
(*count) = 0;
float tmp[3] = {0};
while(fscanf(f, "%g %g %g", &tmp[0], &tmp[1], &tmp[2]) == 3)(*count)++;rewind(f); HUD\_VERTEX vert\[(\*count)\]; memset(vert, 0, sizeof(vert)); for(int i = 0; i < (\*count); i++) { fscanf(f, "%g %g %g", &(vert\[i\].x), &(vert\[i\].y), &(vert\[i\].z)); if(ferror(f)) { fclose(f); return hr; } } fclose(f); int byteLen = sizeof(vert); hr = g\_device->CreateVertexBuffer(byteLen, D3DUSAGE\_WRITEONLY, HUD\_VERTEX\_fvf, D3DPOOL\_MANAGED, buffer, NULL); if(FAILED(hr))return hr; void \*ptr = NULL; hr = (\*buffer)->Lock(0, 0, &ptr, 0); if(FAILED(hr))return hr; memcpy(ptr, vert, byteLen); hr = (\*buffer)->Unlock(); if(FAILED(hr))return hr; } fclose(f); return hr;
}
And here
HRESULT Device_Render(IDirect3DVertexBuffer9 *buffer, int count)
{
HRESULT hr = D3D_OK;
hr = g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0), 0.0, 0);
if(FAILED(hr))return hr;hr = g\_device->BeginScene(); if(FAILED(hr))return hr; g\_device->SetFVF(HUD\_VERTEX\_fvf); g\_device->SetStreamSource(0, buffer, 0, sizeof(HUD\_VERTEX)); g\_device->DrawPrimitive(D3DPT\_TRIANGLESTRIP, 0, count); g\_device->EndScene(); g\_device->Present(NULL, NULL, NULL, NULL);
}
-
By the way I'm using Blender 2.49a for exporting the geometry files Here
HRESULT load_RAW(const char *fname, IDirect3DVertexBuffer9 **buffer, int *count)
{
HRESULT hr = E_FAIL;
FILE *f = fopen(fname, "r");
if(f)
{
(*count) = 0;
float tmp[3] = {0};
while(fscanf(f, "%g %g %g", &tmp[0], &tmp[1], &tmp[2]) == 3)(*count)++;rewind(f); HUD\_VERTEX vert\[(\*count)\]; memset(vert, 0, sizeof(vert)); for(int i = 0; i < (\*count); i++) { fscanf(f, "%g %g %g", &(vert\[i\].x), &(vert\[i\].y), &(vert\[i\].z)); if(ferror(f)) { fclose(f); return hr; } } fclose(f); int byteLen = sizeof(vert); hr = g\_device->CreateVertexBuffer(byteLen, D3DUSAGE\_WRITEONLY, HUD\_VERTEX\_fvf, D3DPOOL\_MANAGED, buffer, NULL); if(FAILED(hr))return hr; void \*ptr = NULL; hr = (\*buffer)->Lock(0, 0, &ptr, 0); if(FAILED(hr))return hr; memcpy(ptr, vert, byteLen); hr = (\*buffer)->Unlock(); if(FAILED(hr))return hr; } fclose(f); return hr;
}
And here
HRESULT Device_Render(IDirect3DVertexBuffer9 *buffer, int count)
{
HRESULT hr = D3D_OK;
hr = g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0), 0.0, 0);
if(FAILED(hr))return hr;hr = g\_device->BeginScene(); if(FAILED(hr))return hr; g\_device->SetFVF(HUD\_VERTEX\_fvf); g\_device->SetStreamSource(0, buffer, 0, sizeof(HUD\_VERTEX)); g\_device->DrawPrimitive(D3DPT\_TRIANGLESTRIP, 0, count); g\_device->EndScene(); g\_device->Present(NULL, NULL, NULL, NULL);
}
I have never looked at the RAW output from Blender, and without compiling this only one thing "jumps" out at me.... g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, count); Are you sure that Blender is outputting a Triangle Strip or a list of triangles? http://en.wikipedia.org/wiki/File:Triangle_Strip_Small.png[^] In this example a Triangle strip would be vertexes A, B, C, D, E, F. But a list of triangles would be: A,B,C, C,B,D, E,C,D, F,E,D You can see how incorrectly sending the right destination from one to the other could render with bizarre results....
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
-
I have never looked at the RAW output from Blender, and without compiling this only one thing "jumps" out at me.... g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, count); Are you sure that Blender is outputting a Triangle Strip or a list of triangles? http://en.wikipedia.org/wiki/File:Triangle_Strip_Small.png[^] In this example a Triangle strip would be vertexes A, B, C, D, E, F. But a list of triangles would be: A,B,C, C,B,D, E,C,D, F,E,D You can see how incorrectly sending the right destination from one to the other could render with bizarre results....
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
Blender is outputting a strange kind of list of triangles. For instance, a circle, it seems that Blender is outputting a triangle fan.
-
Blender is outputting a strange kind of list of triangles. For instance, a circle, it seems that Blender is outputting a triangle fan.
ant-damage wrote:
it seems that Blender is outputting a triangle fan.
Then you will need to define this as a triangle fan for display....
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
-
ant-damage wrote:
it seems that Blender is outputting a triangle fan.
Then you will need to define this as a triangle fan for display....
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
I think that I've found the solution for my problem here.
-
I think that I've found the solution for my problem here.
The RAW Triangle Format is parsed by reading lines, not individual values. That was what I didn't know.
-
The RAW Triangle Format is parsed by reading lines, not individual values. That was what I didn't know.
congrats on finding it! I never worked with raw, we use other model formats.
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
-
congrats on finding it! I never worked with raw, we use other model formats.
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
By the way, which formats do you use?
-
By the way, which formats do you use?
3dStudio, OpenFlight... Mostly basically everything supported by open scene graph. I haven't written my own loader for years. :)
_________________________ John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others." Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....