Click here to Skip to main content
15,887,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: How can we copy and move folders in one folder to another folder. Pin
OriginalGriff20-Apr-18 4:09
mveOriginalGriff20-Apr-18 4:09 
GeneralRe: How can we copy and move folders in one folder to another folder. Pin
BillWoodruff20-Apr-18 15:32
professionalBillWoodruff20-Apr-18 15:32 
AnswerRe: How can we copy and move folders in one folder to another folder. Pin
Gerry Schmitz20-Apr-18 7:09
mveGerry Schmitz20-Apr-18 7:09 
QuestionPLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Jens Eckervogt 19-Apr-18 11:03
Jens Eckervogt 19-Apr-18 11:03 
AnswerRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Gerry Schmitz20-Apr-18 7:26
mveGerry Schmitz20-Apr-18 7:26 
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Jens Eckervogt 20-Apr-18 10:21
Jens Eckervogt 20-Apr-18 10:21 
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Gerry Schmitz20-Apr-18 12:37
mveGerry Schmitz20-Apr-18 12:37 
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Jens Eckervogt 21-Apr-18 0:04
Jens Eckervogt 21-Apr-18 0:04 
Yes But I got working obj loader = no problem but faces of obj are really painfully Frown | :(

Look out picture: [^]

How do I fix it?

My code was very close resolved but faces are not okay now. :/
WavefrontLoader.cs
using OpenTK;
using System;
using System.Collections.Generic;
using System.IO;

namespace Tutorial_08.net.sourceskyboxer
{
    public class WaveFrontLoader
    {
        private static List<Vector3> vertices;
        private static List<Vector2> textures;
        private static List<Vector3> normals;
        private static List<int> indices;

        private static float[] verticesArray;
        private static float[] normalsArray;
        private static float[] texturesArray;
        private static int[] indicesArray;

        public static int vertexPointer;

        public static RawModel LoadObjModel(string filename, Loader loader)
        {
            vertices = new List<Vector3>();
            textures = new List<Vector2>();
            normals = new List<Vector3>();
            indices = new List<int>();

            using (var reader = new StreamReader(new FileStream("Contents/" + filename + ".obj", FileMode.Open, FileAccess.Read)))
            {
                string line;

                try
                {
                    while (true)
                    {
                        line = reader.ReadLine();

                        string[] currentLine = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (currentLine[0] == "v")
                        {
                            Vector3 vertex = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3]));
                            vertices.Add(vertex);
                        } else 
                        if (currentLine[0] == "vt")
                        {
                            Vector2 texture = new Vector2(float.Parse(currentLine[1]), float.Parse(currentLine[2]));
                            textures.Add(texture);
                        }else
                        if (currentLine[0] == "vn")
                        {
                            Vector3 normal = new Vector3(float.Parse(currentLine[1]), float.Parse(currentLine[2]), float.Parse(currentLine[3]));
                            normals.Add(normal);
                        }
                        else
                        if (currentLine[0] == "f")
                        {
                            texturesArray = new float[vertices.Count * 2];
                            normalsArray = new float[vertices.Count * 3];
                            break;
                        }
                    }

                    while (line != null)
                    {
                        if (!line.StartsWith("f"))
                        {
                            line = reader.ReadLine();
                            continue;
                        }

                        string[] currentLine = line.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] vertex_1 = currentLine[1].Split(char.Parse("/"));
                        string[] vertex_2 = currentLine[2].Split(char.Parse("/"));
                        string[] vertex_3 = currentLine[3].Split(char.Parse("/"));

                        ProcessVertex(vertex_1, indices, textures, normals, texturesArray, normalsArray);
                        ProcessVertex(vertex_2, indices, textures, normals, texturesArray, normalsArray);
                        ProcessVertex(vertex_3, indices, textures, normals, texturesArray, normalsArray);
                        line = reader.ReadLine();
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                }

                verticesArray = new float[vertices.Count * 3];
                indicesArray = new int[indices.Count];

                vertexPointer = 0;
                foreach (Vector3 vertex in vertices)
                {
                    verticesArray[vertexPointer++] = vertex.X;
                    verticesArray[vertexPointer++] = vertex.Y;
                    verticesArray[vertexPointer++] = vertex.Z;
                }

                for (int i = 0; i < indices.Count; i++)
                {
                    indicesArray[i] = indices[i];
                }

                reader.Close();
            }

            return loader.loadToVAO(verticesArray, texturesArray, indicesArray);
        }

        private static void ProcessVertex(string[] vertexData, List<int> indices,
            List<Vector2> textures, List<Vector3> normals, float[] texturesArray, float[] normalsArray)
        {
            int currentVertexPointer = int.Parse(vertexData[0]) - 1;
            indices.Add(currentVertexPointer);
            Vector2 currentTex = textures[int.Parse(vertexData[1]) - 1];
            texturesArray[currentVertexPointer * 2] = currentTex.X;
            texturesArray[currentVertexPointer * 2 + 1] = 1 - currentTex.Y;
            Vector3 currentNorm = normals[int.Parse(vertexData[2]) - 1];
            normalsArray[currentVertexPointer * 3] = currentNorm.X;
            normalsArray[currentVertexPointer * 3 + 1] = currentNorm.Y;
            normalsArray[currentVertexPointer * 3 + 2] = currentNorm.Z;
        }
    }
}


And Renderer.cs ( I have fixed cause it looks sometimes wrong GL:DrawElement() or GL.DrawArrays()
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL4;
using Tutorial_08.net.sourceskyboxer.entities;

namespace Tutorial_08.net.sourceskyboxer
{
    public class Renderer
    {
        private const float NEAR_PLANE = 0.1f;
        private const float FAR_PLANE = 1000000;

        public Renderer(StaticShader shader)
        {
            CreateProjectionMatrix();
            shader.start();
            shader.LoadProjectionMatrix(projectionMatrix);
            shader.stop();
        }

        private Matrix4 projectionMatrix;

        public virtual void prepare()
        {
            GL.ClearColor(Color4.Orange);
            GL.Enable(EnableCap.DepthTest);
            GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
        }

        public virtual void render(Entity entity, StaticShader shader)
        {
            TexturedModel model = entity.Model;
            RawModel rawModel = model.RawModel;
            GL.BindVertexArray(rawModel.VaoID);
            GL.EnableVertexAttribArray(0);
            GL.EnableVertexAttribArray(1);

            Matrix4 transformationMatrix = Maths.CreateTransformationMatrix(entity.Position.X, entity.Position.Y, entity.Position.Z, entity.RotX, entity.RotY, entity.RotZ, entity.Scale);
            shader.LoadTransformationMatrix(transformationMatrix);

            GL.ActiveTexture(TextureUnit.Texture0);
            GL.BindTexture(TextureTarget.Texture2D, model.ModelTexture.TextureID);

            GL.DrawElements(BeginMode.Triangles, rawModel.VertexCount, DrawElementsType.UnsignedShort, 0);

            GL.DisableVertexAttribArray(1);
            GL.DisableVertexAttribArray(0);
            GL.BindVertexArray(0);
        }

        private void CreateProjectionMatrix()
        {
            float aspectRadio = Game.GameWIdth / Game.GameHeight;
            projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, aspectRadio, NEAR_PLANE, FAR_PLANE);
        }
    }
}

And I have fixed texture cause CLampEdges makes black
I remove both GL.TexParameter
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using OpenTK.Graphics.OpenGL4;

namespace Tutorial_08.net.sourceskyboxer
{
    public class Loader
    {
        private IList<int> vaos = new List<int>();
        private IList<int> vbos = new List<int>();
        private IList<int> textures = new List<int>();

        public virtual RawModel loadToVAO(float[] positions, float[] textureCoords, int[] indices)
        {
            int vaoID = createVAO();
            bindIndicesBuffer(indices);
            storeDataInAttributeList(0, 3, positions);
            storeDataInAttributeList(1, 2, textureCoords);
            unbindVAO();
            return new RawModel(vaoID, indices.Length);
        }

        public int loadTexture(string textureName)
        {
            if(!File.Exists("Contents/"+textureName+".png"))
            {
                throw new FileNotFoundException("Error: texture files doesn't exist path: "+textureName+".png");
            }

            int textureID = GL.GenTexture();
            textures.Add(textureID);
            GL.BindTexture(TextureTarget.Texture2D, textureID);

            Bitmap bmp = new Bitmap("Contents/" + textureName + ".png");
            System.Drawing.Imaging.BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, data.Width, data.Height, 0, PixelFormat.Rgba, PixelType.UnsignedByte, data.Scan0);
            bmp.UnlockBits(data);

            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
            GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);

