Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Showcase / Gosu / Obj. Loader
- By bestguigui Date 2009-03-09 07:58
Hi everyone ! :)

Thanks to Julian who has given me a new Gosu version with perfect texture support, I'm now able to complete my .obj loader. Because it's not known by everyone, a .obj is a 3D file format which describes a 3D model (vertices, normal vertices and texture coordinates).

It doesn't support animation, but it's one of the easiest formats to understand. Because I'm now able to load a texture using Gosu in a single memory location, I can do it. Note that at the moment, because I worked only like 1 hour on it, it supports .obj files with only one object and one texture.

Here's a windows demo (build with rubyscript2exe) :
http://bestguigui.free.fr/tutoriel_gosu/obj_loader_for_gosu_demo.rar

and here's an awful youtube render :
http://www.youtube.com/watch?v=1OdjMUX9PkY

I promise I will make a better one soon.

Here's a capture :
http://bestguigui.free.fr/tutoriel_gosu/objRender.png

For now, I'm thinking about this feature integration. Because I have to setup a 3D OpenGL environnement to be able to draw it, I'm currently making this in the draw method of my Window class, which is not that bad, but is not as simple to use as I want it to be.

Still thinking about it :)
Thank you for reading this
- By banister Date 2009-03-09 17:10
very cool :)
- By jlnr (dev) Date 2009-03-09 23:29
Are you targetting 3D or 2D scenes? It would be awesome if it could be used just like an Image :)
- By bestguigui Date 2009-03-10 10:51
And that's pretty much what I would like to do, just that I'm not sure it's really possible... I need a gl do... loop to make the 3D drawing possible, so I can inject this into the draw method of my obj_loader class, but for now I'm not able to mix it with Gosu 2D images, although I draw all Gosu images into an OpenGL context (in a 2D matrix view), which is really not what we want here...

Still thinking about it, thanks for your comment :)
- By jlnr (dev) Date 2009-03-10 11:59
Where are the problems with mixing it with 2D images? I think one issue is that the Z ordering will have to be done manually until one can schedule custom gl blocks into the Z ordering queue. (oli.obk actually requested that on the ToDo wiki page so that's on my list for the next some releases.)
- By bestguigui Date 2009-03-10 21:45
There should be no problem if I want to draw on top of a 3D object with a Gosu::Image, but I would like to be able to draw a 3D object on top of a Gosu::Image as well :)
- By jlnr (dev) Date 2009-03-11 00:17
No problem—as long as you only clear the depth buffer, you can draw on the things that have been previously drawn by Gosu. Just be sure that Image#draw is called before Your3DModel#draw (which is why Z ordering for custom OpenGL would be nice).
- By bestguigui Date 2009-03-15 12:23 Edited 2009-03-15 12:28
I'm still working on it and have progress. I'm able to draw on top and behing a classical Gosu::Image but I have troubles.

With this code on my GameWindow draw method :

  def draw
    @bg.draw(-0.1, 0, 0, 0.005, 0.005)
    gl do
      GL.ClearColor(0.0, 0.2, 0.5, 1.0)
      GL.ClearDepth(1.0)
      GL.Clear(GL::DEPTH_BUFFER_BIT)
   
      @logo.draw
    end
    @bg2.draw(0, 0, 0, 0.005, 0.005)
  end

I get this :
http://bestguigui.free.fr/tutoriel_gosu/wip_loader.png

on line "@logo.draw", I have some OpenGL Code to describe the 3D World (Camera) and to draw the 3D Model.
To be able to see the Gosu::Image objects, I had to scale down A LOT and I finally understood that Gosu seems to draw using a classic opengl Window coordinates system, with the origin point on center of the window and coordinates from -1.0 to 1.0 on both 2D axis.

Also, I'm not able anymore to draw a Gosu::Image if hard_border flag is not set to true and if textures are not powers of 2 !

Finally, objects are drawn in method call order, any value set for Z coordinate and images are reversed vertically.

Do you have clues to correct this ?
Thanks a lot
- By jlnr (dev) Date 2009-03-15 17:47

>       GL.ClearColor(0.0, 0.2, 0.5, 1.0)


This line does not to anything because you only set the DEPTH_BUFFER_BIT :)

