Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing OpenTK (z-indexing)
    primarykey
    data
    text
    <p>I'm doing a cross between following some NeHe tutorials (translating it to OpenTK) and an OpenTK sample I found online to draw a triangle (for a simple initial setup):</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.Diagnostics; using System.IO; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL; using System.Windows.Forms; namespace GravSimBasic { class GLMain : GameWindow { int vbo; Vector3[,] vertices; float time = 0.01f; void CreateVertexBuffer() { vertices = new Vector3[2,3]; vertices[0,0] = new Vector3(-1f, -1f, (float)Math.Sin(time)); vertices[0, 1] = new Vector3(0.5f, -1f, (float)Math.Sin(time)); vertices[0, 2] = new Vector3(-0.25f, 1f, -(float)Math.Sin(time)); vertices[1, 0] = new Vector3(-0.5f, -1f, (float)Math.Cos(time)); vertices[1, 1] = new Vector3(1f, -1f, (float)Math.Cos(time)); vertices[1, 2] = new Vector3(0.25f, 1f, -(float)Math.Cos(time)); //MessageBox.Show("Length: " + vertices.Length.ToString()); GL.GenBuffers(1, out vbo); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.BufferData&lt;Vector3&gt;(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); } protected override void OnLoad(EventArgs e) { //set the window area GL.Viewport(0, 0, 400, 400); //background color GL.ClearColor(Color.Black); //set the view area GL.MatrixMode(MatrixMode.Projection); GL.LoadIdentity(); GL.Ortho(-2, 2, -2, 2, 2, -2); //now back to 'scene editing' mode GL.MatrixMode(MatrixMode.Modelview); GL.LoadIdentity(); //make things look nice GL.ShadeModel(ShadingModel.Smooth); //set up our z-rendering logic GL.ClearDepth(2.0000f); GL.Enable(EnableCap.DepthTest); GL.DepthFunc(DepthFunction.Lequal); //other improvements to quality GL.Hint(HintTarget.PerspectiveCorrectionHint, HintMode.Nicest); GL.Hint(HintTarget.LineSmoothHint, HintMode.Nicest); //initialize our scene data CreateVertexBuffer(); } protected override void OnRenderFrame(FrameEventArgs e) { time += 0.01f; vertices[0, 0].Z = (float)Math.Sin(time); vertices[0, 1].Z = (float)Math.Sin(time); vertices[0, 2].Z = -(float)Math.Sin(time); vertices[1, 0].Z = (float)Math.Cos(time); vertices[1, 1].Z = (float)Math.Cos(time); vertices[1, 2].Z = -(float)Math.Cos(time); GL.BufferData&lt;Vector3&gt;(BufferTarget.ArrayBuffer, new IntPtr(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); System.Threading.Thread.Sleep(10); GL.Clear(ClearBufferMask.ColorBufferBit); GL.EnableVertexAttribArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, vbo); GL.Color4(0.75f,0.0f,0.0f,0.25f); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, 0); GL.DrawArrays(BeginMode.Triangles, 0, 3); GL.Color4(0.0f, 0.75f, 0.0f, 0.55f); GL.VertexAttribPointer(3, 3, VertexAttribPointerType.Float, false, 0, 0); GL.DrawArrays(BeginMode.Triangles, 3, 3); GL.DisableVertexAttribArray(0); SwapBuffers(); } } } </code></pre> <p>I suspect the issue is in lines 58-60, but I've changed the value in line 58 between -2.0, 0.00001, and 2.0, none changed the results. It could be the perspective setup a few lines earlier, though. I've tried almost all the functions available as a parameter for line 60 - Lequal seems to be the best option by what I would <em>expect</em>, and it does produce the closest result to what I want, but it isn't quite correct.</p> <p>Setup: There is a green and red triangle. They partially overlap on the x-y axis. The top z-axis of one is mapped by a -sin(time) function, and the bottom, a sin(time) function. The other uses cos() instead of sin, but is otherwise the same. The 'time' value changes each rendering.</p> <p>What I want/expect: Two overlapping triangles - one red, one green. As that rotate back and forth, the non-overlapping portion of each should always be visible, and the overlapping portions should only show the foremost triangle.</p> <p>What I get: (a) nothing (b) a display of both triangles, one on top of other. (c) A changing image of bits of one, both or none of the triangles - even if one or both are showing, the are bits of either triangle, that should be visible, that are missing (background).</p> <p>If I remove time, it shows a correct snapshot - the red triangle in front in the bottom half, and the green in front on the top.</p> <p>Can anyone help diagnose this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload