public void RenderTriMesh(World world, Color clr, Vector3D[] vertices, Vector3D[] normals, int[] triangles, Plane3D[] trianglePlanes, RenderSettings settings) { base.Render(world, null, settings); int vertexCount = vertices.Length; int triangleCount = triangles.GetLength(0) / 3; Debug.Assert(vertexCount == normals.Length); if (_cacheSize < vertexCount) { _cacheSize = Math.Max(_cacheSize, vertexCount); _lightingCache = new float[_cacheSize]; _asinMappingCache = new Vector3D[_cacheSize]; _phongMappingCache = new Vector3D[_cacheSize]; for (int i = 0; i < _cacheSize; i++) { _asinMappingCache[i] = Vector3D.FromXYZ(0, 0, 0); _phongMappingCache[i] = Vector3D.FromXYZ(0, 0, 0); } } Vector3D viewerOrigin = world.Camera.Translation; Vector3D viewerUp = world.Camera.UpAxis; Vector3D lightOrigin = world.Light - world.Camera.RightAxis * world.Camera.ViewportWidth * 50; Vector3D lightUp = world.Camera.UpAxis; RenderUtils.PreCalculate_GouraudIllumination(vertices, normals, lightOrigin, _lightingCache); //RenderUtils.PreCalculate_AsinMapping( vertices, normals, viewerOrigin, viewerUp, _asinMappingCache ); RenderUtils.PreCalculate_PhongMapping(vertices, normals, lightOrigin, viewerOrigin, viewerUp, _phongMappingCache); int index; float intensity; //Vector3D vertex; //Vector3D texture; // --------------------------------------------------- // // draw base texture // GL.glDisable(GL.Option.Blend); GL.glDisable(GL.Option.Texture2D); //GL.glBindTexture( GL.Texture.Texture2D, (uint) _diffuseMapHandle ); GL.glEnable(GL.Option.ColorMaterial); GL.glErrorCheck(); GL.glBegin(GL.Primative.Triangles); //GL.glColor( clr ); for (int t = 0; t < triangleCount; t++) { if (trianglePlanes[t].GetDistanceToPlane(viewerOrigin) > 0) { for (int v = 0; v < 3; v++) { index = triangles[t * 3 + v]; intensity = _lightingCache[index]; GL.glColor3f(clr.R * intensity, clr.G * intensity, clr.B * intensity); GL.glVertex3(vertices[index]); } } } GL.glEnd(); GL.glErrorCheck(); // --------------------------------------------------- // // add specular highlight // GL.glEnable(GL.Option.Blend); GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE); GL.glDisable(GL.Option.ColorMaterial); GL.glEnable(GL.Option.Texture2D); GL.glBindTexture(GL.Texture.Texture2D, (uint)_specularMapHandle); GL.glErrorCheck(); GL.glBegin(GL.Primative.Triangles); GL.glColor(Color.White); for (int t = 0; t < triangleCount; t++) { if (trianglePlanes[t].GetDistanceToPlane(viewerOrigin) > 0) { for (int v = 0; v < 3; v++) { index = triangles[t * 3 + v]; GL.glTexCoord2(_phongMappingCache[index]); GL.glVertex3(vertices[index]); } } } GL.glEnd(); GL.glErrorCheck(); }
public void RenderQuadMesh(World world, Color clr, Vector3D[,] vertices, Vector3D[,] normals, RenderSettings settings) { //Debug2.Push(); base.Render(world, null, settings); int xSize = vertices.GetLength(0); int ySize = vertices.GetLength(1); //Debug.WriteLine( "xSize = " + xSize ); //Debug.WriteLine( "ySize = " + ySize ); Debug.Assert(xSize == normals.GetLength(0)); Debug.Assert(ySize == normals.GetLength(1)); if (_xCacheSize < xSize || _yCacheSize < ySize) { _xCacheSize = Math.Max(_xCacheSize, xSize); _yCacheSize = Math.Max(_yCacheSize, ySize); _lightingCache = new float[_xCacheSize, _yCacheSize]; _textureCache = new Vector3D[_xCacheSize, _yCacheSize]; _phongMappingCache = new Vector3D[_xCacheSize, _yCacheSize]; for (int y = 0; y < _yCacheSize; y++) { for (int x = 0; x < _xCacheSize; x++) { _textureCache[x, y] = Vector3D.FromXYZ(0, 0, 0); _phongMappingCache[x, y] = Vector3D.FromXYZ(0, 0, 0); } } } Vector3D viewerOrigin = world.Camera.Translation; Vector3D viewerUp = world.Camera.UpAxis; Vector3D lightOrigin = world.Light - world.Camera.RightAxis * world.Camera.ViewportWidth * 50; Vector3D lightUp = world.Camera.UpAxis; //RenderUtils.PreCalculate_GouraudIllumination( vertices, normals, lightOrigin, _lightingCache ); //RenderUtils.PreCalculate_AsinMapping( vertices, normals, viewerOrigin, viewerUp, _asinMappingCache ); GL.glPolygonMode(GL.GL_FRONT_AND_BACK, (settings.Wireframe) ? GL.GL_LINE : GL.GL_FILL); GL.glEnable(GL.GL_BLEND); GL.glBlendFunc(GL.BlendSrc.SrcAlpha, GL.BlendDest.OneMinusSrcAlpha); GL.glEnable(GL.GL_COLOR_MATERIAL); GL.glEnable(GL.GL_TEXTURE_2D); if (!settings.FaceColors) { clr = Color.White; } //clr = Color.FromArgb( 128, clr ); if (settings.ZBuffer) { GL.glEnable(GL.GL_DEPTH_TEST); } else { GL.glDisable(GL.GL_DEPTH_TEST); } Vector3D lightSource = world.Camera.Translation; RenderUtils.PreCalculate_GouraudIllumination(vertices, normals, lightSource, _lightingCache); RenderUtils.PreCalculate_AsinMapping(vertices, normals, viewerOrigin, viewerUp, _textureCache); // RenderUtils.PreCalculate_PhongMapping( vertices, normals, lightOrigin, viewerOrigin, viewerUp, _phongMappingCache ); GL.glErrorCheck(); Vector3D vertex; Vector3D texture; float intensity; GL.glBindTexture(GL.Texture.Texture2D, (uint)_textureReflection.OpenGLHandle); for (int y = 0; y < (ySize - 1); y++) { GL.glBegin(GL.Primative.QuadStrip); for (int x = 0; x < xSize; x++) { intensity = _lightingCache[x, y + 1]; GL.glColor4f(clr.R * intensity, clr.G * intensity, clr.B * intensity, 0.75f); texture = _textureCache[x, y + 1]; GL.glTexCoord2f(texture.X, texture.Y); vertex = vertices[x, y + 1]; GL.glVertex3f(vertex.X, vertex.Y, vertex.Z); intensity = _lightingCache[x, y]; GL.glColor4f(clr.R * intensity, clr.G * intensity, clr.B * intensity, 0.75f); texture = _textureCache[x, y]; GL.glTexCoord2f(texture.X, texture.Y); vertex = vertices[x, y]; GL.glVertex3f(vertex.X, vertex.Y, vertex.Z); } GL.glEnd(); } GL.glErrorCheck(); /*GL.glEnable( GL.Option.Blend ); * GL.glBlendFunc( GL.GL_SRC_ALPHA, GL.GL_ONE ); * * GL.glDisable( GL.Option.ColorMaterial ); * GL.glEnable( GL.Option.Texture2D ); * GL.glBindTexture( GL.Texture.Texture2D, (uint) _specularMapHandle ); * * GL.glErrorCheck(); * * GL.glColor( Color.White ); * * for( int y = 0; y < ( ySize - 1 ); y ++ ) { * GL.glBegin( GL.Primative.QuadStrip ); * * for( int x = 0; x < xSize; x ++ ) { * * GL.glTexCoord2( _phongMappingCache[ x, y + 1 ] ); * GL.glVertex3( vertices[ x, y + 1 ] ); * * GL.glTexCoord2( _phongMappingCache[ x, y ] ); * GL.glVertex3( vertices[ x, y ] ); * } * * GL.glEnd(); * } * * GL.glErrorCheck(); */ //Debug2.Pop(); }