/// <summary> /// Open a File to be loaded /// </summary> /// <param name="fileName">The name of the file to open.</param> private void loadFile(string fileName) { System.IO.StreamReader file = new System.IO.StreamReader(fileName); string line; if (l_vertex == null) { l_vertex = new List <SharpGL.SceneGraph.Vertex>(); } if (l_color == null) { l_color = new List <SharpGL.SceneGraph.Vertex>(); } // read each line while ((line = file.ReadLine()) != null) { string[] words = line.Split(','); SharpGL.SceneGraph.Vertex vertex = new SharpGL.SceneGraph.Vertex(); SharpGL.SceneGraph.Vertex color = new SharpGL.SceneGraph.Vertex(); vertex.Set(float.Parse(words[0], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[2], System.Globalization.CultureInfo.InvariantCulture)); l_vertex.Add(vertex); if (vertex.X < minVertex.X) { minVertex.X = vertex.X; } if (vertex.Y < minVertex.Y) { minVertex.Y = vertex.Y; } if (vertex.Z < minVertex.Z) { minVertex.Z = vertex.Z; } if (vertex.X > maxVertex.X) { maxVertex.X = vertex.X; } if (vertex.Y > maxVertex.Y) { maxVertex.Y = vertex.Y; } if (vertex.Z > maxVertex.Z) { maxVertex.Z = vertex.Z; } //ignoring the words[3] color.Set(float.Parse(words[4], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[5], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[6], System.Globalization.CultureInfo.InvariantCulture)); color /= 255.0f; l_color.Add(color); } v_center = (maxVertex + minVertex) / 2.0f; SharpGL.SceneGraph.Vertex distance = (maxVertex - minVertex); f_scale = Math.Max(Math.Max(distance.X, distance.Y), distance.Z); file.Close(); }
/// <summary> /// When the mouse wheel is touched/invoked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openGLControl_MouseWheel(object sender, MouseEventArgs e) { if (e.Delta > 0) //mouse wheel is from back to front { cameraPos += cameraFront * SPEED_MOVE; } else { cameraPos -= cameraFront * SPEED_MOVE; } }
/// <summary> /// Just a debugging function to demostrate how to add new points over the existing list /// </summary> private void testFunctiontoAddCopy() { //to debug/test Random r = new Random(); List <SharpGL.SceneGraph.Vertex> newVertex = new List <SharpGL.SceneGraph.Vertex>(l_vertex); List <SharpGL.SceneGraph.Vertex> newColor = new List <SharpGL.SceneGraph.Vertex>(l_color); for (int k = 0; k < newVertex.Count; k++) { newVertex[k] = new SharpGL.SceneGraph.Vertex(newVertex[k].X + (float)(r.NextDouble() * 50.0), newVertex[k].Y + (float)(r.NextDouble() * 50.0), newVertex[k].Z); newColor[k] = new SharpGL.SceneGraph.Vertex(1, 1, 1); } // lists must exist addPointsAndColor(newVertex, newColor); newVertex.Clear(); newColor.Clear(); }
/// <summary> /// Allow change the color of the List lcolor according the height of the lvertex /// </summary> /// <param name="lvertex"> List of vertex to read the height according the min Vertex</param> /// <param name="lcolor"> List of color to change according its height</param> private void changeColorScale(ref List <SharpGL.SceneGraph.Vertex> lvertex, ref List <SharpGL.SceneGraph.Vertex> lcolor) { //top color SharpGL.SceneGraph.GLColor red = new SharpGL.SceneGraph.GLColor(0.9f, 0.05f, 0.05f, 1); //bottom color SharpGL.SceneGraph.GLColor green = new SharpGL.SceneGraph.GLColor(0.05f, 0.95f, 0.05f, 1); float diff = maxVertex.Z - minVertex.Z; SharpGL.SceneGraph.Vertex colorTemp = new SharpGL.SceneGraph.Vertex(); for (int k = 0; k < lcolor.Count; k++) { float t = (l_vertex[k].Z - minVertex.Z) / diff; //interpolated value colorTemp.Set((red.R * (1.0f - t)) + (green.R * t), (red.G * (1.0f - t)) + (green.G * t), (red.B * (1.0f - t)) + (green.B * t)); lcolor[k] = colorTemp; } }
/// <summary> /// Clean initial data /// </summary> private void reset() { OpenGL gl = openGLControl.OpenGL; //delete ids if (l_vboId != null) { foreach (var element in l_vboId) { gl.DeleteBuffers(2, element); } } if (l_vboId != null) { l_vboId.Clear(); } //delete sizes of list of points if (l_sizes != null) { l_sizes.Clear(); } //clear list of vertex and color if (l_color != null) { l_color.Clear(); } if (l_vertex != null) { l_vertex.Clear(); } //reset to original values the camera //angle = 0.0f; //lx = 0.0f; lz = -1.0f; //x = 0.0f; y = 0.0f; z = +1.0f; maxVertex = new SharpGL.SceneGraph.Vertex(float.MinValue, float.MinValue, float.MinValue); minVertex = new SharpGL.SceneGraph.Vertex(float.MaxValue, float.MaxValue, float.MaxValue); }
/// <summary> /// When the mouse is moving over the screen /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openGLControl_MouseMove(object sender, MouseEventArgs e) { if (bisLeftDrag) //left button clicked { if (firstMouse) { lastX = e.X; lastY = e.Y; firstMouse = false; } float xoffset = e.X - lastX; float yoffset = lastY - e.Y; // Reversed since y-coordinates go from bottom to left lastX = e.X; lastY = e.Y; xoffset *= SPEED_MOVE; //sensivity yoffset *= SPEED_MOVE; //sensivity yaw += xoffset; pitch += yoffset; // Make sure that when pitch is out of bounds, screen doesn't get flipped if (pitch > 89.0f) { pitch = 89.0f; } if (pitch < -89.0f) { pitch = -89.0f; } SharpGL.SceneGraph.Vertex front = new SharpGL.SceneGraph.Vertex(); front.X = (float)(Math.Cos(DegreeToRadian(yaw)) * Math.Cos(DegreeToRadian(pitch))); front.Y = (float)Math.Sin(DegreeToRadian(pitch)); front.Z = (float)(Math.Sin(DegreeToRadian(yaw)) * Math.Cos(DegreeToRadian(pitch))); front.Normalize(); cameraFront = front; } else if (bisRightDrag) //right button { if (firstMouse) { lastX = e.X; lastY = e.Y; firstMouse = false; } float xoffset = e.X - lastX; float yoffset = lastY - e.Y; // Reversed since y-coordinates go from bottom to left lastX = e.X; lastY = e.Y; if (xoffset > 0) //to the right { SharpGL.SceneGraph.Vertex v = cross(cameraFront, cameraUp); v.Normalize(); cameraPos += v * SPEED_MOVE; } else if (xoffset < 0) //to the left { SharpGL.SceneGraph.Vertex v = cross(cameraFront, cameraUp); v.Normalize(); cameraPos -= v * SPEED_MOVE; } if (yoffset > 0) //to go up { cameraPos.Y += SPEED_MOVE / 6.0f; //to adjust the sensivity } else if (yoffset < 0) //to go down { cameraPos.Y -= SPEED_MOVE / 6.0f; //to adjust the sensivity } } }
protected bool firstMouse = true; //to be set the 1st time on click #endregion #region mouseControls /// <summary> /// Simple cross product between vectors /// <param name="a">first vector</param> /// <param name="b">second vector</param> /// <returns> a resulting vector of a x b</returns> /// </summary> SharpGL.SceneGraph.Vertex cross(SharpGL.SceneGraph.Vertex a, SharpGL.SceneGraph.Vertex b) { return(new SharpGL.SceneGraph.Vertex(a.Y * b.Y - a.Z * b.Y, a.Z * b.X - a.X * b.Z, a.X * b.Y - a.Y * b.X)); }
/// <summary> /// Open a File to be loaded /// </summary> /// <param name="fileName">The name of the file to open.</param> private void loadFile(string fileName) { System.IO.StreamReader file = new System.IO.StreamReader(fileName); string line; if (l_vertex == null) l_vertex = new List<SharpGL.SceneGraph.Vertex>(); if (l_color == null) l_color = new List<SharpGL.SceneGraph.Vertex>(); // read each line while ((line = file.ReadLine()) != null) { string[] words = line.Split(','); SharpGL.SceneGraph.Vertex vertex = new SharpGL.SceneGraph.Vertex(); SharpGL.SceneGraph.Vertex color = new SharpGL.SceneGraph.Vertex(); vertex.Set( float.Parse(words[0], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[2], System.Globalization.CultureInfo.InvariantCulture)); l_vertex.Add(vertex); if (vertex.X < minVertex.X) minVertex.X = vertex.X; if (vertex.Y < minVertex.Y) minVertex.Y = vertex.Y; if (vertex.Z < minVertex.Z) minVertex.Z = vertex.Z; if (vertex.X > maxVertex.X) maxVertex.X = vertex.X; if (vertex.Y > maxVertex.Y) maxVertex.Y = vertex.Y; if (vertex.Z > maxVertex.Z) maxVertex.Z = vertex.Z; //ignoring the words[3] color.Set( float.Parse(words[4], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[5], System.Globalization.CultureInfo.InvariantCulture), float.Parse(words[6], System.Globalization.CultureInfo.InvariantCulture)); color /= 255.0f; l_color.Add(color); } v_center = (maxVertex + minVertex) / 2.0f; SharpGL.SceneGraph.Vertex distance = (maxVertex - minVertex); f_scale = Math.Max(Math.Max(distance.X, distance.Y), distance.Z); file.Close(); }
public bool Renderiza(ref SharpGL.SceneControl scene_GLControl, bool Grid = false, bool Modelo = false, double CotaZ = double.NaN, bool BBox = false) { bool Res = true; scene_GLControl.Scene.RenderBoundingVolumes = BBox; for (int i = 0; i < scene_GLControl.Scene.SceneContainer.Children.Count(); i++) { switch (scene_GLControl.Scene.SceneContainer.Children[i].Name) { case constPlanoSLT: if (CotaZ != double.NaN) { scene_GLControl.Scene.SceneContainer.RemoveChild(scene_GLControl.Scene.SceneContainer.Children[i]); i--; } break; case constModeloSLT: if (Modelo) { scene_GLControl.Scene.SceneContainer.RemoveChild(scene_GLControl.Scene.SceneContainer.Children[i]); i--; } break; case constDesignPrimitives: if (Grid) { if (scene_GLControl.Scene.SceneContainer.Children[i] is SharpGL.SceneGraph.Primitives.Folder) { SharpGL.SceneGraph.Primitives.Folder Foldr = scene_GLControl.Scene.SceneContainer.Children[i] as SharpGL.SceneGraph.Primitives.Folder; for (int j = 0; j < Foldr.Children.Count(); j++) { if (Foldr.Children[j] is SharpGL.SceneGraph.Primitives.Grid) { SharpGL.SceneGraph.Primitives.Grid Grd = Foldr.Children[j] as SharpGL.SceneGraph.Primitives.Grid; if (Grd.Name == constDesignGrid) { Foldr.RemoveChild(Grd); j--; } } else if (Foldr.Children[j] is SharpGL.SceneGraph.Primitives.Axies) { SharpGL.SceneGraph.Primitives.Axies Ax = Foldr.Children[j] as SharpGL.SceneGraph.Primitives.Axies; if (Ax.Name == constDesignAxie) { Foldr.RemoveChild(Ax); j--; } } } } } break; default: break; } } if (Grid) { SharpGL.SceneGraph.Primitives.Folder Foldr = null; for (int i = 0; i < scene_GLControl.Scene.SceneContainer.Children.Count(); i++) { if (scene_GLControl.Scene.SceneContainer.Children[i].Name == constDesignPrimitives) { if (scene_GLControl.Scene.SceneContainer.Children[i] is SharpGL.SceneGraph.Primitives.Folder) { Foldr = scene_GLControl.Scene.SceneContainer.Children[i] as SharpGL.SceneGraph.Primitives.Folder; break; } } } if (Foldr != null) { SharpGL.SceneGraph.Primitives.Grid grid = new SharpGL.SceneGraph.Primitives.Grid(); grid.Name = constDesignGrid; SharpGL.SceneGraph.Primitives.Axies axie = new SharpGL.SceneGraph.Primitives.Axies(); axie.Name = constDesignAxie; if (_Facets.Count > 0) { //printer Axies SharpGL.SceneGraph.Transformations.LinearTransformation linearAxiesTransformation = new SharpGL.SceneGraph.Transformations.LinearTransformation(); linearAxiesTransformation.ScaleX = 10; linearAxiesTransformation.ScaleY = 10; linearAxiesTransformation.ScaleZ = 10; SharpGL.SceneGraph.Effects.LinearTransformationEffect linearAxiesTransformationEffect = new SharpGL.SceneGraph.Effects.LinearTransformationEffect(); linearAxiesTransformationEffect.LinearTransformation = linearAxiesTransformation; axie.AddEffect(linearAxiesTransformationEffect); //Printer Grid SharpGL.SceneGraph.Transformations.LinearTransformation linearGridTransformation = new SharpGL.SceneGraph.Transformations.LinearTransformation(); linearGridTransformation.TranslateX = 200; linearGridTransformation.TranslateY = 200; linearGridTransformation.ScaleX = 20; linearGridTransformation.ScaleY = 20; SharpGL.SceneGraph.Effects.LinearTransformationEffect linearGridTransformationEffect = new SharpGL.SceneGraph.Effects.LinearTransformationEffect(); linearGridTransformationEffect.LinearTransformation = linearGridTransformation; grid.AddEffect(linearGridTransformationEffect); } Foldr.AddChild(axie); Foldr.AddChild(grid); } } if (!double.IsNaN(CotaZ)) { //Dibuja el plano Z. SharpGL.SceneGraph.Primitives.Polygon Polig = new SharpGL.SceneGraph.Primitives.Polygon(); Polig.Name = constPlanoSLT; Polig.DrawNormals = false; SharpGL.SceneGraph.Vertex[] Vertices = new SharpGL.SceneGraph.Vertex[4]; float MargenX = (float)this.Ancho * 0.1f; float MargenY = (float)this.Alto * 0.1f; Vertices[0] = new SharpGL.SceneGraph.Vertex(_IzqFrontInf.Xf - MargenX, _IzqFrontInf.Yf - MargenY, (float)CotaZ); Vertices[1] = new SharpGL.SceneGraph.Vertex(_IzqFrontInf.Xf - MargenX, _DerPostSup.Yf + MargenY, (float)CotaZ); Vertices[2] = new SharpGL.SceneGraph.Vertex(_DerPostSup.Xf + MargenX, _DerPostSup.Yf + MargenY, (float)CotaZ); Vertices[3] = new SharpGL.SceneGraph.Vertex(_DerPostSup.Xf + MargenX, _IzqFrontInf.Yf - MargenY, (float)CotaZ); Polig.AddFaceFromVertexData(Vertices); Polig.Validate(true); scene_GLControl.Scene.SceneContainer.AddChild(Polig); } if (Modelo && _Facets.Count > 0) { SharpGL.SceneGraph.Primitives.Polygon Polig = new SharpGL.SceneGraph.Primitives.Polygon(); Polig.Name = constModeloSLT; Polig.DrawNormals = false; foreach (FacetSLT F in _Facets) { foreach (LoopSLT L in F._Loops) { bool Representar = true; if (!double.IsNaN(CotaZ)) { //Evalua si se tiene que mostrar o no por encontrarse bajo el plano foreach (VertexSLT V in L.Vertices) { if (V.Z <= CotaZ) { Representar = true; break; } else { Representar = false; } } } if (Representar) { SharpGL.SceneGraph.Vertex[] Vertices = new SharpGL.SceneGraph.Vertex[L.Vertices.Count()]; for (int i = 0; i < L.Vertices.Count(); i++) { //Polig.Vertices.Add(new SharpGL.SceneGraph.Vertex(V._X, V._Y, V._Z)); Vertices[i] = new SharpGL.SceneGraph.Vertex(L.Vertices[i].Xf + (float)_Tx, L.Vertices[i].Yf + (float)_Ty, L.Vertices[i].Zf + (float)_Tz); } Polig.AddFaceFromVertexData(Vertices); } } } Polig.Validate(true); scene_GLControl.Scene.SceneContainer.AddChild(Polig); } return(Res); }
/// <summary> /// When the mouse is moving over the screen /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openGLControl_MouseMove(object sender, MouseEventArgs e) { if (bisLeftDrag) //left button clicked { if (firstMouse) { lastX = e.X; lastY = e.Y; firstMouse = false; } float xoffset = e.X - lastX; float yoffset = lastY - e.Y; // Reversed since y-coordinates go from bottom to left lastX = e.X; lastY = e.Y; xoffset *= SPEED_MOVE; //sensivity yoffset *= SPEED_MOVE; //sensivity yaw += xoffset; pitch += yoffset; // Make sure that when pitch is out of bounds, screen doesn't get flipped if (pitch > 89.0f) pitch = 89.0f; if (pitch < -89.0f) pitch = -89.0f; SharpGL.SceneGraph.Vertex front = new SharpGL.SceneGraph.Vertex(); front.X = (float)(Math.Cos(DegreeToRadian(yaw)) * Math.Cos(DegreeToRadian(pitch))); front.Y = (float)Math.Sin(DegreeToRadian(pitch)); front.Z = (float)(Math.Sin(DegreeToRadian(yaw)) * Math.Cos(DegreeToRadian(pitch))); front.Normalize(); cameraFront = front; } else if (bisRightDrag) //right button { if (firstMouse) { lastX = e.X; lastY = e.Y; firstMouse = false; } float xoffset = e.X - lastX; float yoffset = lastY - e.Y; // Reversed since y-coordinates go from bottom to left lastX = e.X; lastY = e.Y; if (xoffset > 0) //to the right { SharpGL.SceneGraph.Vertex v = cross(cameraFront, cameraUp); v.Normalize(); cameraPos += v * SPEED_MOVE; } else if (xoffset < 0) //to the left { SharpGL.SceneGraph.Vertex v = cross(cameraFront, cameraUp); v.Normalize(); cameraPos -= v * SPEED_MOVE; } if (yoffset > 0) //to go up { cameraPos.Y += SPEED_MOVE / 6.0f; //to adjust the sensivity } else if (yoffset < 0) //to go down { cameraPos.Y -= SPEED_MOVE / 6.0f; //to adjust the sensivity } } }
/// <summary> /// When the mouse wheel is touched/invoked /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void openGLControl_MouseWheel(object sender, MouseEventArgs e) { if (e.Delta > 0) //mouse wheel is from back to front cameraPos += cameraFront * SPEED_MOVE; else cameraPos -= cameraFront * SPEED_MOVE; }
/// <summary> /// Clean initial data /// </summary> private void reset() { OpenGL gl = openGLControl.OpenGL; //delete ids if (l_vboId != null) { foreach (var element in l_vboId) gl.DeleteBuffers(2, element); } if(l_vboId != null) l_vboId.Clear(); //delete sizes of list of points if(l_sizes != null) l_sizes.Clear(); //clear list of vertex and color if(l_color != null) l_color.Clear(); if(l_vertex != null) l_vertex.Clear(); //reset to original values the camera //angle = 0.0f; //lx = 0.0f; lz = -1.0f; //x = 0.0f; y = 0.0f; z = +1.0f; maxVertex = new SharpGL.SceneGraph.Vertex(float.MinValue, float.MinValue, float.MinValue); minVertex = new SharpGL.SceneGraph.Vertex(float.MaxValue, float.MaxValue, float.MaxValue); }
/// <summary> /// Just a debugging function to demostrate how to add new points over the existing list /// </summary> private void testFunctiontoAddCopy() { //to debug/test Random r = new Random(); List<SharpGL.SceneGraph.Vertex> newVertex = new List<SharpGL.SceneGraph.Vertex>(l_vertex); List<SharpGL.SceneGraph.Vertex> newColor = new List<SharpGL.SceneGraph.Vertex>(l_color); for (int k = 0; k < newVertex.Count; k++) { newVertex[k] = new SharpGL.SceneGraph.Vertex(newVertex[k].X + (float)(r.NextDouble() * 50.0), newVertex[k].Y + (float)(r.NextDouble() * 50.0), newVertex[k].Z); newColor[k] = new SharpGL.SceneGraph.Vertex(1, 1, 1); } // lists must exist addPointsAndColor(newVertex, newColor); newVertex.Clear(); newColor.Clear(); }
/// <summary> /// Allow change the color of the List lcolor according the height of the lvertex /// </summary> /// <param name="lvertex"> List of vertex to read the height according the min Vertex</param> /// <param name="lcolor"> List of color to change according its height</param> private void changeColorScale(ref List<SharpGL.SceneGraph.Vertex> lvertex, ref List<SharpGL.SceneGraph.Vertex> lcolor) { //top color SharpGL.SceneGraph.GLColor red = new SharpGL.SceneGraph.GLColor(0.9f, 0.05f, 0.05f, 1); //bottom color SharpGL.SceneGraph.GLColor green = new SharpGL.SceneGraph.GLColor(0.05f, 0.95f, 0.05f, 1); float diff = maxVertex.Z - minVertex.Z; SharpGL.SceneGraph.Vertex colorTemp = new SharpGL.SceneGraph.Vertex(); for (int k = 0; k < lcolor.Count; k++) { float t = (l_vertex[k].Z - minVertex.Z) / diff; //interpolated value colorTemp.Set( (red.R * (1.0f - t)) + (green.R * t), (red.G * (1.0f - t)) + (green.G * t), (red.B * (1.0f - t)) + (green.B * t)); lcolor[k] = colorTemp; } }