Example #1
0
        public void LoadSphereData(int[] SphereArray, ModelUtility sphere, int[] mVertexArrayObjectIDs, int x, int location, int normlLocation)
        {
            #region load in vertices and indices
            GL.GenBuffers(2, SphereArray);

            GL.BindBuffer(BufferTarget.ArrayBuffer, SphereArray[0]);
            GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(sphere.Vertices.Length * sizeof(float)), sphere.Vertices, BufferUsageHint.StaticDraw);

            int size;
            GL.GetBufferParameter(BufferTarget.ArrayBuffer, BufferParameterName.BufferSize, out size);

            if (sphere.Vertices.Length * sizeof(float) != size)
            {
                throw new ApplicationException("Vertex data not loaded onto graphics card correctly");
            }

            GL.BindBuffer(BufferTarget.ElementArrayBuffer, SphereArray[1]);
            GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(sphere.Indices.Length * sizeof(uint)), sphere.Indices, BufferUsageHint.StaticDraw);

            GL.GetBufferParameter(BufferTarget.ElementArrayBuffer, BufferParameterName.BufferSize, out size);

            if (sphere.Indices.Length * sizeof(uint) != size)
            {
                throw new ApplicationException("Index data not loaded onto graphics card correctly");
            }
            #endregion
            BindSphereData(mVertexArrayObjectIDs, x, SphereArray, location, normlLocation);
        }
Example #2
0
        private static ModelUtility LoadFromSJG(string pModelFile)
        {
            ModelUtility model = new ModelUtility();
            StreamReader reader;

            reader = new StreamReader(pModelFile);
            string line             = reader.ReadLine(); // vertex format
            int    numberOfVertices = 0;
            int    floatsPerVertex  = 5;

            if (!int.TryParse(reader.ReadLine(), out numberOfVertices))
            {
                throw new Exception("Error when reading number of vertices in model file " + pModelFile);
            }

            model.Vertices = new float[numberOfVertices * floatsPerVertex];

            string[] values;
            for (int i = 0; i < model.Vertices.Length;)
            {
                line   = reader.ReadLine();
                values = line.Split(',');
                foreach (string s in values)
                {
                    if (!float.TryParse(s, out model.Vertices[i]))
                    {
                        throw new Exception("Error when reading vertices in model file " + pModelFile + " " + s + " is not a valid number");
                    }
                    ++i;
                }
            }

            reader.ReadLine();
            int numberOfTriangles = 0;

            line = reader.ReadLine();
            if (!int.TryParse(line, out numberOfTriangles))
            {
                throw new Exception("Error when reading number of triangles in model file " + pModelFile);
            }

            model.Indices = new int[numberOfTriangles * 3];

            for (int i = 0; i < numberOfTriangles * 3;)
            {
                line   = reader.ReadLine();
                values = line.Split(',');
                foreach (string s in values)
                {
                    if (!int.TryParse(s, out model.Indices[i]))
                    {
                        throw new Exception("Error when reading indices in model file " + pModelFile + " " + s + " is not a valid index");
                    }
                    ++i;
                }
            }

            reader.Close();
            return(model);
        }
Example #3
0
        private static ModelUtility LoadFromBIN(string pModelFile)
        {
            ModelUtility model  = new ModelUtility();
            BinaryReader reader = new BinaryReader(new FileStream(pModelFile, FileMode.Open));

            int numberOfVertices = reader.ReadInt32();
            int floatsPerVertex  = 6;

            model.Vertices = new float[numberOfVertices * floatsPerVertex];

            byte[] byteArray = new byte[model.Vertices.Length * sizeof(float)];
            byteArray = reader.ReadBytes(byteArray.Length);

            Buffer.BlockCopy(byteArray, 0, model.Vertices, 0, byteArray.Length);

            int numberOfTriangles = reader.ReadInt32();

            model.Indices = new int[numberOfTriangles * 3];

            byteArray = new byte[model.Indices.Length * sizeof(int)];
            byteArray = reader.ReadBytes(model.Indices.Length * sizeof(int));
            Buffer.BlockCopy(byteArray, 0, model.Indices, 0, byteArray.Length);

            reader.Close();
            return(model);
        }
