Loading ...
Sorry, an error occurred while loading the content.

Problem with rendering an example with LablGl

Expand Messages
  • consulatewizard
    Hi, I have two programs that are supposed to render the same scene (second one has a few extra features to rotate cube, enable/disable lighting, change texture
    Message 1 of 16 , Apr 8, 2006
    • 0 Attachment
      Hi,

      I have two programs that are supposed to render the same scene
      (second one has a few extra features to rotate cube, enable/disable
      lighting, change texture quality).

      Anyways, first is just a bunch of functions, which works as
      expected. The second uses an object to encapsulate all the state. I
      tried to model it similarly to the lablglut gears example.

      For the life of me, I can't figure out what's wrong with the second
      example. All I get is a black screen.

      Would appreciate some extra eyes that might be able to pinpoint the
      problem -- I've been staring at the code for hours & hours ;(

      Working example: http://www.purevoid.org/tut7a.ml
      Non-working example: http://www.purevoid.org/tut7b.ml
      Texture used for the examples: http://www.purevoid.org/texture.raw

      The code is based on Tutorial 7 @ NeHe
      <http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=07>

      Kindest Regards,

      Jonathan Roewen
    • Chris Campbell
      Partial application/whatever it s name is? Check glerror? I haven t looked closely at your code, but it looks like it Should Work. It s the worst kind of
      Message 2 of 16 , Apr 9, 2006
      • 0 Attachment
        Partial application/whatever it's name is? Check glerror? I haven't
        looked closely at your code, but it looks like it Should Work. It's
        the worst kind of code, the code that looks right but you can't figure
        out what's wrong with it.

        Btw, don't call the draw routine in idle callback, it can lead to
        problems with redrawing. Instead, use Glut.postRedisplay. Using the
        draw routine in the idle callback got me and there was a serious issue
        while it was running, though I can't remember if it the machine got
        really slow or incomplete drawing, though both seem right. Also been
        bitten by partial application in the display callback.


        Regards,
        Chris

        p.s. 7a isn't up there.
      • Jonathan Roewen
        I m a dumbass ;-) I set the depth to 0.0 .. can t believe how long it took to finally find it... On another note: it seems that without lighting on, everything
        Message 3 of 16 , Apr 9, 2006
        • 0 Attachment
          I'm a dumbass ;-) I set the depth to 0.0 .. can't believe how long it
          took to finally find it...

          On another note: it seems that without lighting on, everything is
          rendered without any blue. IE: white surfaces show up as yellow, etc.

          Jonathan
        • Chris Campbell
          ... Hehe. ... Did you change the GlDraw.color call inside the begin block? It is set to GlDraw.color (1.0, 0.8, 0.3); OpenGL only retains the most recent
          Message 4 of 16 , Apr 10, 2006
          • 0 Attachment
            On 10/04/06, Jonathan Roewen <jonathan.roewen@...> wrote:
            > I'm a dumbass ;-) I set the depth to 0.0 .. can't believe how long it
            > took to finally find it...

            Hehe.

            > On another note: it seems that without lighting on, everything is
            > rendered without any blue. IE: white surfaces show up as yellow, etc.

            Did you change the GlDraw.color call inside the begin block? It is
            set to GlDraw.color (1.0, 0.8, 0.3); OpenGL only retains the most
            recent colour, so if you set it outside of the begin block, it will be
            over-ridden by the call inside it.
          • Jonathan Roewen
            ... Ohh! I didn t see that :-) And I ve just discovered the differences in colouring a surface with lighting and without lighting, so it s all finally making a
            Message 5 of 16 , Apr 10, 2006
            • 0 Attachment
              > Did you change the GlDraw.color call inside the begin block? It is
              > set to GlDraw.color (1.0, 0.8, 0.3); OpenGL only retains the most
              > recent colour, so if you set it outside of the begin block, it will be
              > over-ridden by the call inside it.

              Ohh! I didn't see that :-) And I've just discovered the differences in
              colouring a surface with lighting and without lighting, so it's all
              finally making a bit of sense :-)

              Jonathan
            • Jonathan Roewen
              I have a new problem :-) Here s snippets from my implementation: class view = let texture = GlTex.gen_textures 2 and quadric = GluQuadric.create () in
              Message 6 of 16 , Apr 10, 2006
              • 0 Attachment
                I have a new problem :-)

                Here's snippets from my implementation:

                class view =
                let texture = GlTex.gen_textures 2
                and quadric = GluQuadric.create () in
                object(self)
                method draw_ball =
                GlDraw.color (1.0, 1.0, 1.0);
                GlTex.bind_texture `texture_2d texture.(1);
                GluQuadric.sphere ~quad:quadric ~radius:0.35 ~slices:32
                ~stacks:16 ()

                method draw_floor =
                GlTex.bind_texture `texture_2d texture.(0);
                GlDraw.begins `quads;
                ...
                GlDraw.ends ()

                method draw =
                (* calls self#draw_ball & self#draw_floor *)
                Glut.swapBuffers ()

                initializer
                (*
                let image = [|
                load_raw "envwall.raw" 128, 128;
                load_raw "ball.raw" 128, 128;
                |] in
                for i = 0 to 1 do
                GlTex.bind_texture `texture_2d texture.(i);
                GlTex.image2d
                (GlPix.of_raw (fst image.(i)) `rgb (snd image.(i))
                (snd image.(i)));
                GlTex.parameter `texture_2d (`mag_filter `linear);
                GlTex.parameter `texture_2d (`min_filter `linear);
                done;
                *)
                let image0 = load_raw "envwall.raw" 128 in
                let image1 = load_raw "ball.raw" 128 in
                (* first texture *)
                GlTex.bind_texture `texture_2d texture.(0);
                GlTex.image2d (GlPix.of_raw image0 `rgb 128 128);
                GlTex.parameter `texture_2d (`mag_filter `linear);
                GlTex.parameter `texture_2d (`min_filter `linear);
                (* second texture *)
                GlTex.bind_texture `texture_2d texture.(1);
                GlTex.image2d (GlPix.of_raw image1 `rgb 128 128);
                GlTex.parameter `texture_2d (`mag_filter `linear);
                GlTex.parameter `texture_2d (`min_filter `linear);
                GluQuadric.normals quadric `smooth;
                GluQuadric.texture quadric true;
                GlTex.gen `s (`mode `sphere_map);
                GlTex.gen `t (`mode `sphere_map)
                end

                Okay, quite a bit of code in there. Anyways, what happens is the
                texture used on both the ball AND the floor are the same (the texture
                used is the last one to be loaded).

                Is this a bug in LablGL's GlTex.gen_textures <n>? I can't see how else
                this could happen...

                Jonathan
              • Jonathan Roewen
                ... Changing above (and other code it depends on) to: let texture1 = GlTex.gen_texture () and texture2 = GlTex.gen_texture () ... and I still get the same
                Message 7 of 16 , Apr 10, 2006
                • 0 Attachment
                  > class view =
                  > let texture = GlTex.gen_textures 2
                  > and quadric = GluQuadric.create () in

                  Changing above (and other code it depends on) to:
                  let texture1 = GlTex.gen_texture ()
                  and texture2 = GlTex.gen_texture ()
                  ...

                  and I still get the same behaviour. Perhaps this is because I'm using a class?
                • Jonathan Roewen
                  ... Here s some debug info (after discovering that GlTex.texture_id is represented as an int32...): for i = 0 to Array.length texture - 1 do Printf.printf
                  Message 8 of 16 , Apr 10, 2006
                  • 0 Attachment
                    > > let texture = GlTex.gen_textures 2
                    > > and quadric = GluQuadric.create () in

                    Here's some debug info (after discovering that GlTex.texture_id is
                    represented as an int32...):

                    for i = 0 to Array.length texture - 1 do
                    Printf.printf "Texture ID: %ld\n%!" (Obj.magic texture.(i));
                    done;

                    Outputs:
                    C:\reflection>ocaml -I +lablgl lablgl.cma lablglut.cma reflection.ml
                    Texture ID: 4683992
                    Texture ID: 4683992

                    In fact, using my own constants, and using Obj.magic to turn them into
                    GlTex.texture_id values, my scene renders correctly.

                    Jonathan
                  • Chris Campbell
                    I think I know what s happening here. The code for gen_textures is as follows. (lablgl 1.02) let gen_textures n = Array.init n (fun _ - gen_texture ()) This
                    Message 9 of 16 , Apr 10, 2006
                    • 0 Attachment
                      I think I know what's happening here. The code for gen_textures is as
                      follows. (lablgl 1.02)

                      let gen_textures n = Array.init n (fun _ -> gen_texture ())

                      This is wrong because gen_texture which calls glGenTexture(s) to
                      generate an id. OpenGL will return a free id, but in this case the
                      driver is returning the same free id over and over because it is free.
                      There is no guarantee successive ids will be returned by opengl,
                      which this code assumes. To get the correct behaviour, the
                      gen_textures function should call glGenTextures with an array and copy
                      it across.

                      I will fix this tonight (lablgl isn't my code, but i'll patch it and
                      send it to the maintainer). My understanding of the Ocaml Native
                      Interface is sketchy so it might not be perfect. Will need testing.


                      Regards,
                      Chris Campbell
                    • Jonathan Roewen
                      ... Why is it free? I checked the man pages for glGenTextures, and it says you can call glGenTextures multiple times. In fact, the same code works fine without
                      Message 10 of 16 , Apr 10, 2006
                      • 0 Attachment
                        > This is wrong because gen_texture which calls glGenTexture(s) to
                        > generate an id. OpenGL will return a free id, but in this case the
                        > driver is returning the same free id over and over because it is free.

                        Why is it free? I checked the man pages for glGenTextures, and it says
                        you can call glGenTextures multiple times.

                        In fact, the same code works fine without modification on windows. I'm
                        starting to think it's a bug with some library component on
                        Windows....

                        Jonathan
                      • Chris Campbell
                        ... Actually you re correct, the spec does say it should return different values but perhaps your driver isn t following the spec correctly. There are a few
                        Message 11 of 16 , Apr 11, 2006
                        • 0 Attachment
                          On 11/04/06, Jonathan Roewen <jonathan.roewen@...> wrote:
                          > > This is wrong because gen_texture which calls glGenTexture(s) to
                          > > generate an id. OpenGL will return a free id, but in this case the
                          > > driver is returning the same free id over and over because it is free.
                          >
                          > Why is it free? I checked the man pages for glGenTextures, and it says
                          > you can call glGenTextures multiple times.
                          >

                          Actually you're correct, the spec does say it should return different
                          values but perhaps your driver isn't following the spec correctly.
                          There are a few posts in the newsgroup archives about a similar issue,
                          perhaps it is a known issue. Which opengl libraries do you use? The
                          windows ones or the nvidia/ati native drivers?

                          Chris
                        • Jonathan Roewen
                          ... opengl32.dll is by MS. and using latest nvidia drivers.
                          Message 12 of 16 , Apr 11, 2006
                          • 0 Attachment
                            > Actually you're correct, the spec does say it should return different
                            > values but perhaps your driver isn't following the spec correctly.
                            > There are a few posts in the newsgroup archives about a similar issue,
                            > perhaps it is a known issue. Which opengl libraries do you use? The
                            > windows ones or the nvidia/ati native drivers?

                            opengl32.dll is by MS. and using latest nvidia drivers.
                          • Jonathan Roewen
                            It was my problem =) I didn t know let in a class definition binds -static- variables inside the class that are evaluated at -init- time. basically: class c =
                            Message 13 of 16 , Apr 11, 2006
                            • 0 Attachment
                              It was my problem =)

                              I didn't know let in a class definition binds -static- variables
                              inside the class that are evaluated at -init- time.

                              basically:

                              class c =
                              let textures = GlTex.gen_textures n in
                              object(self)
                              (* class definition *)
                              end

                              is very, very wrong =P

                              I'll have to check the documentation on objects...

                              Jonathan
                            • Chris Campbell
                              Ok, I need to turn my brain on instead of chasing the wrong thing. I m still puzzled as to why it works on one system but not another. You said it worked in
                              Message 14 of 16 , Apr 11, 2006
                              • 0 Attachment
                                Ok, I need to turn my brain on instead of chasing the wrong thing.
                                I'm still puzzled as to why it works on one system but not another.
                                You said it worked in one case but not in another, right? Was the
                                difference the compiler/driver or was it a different program?
                              • Jonathan Roewen
                                ... Perhaps it was a lenient GL/GLUT implementation on linux. Basically, what happened is it was generating texture ids before an opengl context was created.
                                Message 15 of 16 , Apr 11, 2006
                                • 0 Attachment
                                  > Ok, I need to turn my brain on instead of chasing the wrong thing.
                                  > I'm still puzzled as to why it works on one system but not another.
                                  > You said it worked in one case but not in another, right? Was the
                                  > difference the compiler/driver or was it a different program?

                                  Perhaps it was a lenient GL/GLUT implementation on linux. Basically,
                                  what happened is it was generating texture ids before an opengl
                                  context was created.

                                  Why linux ignores the error is beyond me :-)

                                  (Same program, same ocaml compiler & library versions -- different
                                  operating systems).

                                  Jonathan
                                • Martin Jambon
                                  ... I agree that it may be surprising. Just add a function parameter and it should do what you want: class c () = let textures = GlTex.gen_textures n in
                                  Message 16 of 16 , Apr 11, 2006
                                  • 0 Attachment
                                    On Wed, 12 Apr 2006, Jonathan Roewen wrote:

                                    > It was my problem =)
                                    >
                                    > I didn't know let in a class definition binds -static- variables
                                    > inside the class that are evaluated at -init- time.
                                    >
                                    > basically:
                                    >
                                    > class c =
                                    > let textures = GlTex.gen_textures n in
                                    > object(self)
                                    > (* class definition *)
                                    > end
                                    >
                                    > is very, very wrong =P

                                    I agree that it may be surprising. Just add a function parameter and it
                                    should do what you want:

                                    class c () =
                                    let textures = GlTex.gen_textures n in
                                    object(self)
                                    (* class definition *)
                                    end


                                    Martin

                                    --
                                    Martin Jambon, PhD
                                    http://martin.jambon.free.fr

                                    Edit http://wikiomics.org, bioinformatics wiki
                                  Your message has been successfully submitted and would be delivered to recipients shortly.