public Object3d(float[] _vertexes, float[] _colors, RawImageLoader imageLoader) { modelMatrix = GLCommon.Identity(); rotationMatrix = (float[])modelMatrix; translationMatrix = (float[])modelMatrix; //TODO: esses vertices estão aqui só pra teste. Mais pra frente eles virão de sources. vertices = _vertexes; colors = _colors; //TODO: no futuro isso deve ser incorporado à uma classe de Material, mas no momento pode ficar assim. ShaderSourceLoader shaderSource = new ShaderSourceLoader("simpleVertexShader.glsl", "simpleFragmentShader.glsl"); shaderProgram = new ShaderProgram(shaderSource.VertexShaderSourceCode, shaderSource.FragmentShaderSourceCode); //TODO: criação da textura no opengl tem que ir pra uma classe apropriada pra isso. GL.GenTextures(1, idTextura); GL.BindTexture(All.Texture2D, idTextura[0]); GL.TexParameter(All.Texture2D, All.TextureMagFilter, (int)All.Linear); GL.TexParameter(All.Texture2D, All.TextureMinFilter, (int)All.Linear); GL.TexParameter(All.Texture2D, All.TextureWrapS, (int)All.ClampToEdge); GL.TexParameter(All.Texture2D, All.TextureWrapT, (int)All.ClampToEdge); //(OpenTK.Graphics.ES30.All,int,OpenTK.Graphics.ES30.All,int,int,int,OpenTK.Graphics.ES30.All,OpenTK.Graphics.ES30.All,!!0[]) IntPtr unmanagedPointer = Marshal.AllocHGlobal(b.Length); Marshal.Copy(b, 0, unmanagedPointer, b.Length); //GL.TexImage2D(All.Texture2D, 0, All.Rgb, imageLoader.ImageWidth, imageLoader.ImageWidth, 0, All.Rgb, All.Byte, unmanagedPointer); //GL.TexImage2D(All.Texture2D, 0, All.Rgb, imageLoader.ImageWidth, imageLoader.ImageWidth, 0, All.Rgb, All.Byte, imageLoader.ImageBuffer); GL.BindTexture(All.Texture2D, 0);//pára de trabalhar com a textura. }
public Object3d Fabricate() { //TODO: usar normais //TODO: usar texture coordinates //TODO: usar indexes. //var result = attributes.Where(attr => attr.Name.Equals(name)).ToList(); //abre o arquivo do cubo //TODO: tirar esse hardcoded daqui e pas TextFileReaderFromResources objReader = new TextFileReaderFromResources("cube.obj3d", "objects3d"); TextFileReaderFromResources mtlReader = new TextFileReaderFromResources("cube.mtl", "objects3d"); //parsing dos dados //picota por linha; string[] crSeparator = { "\n" }; List <String> lines = new List <string>(objReader.Text.Split(crSeparator, StringSplitOptions.RemoveEmptyEntries)); //remoção dos comentários var noComments = lines.Where(str => !str.StartsWith("#")).ToList(); //Cada linha em sua caixinha var vertexDefinitionLines = noComments.Where(str => str.StartsWith("v ")).ToList(); var normalsDefinitionLines = noComments.Where(str => str.StartsWith("vn")).ToList(); var textureCoordinatesDefinitionLines = noComments.Where(str => str.StartsWith("vt")).ToList(); var facesDefinitionsLines = noComments.Where(str => str.StartsWith("f")).ToList(); //agora extraio os vértices List <Vector3> vertexData = vertexDefinitionLines.Select(strVert => { string[] _separator = { " " }; string[] coordsAsStr = strVert.Split(_separator, StringSplitOptions.RemoveEmptyEntries); return(new Vector3(float.Parse(coordsAsStr[1].Trim()), float.Parse(coordsAsStr[2].Trim()), float.Parse(coordsAsStr[3].Trim()))); }).ToList(); //Agora extraio as normais List <Vector3> normalData = normalsDefinitionLines.Select(strNormal => { string[] _separator = { " " }; string[] coordsAsStr = strNormal.Split(_separator, StringSplitOptions.RemoveEmptyEntries); return(new Vector3(float.Parse(coordsAsStr[1].Trim()), float.Parse(coordsAsStr[2].Trim()), float.Parse(coordsAsStr[3].Trim()))); }).ToList(); //Agora extraio as texture coordinates List <Vector2> tcData = textureCoordinatesDefinitionLines.Select(strTC => { string[] _separator = { " " }; string[] coordsAsStr = strTC.Split(_separator, StringSplitOptions.RemoveEmptyEntries); return(new Vector2( float.Parse(coordsAsStr[1].Trim()), float.Parse(coordsAsStr[2].Trim()) )); }).ToList(); //Agora extraio as faces List <FaceData> fData = facesDefinitionsLines.Select(strF => { string[] _sepBetweenVerts = { " " }; string[] _strVertices = strF.Split(_sepBetweenVerts, StringSplitOptions.RemoveEmptyEntries); string[] _sepInsideVert = { "/" }; string[] _strVertData0 = _strVertices[1].Split(_sepInsideVert, StringSplitOptions.RemoveEmptyEntries); string[] _strVertData1 = _strVertices[2].Split(_sepInsideVert, StringSplitOptions.RemoveEmptyEntries); string[] _strVertData2 = _strVertices[3].Split(_sepInsideVert, StringSplitOptions.RemoveEmptyEntries); FaceData face = new FaceData(); face.v0 = int.Parse(_strVertData0[0]); face.n0 = int.Parse(_strVertData0[1]); face.tc0 = int.Parse(_strVertData0[2]); face.v1 = int.Parse(_strVertData1[0]); face.n0 = int.Parse(_strVertData1[1]); face.tc0 = int.Parse(_strVertData1[2]); face.v2 = int.Parse(_strVertData2[0]); face.n0 = int.Parse(_strVertData2[1]); face.tc0 = int.Parse(_strVertData2[2]); return(face); }).ToList(); //pronto, todos os dados foram extraídos do arquivo e estão em estruturas de dados. //Usando os dados do fData, cria uma lista ordenada de vértices, ordenada pela informação v* do //FaceData. Como eu ainda não uso indices é assim que eu tenho que mandar os dados. OBS, a lista //é 1-based e não 0-based como todas as outras listas do mundo. List <float> lstVertexBuffer = new List <float>(); List <float> lstVertexColor = new List <float>();//TODO: isso aqui é temporário, um dia vai ser removido. foreach (var f in fData) { Vector3 v0 = vertexData[f.v0 - 1]; lstVertexBuffer.Add(v0.X); lstVertexBuffer.Add(v0.Y); lstVertexBuffer.Add(v0.Z); Vector3 c0 = new Vector3(v0); c0.Normalize(); lstVertexColor.Add(c0.X); lstVertexColor.Add(c0.Y); lstVertexColor.Add(c0.Z); lstVertexColor.Add(1); Vector3 v1 = vertexData[f.v1 - 1]; lstVertexBuffer.Add(v1.X); lstVertexBuffer.Add(v1.Y); lstVertexBuffer.Add(v1.Z); Vector3 c1 = new Vector3(v1); c1.Normalize(); lstVertexColor.Add(c1.X); lstVertexColor.Add(c1.Y); lstVertexColor.Add(c1.Z); lstVertexColor.Add(1); Vector3 v2 = vertexData[f.v2 - 1]; lstVertexBuffer.Add(v2.X); lstVertexBuffer.Add(v2.Y); lstVertexBuffer.Add(v2.Z); Vector3 c2 = new Vector3(v2); c2.Normalize(); lstVertexColor.Add(c2.X); lstVertexColor.Add(c2.Y); lstVertexColor.Add(c2.Z); lstVertexColor.Add(1); } //TODO: Area de teste da carga de textura RawImageLoader imageLoader = new RawImageLoader("teste.raw"); Object3d obj = new Object3d(lstVertexBuffer.ToArray(), lstVertexColor.ToArray(), imageLoader); return(obj); }