private static void RenderInstance(ModelInstance inst) { ModelAsset asset = inst.Asset; Program shaders = asset.Shaders; // bind the shaders shaders.Use(); // set the shader uniforms Matrix m = _gCamera.CombinedMatrix; shaders.SetUniform("camera", ref m); shaders.SetUniform("model", ref inst.Transform); shaders.SetUniform("tex", 0); // set to 0 because the texture will be bound to GL_TEXTURE0 shaders.SetUniform("light.position", _gLight.Position); shaders.SetUniform("light.intensities", _gLight.Intensities); //bind the texture GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture2D, asset.Texture.GLObject); // bind VAO and draw GL.BindVertexArray(asset.Vao); GL.DrawArrays(asset.DrawType, asset.DrawStart, asset.DrawCount); // unbind everything GL.BindVertexArray(0); GL.BindTexture(TextureTarget.Texture2D, 0); shaders.StopUsing(); }
// initialises the _gWoodenCrate global private static void LoadWoodenCrateAsset() { // set all the elements of gWoodenCrate _gWoodenCrate = new ModelAsset(); _gWoodenCrate.Shaders = LoadShaders("vertex-shader.glsl", "fragment-shader.glsl"); _gWoodenCrate.DrawType = BeginMode.Triangles; _gWoodenCrate.DrawStart = 0; _gWoodenCrate.DrawCount = 6 * 2 * 3; _gWoodenCrate.Texture = LoadTexture("wooden-crate.png"); _gWoodenCrate.Shininess = 80.0f; _gWoodenCrate.SpecularColor = new Vector3(1.0f, 1.0f, 1.0f); GL.GenBuffers(1, out _gWoodenCrate.Vbo); GL.GenVertexArrays(1, out _gWoodenCrate.Vao); // bind the VAO GL.BindVertexArray(_gWoodenCrate.Vao); // bind the VBO GL.BindBuffer(BufferTarget.ArrayBuffer, _gWoodenCrate.Vbo); // Make a cube out of triangles (two triangles per side) float[] vertexData = new float[] { // X Y Z U V Normal // bottom -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f,-1.0f,-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0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f,-1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,-1.0f,-1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f,-1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f,-1.0f, 1.0f, 0.0f, -1.0f, 0.0f, 0.0f, // right 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,-1.0f,-1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,-1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,-1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f }; GL.BufferData<float>(BufferTarget.ArrayBuffer, new IntPtr(vertexData.Length * sizeof(float)), vertexData, BufferUsageHint.StaticDraw); // connect the xyz to the "vert" attribute of the vertex shader GL.EnableVertexAttribArray(_gWoodenCrate.Shaders.Attrib("vert")); GL.VertexAttribPointer(_gWoodenCrate.Shaders.Attrib("vert"), 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 0); // connect the uv coords to the "vertTexCoord" attribute of the vertex shader GL.EnableVertexAttribArray(_gWoodenCrate.Shaders.Attrib("vertTexCoord")); GL.VertexAttribPointer(_gWoodenCrate.Shaders.Attrib("vertTexCoord"), 2, VertexAttribPointerType.Float, true, 8 * sizeof(float), 3 * sizeof(float)); // connect the normal to the "vertNormal" attribute of the vertex shader GL.EnableVertexAttribArray(_gWoodenCrate.Shaders.Attrib("vertNormal")); GL.VertexAttribPointer(_gWoodenCrate.Shaders.Attrib("vertNormal"), 3, VertexAttribPointerType.Float, true, 8 * sizeof(float), 5 * sizeof(float)); // unbind the VAO GL.BindVertexArray(0); }
// initialises the _gWoodenCrate global private static void LoadWoodenCrateAsset() { // set all the elements of gWoodenCrate _gWoodenCrate = new ModelAsset(); _gWoodenCrate.Shaders = LoadShaders("vertex-shader.glsl", "fragment-shader.glsl"); _gWoodenCrate.DrawType = BeginMode.Triangles; _gWoodenCrate.DrawStart = 0; _gWoodenCrate.DrawCount = 6 * 2 * 3; _gWoodenCrate.Texture = LoadTexture("wooden-crate.png"); GL.GenBuffers(1, out _gWoodenCrate.Vbo); GL.GenVertexArrays(1, out _gWoodenCrate.Vao); // bind the VAO GL.BindVertexArray(_gWoodenCrate.Vao); // bind the VBO GL.BindBuffer(BufferTarget.ArrayBuffer, _gWoodenCrate.Vbo); // Make a cube out of triangles (two triangles per side) float[] vertexData = new float[] { // X Y Z U V Normal // bottom -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, -1.0f, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, -1.0f, 0.0f, // top -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0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 0.0f, 0.0f, 1.0f, -1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f }; GL.BufferData <float>(BufferTarget.ArrayBuffer, new IntPtr(vertexData.Length * sizeof(float)), vertexData, BufferUsageHint.StaticDraw); // connect the xyz to the "vert" attribute of the vertex shader GL.EnableVertexAttribArray(_gWoodenCrate.Shaders.Attrib("vert")); GL.VertexAttribPointer(_gWoodenCrate.Shaders.Attrib("vert"), 3, VertexAttribPointerType.Float, false, 8 * sizeof(float), 0); // connect the uv coords to the "vertTexCoord" attribute of the vertex shader GL.EnableVertexAttribArray(_gWoodenCrate.Shaders.Attrib("vertTexCoord")); GL.VertexAttribPointer(_gWoodenCrate.Shaders.Attrib("vertTexCoord"), 2, VertexAttribPointerType.Float, true, 8 * sizeof(float), 3 * sizeof(float)); // connect the normal to the "vertNormal" attribute of the vertex shader GL.EnableVertexAttribArray(_gWoodenCrate.Shaders.Attrib("vertNormal")); GL.VertexAttribPointer(_gWoodenCrate.Shaders.Attrib("vertNormal"), 3, VertexAttribPointerType.Float, true, 8 * sizeof(float), 5 * sizeof(float)); // unbind the VAO GL.BindVertexArray(0); }