Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. RAW Triangle Format: How to parse it properly

RAW Triangle Format: How to parse it properly

Scheduled Pinned Locked Moved C / C++ / MFC
game-devhelptutorial
13 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A ant damage

    Hi I'm creating a 3D app which the main purpose for it is to be a game engine renderer. The file format that I will use for geometry is the RAW Triangle, since it is very simple to understand. I've created a function able to read the entire 3d file and parse it to a IDirect3DVertexBuffer9. Things are going fine here, but the problem is when it becomes rendered. I'm not getting the full object rendered, it seems that file is corrupted.

    E Offline
    E Offline
    El Corazon
    wrote on last edited by
    #2

    ant-damage wrote:

    Things are going fine here, but the problem is when it becomes rendered. I'm not getting the full object rendered, it seems that file is corrupted.

    Without seeing the code it is hard to know, but when you issue the call from your VertexBuffer are you setting the size and stride values correctly? Tutorial on IDirect3DVertexBuffer9()[^]

    _________________________ 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....

    A 1 Reply Last reply
    0
    • E El Corazon

      ant-damage wrote:

      Things are going fine here, but the problem is when it becomes rendered. I'm not getting the full object rendered, it seems that file is corrupted.

      Without seeing the code it is hard to know, but when you issue the call from your VertexBuffer are you setting the size and stride values correctly? Tutorial on IDirect3DVertexBuffer9()[^]

      _________________________ 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....

      A Offline
      A Offline
      ant damage
      wrote on last edited by
      #3

      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:
      PostQui

      E 1 Reply Last reply
      0
      • A ant damage

        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:
        PostQui

        E Offline
        E Offline
        El Corazon
        wrote on last edited by
        #4

        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....

        A 1 Reply Last reply
        0
        • E El Corazon

          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....

          A Offline
          A Offline
          ant damage
          wrote on last edited by
          #5

          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);
          

          }

          E 1 Reply Last reply
          0
          • A ant damage

            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);
            

            }

            E Offline
            E Offline
            El Corazon
            wrote on last edited by
            #6

            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....

            A 1 Reply Last reply
            0
            • E El Corazon

              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....

              A Offline
              A Offline
              ant damage
              wrote on last edited by
              #7

              Blender is outputting a strange kind of list of triangles. For instance, a circle, it seems that Blender is outputting a triangle fan.

              E 1 Reply Last reply
              0
              • A ant damage

                Blender is outputting a strange kind of list of triangles. For instance, a circle, it seems that Blender is outputting a triangle fan.

                E Offline
                E Offline
                El Corazon
                wrote on last edited by
                #8

                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....

                A 1 Reply Last reply
                0
                • E El Corazon

                  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....

                  A Offline
                  A Offline
                  ant damage
                  wrote on last edited by
                  #9

                  I think that I've found the solution for my problem here.

                  A 1 Reply Last reply
                  0
                  • A ant damage

                    I think that I've found the solution for my problem here.

                    A Offline
                    A Offline
                    ant damage
                    wrote on last edited by
                    #10

                    The RAW Triangle Format is parsed by reading lines, not individual values. That was what I didn't know.

                    E 1 Reply Last reply
                    0
                    • A ant damage

                      The RAW Triangle Format is parsed by reading lines, not individual values. That was what I didn't know.

                      E Offline
                      E Offline
                      El Corazon
                      wrote on last edited by
                      #11

                      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....

                      A 1 Reply Last reply
                      0
                      • E El Corazon

                        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....

                        A Offline
                        A Offline
                        ant damage
                        wrote on last edited by
                        #12

                        By the way, which formats do you use?

                        E 1 Reply Last reply
                        0
                        • A ant damage

                          By the way, which formats do you use?

                          E Offline
                          E Offline
                          El Corazon
                          wrote on last edited by
                          #13

                          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....

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups