public void Draw(uint shader, Transform transform, Camera camera, Projection projection, Lighting lighting) { uint diffuseNumber = 1; uint specularNumber = 1; if (Textures.Count == 0) { Gl.glActiveTexture(Gl.GL_TEXTURE0); int textureLoc = Gl.glGetUniformLocation(shader, "material.textureDiffuse1"); Gl.glUniform1i(textureLoc, 0); Gl.glBindTexture(Gl.GL_TEXTURE_2D, CustomTexture); } for (int i = 0; i < Textures.Count; ++i) { Gl.glActiveTexture(Gl.GL_TEXTURE0 + i); string name = "texture" + Textures[i].Type.ToString(); if (Textures[i].Type == Texture.TextureType.Diffuse) { name += diffuseNumber.ToString(); ++diffuseNumber; } else { name += specularNumber.ToString(); ++specularNumber; } int textureLoc = Gl.glGetUniformLocation(shader, "material." + name.ToString()); Gl.glUniform1i(textureLoc, i); Gl.glBindTexture(Gl.GL_TEXTURE_2D, Textures[i].Id); } Gl.glUseProgram(shader); Gl.glActiveTexture(Gl.GL_TEXTURE0); Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, ebo); Gl.glBufferData(Gl.GL_ELEMENT_ARRAY_BUFFER, (IntPtr)(Indices.Count * sizeof(float)), Indices.ToArray(), Gl.GL_STATIC_DRAW); Gl.glBindBuffer(Gl.GL_ARRAY_BUFFER, vbo); var buf = GetVertices(); Gl.glBufferData(Gl.GL_ARRAY_BUFFER, (IntPtr)(buf.Length * sizeof(float)), GetVertices(), Gl.GL_STATIC_DRAW); Gl.glVertexAttribPointer(0, 3, Gl.GL_FLOAT, Gl.GL_FALSE, 8 * sizeof(float), new IntPtr(0)); Gl.glEnableVertexAttribArray(0); Gl.glVertexAttribPointer(1, 2, Gl.GL_FLOAT, Gl.GL_FALSE, 8 * sizeof(float), new IntPtr(3 * sizeof(float))); Gl.glEnableVertexAttribArray(1); Gl.glVertexAttribPointer(2, 3, Gl.GL_FLOAT, Gl.GL_FALSE, 8 * sizeof(float), new IntPtr(5 * sizeof(float))); Gl.glEnableVertexAttribArray(2); int shininessLoc = Gl.glGetUniformLocation(shader, "material.shininess"); Gl.glUniform1f(shininessLoc, 32.0f); lighting.Setup(shader); transform.Setup(shader); camera.Setup(shader); projection.Setup(shader); Gl.glBindBuffer(Gl.GL_ELEMENT_ARRAY_BUFFER, ebo); Gl.glDrawElements(Gl.GL_TRIANGLES, Indices.Count, Gl.GL_UNSIGNED_INT, new IntPtr(0)); }
public void Render(Camera camera, Projection projection, Lighting lighting) { Renderable.Draw(Shader, ModelTransform, camera, projection, lighting); }
private void Scene_Load(object sender, EventArgs e) { GlInit(); CreateLanternShader(); CreateModelShader(); CreateProcTexturingShader(); camera = new Camera(new vec3(0, 0, 2)); projection = new Projection(); lighting = new Lighting(); CreateRoom(); // nanosuit var model = new Model(@"..\..\NanosuitModel\nanosuit.obj", "NanosuitModel"); mat4 modelMatrix = new mat4(1); modelMatrix = glm.scale(modelMatrix, new vec3(0.15f, 0.15f, 0.15f)); modelMatrix = glm.translate(modelMatrix, new vec3(-28, -6.9f, -18)); modelMatrix = glm.rotate(modelMatrix, glm.radians(45), new vec3(0, 1, 0)); Transform transform = new Transform() { ModelMatrix = modelMatrix }; renderObjects.Add(new RenderObject(model, transform, modelShader)); // girl /* * modelMatrix = new mat4(1); * modelMatrix = glm.scale(modelMatrix, new vec3(0.01f, 0.01f, 0.01f)); * modelMatrix = glm.translate(modelMatrix, new vec3(300, -100.9f, -45)); * modelMatrix = glm.rotate(modelMatrix, glm.radians(-80), new vec3(0, 1, 0)); * transform = new Transform() * { * ModelMatrix = modelMatrix * }; * model = new Model(@"..\..\Girl\10016_w_Myriam_30k.OBJ", "Girl"); * var texture = (uint)TextureUtil.LoadTexture(@"..\..\Girl\10016_w_Myriam_Body_D_2k.jpg"); * model.SetCustomTexture(texture); * renderObjects.Add(new RenderObject(model, transform, modelShader));*/ // car /* * modelMatrix = new mat4(1); * modelMatrix = glm.scale(modelMatrix, new vec3(0.06f, 0.06f, 0.06f)); * modelMatrix = glm.translate(modelMatrix, new vec3(-23, -16.1f, 18)); * modelMatrix = glm.rotate(modelMatrix, glm.radians(90), new vec3(0, 1, 0)); * transform = new Transform() * { * ModelMatrix = modelMatrix * }; * model = new Model(@"..\..\Car\L200-OBJ.obj", "Car"); * texture = (uint)TextureUtil.LoadTexture(@"..\..\Car\truck_color_clean.jpg"); * model.SetCustomTexture(texture); * renderObjects.Add(new RenderObject(model, transform, modelShader)); */ // Drone /* * modelMatrix = new mat4(1); * modelMatrix = glm.scale(modelMatrix, new vec3(0.6f, 0.6f, 0.6f)); * modelMatrix = glm.translate(modelMatrix, new vec3(0, -3f, 0f)); * modelMatrix = glm.rotate(modelMatrix, glm.radians(-45), new vec3(0, 1, 0)); * transform = new Transform() * { * ModelMatrix = modelMatrix * }; * model = new Model(@"..\..\Drone\Drone.obj", "Drone"); * texture = (uint)TextureUtil.LoadTexture(@"..\..\Drone\winter_cam.jpg"); * model.SetCustomTexture(texture); * renderObjects.Add(new RenderObject(model, transform, modelShader)); */ // House1 modelMatrix = new mat4(1); modelMatrix = glm.scale(modelMatrix, new vec3(0.01f, 0.01f, 0.01f)); modelMatrix = glm.translate(modelMatrix, new vec3(-4, -100, -4f)); modelMatrix = glm.rotate(modelMatrix, glm.radians(0), new vec3(0, 1, 0)); transform = new Transform() { ModelMatrix = modelMatrix }; model = new Model(@"..\..\House1\Barrack.obj", "House1"); model.SetCustomTexture(texture); renderObjects.Add(new RenderObject(model, transform, modelShader)); // Prison modelMatrix = new mat4(1); modelMatrix = glm.scale(modelMatrix, new vec3(0.006f, 0.006f, 0.006f)); modelMatrix = glm.translate(modelMatrix, new vec3(-1, 3f, -4f)); modelMatrix = glm.rotate(modelMatrix, glm.radians(0), new vec3(0, 1, 0)); transform = new Transform() { ModelMatrix = modelMatrix }; model = new Model(@"..\..\House2\OldHouse.obj", "House2"); model.SetCustomTexture(texture); renderObjects.Add(new RenderObject(model, transform, modelShader)); // lanterns CreateLantern(-3, 1.3f, -6.87f); CreateLantern(-6.87f, 1.3f, -4f, 90); CreateLantern(-6.87f, 1.3f, 4f, 90); CreateLantern(6.87f, 1.3f, -4f, -90); CreateLantern(6.87f, 1.3f, 4f, -90); //lighting.PointLights.Add(new Lighting.PointLight() { Position = new vec3(-3f, 1.3f, -6.80f) }); lighting.PointLights.Add(new Lighting.PointLight() { Position = new vec3(-6.8f, 1.3f, -4f) }); lighting.PointLights.Add(new Lighting.PointLight() { Position = new vec3(-6.8f, 1.3f, 4f) }); lighting.PointLights.Add(new Lighting.PointLight() { Position = new vec3(6.8f, 1.3f, -4f) }); lighting.PointLights.Add(new Lighting.PointLight() { Position = new vec3(6.8f, 1.3f, 4f) }); RenderTimer.Start(); }