adityatan's picture

3D Canvas

Hello:

I'm creating a GUI for a structural analysis program, which is quite similar to the setting in Google SketchUp.

Since I don't know much about canvas in OpenTK (and I can't find a good reference on the web), I decided to post the question here.

Is it feasible to create a 3D canvas on OpenTK? If so, is there any tutorial for that?

If it is not feasible, is there any recommendation?

Thank you very much for your help!

AT


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
the Fiddler's picture

OpenTK allows you to create an OpenGL window (window/control/surface/canvas) that you can either use standalone or integrate into a GUI.

You can find a detailed WinForms+OpenTK tutorial here. You can also use this OpenTK.GLControl in a WPF application and there is an equivalent GLWidget class for GTK# (for cross-platform applications).

Note that OpenTK is a low-level library: it will give you access to OpenGL commands and a number of supporting classes (math, input, display modes) but will not, for instance, load 3d models for you (you will need to do that yourself or find a model-loading library, e.g. meshomatic).

adityatan's picture

Dear Fiddler,

Thanks for the explanation.

Sorry for my non-specific question. What I meant by "canvas" is if I can paint circles, lines or points using mouse (by click-and-dragging). I have created the WinForm using GLControl where it loads my 3D Grid, and now what I want to do is to place nodes where the grids intersect.

Thanks again.

AT

the Fiddler's picture

This is indeed possible (that's the point of OpenGL). There are two things you need to do:

  • Hook the MouseDown event in GLControl and translate the mouse coordinates from WinForms (where (0, 0) is top-left) to the coordinate system you are using.
  • Render the selected shape at that position.

OpenGL offers built-in graphics primitives for points, lines, triangles and quads (quads have been deprecated in favor of triangles). Circles can be represented as a collection of connected lines (simple approach: evaluate (x, y) = r * (cos(φ), sin(φ)) where φ = [0, 2*π) and store the results for rendering). All complex shapes can be built using these primitives (e.g. curves, 3d solids).

Check the geometry section in the graphics documentation.

adityatan's picture

Thanks, Fiddler. I will try, and I'll consult again if I have questions.

AT

adityatan's picture

Hi there:

This is the continuation of my question regarding 3D canvas.

I've not learned the 3D to 2D transformation matrix yet; what I'm trying to do here is to paint a node on my gl_Control1 using a click of a mouse. I managed to do this using keyboard; but using a mouse, it seems that the gl_Control1 won't get "updated" or "repainted".

Below is some of my code:

private void glControl1_Paint(object sender, PaintEventArgs e)
        {
            ...
 
            paintGridNodeSet();
 
            glControl1.SwapBuffers();
        }
 
        private void paintGridNodeSet()
        {
            GL.PointSize(2);
            GL.Color3(Color.Black);
            GL.Begin(BeginMode.Points);
 
            foreach (var points in GridNodeSet)
            {
                GL.Vertex3(points);
            }
            GL.End();
 
            GL.PointSize(10);
            GL.Color3(Color.Red);
            GL.Begin(BeginMode.Points);
            GL.Vertex3(newnodes, newnodes, newnodes);
            GL.Vertex3(oldMouse.X, oldMouse.Y, 3);
            GL.End();
 
            glControl1.Invalidate();
        }
 
        private void glControl1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                oldMouse = e.Location;
            }
 
            glControl1.Invalidate();
        }

Am I missing something? Thanks guys!

AT

adityatan's picture

Indeed, *cough*, I forgot to put down the glControl1_MouseMove event.

It works now. Thanks!

AT