Sign in to follow this  
richy19

OpenGL Simple OpenTK program not working

Recommended Posts

richy19    115
I was trying to port some of my old OpenGL stuff to OpenTK however I couldnt get it to work.
I am now trying a simple program to display a triangle:
This is my setup:
MVP = Matrix4.CreateOrthographicOffCenter(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 2.0f);
where the screen is:

[source lang="plain"]
0 - 0 1 - 0
---------------------------------------------
| |
| |
| |
| |
| |
---------------------------------------------
0 - 1 1 - 1[/source]


And these verticies:

[source lang="csharp"]
float[] vertices = { 0.5f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f };[/source]

The triangle should be right in the midle of the screen, well for some reason it isnt.

Heres all the code encase its something other than positioning
[source lang="csharp"]// Released to the public domain. Use, modify and relicense at will.
using System;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Audio;
using OpenTK.Audio.OpenAL;
using OpenTK.Input;
namespace StarterKit
{
class Game : GameWindow
{

float[] vertices = { 0.5f, 0.0f, 1.0f,
0.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f };
int vbo;
string fragmentSource = "#version 120\n" +
"void main(){gl_FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);}";

string vertexSource = "#version 120 \n" +
"uniform mat4 MVP; attribute vec4 Position;" +
"void main(){ gl_Position = MVP * Position; }";

int shaderProgram, vertexShader, fragmentShader;
int MVPID, PositionID;

Matrix4 MVP;

/// <summary>Creates a 800x600 window with the specified title.</summary>
public Game ()
: base(800, 600, GraphicsMode.Default, "OpenTK Quick Start Sample")
{
VSync = VSyncMode.On;
}

protected override void OnDisposed(EventArgs e)
{

GL.DetachShader( shaderProgram, vertexShader );
GL.DetachShader( shaderProgram, fragmentShader );

GL.DeleteShader(vertexShader);
GL.DeleteShader(fragmentShader);

GL.DeleteProgram(shaderProgram);

GL.DeleteBuffers(1, ref vbo);

base.OnDisposed(e);
}

/// <summary>Load resources here.</summary>
/// <param name="e">Not used.</param>
protected override void OnLoad (EventArgs e)
{
base.OnLoad (e);
GL.ClearColor (0.1f, 0.2f, 0.5f, 0.0f);
GL.Enable (EnableCap.DepthTest);





GL.GenBuffers (1, out vbo);
GL.BindBuffer (BufferTarget.ArrayBuffer, vbo);
GL.BufferData (BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * sizeof(float)), vertices, BufferUsageHint.StaticDraw);

vertexShader = GL.CreateShader(ShaderType.VertexShader);
GL.ShaderSource( vertexShader, vertexSource );
GL.CompileShader( vertexShader );


fragmentShader = GL.CreateShader( ShaderType.FragmentShader );
GL.ShaderSource( fragmentShader, fragmentSource );
GL.CompileShader( fragmentShader );


shaderProgram = GL.CreateProgram();
GL.AttachShader( shaderProgram, vertexShader );
GL.AttachShader( shaderProgram, fragmentShader );

GL.LinkProgram(shaderProgram);


MVPID = GL.GetUniformLocation(shaderProgram, "MVP");
PositionID = GL.GetAttribLocation(shaderProgram, "Position");

MVP = Matrix4.CreateOrthographicOffCenter(0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 2.0f);
}
/// <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 override void OnResize (EventArgs e)
{
base.OnResize (e);
GL.Viewport (ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height);
}
/// <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>
protected override void OnUpdateFrame (FrameEventArgs e)
{
base.OnUpdateFrame (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>
protected override void OnRenderFrame (FrameEventArgs e)
{
base.OnRenderFrame (e);
GL.Clear (ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.UseProgram (shaderProgram);

GL.UniformMatrix4(MVPID, false, ref MVP);

GL.EnableVertexAttribArray(PositionID);
GL.BindBuffer(BufferTarget.ArrayBuffer, vbo);
GL.VertexAttribPointer(PositionID, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero);

GL.DrawArrays(BeginMode.Triangles, 0, 3);

GL.DisableVertexAttribArray(PositionID);
GL.UseProgram (0);
SwapBuffers ();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void 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 (Game game = new Game()) {
game.Run (30.0);
}
}
}
}[/source]

Share this post


Link to post
Share on other sites
Krum110487    110
I could be wrong, but you might have your near a bit off on the orthographicOffCenter, try:

CreateOrthographicOffCenter(0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 2.0f);

Share this post


Link to post
Share on other sites
richy19    115
[quote name='Krum110487' timestamp='1345250216' post='4970714']
I could be wrong, but you might have your near a bit off on the orthographicOffCenter, try:

CreateOrthographicOffCenter(0.0f, 1.0f, 1.0f, 0.0f, -1.0f, 2.0f);
[/quote]

It ended up being the z distance thanks.
I dont really understand tho, say I put 0 and -2 as the near and far distance. Am I correct to assume that anything with a Z value between these will be displayed?

Let me rephrase the question, is the zFar value a point in 3d space (making my logic valid) or is it a length in the negative direction from the zNear value(which is what it seems to be)?
by the second explanation I mean for example using -1 and 3 would make anything visible between -1 and -4 Edited by richy19

Share this post


Link to post
Share on other sites
Krum110487    110
the near and far (to my understanding) is how many units towards(near) and away(far) from the camera.

so -1 would be (origin - 1) (towards) and 2 would be (origin + 2) (away), so anything will render in the range of -1 to 0 or 0 to 2

so anything lower than -1 or higher than 2 wouldn't show, and it may or may not include 2 and -1 as not rendering, I am unsure, I just noticed you were cutting it close, it is always a good idea to play it safe and have some padding.

although I am new, so if you want a more detailed explanation, you might wait for someone else. Edited by Krum110487

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
       
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now