Example #4
0
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            GL.ClearColor(Color4.CornflowerBlue);
            GL.Enable(EnableCap.CullFace);
            GL.Enable(EnableCap.DepthTest);
            mSphere   = ModelUtility.LoadModel(@"Models/sphere.bin");
            mCylinder = ModelUtility.LoadModel(@"Models/cylinder.bin");
            MBox      = ModelUtility.LoadModel(@"Models/TexturedCube.sjg");
            sphere    = new Sphere(); box = new Box(MBox); cylinder = new Cylinder();

            int textureOne = box.LoadBoxTexture(@"Textures/texture2.jpg", TextureUnit.Texture0);

            boxShader   = new ShaderUtility(@"Shaders/BoxVertexShader.vert", @"Shaders/BoxFragmentShader.frag");
            modelShader = new ShaderUtility(@"Shaders/vertexShader.vert", @"Shaders/fragmentShader.frag");
            GL.UseProgram(boxShader.ShaderProgramID);
            #region location variables for the box shader
            int uView = GL.GetUniformLocation(boxShader.ShaderProgramID, "uView");
            int boxPositionLocation     = GL.GetAttribLocation(boxShader.ShaderProgramID, "boxVertexPosition");
            int uProjectionLocation     = GL.GetAttribLocation(boxShader.ShaderProgramID, "uProjection");
            int uTextureSamplerLocation = GL.GetUniformLocation(boxShader.ShaderProgramID, "baseMap");
            int pixelSixeLocation       = GL.GetUniformLocation(boxShader.ShaderProgramID, "pixelSize");
            int vTexCoordsLocation      = GL.GetAttribLocation(boxShader.ShaderProgramID, "vTexCoords");
            int uLightDirectionLocation = GL.GetUniformLocation(boxShader.ShaderProgramID, "uLightDirection");
            int vNormalVlocation        = GL.GetAttribLocation(boxShader.ShaderProgramID, "vNormal");
            int glowLocation            = GL.GetUniformLocation(boxShader.ShaderProgramID, "glowColour");
            #endregion
            Vector3 normalisedLightDirection, lightDirection = new Vector3(1, 1, 1);
            Vector3.Normalize(ref lightDirection, out normalisedLightDirection);
            mView       = Matrix4.CreateTranslation(0, 0, -2);
            uProjection = Matrix4.CreatePerspectiveFieldOfView(1, (float)ClientRectangle.Width / ClientRectangle.Height, 0.5f, 5);

            #region linking variables for the box shader
            Vector4 glowColour = new Vector4(0, 2.0f, 0, 1.0f);
            GL.Uniform1(pixelSixeLocation, pixelSixe);
            GL.Uniform4(glowLocation, glowColour);
            GL.Uniform3(uLightDirectionLocation, normalisedLightDirection);
            GL.UniformMatrix4(uProjectionLocation, true, ref uProjection);
            GL.UniformMatrix4(uView, true, ref mView);
            GL.Uniform1(uTextureSamplerLocation, 0);
            #endregion
            box.LoadBoxData(BoxArray, mVertexArrayObjectIDs, boxNumber, boxPositionLocation, vTexCoordsLocation);
            // cylinder.LoadCylinderData(CylinderArray,mCylinder ,mVertexArrayObjectIDs, cylinderNumber, positionLocation, normalLocation);
            //   sphere.LoadSphereData(SphereArray, mVertexArrayObjectIDs,mSphere ,sphereNumber, positionLocation, normalLocation );
        }
Example #5
0
 public Box(ModelUtility inbox)
 {
     box = inbox;
 }