The MASM Forum Archive 2004 to 2012

Project Support Forums => OpenGL Forum => Topic started by: Farabi on July 08, 2009, 07:53:53 AM

Title: Object Loader with texture?
Post by: Farabi on July 08, 2009, 07:53:53 AM
Anyone here able to load a popular object with its texture for openGL?
Title: Re: Object Loader with texture?
Post by: Farabi on July 12, 2009, 06:18:08 AM
OKay since no one seems to answer, I guess it is a difficult thing to handle.
Im gonna use a file named as a .tri file.
The .tri file is a simple 3D object format using plain text.
This file type is using GL_TRIANGLE to define each face.
To read this format type is simple. We only need to read each 3 line to form a face.
Each line have a format like this [Position X, Position Y, Position Z, Normal X, Normal Y, normal Z, UV X, UV Y]
This format is very simple is not it? I will try to make a function loader for this.

I uploaded a tri format file of a cube.

[attachment deleted by admin]
Title: Re: Object Loader with texture?
Post by: Farabi on July 13, 2009, 03:23:58 PM
Got it working.
I made a TRI object loader, but not completed yet.
This uploaded project will demonstrate you how to load a TRI file and then show it.
I will made this loader able to load the texture or coloring it.

On this file I also included a blender script which will be able to export your 3D model into TRI file.
Title: Re: Object Loader with texture?
Post by: dedndave on July 13, 2009, 03:51:58 PM
i see a black box, Farabi
xp sp2
Title: Re: Object Loader with texture?
Post by: rags on July 14, 2009, 01:43:27 AM
I can barely make out a grey square centered on a black background, with another grey square sqewed out of square to the right of the center square.

winxp home sp2
Title: Re: Object Loader with texture?
Post by: dedndave on July 14, 2009, 02:11:47 AM
oh yah - i see em - lol
if i maximize the window, they are in the lower left corner
Title: Re: Object Loader with texture?
Post by: Farabi on July 14, 2009, 02:36:16 AM
Yes it is It will only draw a cube.
Press a w s d to move in the 3D world. I will try to load a texture and view it.
So I guess no crashes.
Title: Re: Object Loader with texture?
Post by: dedndave on July 14, 2009, 03:39:33 AM
i couldn't make it crash Farabi  :U
Title: Re: Object Loader with texture?
Post by: Farabi on July 14, 2009, 03:32:21 PM
Okay, the TRI file loader able to load a texture. The real texture format is a TGA file but on this example I modify the tri file so the texture format became jpg. I dont really like jpg even it smaller in size, because the texture became bad and blurred.

I will reveal the magic trick of this picture.
(http://omploader.org/vMXdsZA/t3.JPG)

The loader on this project is very slow since it do a heavy transfer between RAM and VRAM. I can make it faster but the memory for the texture will be limited to 4 MB. I think I can use the VRAM up to 256 MB but for compatibility reason, I will stick to 16 MB.

Here is the bottle neck of my loader
Quote
fLoadTexture proc uses esi edi hBmp:dword,Wrap:dword
   LOCAL texture:dword
   LOCAL h:BITMAP
   LOCAL frm:dword
   LOCAL buff[256]:dword
   
   invoke glGenTextures,1,addr texture
   invoke glBindTexture,GL_TEXTURE_2D, texture
   invoke glTexEnvf,GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_MODULATE
   invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST
   invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR
   
   .if Wrap
      invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT
      invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT
   .else
      invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP
      invoke glTexParameterf,GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP
   .endif
   invoke GetObject,hBmp,sizeof BITMAP,addr h
   
   xor edx,edx
   xor eax,eax
   mov ax,h.bmBitsPixel
   mov ecx,8
   div ecx
   mov ecx,eax
   
   .if eax==3
      push GL_BGR_EXT
      pop frm
   .elseif eax==4
      push GL_BGRA_EXT
      pop frm
   .else   
      invoke MessageBox,0,CADD("Picture Depth Bit must be 24 or 32"),0,0
   .endif
   
   .if h.bmBits==0
      invoke MessageBox,0,CADD("Error"),0,0
   .endif
   
   invoke gluBuild2DMipmaps,GL_TEXTURE_2D,ecx,h.bmWidth,h.bmHeight,frm,GL_UNSIGNED_BYTE,h.bmBits
   .if eax!=0
      invoke gluErrorString,eax
      invoke MessageBox,0,eax,0,0
   .endif
   mov eax,texture
   
   ret
fLoadTexture endp

I called gluBuild2DMipmaps each time my object need to be drawn. This call function is to send data between RAM to VRAM. I can make this call only called once, but it will mean a confusing texture management where I only limited to 16 MB texture memory. If anyone had a good idea for optimizing this function, you are welcome.
Title: Re: Object Loader with texture?
Post by: dedndave on July 14, 2009, 03:36:33 PM
bmp format is lossless - not that small
you might be able to use the 256-color format
Title: Re: Object Loader with texture?
Post by: Farabi on July 15, 2009, 03:51:41 AM
Quote from: dedndave on July 14, 2009, 03:36:33 PM
bmp format is lossless - not that small
you might be able to use the 256-color format
Well, for shading 256-color not really good.
Maybe I can make the jpg picture not losless some how, even the file will be bigger but on 1024x1024 pixels the size is less than 200 kb.
Title: Re: Object Loader with texture?
Post by: dedndave on July 15, 2009, 05:41:01 AM
many photo editor programs allow you to adjust the "smoothing" and compression for jpegs
i sometimes use uncompressed 24-bit bmps for editing - they can be a bit large, though
Title: Re: Object Loader with texture?
Post by: Siekmanski on July 15, 2009, 06:43:13 AM
Hi Farabi

Is PNG an option ?

It's lossless data compression.
PNG supports palette-based (palettes of 24-bit RGB colors)
Title: Re: Object Loader with texture?
Post by: Farabi on July 15, 2009, 07:14:16 AM
Quote from: Siekmanski on July 15, 2009, 06:43:13 AM
Hi Farabi

Is PNG an option ?

It's lossless data compression.
PNG supports palette-based (palettes of 24-bit RGB colors)

Yes why not? Everything can be used as long as windows support it.
Title: Re: Object Loader with texture?
Post by: Farabi on July 15, 2009, 07:19:27 AM
I finished the texture loader. I will erase all other projects posts above.

Here is my comparation between Raydium and Mine.

Raydium
(http://omploader.org/vMXphaA/RaydiumSS.JPG)

Mine:
(http://omploader.org/vMXphZw/fGameEngineSS.JPG)

I cut the CPU Usage from 75% to 29%. And increase the FPS from 20 to 32.
I will try to integrate my Game Engine with Physic Engine and see the performance.

I uploaded the finished 3D models loader.

[attachment deleted by admin]