public override void Render(World world, Faces faces, RenderSettings settings) { base.Render(world, faces, settings); Vector3D viewerOrigin = world.Camera.Translation; Vector3D viewerUp = world.Camera.UpAxis; Vector3D lightOrigin = world.Camera.Translation; // - world.Camera.RightAxis * world.Camera.ViewportWidth * 50; Vector3D lightUp = world.Camera.UpAxis; GL.glPolygonMode(GL.GL_FRONT_AND_BACK, (settings.Wireframe) ? GL.GL_LINE : GL.GL_FILL); GL.glDisable(GL.GL_TEXTURE_2D); Color clr = Color.White; foreach (Face face in faces) { int faceVerticies = face.Points.Count; Vector3DCollection worldCoords = face.Points; Vector3DCollection textureCoords = null; Vector3D normal = face.Normal; // draw base texture if (settings.ZBuffer) { GL.glEnable(GL.GL_DEPTH_TEST); } GL.glDisable(GL.GL_BLEND); GL.glEnable(GL.GL_COLOR_MATERIAL); if (settings.FaceColors) { clr = face.Color; } bool vertexNormals = face.IsVertexNormals; bool texture = settings.Textures && face.IsTexture; // draw base texture if (texture) { GL.glEnable(GL.GL_TEXTURE_2D); GL.glBindTexture(GL.Texture.Texture2D, (uint)face.Texture.OpenGLHandle); textureCoords = face.TextureCoords; } else { GL.glDisable(GL.GL_TEXTURE_2D); } GL.glBegin(GL.Primative.Polygon); for (int index = 0; index < faceVerticies; index++) { if (vertexNormals) { normal = face.VertexNormals[index]; } RenderUtils.glColor_GouraudIllumination(clr, worldCoords[index], normal, lightOrigin); if (texture) { RenderUtils.glTexture(textureCoords[index]); } RenderUtils.glVertex(worldCoords[index]); } GL.glEnd(); /*// blend in diffuse component * * GL.glEnable( GL.GL_BLEND ); * GL.glBlendFunc( GL.GL_ZERO, GL.GL_SRC_COLOR ); * GL.glColor( Color.White ); * GL.glEnable( GL.GL_TEXTURE_2D ); * GL.glBindTexture( GL.Texture.Texture2D, (uint) _diffuseMapHandle ); * GL.glBegin( GL.Primative.Polygon ); * * if( RenderUtils.IsFacing( face.MidPoint, face.Normal, lightOrigin ) ) { * for( int index = 0; index < faceVerticies; index ++ ) { * Vector3D worldCoord = faceWorldCoords[ index ]; * * if( vertexNormals ) { * normal = face.VertexNormals[ index ]; * } * * RenderUtils.glTexture_AsinMapping( worldCoord, normal, lightOrigin, lightUp ); * RenderUtils.glVertex( worldCoord ); * } * } * else { * for( int index = 0; index < faceVerticies; index ++ ) { * Vector3D worldCoord = faceWorldCoords[ index ]; * * if( vertexNormals ) { * normal = face.VertexNormals[ index ]; * } * * RenderUtils.glTexture( new Vector3D( 0, 0, 0 ) ); * RenderUtils.glVertex( worldCoord ); * } * } * GL.glEnd(); */ // determine if no light is hitting face if (RenderUtils.IsFacing(face.MidPoint, face.Normal, lightOrigin, viewerOrigin)) { // blend in specular component GL.glEnable(GL.GL_BLEND); GL.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE); GL.glEnable(GL.GL_TEXTURE_2D); GL.glBindTexture(GL.Texture.Texture2D, (uint)_specularMapHandle); GL.glColor(Color.White); GL.glBegin(GL.Primative.Polygon); for (int index = 0; index < faceVerticies; index++) { Vector3D worldCoord = worldCoords[index]; if (vertexNormals) { normal = face.VertexNormals[index]; } RenderUtils.glTexture_PhongMapping(worldCoord, normal, lightOrigin, viewerOrigin, viewerUp); RenderUtils.glVertex(worldCoord); } GL.glEnd(); } } GL.glDisable(GL.GL_BLEND); }
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(); }
public override void Render(World world, Faces faces, RenderSettings settings) { base.Render(world, faces, settings); Vector3D viewerOrigin = world.Camera.Translation; Vector3D viewerUp = world.Camera.UpAxis; Vector3D lightOrigin = world.Camera.Translation - world.Camera.RightAxis * world.Camera.ViewportWidth * 50; Vector3D lightUp = world.Camera.UpAxis; GL.glPolygonMode(GL.GL_FRONT_AND_BACK, (settings.Wireframe) ? GL.GL_LINE : GL.GL_FILL); GL.glDisable(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) { GL.glColor(Color.White); } if (settings.ZBuffer) { GL.glEnable(GL.GL_DEPTH_TEST); } else { GL.glDisable(GL.GL_DEPTH_TEST); } Vector3D lightSource = world.Camera.Translation; Color clr = Color.White; //float r = 1, g = 1, b = 1; foreach (Face face in faces) { int faceVerticies = face.Points.Count; Vector3DCollection faceWorldCoords = face.Points; Vector3DCollection faceTextureCoords = face.TextureCoords; Vector3D normal = face.Normal; // blend in reflection component GL.glBindTexture(GL.Texture.Texture2D, (uint)_textureReflection.OpenGLHandle); GL.glBegin(GL.Primative.Polygon); if (settings.FaceColors) { clr = face.Color; } bool bVertexNormals = face.IsVertexNormals; for (int index = 0; index < faceVerticies; index++) { Vector3D worldCoord = faceWorldCoords[index]; if (bVertexNormals) { normal = face.VertexNormals[index]; } RenderUtils.glColor_GouraudIllumination(clr, worldCoord, normal, lightSource); RenderUtils.glTexture_AsinMapping(worldCoord, normal, viewerOrigin, viewerUp); RenderUtils.glVertex(worldCoord); } GL.glEnd(); /*GL.glBegin( GL.Primative.Lines ); * * if( bVertexNormals ) { * for( int index = 0; index < faceVerticies; index ++ ) { * Vector3D worldCoord = faceWorldCoords[ index ]; * normal = face.VertexNormals[ index ]; * * RenderUtils.glColor( normal.ToColor() ); * RenderUtils.glVertex( worldCoord ); * RenderUtils.glVertex( worldCoord + normal * 30 ); * } * } * else { * RenderUtils.glColor( Color.Yellow ); * RenderUtils.glVertex( face.MidPoint ); * RenderUtils.glVertex( face.MidPoint + normal * 30 ); * } * * GL.glEnd();*/ GL.glErrorCheck(); } }
public override void Render(World world, Faces faces, RenderSettings settings) { base.Render(world, faces, settings); GL.glDisable(GL.GL_BLEND); GL.glEnable(GL.GL_COLOR_MATERIAL); if (settings.Textures) { GL.glEnable(GL.GL_TEXTURE_2D); } else { GL.glDisable(GL.GL_TEXTURE_2D); } if (settings.ZBuffer) { GL.glEnable(GL.GL_DEPTH_TEST); } else { GL.glDisable(GL.GL_DEPTH_TEST); } GL.glPolygonMode(GL.GL_FRONT_AND_BACK, (settings.Wireframe) ? GL.GL_LINE : GL.GL_FILL); GL.glColor(Color.White); foreach (Face face in faces) { int vertexCount = face.Points.Count; Vector3DCollection worldCoords = face.Points; Vector3DCollection textureCoords = null; bool texture = (settings.Textures && face.IsTexture); if (settings.Textures) { if (texture) { GL.glEnable(GL.GL_TEXTURE_2D); GL.glBindTexture(GL.Texture.Texture2D, face.Texture.OpenGLHandle); textureCoords = face.TextureCoords; } else { GL.glDisable(GL.GL_TEXTURE_2D); } } if (settings.FaceColors) { GL.glColor(face.Color); } GL.glBegin(GL.Primative.Polygon); for (int index = 0; index < vertexCount; index++) { if (texture) { RenderUtils.glTexture(textureCoords[index]); } RenderUtils.glVertex(worldCoords[index]); } GL.glEnd(); } }
public override void Render(World world, Faces faces, RenderSettings settings) { base.Render(world, faces, settings); //Faces visibleFaces = BSPTreeVizOperator.GetVisibleFaces( world.Camera.Translation, world.BSPTreeRoot ); //world.PolygonCount += faces.Count; GL.glPolygonMode(GL.GL_FRONT_AND_BACK, (settings.Wireframe) ? GL.GL_LINE : GL.GL_FILL); GL.glDisable(GL.GL_BLEND); GL.glEnable(GL.GL_COLOR_MATERIAL); GL.glErrorCheck(); if (settings.Textures) { GL.glEnable(GL.GL_TEXTURE_2D); } else { GL.glDisable(GL.GL_TEXTURE_2D); } GL.glErrorCheck(); if (settings.ZBuffer) { GL.glEnable(GL.GL_DEPTH_TEST); } else { GL.glDisable(GL.GL_DEPTH_TEST); } GL.glErrorCheck(); Vector3D ptCamera = world.Camera.ForwardAxis; Vector3D lightSource = world.Light; Color clr = Color.White; //float r = 1, g = 1, b = 1; foreach (Face face in faces) { bool texture = (settings.Textures && face.IsTexture); if (texture) { if (face.IsTexture) { GL.glBindTexture(GL.Texture.Texture2D, face.Texture.OpenGLHandle); } } GL.glBegin(GL.Primative.Polygon); if (settings.FaceColors) { clr = face.Color; } RenderUtils.glColor_GouraudIllumination(clr, face.MidPoint, face.Normal, lightSource); //GL.glColor3f( r * lightIntensity, g * lightIntensity, b * lightIntensity ); int count = face.Points.Count; for (int i = 0; i < count; i++) { if (texture) { RenderUtils.glTexture(face.TextureCoords[i]); } RenderUtils.glVertex(face.Points[i]); } GL.glEnd(); } GL.glErrorCheck(); }
public override void Render(World world, Faces faces, RenderSettings settings) { base.Render(world, faces, settings); GL.glDisable(GL.GL_BLEND); GL.glEnable(GL.GL_COLOR_MATERIAL); if (settings.Textures) { GL.glEnable(GL.GL_TEXTURE_2D); } else { GL.glDisable(GL.GL_TEXTURE_2D); } if (settings.ZBuffer) { GL.glEnable(GL.GL_DEPTH_TEST); } else { GL.glDisable(GL.GL_DEPTH_TEST); } GL.glPolygonMode(GL.GL_FRONT_AND_BACK, (settings.Wireframe) ? GL.GL_LINE : GL.GL_FILL); Color clr = Color.White; Vector3D light = world.Light; foreach (Face face in faces) { int vertexCount = face.Points.Count; Vector3DCollection worldCoords = face.Points; Vector3DCollection textureCoords = null; Vector3D normal = face.Normal; if (settings.FaceColors) { clr = face.Color; } bool texture = (settings.Textures && face.IsTexture); if (settings.Textures) { if (texture) { GL.glEnable(GL.GL_TEXTURE_2D); GL.glBindTexture(GL.Texture.Texture2D, face.Texture.OpenGLHandle); textureCoords = face.TextureCoords; } else { GL.glDisable(GL.GL_TEXTURE_2D); } } GL.glBegin(GL.Primative.Polygon); bool vertexNormals = face.IsVertexNormals; for (int index = 0; index < vertexCount; index++) { Vector3D worldCoord = worldCoords[index]; if (vertexNormals) { normal = face.VertexNormals[index]; } RenderUtils.glColor_PhongIllumination(clr, worldCoord, normal, light, Color.White, light, 30); if (texture) { RenderUtils.glTexture(textureCoords[index]); } RenderUtils.glVertex(worldCoord); } GL.glEnd(); } }