/// <summary> /// Prepare VBO content and upload it to the GPU. /// </summary> private void PrepareDataBuffers() { if (useVBO && scene != null && scene.Triangles > 0) { GL.EnableClientState(ArrayCap.VertexArray); if (scene.HasNormals()) { GL.EnableClientState(ArrayCap.NormalArray); } if (scene.HasColors()) { GL.EnableClientState(ArrayCap.ColorArray); } // Vertex array: [color] [normal] coord GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]); int vertexBufferSize = scene.VertexBufferSize(true, false, true, true); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)vertexBufferSize, IntPtr.Zero, BufferUsageHint.StaticDraw); IntPtr videoMemoryPtr = GL.MapBuffer(BufferTarget.ArrayBuffer, BufferAccess.WriteOnly); unsafe { stride = scene.FillVertexBuffer((float *)videoMemoryPtr.ToPointer(), true, false, true, true); } GL.UnmapBuffer(BufferTarget.ArrayBuffer); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); // Index buffer GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOid[1]); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(scene.Triangles * 3 * sizeof(uint)), IntPtr.Zero, BufferUsageHint.StaticDraw); videoMemoryPtr = GL.MapBuffer(BufferTarget.ElementArrayBuffer, BufferAccess.WriteOnly); unsafe { scene.FillIndexBuffer((uint *)videoMemoryPtr.ToPointer()); } GL.UnmapBuffer(BufferTarget.ElementArrayBuffer); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } else { GL.DisableClientState(ArrayCap.VertexArray); GL.DisableClientState(ArrayCap.NormalArray); GL.DisableClientState(ArrayCap.ColorArray); if (useVBO) { GL.BindBuffer(BufferTarget.ArrayBuffer, VBOid[0]); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)0, IntPtr.Zero, BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.BindBuffer(BufferTarget.ElementArrayBuffer, VBOid[1]); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)0, IntPtr.Zero, BufferUsageHint.StaticDraw); GL.BindBuffer(BufferTarget.ElementArrayBuffer, 0); } } }
private void RenderScene(SceneBrep scene) { if (scene != null && scene.Triangles > 0) { if (scene.HasNormals() && scene.Normals > 0) { GL.Begin(PrimitiveType.Triangles); for (int i = 0; i < scene.Triangles; ++i) { int v1, v2, v3; scene.GetTriangleVertices(i, out v1, out v2, out v3); GL.Normal3(scene.GetNormal(v1)); GL.Vertex3(scene.GetVertex(v1)); GL.Normal3(scene.GetNormal(v2)); GL.Vertex3(scene.GetVertex(v2)); GL.Normal3(scene.GetNormal(v3)); GL.Vertex3(scene.GetVertex(v3)); } GL.End(); } else { GL.End(); GL.Begin(PrimitiveType.Triangles); for (int i = 0; i < scene.Triangles; ++i) { Vector3 v1, v2, v3; scene.GetTriangleVertices(i, out v1, out v2, out v3); GL.Vertex3(v1); GL.Vertex3(v2); GL.Vertex3(v3); } GL.End(); } } else // color cube (JB) { GL.Begin(PrimitiveType.Quads); GL.Color3(0.0f, 1.0f, 0.0f); // Set The Color To Green GL.Vertex3(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top) GL.Vertex3(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top) GL.Vertex3(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top) GL.Vertex3(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top) GL.Color3(1.0f, 0.5f, 0.0f); // Set The Color To Orange GL.Vertex3(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom) GL.Vertex3(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom) GL.Vertex3(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom) GL.Vertex3(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom) GL.Color3(1.0f, 0.0f, 0.0f); // Set The Color To Red GL.Vertex3(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front) GL.Vertex3(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front) GL.Vertex3(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front) GL.Vertex3(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front) GL.Color3(1.0f, 1.0f, 0.0f); // Set The Color To Yellow GL.Vertex3(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back) GL.Vertex3(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back) GL.Vertex3(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back) GL.Vertex3(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back) GL.Color3(0.0f, 0.0f, 1.0f); // Set The Color To Blue GL.Vertex3(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left) GL.Vertex3(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left) GL.Vertex3(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left) GL.Vertex3(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left) GL.Color3(1.0f, 0.0f, 1.0f); // Set The Color To Violet GL.Vertex3(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right) GL.Vertex3(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right) GL.Vertex3(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right) GL.Vertex3(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right) GL.End(); } }