public void ClearDepthBuffer() { using (ScopedLock lLock = LockContext()) { Gl.glClear(Gl.GL_DEPTH_BUFFER_BIT); } }
public void PopTransform() { using (ScopedLock lLock = LockContext()) { Gl.glPopMatrix(); } }
protected void SetViewPort(int xiWidth, int xiHeight) { using (ScopedLock lLock = LockContext()) { Gl.glViewport(0, 0, xiWidth, xiHeight); } }
public void DisableLighting() { using (ScopedLock lLock = LockContext()) { Gl.glDisable(Gl.GL_LIGHTING); Gl.glDisable(Gl.GL_COLOR_MATERIAL); } }
public void PushTransform(Matrix xiTransform) { using (ScopedLock lLock = LockContext()) { Gl.glPushMatrix(); Gl.glMultMatrixd(xiTransform.ToArray()); } }
public void EnableLighting() { using (ScopedLock lLock = LockContext()) { Gl.glEnable(Gl.GL_LIGHTING); Gl.glEnable(Gl.GL_COLOR_MATERIAL); Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT_AND_DIFFUSE); } }
public void Clear() { using (ScopedLock lLock = LockContext()) { Gl.glClear(Gl.GL_COLOR_BUFFER_BIT | Gl.GL_DEPTH_BUFFER_BIT); } ResetCounters(); }
public int SetLight(int xiLightId, Light xiLight) { int lGLLightId = GetLightId(xiLightId); using (ScopedLock lLock = LockContext()) { Gl.glLightfv( lGLLightId, Gl.GL_POSITION, new float[] { (float)xiLight.Position.x, (float)xiLight.Position.y, (float)xiLight.Position.z, 1f }); Gl.glLightfv( lGLLightId, Gl.GL_DIFFUSE, new float[] { (float)xiLight.DiffuseColor.R / 255, (float)xiLight.DiffuseColor.G / 255, (float)xiLight.DiffuseColor.B / 255, (float)xiLight.DiffuseIntensity }); Gl.glLightfv( lGLLightId, Gl.GL_AMBIENT, new float[] { (float)xiLight.AmbientColor.R / 255, (float)xiLight.AmbientColor.G / 255, (float)xiLight.AmbientColor.B / 255, (float)xiLight.AmbientIntensity }); Gl.glLightfv( lGLLightId, Gl.GL_SPECULAR, new float[] { (float)xiLight.SpecularColor.R / 255, (float)xiLight.SpecularColor.G / 255, (float)xiLight.SpecularColor.B / 255, (float)xiLight.SpecularIntensity }); Gl.glEnable(lGLLightId); } return(xiLightId); }
protected virtual void Init() { using (ScopedLock lLock = LockContext()) { Gl.glEnable(Gl.GL_TEXTURE_2D); Gl.glShadeModel(Gl.GL_SMOOTH); Gl.glClearColor(0, 0, 0, 0.5f); Gl.glClearDepth(1); Gl.glEnable(Gl.GL_DEPTH_TEST); Gl.glDepthFunc(Gl.GL_LEQUAL); Gl.glHint(Gl.GL_PERSPECTIVE_CORRECTION_HINT, Gl.GL_NICEST); Gl.glColorMaterial(Gl.GL_FRONT_AND_BACK, Gl.GL_AMBIENT_AND_DIFFUSE); } }
public void ResetLights() { using (ScopedLock lLock = LockContext()) { Gl.glDisable(Gl.GL_LIGHT0); Gl.glDisable(Gl.GL_LIGHT1); Gl.glDisable(Gl.GL_LIGHT2); Gl.glDisable(Gl.GL_LIGHT3); Gl.glDisable(Gl.GL_LIGHT4); Gl.glDisable(Gl.GL_LIGHT5); Gl.glDisable(Gl.GL_LIGHT6); Gl.glDisable(Gl.GL_LIGHT7); } mNextLight = 0; }
public void SetCamera(Camera xiCamera) { using (ScopedLock lLock = LockContext()) { Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glLoadIdentity(); int[] lViewPort = new int[4]; Gl.glGetIntegerv(Gl.GL_VIEWPORT, lViewPort); if (mPickMode) { Glu.gluPickMatrix((double)mPickX, (double)(lViewPort[3] - mPickY), 1, 1, lViewPort); } switch (xiCamera.ProjectionMode) { case eProjectionMode.Perspective: Glu.gluPerspective( xiCamera.Fov, (double)(lViewPort[0] - lViewPort[3]) / (double)(lViewPort[3] - lViewPort[1]), xiCamera.NearClip, xiCamera.FarClip); break; case eProjectionMode.Orthographic: double lSize = xiCamera.Position.GetPositionVector() * xiCamera.ZAxis; double lRatio = (double)lViewPort[3] / (double)lViewPort[2]; Gl.glOrtho( -lSize / 2, lSize / 2, -lSize * lRatio / 2, lSize * lRatio / 2, xiCamera.NearClip, xiCamera.FarClip); break; default: throw new Exception("Unrecognised projection mode"); } Gl.glMatrixMode(Gl.GL_MODELVIEW); Gl.glLoadMatrixd(xiCamera.Transform.Inverse().ToArray()); } }
public void Detach() { if (mRenderingContext != IntPtr.Zero) { try { using (ScopedLock lLock = ScopedLock.Lock(mRenderingContext)) { if (Wgl.wglGetCurrentContext() == mRenderingContext) { if (!Wgl.wglMakeCurrent(IntPtr.Zero, IntPtr.Zero)) { throw new Exception("Could not release rendering context"); } } lock (mRenderingContexts) { mRenderingContexts.Remove(mRenderingContext); if (!Wgl.wglDeleteContext(mRenderingContext)) { throw new Exception("Could not delete rendering context"); } } } } finally { try { if (mSurface != null) { mSurface.Resize -= Surface_Resize; mSurface.ReleaseDeviceContext -= Surface_ReleaseDeviceContext; mSurface.Paint -= Surface_Paint; } } finally { mSurface = null; mRenderingContext = IntPtr.Zero; } } } }
public void RenderMesh(Mesh xiMesh, RenderOptions xiOptions) { if (mPickMode) { using (ScopedLock lLock = LockContext()) { Gl.glLoadName(mPickIndex); } mPickMeshes.Add(mPickIndex, xiMesh); ++mPickIndex; } DateTime lStart = DateTime.Now; RenderMeshInternal(xiMesh, xiOptions); mRenderTime += (DateTime.Now - lStart).Milliseconds; ++mMeshCount; mVertexCount += xiMesh.Vertices.Count; }
private static int LoadTexture(Bitmap xiTexture) { int lTextureId; lock (mRenderingContexts) { if (mRenderingContexts.Count == 0) { throw new Exception("No rendering contexts exist"); } Rectangle rectangle = new Rectangle(0, 0, xiTexture.Width, xiTexture.Height); BitmapData lData = xiTexture.LockBits( new Rectangle(0, 0, xiTexture.Width, xiTexture.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); using (ScopedLock lLock = ScopedLock.Lock(mRenderingContexts[0])) { Gl.glGenTextures(1, out lTextureId); Gl.glBindTexture(Gl.GL_TEXTURE_2D, lTextureId); Gl.glTexImage2D( Gl.GL_TEXTURE_2D, 0, Gl.GL_RGB8, xiTexture.Width, xiTexture.Height, 0, Gl.GL_BGR, Gl.GL_UNSIGNED_BYTE, lData.Scan0); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); Gl.glTexParameteri(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); } xiTexture.UnlockBits(lData); } return(lTextureId); }
protected ScopedLock LockContext() { ScopedLock lLock = ScopedLock.Lock(mRenderingContext); try { if (Wgl.wglGetCurrentContext() != mRenderingContext) { if (!Wgl.wglMakeCurrent(mSurface.DeviceContext, mRenderingContext)) { throw new Exception("Could not set the rendering context"); } } } catch { lLock.Dispose(); throw; } return(lLock); }
protected override void RenderMeshInternal(Mesh xiMesh, RenderOptions xiOptions) { using (ScopedLock lLock = LockContext()) { RenderMode lMode = DefaultRenderMode; if (FixedRenderMode != RenderMode.Undefined) { lMode = FixedRenderMode; } else if (xiMesh.RenderMode != RenderMode.Undefined) { lMode = xiMesh.RenderMode; } switch (lMode) { case RenderMode.Points: Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_POINT); Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0); break; case RenderMode.Wireframe: Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_LINE); Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0); break; case RenderMode.Filled: case RenderMode.TranslucentFilled: Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL); Gl.glBindTexture(Gl.GL_TEXTURE_2D, 0); break; default: case RenderMode.Textured: Gl.glPolygonMode(Gl.GL_FRONT_AND_BACK, Gl.GL_FILL); Gl.glBindTexture(Gl.GL_TEXTURE_2D, xiMesh.Texture); break; } switch (xiMesh.PolygonMode) { case PolygonMode.Lines: Gl.glBegin(Gl.GL_LINES); break; case PolygonMode.Triangles: Gl.glBegin(Gl.GL_TRIANGLES); break; case PolygonMode.Quads: Gl.glBegin(Gl.GL_QUADS); break; default: throw new Exception("Unrecognised polygon type"); } foreach (Vertex lVertex in xiMesh.Vertices) { if (lMode == RenderMode.TranslucentFilled) { Gl.glColor4d( (double)lVertex.Color.R / 255, (double)lVertex.Color.G / 255, (double)lVertex.Color.B / 255, (double)lVertex.Color.A / 255); } else { Gl.glColor3d( (double)lVertex.Color.R / 255, (double)lVertex.Color.G / 255, (double)lVertex.Color.B / 255); } Gl.glNormal3d(lVertex.Normal.x, lVertex.Normal.y, lVertex.Normal.z); Gl.glTexCoord2d(lVertex.TexCoordX, lVertex.TexCoordY); Gl.glVertex3d(lVertex.Position.x, lVertex.Position.y, lVertex.Position.z); } Gl.glEnd(); if ((xiOptions & RenderOptions.ShowNormals) == RenderOptions.ShowNormals) { Gl.glBegin(Gl.GL_LINES); Gl.glColor3d(0.0, 0.0, 1.0); foreach (Vertex lVertex in xiMesh.Vertices) { Gl.glVertex3d(lVertex.Position.x, lVertex.Position.y, lVertex.Position.z); Point lNormalEnd = lVertex.Position + 25 * lVertex.Normal; Gl.glVertex3d(lNormalEnd.x, lNormalEnd.y, lNormalEnd.z); } Gl.glEnd(); } } }
public Mesh Pick(int x, int y) { using (ScopedLock lLock = LockContext()) { int lPickIndex = int.MinValue; try { mPickMode = true; mPickX = x; mPickY = y; mPickMeshes.Clear(); mPickIndex = 0; int[] lPickBuffer = new int[512]; Gl.glSelectBuffer(lPickBuffer.Length, lPickBuffer); Gl.glRenderMode(Gl.GL_SELECT); Gl.glInitNames(); Gl.glPushName(0); Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glPushMatrix(); Gl.glMatrixMode(Gl.GL_MODELVIEW); if (NextFrame != null) { NextFrame(this, new NextFrameArgs(true)); } Gl.glMatrixMode(Gl.GL_PROJECTION); Gl.glPopMatrix(); Gl.glMatrixMode(Gl.GL_MODELVIEW); int lHits = Gl.glRenderMode(Gl.GL_RENDER); int lDepth = int.MaxValue; for (int ii = 0; ii < lHits; ++ii) { if (lPickBuffer[ii * 4 + 1] < lDepth) { lDepth = lPickBuffer[ii * 4 + 1]; lPickIndex = lPickBuffer[ii * 4 + 3]; } } } finally { mPickMode = false; } if (mPickMeshes.ContainsKey(lPickIndex)) { return(mPickMeshes[lPickIndex]); } else { return(null); } } }