> To be able to see the Gosu::Image objects, I had to scale down A LOT and I finally understood that Gosu seems to draw using a classic opengl Window coordinates system, with the origin point on center of the window and coordinates from -1.0 to 1.0 on both 2D axis.


Hmm, usually Gosu tries its best to give the code inside the gl{} block a default OpenGL setup, and to restore Gosu's rendering state completely afterwards. If your code changes the way the two images appear, you must be pushing and not popping something or so? :)

> Also, I'm not able anymore to draw a Gosu::Image if hard_border flag is not set to true and if textures are not powers of 2 !


Not in a 3D scene or not at all?

Hope I can clear things up!
- By bestguigui Date 2009-03-15 18:43
For the line being useless, I code roughtly sometimes and don't delete what I used before ;)

If I never call OpenGL in a gl..do loop, no problem at all using Gosu::Image objects as usual. No change from before.
But right now there is still this problem, when I set a 3D Scene.

There are only two blocks of opengl code for now in my script :

in my GameWindow class :
  def draw
    @bg.draw(-1.0, 0, 0, 0.005, 0.005)
    gl do
      GL.Clear(GL::DEPTH_BUFFER_BIT)   
      @logo.draw
    end
    @bg2.draw(0.0, 0.0, 0.0, 0.005, 0.005)
  end

in my 3D Object loader class :
  def draw
    GL.Enable(GL::DEPTH_TEST)
   
    GL.ShadeModel(GL::SMOOTH);
    GL.Enable(GL::LIGHTING);
    GL.Enable(GL::LIGHT0)

    GL.MatrixMode(GL::PROJECTION)
    GL.LoadIdentity()   
   
    GLU.Perspective(45, 800.to_f/600.to_f, 0.01, 100.0)
   
    GL.MatrixMode(GL::MODELVIEW)
    GL.LoadIdentity()
    GLU.LookAt(0.0, 15.0, 30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0)    

    position = [0.0, -5.0, 8.0, 10.0]
    GL.Light(GL::LIGHT0, GL::POSITION, position)
   
    light_ambient = [1.0, 1.0, 1.0, 1.0]
    GL::Light(GL::LIGHT0, GL::AMBIENT, light_ambient)
   
    GL.CallList(@display_list)
  end

These blocks shouldn't cause any problem I suppose. Did I miss something ?
Thanks a lot
- By bestguigui Date 2009-03-16 07:26 Edited 2009-03-16 07:28
I'm now having a strange texture issue : in a random time gap, textures are lost in memory. On program launch, everything is correct, but if I wait about 30 seconds, textures start to disapear, turning my 3D models to grey. I added a reloadTexture method to test this, which is working : my texture is back.

I never had such a problem in the past. My objects are drawn each frame, so they are used all the time. I'm starting to think it has to do with Ruby and its garbage collector mechanism, is there a way to know that my texture is lost, to be able to self call reloadTexture inside my object ?

http://bestguigui.free.fr/tutoriel_gosu/bug/with_texture.png

After about 15~30 seconds...

http://bestguigui.free.fr/tutoriel_gosu/bug/without_texture.png

And the other textures disepear too if I wait a little more.

Thanks a lot !
- By jlnr (dev) Date 2009-03-16 18:30
It should be enough if you hold a reference to the Gosu::Image. As the GL texture name is just a Fixnum, it cannot hold the Image/texture alive.
- By bestguigui Date 2009-03-16 22:03
It works perfectly ! Thanks ! I changed a very little my texture handle as you suggested and textures don't disapear anymore :)
- By bestguigui Date 2009-03-23 21:52
Still in progress !

http://www.youtube.com/watch?v=o0mAqPfNi34&feature=channel_page
http://img4.imageshack.us/img4/185/wipu.png
http://bestguigui.free.fr/tutoriel_gosu/camera_fps_demo.rar

I made a little 3D Scene with a Spiderman character, a very small portion of road and a Skybox. It's running at 60 FPS without any lag (the Youtube video is not showing that, but my computer is so slow when I capture a video... :( )

I still don't know why I can't get Gosu rendering after my Gosu::Window.gl do...end loop, even if I studied a lot my code, comparing it to rubyOpenGLIntegration.rb. I tried to draw a text using a Gosu::Font object after my gl loop, and it just doesn't show :(
Up Topic Gosu / Gosu Showcase / Gosu / Obj. Loader

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill