private void glControl1_MouseClick(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); glControl1.MakeCurrent(); GL.UseProgram(pgm2ID); GL.EnableVertexAttribArray(0); camX.Text = cPosition.X.ToString(); camY.Text = cPosition.Y.ToString(); camZ.Text = cPosition.Z.ToString(); Matrix4 View = Matrix4.LookAt(cPosition, cTarget, Vector3.UnitZ); Matrix4 worldView = View * projection; if (modReady && mobyCheck.Checked) { GL.ClearColor(0, 0, 0, 0); foreach (RatchetMoby mob in DataStore.mobs) { objModel = DataStore.spawnableModels.Find(x => x.modelID == mob.modelID); int insx = mobList.IndexOf((uint)objModel.modelID); if (insx != -1) { int mobNum = DataStore.mobs.IndexOf(mob); byte[] cols = BitConverter.GetBytes(mobNum); Matrix4 modTrans = Matrix4.CreateTranslation(mob.x, mob.y, mob.z); Matrix4 modScale = Matrix4.CreateScale(objModel.size * mob.size); Matrix4 xRot = Matrix4.CreateRotationX(mob.rot1); Matrix4 yRot = Matrix4.CreateRotationY(mob.rot2); Matrix4 zRot = Matrix4.CreateRotationZ(mob.rot3); Matrix4 modRot = xRot * yRot * zRot; mvp = modScale * modRot * modTrans * worldView; //Has to be done in this order to work correctly GL.UniformMatrix4(MatrixID, false, ref mvp); GL.BindBuffer(BufferTarget.ArrayBuffer, mobVerts[insx]); GL.BindBuffer(BufferTarget.ElementArrayBuffer, mobInds[insx]); //Verts GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, sizeof(float) * 8, 0); GL.Uniform4(pgmID_rcID, new Vector4(cols[0] / 255f, cols[1] / 255f, cols[2] / 255f, 1)); GL.DrawElements(PrimitiveType.Triangles, objModel.indiceBuff.Count(), DrawElementsType.UnsignedShort, 0); } } Byte4 pixel = new Byte4(); GL.ReadPixels(glControl1.PointToClient(Cursor.Position).X, glControl1.Height - glControl1.PointToClient(Cursor.Position).Y, 1, 1, OpenTK.Graphics.OpenGL.PixelFormat.Rgba, PixelType.UnsignedByte, ref pixel); if (pixel.A == 0) { mainForm.objViewer.selectList(pixel.ToUInt32()); } GL.ClearColor(Color.SkyBlue); } glControl1.Invalidate(); } }
/// <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) { GL.Color3(Color.White); GL.Enable(EnableCap.ColorArray); #region Pass 1: Draw Object and pick Triangle GL.ClearColor(1f, 1f, 1f, 1f); // clears to uint.MaxValue GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Matrix4 modelview = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY); GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); GL.Translate(0f, 0f, -3f); GL.Rotate(angle, Vector3.UnitX); GL.Rotate(angle, Vector3.UnitY); angle += (float)e.Time * 3.0f; // You may re-enable the shader, but it works perfectly without and will run on intel HW too // GL.UseProgram(ProgramObject); GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length); // GL.UseProgram(0); // Read Pixel under mouse cursor Byte4 Pixel = new Byte4(); GL.ReadPixels(Mouse.X, this.Height - Mouse.Y, 1, 1, PixelFormat.Rgba, PixelType.UnsignedByte, ref Pixel); SelectedTriangle = Pixel.ToUInt32(); #endregion Pass 1: Draw Object and pick Triangle GL.Color3(Color.White); GL.Disable(EnableCap.ColorArray); #region Pass 2: Draw Shape if (SelectedTriangle == uint.MaxValue) { GL.ClearColor(.2f, .1f, .3f, 1f); // purple } else { GL.ClearColor(0f, .2f, .3f, 1f); // cyan } GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Color3(1f, 1f, 1f); GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length); GL.PolygonMode(MaterialFace.Front, PolygonMode.Line); GL.Color3(Color.Red); GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length); GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill); if (SelectedTriangle != uint.MaxValue) { GL.Disable(EnableCap.DepthTest); GL.Color3(Color.Green); GL.DrawArrays(VBO_PrimMode, (int)SelectedTriangle * 3, 3); GL.Enable(EnableCap.DepthTest); } #endregion Pass 2: Draw Shape this.SwapBuffers(); ErrorCode err = GL.GetError(); if (err != ErrorCode.NoError) { Trace.WriteLine("Error at Swapbuffers: " + err); } }
/// <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) { GL.Color3(Color.White); GL.Enable(EnableCap.ColorArray); #region Pass 1: Draw Object and pick Triangle GL.ClearColor(1f, 1f, 1f, 1f); // clears to uint.MaxValue GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); Matrix4 modelview = Matrix4.LookAt(Vector3.UnitZ, Vector3.Zero, Vector3.UnitY); GL.MatrixMode(MatrixMode.Modelview); GL.LoadMatrix(ref modelview); GL.Translate(0f, 0f, -3f); GL.Rotate(angle, Vector3.UnitX); GL.Rotate(angle, Vector3.UnitY); angle += (float)e.Time * 3.0f; // You may re-enable the shader, but it works perfectly without and will run on intel HW too // GL.UseProgram(ProgramObject); GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length); // GL.UseProgram(0); // Read Pixel under mouse cursor Byte4 Pixel = new Byte4(); GL.ReadPixels(Mouse.X, this.Height - Mouse.Y, 1, 1, PixelFormat.Rgba, PixelType.UnsignedByte, ref Pixel); SelectedTriangle = Pixel.ToUInt32(); #endregion Pass 1: Draw Object and pick Triangle GL.Color3(Color.White); GL.Disable(EnableCap.ColorArray); #region Pass 2: Draw Shape if (SelectedTriangle == uint.MaxValue) GL.ClearColor(.2f, .1f, .3f, 1f); // purple else GL.ClearColor(0f, .2f, .3f, 1f); // cyan GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Color3(1f, 1f, 1f); GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length); GL.PolygonMode(MaterialFace.Front, PolygonMode.Line); GL.Color3(Color.Red); GL.DrawArrays(VBO_PrimMode, 0, VBO_Array.Length); GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill); if (SelectedTriangle != uint.MaxValue) { GL.Disable(EnableCap.DepthTest); GL.Color3(Color.Green); GL.DrawArrays(VBO_PrimMode, (int)SelectedTriangle * 3, 3); GL.Enable(EnableCap.DepthTest); } #endregion Pass 2: Draw Shape this.SwapBuffers(); ErrorCode err = GL.GetError(); if (err != ErrorCode.NoError) Trace.WriteLine("Error at Swapbuffers: " + err); }