fkj's picture

A white square on a black background example

I'm a beginner at OpenTK, and am trying to make a simple example of a white square on a black background. The example is inspired by the "OpenTK Quick Start Sample", and the first example from the "OpenGL Programming Guide 6th.ed."

In a subclass to GameWindow I use the following (only the three important methods included), but it only displays a black background, nothing more:

protected override void OnLoad( EventArgs e ) {
  base.OnLoad( e );
  GL.ClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
  GL.Enable( EnableCap.DepthTest );
protected override void OnResize( EventArgs e ) {
  base.OnResize( e );
  GL.Viewport( ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width, ClientRectangle.Height );
  Matrix4 projection = Matrix4.CreatePerspectiveFieldOfView( (float) Math.PI / 4, (float) Width / Height, 1, 64 );
  GL.MatrixMode( MatrixMode.Projection );
  GL.LoadMatrix( ref projection );
protected override void OnRenderFrame( FrameEventArgs e ) {
  base.OnRenderFrame( e );
  GL.Clear( ClearBufferMask.ColorBufferBit );
  Vector3 eye = new Vector3( 0, 0, 10 );
  Vector3 target = new Vector3( 0, 0, 0 );
  Vector3 up = new Vector3( 0, 1, 0 );
  Matrix4 modelview = Matrix4.LookAt( eye, target, up );
  GL.MatrixMode( MatrixMode.Modelview );
  GL.LoadMatrix( ref modelview );
  GL.Begin( BeginMode.Polygon );
    GL.Color3( 1.0, 1.0, 1.0 );
    GL.Vertex3( -0.5, -0.5, 0.0 );
    GL.Vertex3(  0.5, -0.5, 0.0 );
    GL.Vertex3(  0.5,  0.5, 0.0 );
    GL.Vertex3( -0.5,  0.5, 0.0 );


Comment viewing options

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

Well, at a quick glance I see that you've set your clear color alpha to 0.0, which means you're trying to make everything completely transparent! Try GL.ClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); instead.

fkj's picture

I tried to set alpha on the ClearColor to 1.0, but it made no difference. Also I believe alpha on the ClearColor, only applies to the ClearColor, not any other color in the program. Both examples I refer to uses a ClearColor with alpha set to 0.0.

migueltk's picture

adjust this line of code GL.Clear( ClearBufferMask.ColorBufferBit ); for this code GL.Clear( ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit );

the Fiddler's picture

Also make sure your object is inside the viewport (maybe it falls behind the camera?) Check the example browser that comes with OpenTK.

fkj's picture

Thanks a lot for your replies!

To set 'ClearBufferMask.DepthBufferBit' on the parameter to Clear() solved the problem.

The square was safely inside the view.