LDraw.org Discussion Forums

Full Version: Simple renderer in .net with SharpGL (openGL)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6
Yes, with my approach I think it is easy to rotate the part and zoom in and out to look for some details. This should be at the end only mouse driven like ldview does, as everybody is familar with that tool.

Now I only need someone who tells me how to do that calculation with the given tools of .net Smile
I can't directly answer your question, since I don't know Direct3D. However, I can give an overview of what LDView does.

IIRC, LDView first calculates the bounding box for the whole model. Next, using the center of that box, it calculates the bounding sphere for the whole model. It then moves the camera to the center of the sphere, then backs it up by an amount calculated based on the FOV and the sphere radius. I believe that the distance to move back is:

radius / sin(fov/2)

Note that I believe fov in the above is either the horizontal FOV (if the window is taller than it is wide) or the vertical FOV (if the window is wider than it is tall). OpenGL expects you to always give it the vertical FOV, so LDView calculates that based on the horizontal FOV when the window is taller than it is wide. I suspect that Direct3D also wants the vertical FOV. If you always use the same FOV value, no matter what the window shape, I believe you'll get a cropped image if you use that in the above calculation when the window is taller than it is wide.

If have have the horizontal FOV, the vertical FOV can be calculated like so:

VFOV = 2*atan(tan(hfov/2)/(width/height))

Note also that LDView's default zoom level (calculated as per above) is set up so that no matter how you rotate the model, it will always be fully visible (which is why it uses the bounding sphere).

LDView's "zoom to fit" functionality uses an algorithm I got from Lars C. Hassing that solves numerous equations for numerous unknowns to move the view pyramid around until at least two points on the model are exactly at the edge of the view. (Those two points are either at the top and bottom of the view, or at the left and right.) The pyramid is moved in such a way that the view direction remains constant, but the camera position moves in all three dimensions, so the look-at point also moves.
Thanks for leaving your ideas here Smile

The way of calculating the camera position is the same I thought that should be a good way, so I will go with that.

For the FOV I use the following function:
http://msdn.microsoft.com/query/dev10.qu...29&rd=true

So it seems that you are right with your guess Smile

Now I _only_ have to put that into some code.

I'll keep you informed.
Grmph....

Now I realized that DirectX is just not the choice that I should take, as this requires wine under linux.

I need to use a .net version of an openGL engine.

If someone has experience with that I would be glad to get help Smile
All you need are the header files, I'm sure a quick search will point you to them if they aren't available by default. The default ones might be old, but for basic LDraw rendering OpenGL 2.0 is good enough unless you want to do some very fancy stuff.
I have taken the openTK library to get it simple Smile

First steps are made.
Why isn't there a good tutorial on these things on the net ? Sad

After several hours of searching I am close before shutting down my idea.

Current status:
I have a control where I can draw a triangle in ortho mode.
All attempts that I made so far for a 3D part (at least a simple box) failed.

If someone has already experience with OpenTK (please not direct openGL) please let me know in simple words:

How do I set the camera in a world.
How do I set a light in a world.
How do I set the lookat point in a world.

Any help is very much wished.
Currently i am far away from the point I already reached with DirectX, but I need to switch to openTK for different os's.
For example:

Code:
Private Sub GlControl1_Load(sender As Object, e As System.EventArgs) Handles GlControl1.Load
        glLoaded = True
        GL.ClearColor(Color.SkyBlue) ' .NET Colors can be used directly!
        SetupViewport()
End Sub
Private Sub SetupViewport()
        Dim w As Integer = GlControl1.Width
        Dim h As Integer = GlControl1.Height
        GL.MatrixMode(MatrixMode.Projection)
        GL.LoadIdentity()
        GL.Ortho(0, w, 0, h, -1, 1) ' // Bottom-left corner pixel has coordinate (0, 0)
        GL.Viewport(0, 0, w, h) ' // Use all of the glControl painting area
End Sub
Public Sub GlControl1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles GlControl1.Paint
        If Not glLoaded Then Exit Sub

        GL.Clear(ClearBufferMask.ColorBufferBit Or ClearBufferMask.DepthBufferBit)

        GL.MatrixMode(MatrixMode.Modelview)
        GL.LoadIdentity()
        GL.Color3(Color.Brown)

        GL.Begin(BeginMode.Triangles)
        GL.Vertex2(10, 20)
        GL.Vertex2(100, 20)
        GL.Vertex2(100, 50)
        GL.End()

        GlControl1.SwapBuffers()
End Sub

This is the code that currently works.
Why don't you want to use the OpenGL api directly? There are tons of tutorials/examples for that, as it doesn't really matter which language you are using.

Also you your above code seems to use intermediate mode, which is something you want to avoid for the core part mesh data.
Why I do not use the api directly? In visual basic you can not just add the header files from another language. You have to define each function etc separately. So this is already done by the openTK team.

What do you mean by
Roland Melkert Wrote:Also you your above code seems to use intermediate mode, which is something you want to avoid for the core part mesh data.

I am an absolutly newbee if we coming to 3D coding, so please explain for stupid guys Smile
Pages: 1 2 3 4 5 6