Ubinator's picture

Help with OpenGL matrix tutorial

My problem is that I'm following this tutorial:

And I just can't get it work, I have played with everything, but the triangle just won't appear, it always a green screen, so I'm thinking that somewhere something must be unique to OpenTK which is hindering me, that or I suck.

Heres all my code:

Imports OpenTK
Imports OpenTK.Graphics
Imports OpenTK.Graphics.OpenGL
Imports OpenTK.Input
Public Class OpenWindow
    Inherits GameWindow
    Private Vertex_Buffer_Data() As Single = {
        -1.0F, -1.0F, 0.0F,
        1.0F, -1.0F, 0.0F,
        0.0F, 1.0F, 0.0F
    Private Vertex_Buffer As Integer
    Private ProgramID As Integer
    Private MatrixID As Integer
    Private MVP As Matrix4
    ''' <summary>Creates a 800x600 window with the specified title.</summary>
    Public Sub New()
        MyBase.New(1024, 768, New GraphicsMode(32, 24, 8, 4), "RadarTracker 6000")
        VSync = VSyncMode.[On]
    End Sub
#Region "OnLoad"
    Protected Overrides Sub OnLoad(ByVal e As EventArgs)
        Dim VertexArrayID As Integer
        GL.GenVertexArrays(1, VertexArrayID)
        GL.GenBuffers(1, Vertex_Buffer)
        GL.BindBuffer(BufferTarget.ArrayBuffer, Vertex_Buffer)
        GL.BufferData(BufferTarget.ArrayBuffer, New IntPtr(Vertex_Buffer_Data.Length * Vector3.SizeInBytes), Vertex_Buffer_Data, BufferUsageHint.StaticDraw)
        'Create and compile GLSL program from shaders
        ProgramID = LoadShaders()
        'Get a handle for the MVP uniform
        MatrixID = GL.GetUniformLocation(ProgramID, "MVP")
        'Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
        Dim Projection As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.0E-14F, 4.0F / 3.0F, 0.1F, 100.0F)
        'Dim Projection As Matrix4 = Matrix4.CreateOrthographicOffCenter(-10.0F, 10.0F, -10.0F, 10.0F, 0.0F, 100.0F)
        'Camera Matrix
        Dim View As Matrix4 = Matrix4.LookAt(New Vector3(4, 3, 3), New Vector3(0, 0, 0), New Vector3(0, -1, 0))
        'Model Matrix
        Dim Model As Matrix4 = Matrix4.Identity
        MVP = Projection * View * Model
        GL.ClearColor(0.0F, 1.0F, 0.0F, 1.0F)
    End Sub
#End Region
#Region "OnResize"
    ''' <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">Not used.</param>
    Protected Overrides Sub OnResize(ByVal e As EventArgs)
    End Sub
#End Region
#Region "OnUpdateFrame"
    ''' <summary>
    ''' Called when it is time to setup the next frame. Add your game logic here.
    ''' </summary>
    ''' <param name="e">Contains timing information for framerate independent logic.</param>
    Protected Overrides Sub OnUpdateFrame(ByVal e As FrameEventArgs)
    End Sub
#End Region
#Region "OnRenderFrame"
    ''' <summary>
    ''' Called when it is time to render the next frame. Add your rendering code here.
    ''' </summary>
    ''' <param name="e">Contains timing information.</param>
    Protected Overrides Sub OnRenderFrame(ByVal e As FrameEventArgs)
        'Clear the screen
        'Send transformation to the currently bound shader
        'GL.UniformMatrix4(MatrixID, 1, False, MVP.M11)
        GL.BindBuffer(BufferTarget.ArrayBuffer, Vertex_Buffer)
        GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, False, 0, 0)
        GL.DrawArrays(BeginMode.Triangles, 0, 3)
    End Sub
