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
check if you have an old version of sharpgl containing this bug
http://sharpgl.codeplex.com/workitem/894

by the way, why did you prefer sharpgl over opentk?
Thanks for mention this issue with SharpGL. Maybe this explains the behaviour I see at present. I am using 2.0 and according to the link you provided version 2.1 should be fine without this error.

I switched to SharpGL for two reasons:
1) At many places on the net I have read that openTK is dead.
2) The naming of the functions in SharpGL looks for me more like the standard C functions, so discussing the features seems to be easier.
I have now the colors ready, but I am in trouble with the global behaviour of the openGL understanding.
As far as I have understand the whole thing, I have three different matrices:
Projection
Modelview
Textures (not yet used)

At first I set the projection matrix to the values I need.
At second step I set the modelview matrix.
Now I put the model into the modelview.

The following code currently gives blank window:
Code:
gl = Me.OpenGLControl2.OpenGL

        gl.Clear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT)
        gl.Viewport(0, 0, OpenGLControl2.Width, OpenGLControl2.Height)

        gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Projection)
        gl.LoadIdentity()
        gl.LookAt(CDbl(gradiusOfBoundingbox / Math.Sin(fov / 2)), CDbl(gradiusOfBoundingbox / Math.Sin(5 / 2)), CDbl(gradiusOfBoundingbox / Math.Sin(5 / 2)), CDbl(0), CDbl(0), CDbl(0), CDbl(0), CDbl(-1), CDbl(0))
        gl.Perspective(CDbl(fov), CDbl(OpenGLControl2.Width / OpenGLControl2.Height), CDbl(2), CDbl(2 * gradiusOfBoundingbox + 2))

        gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Modelview)
        gl.LoadIdentity()
        gl.Rotate(45.0F, 0.0F, 1.0F, 0.0F) 'Rotation for LDView like viewing angle
        gl.Rotate(22.5, 1, 0, 1)                'Rotation for LDView like viewing angle
        gl.Translate(gradiusOfBoundingbox, 0, gradiusOfBoundingbox)
        DrawLines(gl)
      
        gl.Flush()

I am right with my thoughts how this should work and I have only a stupid thinking error, or is the behaviour different than I wrote above??

Any help is very welcome.
I'm not familiar with SharpGL, so I'm not sure what you are doing in the projection part. Only thing I can tell you is you don't need lookat there (don't know what the gl.LookAt function does).

You only need to apply your lookat it in the viewmatrix, using a negative translate.
SharpGL should (hopefully) totally identically with standard OpenGL functions.
The glulookat function is part of the OpenGL Grafics System Utility Library
It should make life easier Smile

But you did not answer my global question. Did I understand opengl right if I have a world matrix and a model matrix that i can individually transform and both together make the result?
I did not realize it's the gluLookAt function. Anyway that's a modelview function, but the following perspective (gluPerspective i assume) will override it. This is because your preceding matrixmode indicated all matrix related functions must use the projection matrix.

The projection and viewmatrix must be set separately, and will always stay separate. In practice you initialize the projection matrix just once, and do all the per frame (camera control) stuff in the viewmatrix.

If you use the lookat function (after the matrixmode switch to modelview), you don't need to translate as the lookat function constructs the viewmatrix includiing lookat correction, hence the name.

I hope this is what you ment.
Ok, I changed to use the lookat at the modelview matrix.

I can now see something, but I need to have still an error in my view.

It's very frustrating Sad

Now I see not the full model, it is cutted. I believe it depends on the near, far, right, left, top, buttom parameters, but I stupid guy can not solve my problem so far. Sad
Near should be a fairly small number (1? ... 0.1?). Far should be greater than the distance to the farthest point in the model from the camera. If you have calulated a bounding sphere, you can probably just add the sphere's diameter to the distance between the camera and the lookat point. (You can use radius, but only if the lookat point is the center of the sphere.)
Yes, that is what I also thinking about. But there needs to be another thinking error. I have to check with a lot of coffee this morning Smile

Here is my current code (I see - nothing), that I now explain:
loadidentity - set the current matrix (Projection) to start modus
viewport - is the 2d window that should be used (left bottom corner at 0,0 and upper right corner from control width and height)
perspective (field of view in radian, ratio, znear, zfar) - for zfar I use two times the radius of the bounding box.
frustum (left, right, bottom, top, znear, zfar) - for left, right, bottom, top I use the radius of the bounding box, zfar like above.
Then I switch to the modelview and start with a fresh matrix.
The only transformation now is with the lookat matrix
lookat (eyex,eyey,eyez,centerx,centery,centerz,upx,upy,upz)
Code:
gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Projection)
        gl.LoadIdentity()
        gl.Viewport(0, 0, OpenGLControl2.Width, OpenGLControl2.Height)
        gl.Perspective(CDbl(fov), CDbl(OpenGLControl2.Width / OpenGLControl2.Height), CDbl(0.1), CDbl(2 * gradiusOfBoundingbox))
        gl.Frustum(CDbl(gradiusOfBoundingbox + 100), CDbl((gradiusOfBoundingbox + 100) * -1), CDbl((gradiusOfBoundingbox + 100) * -1), CDbl(gradiusOfBoundingbox + 100), CDbl(0.1), CDbl(2 * gradiusOfBoundingbox))

        gl.MatrixMode(SharpGL.Enumerations.MatrixMode.Modelview)
        gl.LoadIdentity()

        gl.LookAt(CDbl(0), CDbl(0), CDbl(0), CDbl(0), CDbl(0), CDbl((gradiusOfBoundingbox / Math.Sin(fov / 2)) * 2), CDbl(0), CDbl(-1), CDbl(0))

        DrawLines(gl)

        gl.Flush()

Where is my error??
OpenTK has the advantage that it is - different than SharpGL - not tied to Windows Forms.
This means that supporting Mono on e.g. Ubuntu will be easier,
where - AFAIK - the Windows Forms implementation is still poor (tell me if I'm wrong).
A good combination for you could be GTK# (for the GUI), plus GLWidget (for a GUI container holding OpenTK),
and inside that OpenTK for the OpenGL stuff.
http://stackoverflow.com/questions/94562...k-reliable
http://stackoverflow.com/questions/97788...onodevelop
This will give you a heavily portable, OS independent C# application.
OR can anybody recommend a better combination maybe? Including reasons? I'd be very curious to know.

Have fun!
Pages: 1 2 3 4 5 6