Anonymous's picture

Usage OpenTK

Hello.
How use OpenTK to draw landscape?
I have array of squares. I try compile QuickStart/game.cs and I can`t did it.


Comments

Comment viewing options

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

If the array doesn't show up on screen, it could be that the projection matrix is not setup correctly.

Please, post your QuickStart/game.cs file here (between code tags), and we'll find what's wrong.

Anonymous's picture

QuickStart/game.cs is a part of "opentk.zip" which I downloaded from sourceforge.net.

the Fiddler's picture

I thought you had changed game.cs (I have array of squares.)

Are you using an IDE or are you trying to compile through the command line?

Anonymous's picture

So strange. I have two version of QuickStart.

1. All Ok. Open with C#2008 Express:
...
GL.Begin(BeginMode.Triangles);

GL.Color3(Color.LightYellow); GL.Vertex3(-1.0f, -1.0f, 4.0f);
GL.Color3(Color.LightYellow); GL.Vertex3(1.0f, -1.0f, 4.0f);
GL.Color3(Color.LightSkyBlue); GL.Vertex3(0.0f, 1.0f, 4.0f);

GL.End();
...

2. Not Ok.
...
public struct SVertex
{
public float x, y, z;
public float r, g, b;
};
...
List Vertex = new List();
...
Vertex[0].x = -1; Vertex[0].y = -1; Vertex[0].z = 4; //Compile error
...
GL.VertexPointer(3, VertexPointerType.Float, 2, Vertex); //Compile error

How create and use Vertex Array?

the Fiddler's picture

This code is not entirely correct:

List Vertex = new List();
...
Vertex[0].x = -1; Vertex[0].y = -1; Vertex[0].z = 4; //Compile error
Vertex[0].x = -1.0f; Vertex[0].y = -1.0f; Vertex[0].z = 4.0f; // Should work

You can also try this, which is a little cleaner:

public struct SVertex
{
    public float x, y, z;
    public float r, g, b;
 
    public SVertex(float new_x, float new_y, float new_z)
    {
        x = new_x;
        y = new_y;
        z = new_z;
    }
};
 
List<SVertex> data = new List<SVertex>();
...
data.Add(new SVertex(-1.0f, -1.0f, 4.0f));

Regarding Vertex Arrays, I'll direct you towards this page in the manual which discusses Vertex Buffer Objects: http://www.opentk.com/doc/chapter/2/opengl/geometry/vbo. It explains how to create and use them, showing the necessary code.

Anonymous's picture

//Now this code compiled. What I must add or change?

using System;
using System.Drawing;
using System.Collections.Generic;
 
using OpenTK;
using OpenTK.OpenGL;
using OpenTK.OpenGL.Enums;
using OpenTK.Fonts;
using OpenTK.Math;
using OpenTK.Input;
using OpenTK.Platform;
 
namespace QuickStart
{
    class Game : GameWindow
    {
        public struct SVertex
        {
            public float x, y, z;
            public float r, g, b;
 
            public SVertex(float new_x, float new_y, float new_z)
            {
                x = new_x;
                y = new_y;
                z = new_z;
                //True black color
                r = 255;
                g = 255;
                b = 255;
            }
        };
 
        List<SVertex> Vertex = new List<SVertex>();
 
        /// <summary>
        /// Creates a 800x600 window with the specified title.
        /// </summary>
        public Game() : base(new DisplayMode(800, 600), "OpenTK Quick Start Sample") { }
 
        /// <summary>
        /// Load resources here.
        /// </summary>
        /// <param name="e">Not used.</param>
        public override void OnLoad(EventArgs e)
        {
            GL.ClearColor(System.Drawing.Color.SteelBlue);
            GL.Enable(EnableCap.DepthTest);
            GL.EnableClientState(EnableCap.VertexArray);
 
            Vertex.Add(new SVertex(0, 1, 1));
            Vertex.Add(new SVertex(-1, -1, 0));
            Vertex.Add(new SVertex( 1, -1, 1));
 
            GL.VertexPointer(3, VertexPointerType.Float, 3, Vertex.ToArray());
        }
 
        /// <summary>
        /// Called when your window is resized. Set your viewport here. It is also
        /// a good place to set up your projection matrix (which probably changes
        /// along when the aspect ratio of your window).
        /// </summary>
        /// <param name="e">Contains information on the new Width and Size of the GameWindow.</param>
        protected override void OnResize(ResizeEventArgs e)
        {
            GL.Viewport(0, 0, Width, Height);
            GL.MatrixMode(MatrixMode.Projection);
            GL.LoadIdentity();
            Glu.Perspective(45.0, Width / (double)Height, 1.0, 64.0);
        }
 
        /// <summary>
        /// Called when it is time to setup the next frame. Add you game logic here.
        /// </summary>
        /// <param name="e">Contains timing information for framerate independent logic.</param>
        public override void OnUpdateFrame(UpdateFrameEventArgs e)
        {
            if (Keyboard[Key.Escape])
                Exit();
        }
 
        /// <summary>
        /// Called when it is time to render the next frame. Add your rendering code here.
        /// </summary>
        /// <param name="e">Contains timing information.</param>
        public override void OnRenderFrame(RenderFrameEventArgs e)
        {
            GL.Clear(ClearBufferMask.ColorBufferBit |
                     ClearBufferMask.DepthBufferBit);
 
            GL.MatrixMode(MatrixMode.Modelview);
            GL.LoadIdentity();
            Glu.LookAt(Vector3.Zero, Vector3.UnitZ, Vector3.UnitY);
 
            GL.Begin(BeginMode.Polygon); 
 
            GL.DrawArrays(BeginMode.Triangles, 0, 1);
 
            GL.End();
 
            SwapBuffers();
        }
 
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            // The 'using' idiom guarantees proper resource cleanup.
            using (Game game = new Game())
            {
                game.Run(60.0, 0.0);
            }
        }
    }
}
Inertia's picture

GL.VertexPointer(3, VertexPointerType.Float, 3, Vertex.ToArray());

Using Vertex Arrays in C# is a bit problematic, you must pin the managed array until GL.DrawArrays has finished execution on the GPU (not CPU). Rather use the more elegant Vertex Buffer Objects or immediate mode.

A working approach can be found here http://www.opentk.com/doc/chapter/2/opengl/geometry