#End Region
#Region "Main"
    ''' <summary>
    ''' The main entry point for the application.
    ''' </summary>
    <STAThread()> _
    Public Shared Sub Main()
        ' The 'using' idiom guarantees proper resource cleanup.
        ' We request 30 UpdateFrame events per second, and unlimited
        ' RenderFrame events (as fast as the computer can handle).
        Using radar As New OpenRadar()
        End Using
    End Sub
#End Region
End Class

Vertex Shader

#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;
void main(){
	// Output position of the vertex, in clip space : MVP * position
	gl_Position =  MVP * vec4(vertexPosition_modelspace,1);
	//gl_Position.xyz = vertexPosition_modelspace;
    //gl_Position.w = 1.0;

and LoadShaders()

Public Function LoadShaders() As Integer
        Dim VertexShaderID As Integer = GL.CreateShader(ShaderType.VertexShader)
        Dim FragmentShaderID As Integer = GL.CreateShader(ShaderType.FragmentShader)
        'Read From file
        Using sr As StreamReader = New StreamReader("Shaders/SimpleVertexShader.txt")
            GL.ShaderSource(VertexShaderID, sr.ReadToEnd)
        End Using
        Dim result As Integer = 0
        'Check Shader for errors
        GL.GetShader(VertexShaderID, ShaderParameter.CompileStatus, result)
        Debug.WriteLine("VertexShader: " & result)
        'Read from file
        Using sr As StreamReader = New StreamReader("Shaders/SimpleFragmentShader.txt")
            GL.ShaderSource(FragmentShaderID, sr.ReadToEnd)
        End Using
        'Check For errors
        GL.GetShader(FragmentShaderID, ShaderParameter.CompileStatus, result)
        Debug.WriteLine("FragmentShader: " & result)
        'Link program
        Dim ProgramID As Integer = GL.CreateProgram
        GL.AttachShader(ProgramID, VertexShaderID)
        GL.AttachShader(ProgramID, FragmentShaderID)
        'Check for errors in link
        GL.GetProgram(ProgramID, ProgramParameter.LinkStatus, result)
        Debug.WriteLine("LinkStatus: " & result)
        Return ProgramID
    End Function

Sorry for the code dump, but I just can't narrow this down at all.


Comment viewing options

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

Two things jump out:

        Dim Projection As Matrix4 = Matrix4.CreatePerspectiveFieldOfView(1.0E-14F, 4.0F / 3.0F, 0.1F, 100.0F)

The first parameter sets a field of view equal to 0.00000000000001 radians. Try something like MathHelper.PiOver2 instead (equivalent to 45 degrees).

        'Send transformation to the currently bound shader
        'GL.UniformMatrix4(MatrixID, 1, False, MVP.M11)

The second line is commented-out, so the transformation matrix will not be sent.

I would suggest uploading your project to a github repository and posting the link. That way, people would be able to compile and run it directly, instead of guessing what might be wrong.

Ubinator's picture

Its late and those things, unfortunately are things I attempted to try and find errors, the first one was originally 0.785398163 and the second was just trying to get the triangle back.

Where it says MVP.M11, is that the same as MVP[0][0] in C++?

I don't feel like fighting with github so I just put it on mediafire if thats alright.

the Fiddler's picture

Where it says MVP.M11, is that the same as MVP[0][0] in C++?

Yes. You can also use:

GL.UniformMatrix4(MatrixID, False, MVP)

to pass the matrix directly. Both compile to the same code, so use whichever one you prefer.

The issue lies here:

        MVP = Projection * View * Model

Change that to:

        MVP = Model * View * Projection

and it will work.

A few years ago, OpenTK decided to match the XNA math library instead of something more typical to OpenGL. On one hand, this makes porting between XNA<->OpenTK easier. On the other hand, you need to flip the order of matrix multiplications compared to glm.

It's ugly, but what's done is done and we have to live with it now...

Ubinator's picture

Thanks for that, I guess I was right when I said I thought it was something unique to OpenTK, such a simple thing as well!