            return textureID;
        }

        public virtual void cleanUp()
        {
            foreach (int vao in vaos)
            {
                GL.DeleteVertexArray(vao);
            }
            foreach (int vbo in vbos)
            {
                GL.DeleteBuffer(vbo);
            }
            foreach(int texture in textures)
            {
                GL.DeleteTexture(texture);
            }
        }

        private int createVAO()
        {
            int vaoID = GL.GenVertexArray();
            vaos.Add(vaoID);
            GL.BindVertexArray(vaoID);
            return vaoID;
        }

        private void storeDataInAttributeList(int attributeNumber, int coordinateSize, float[] data)
        {
            int vboID = GL.GenBuffer();
            vbos.Add(vboID);
            GL.BindBuffer(BufferTarget.ArrayBuffer, vboID);
            GL.BufferData(BufferTarget.ArrayBuffer, (data.Length * sizeof(float)), data, BufferUsageHint.StaticDraw);
            GL.VertexAttribPointer(attributeNumber, coordinateSize, VertexAttribPointerType.Float, false, 0, 0);
            GL.BindBuffer(BufferTarget.ArrayBuffer, 0);
        }

        private void bindIndicesBuffer(int[] indices)
        {
            int vboId = GL.GenBuffer();
            vbos.Add(vboId);
            GL.BindBuffer(BufferTarget.ElementArrayBuffer, vboId);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw);
        }

        private void unbindVAO()
        {
            GL.BindVertexArray(0);
        }
    }
}


Do you mean I should replace StreamReader() with path of bitmap/texture? Or I should fix faces of WavefrontLoader.cs?
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Jens Eckervogt 20-Apr-18 9:39
Jens Eckervogt 20-Apr-18 9:39 
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Gerry Schmitz20-Apr-18 13:08
mveGerry Schmitz20-Apr-18 13:08 
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Pete O'Hanlon20-Apr-18 22:11
mvePete O'Hanlon20-Apr-18 22:11 
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Jens Eckervogt 21-Apr-18 5:15
Jens Eckervogt 21-Apr-18 5:15 
GeneralRe: PLEASE HELP ME - I have problem with OpenTK and Loading WaveFront ( obj without materials ) Pin
Pete O'Hanlon21-Apr-18 5:38
mvePete O'Hanlon21-Apr-18 5:38 
Questionc# how to Temporary pause button click event until mousedoubleclick on new form-listbox shown? Pin
Member 1376930818-Apr-18 23:06
Member 1376930818-Apr-18 23:06 
AnswerRe: c# how to Temporary pause button click event until mousedoubleclick on new form-listbox shown? Pin
Eddy Vluggen18-Apr-18 23:10
professionalEddy Vluggen18-Apr-18 23:10 
AnswerRe: c# how to Temporary pause button click event until mousedoubleclick on new form-listbox shown? Pin
OriginalGriff18-Apr-18 23:50
mveOriginalGriff18-Apr-18 23:50 
AnswerRe: c# how to Temporary pause button click event until mousedoubleclick on new form-listbox shown? Pin
BillWoodruff20-Apr-18 3:59
professionalBillWoodruff20-Apr-18 3:59 
Questionplease hellp me how to write a code in c# which is send warning message before specific time thanks Pin
ahmedoze18-Apr-18 16:09
ahmedoze18-Apr-18 16:09 
AnswerRe: please hellp me how to write a code in c# which is send warning message before specific time thanks Pin
OriginalGriff18-Apr-18 19:50
mveOriginalGriff18-Apr-18 19:50 
QuestionRe: please hellp me how to write a code in c# which is send warning message before specific time thanks Pin
Eddy Vluggen18-Apr-18 22:50
professionalEddy Vluggen18-Apr-18 22:50 
AnswerRe: please hellp me how to write a code in c# which is send warning message before specific time thanks Pin
Richard MacCutchan19-Apr-18 0:57
mveRichard MacCutchan19-Apr-18 0:57 
AnswerRe: please hellp me how to write a code in c# which is send warning message before specific time thanks Pin
Pete O'Hanlon18-Apr-18 23:16
mvePete O'Hanlon18-Apr-18 23:16 
AnswerRe: please hellp me how to write a code in c# which is send warning message before specific time thanks Pin
Kandula Santosh21-Apr-18 8:15
Kandula Santosh21-Apr-18 8:15 
QuestionProcess.start is slow compared to Powershell Start-Process Pin
Neal Conrardy18-Apr-18 8:47
Neal Conrardy18-Apr-18 8:47 
AnswerRe: Process.start is slow compared to Powershell Start-Process Pin
Dave Kreskowiak18-Apr-18 17:20
mveDave Kreskowiak18-Apr-18 17:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.