public SurfaceManager(ShaderManager shaderMan) { // matrices this.ProjectionMatrix = new Matrix4Uniform("projectionMatrix"); this.ModelviewMatrix = new Matrix4Uniform("modelviewMatrix"); this.MeshTransform = new Matrix4Uniform("meshTransform"); // create shaders shaderMan.MakeShaderProgram("primitives"); shaderMan.MakeShaderProgram("uvcolor"); shaderMan.MakeShaderProgram("mesh"); // surfaces this.Primitives = new IndexedSurface <PrimitiveVertexData>(); this.Primitives.AddSettings(this.ProjectionMatrix, this.ModelviewMatrix); shaderMan["primitives"].UseOnSurface(this.Primitives); this.Text = new IndexedSurface <UVColorVertexData>(); this.Text.AddSettings(this.ProjectionMatrix, this.ModelviewMatrix, new TextureUniform("diffuseTexture", new Texture("data/fonts/inconsolata.png", true))); shaderMan["uvcolor"].UseOnSurface(this.Text); var obj = ObjFileMesh.FromFile("data/obj/teapot.obj"); var mesh = obj.ToMesh(); this.Mesh = mesh.ToIndexedSurface <MeshVertex>(); this.Mesh.AddSettings(this.ProjectionMatrix, this.ModelviewMatrix, this.MeshTransform); this.Mesh.ClearOnRender = false; shaderMan["mesh"].UseOnSurface(this.Mesh); }
public void Render() { if (this.Active) { Matrix4Uniform textProjection = Engine.UniformManager.GetUniform <Matrix4Uniform>("textProjection"); textProjection.Matrix = this.Camera.GetProjectionMatrix(); textProjection.Set(this.GetShader()); Vector3Uniform textColor = Engine.UniformManager.GetUniform <Vector3Uniform>("textColor"); textColor.Value = new Vector3(this.Color.R, this.Color.G, this.Color.B); textColor.Set(this.GetShader()); GL.ActiveTexture(TextureUnit.Texture0); this.vertexArray.Bind(); this.vertexBuffer.Bind(); GL.BufferData(BufferTarget.ArrayBuffer, sizeof(float) * 6 * 4, (IntPtr)null, BufferUsageHint.DynamicDraw); for (int i = 0; i < this.Text.Length; i++) { Font.Character character = this.Font.Characters[this.Text[i]]; GL.BindTexture(TextureTarget.Texture2D, character.TextureId); TextVertex[] subVertices = textVertices.SubArray(i * 6, 6); this.vertexBuffer.Bind(); this.vertexBuffer.BufferSubData((IntPtr)0, TextVertex.Size * subVertices.Length, subVertices); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.DrawArrays(PrimitiveType.Triangles, 0, 6); } } }
public Camera(int _width, int _height) { width = _width; height = _height; projectionMatrix = new Matrix4Uniform("projectionMatrix"); RefreshProjectionMatrix(); }
public MeshInstance(GameState game, Surface meshSurface, Matrix4Uniform transformUniform, Vector3 position, float scale) : base(game) { this.instance = new MeshSurfaceInstance(meshSurface, transformUniform) { Translation = position, Scale = scale, }; }
private void setModel(Matrix4Uniform modelMatrix, Vector2 position, float angle = 0f, float scale = 1f, float horizontalAngle = 0f, bool flipHorizontal = false) { modelMatrix.Matrix = Matrix4.CreateScale(scale) * Matrix4.CreateRotationZ(horizontalAngle) * Matrix4.CreateRotationY(-angle) * Matrix4.CreateTranslation(new Vector3(position.X, 0, position.Y)); if (flipHorizontal) { Matrix4 flipMatrix = Matrix4.Identity; flipMatrix.M22 = -1f; modelMatrix.Matrix = flipMatrix * modelMatrix.Matrix; } }
private void initMatrices() { this.gameProjection = new Matrix4Uniform("projectionMatrix"); this.gameModelview = new Matrix4Uniform("modelviewMatrix"); this.screenModelview = new Matrix4Uniform("modelviewMatrix"); this.makeGameProjectionMatrix(); this.makeGameModelviewMatrix(); this.makeScreenModelviewMatrix(); }
public void Render() { Matrix4Uniform cameraProjection = Engine.UniformManager.GetUniform <Matrix4Uniform>("cameraProjection"); cameraProjection.Matrix = Engine.RenderingPool.UICamera.GetProjectionMatrix(); cameraProjection.Set(this.GetShader()); this.vertexBuffer.Bind(); this.vertexArray.Bind(); this.vertexBuffer.BufferData(); this.vertexBuffer.Draw(); }
public void UpdateVertexBuffer(Mesh mesh) { Matrix4 transformMatrix = this.gameObject.Transform.GetLocalToWorldMatrix(); if (mesh.LocalMatrix != Matrix4.Identity) { transformMatrix = mesh.LocalMatrix * transformMatrix; } Matrix4Uniform modelMatrix = Engine.UniformManager.GetUniform <Matrix4Uniform>("modelMatrix"); modelMatrix.Matrix = transformMatrix; modelMatrix.Set(this.GetShader()); }
public GameSurfaceManager( ShaderManager shaders, Matrix4Uniform view, Matrix4Uniform projection) { var hex = new IndexedSurface <UVColorVertexData>() .WithShader(shaders["Deferred/gSprite"]) .AndSettings( view, projection, new TextureUniform("diffuseTexture", new Texture(sprite("hex-diffuse.png")), TextureUnit.Texture0), new TextureUniform("normalTexture", new Texture(sprite("hex-normal.png")), TextureUnit.Texture1) ); Surfaces.Add("hex", hex); SurfaceList = Surfaces.Values.ToList <Surface>().AsReadOnly(); }
public void Render() { Matrix4Uniform cameraProjection = Engine.UniformManager.GetUniform <Matrix4Uniform>("cameraProjection"); cameraProjection.Matrix = (this.Camera != null) ? this.Camera.GetProjectionMatrix() : Matrix4.Identity; cameraProjection.Set(this.GetShader()); this.Sprite.Texture.Bind(); Sampler2DUniform spriteUniform = Engine.UniformManager.GetUniform <Sampler2DUniform>("spriteTexture"); spriteUniform.SamplerId = 0; spriteUniform.Set(this.GetShader()); this.vertexArray.Bind(); this.vertexBuffer.Bind(); this.vertexBuffer.BufferData(); this.vertexBuffer.Draw(); }
public Ghost(Vector2 startingPos, Level level, int listIndex) : base(startingPos, Vector2.Zero, level, level.Graphics.GhostSurface) { this.State = States.Alive; this.ListIndex = listIndex; this.colorMatrix = this.Level.Graphics.Red; if (listIndex == 1) { this.colorMatrix = this.Level.Graphics.Pink; } if (listIndex == 2) { this.colorMatrix = this.Level.Graphics.Cyan; } if (listIndex == 3) { this.colorMatrix = this.Level.Graphics.Orange; } this.colorMatrixUniform = this.Level.Graphics.GhostColorUniform; }
public void RenderPool() { if (this.loaded && this.renderCamera != null) { Matrix4 modelview = this.renderCamera.GetViewMatrix(); Matrix4Uniform projectionMatrixUniform = Engine.UniformManager.GetUniform <Matrix4Uniform>("projectionMatrix"); Vector3Uniform cameraPositionUniform = Engine.UniformManager.GetUniform <Vector3Uniform>("cameraPosition"); projectionMatrixUniform.Matrix = Matrix4.Mult(modelview, this.projectionMatrix); cameraPositionUniform.Value = this.renderCamera.GetGameObject().Transform.Position; // Render the pool list foreach (var renderer in this.rendererPool.ToList()) { // Activate shader program and set uniforms ShaderProgram rendererShader = renderer.GetShader(); rendererShader.Use(); projectionMatrixUniform.Set(rendererShader); cameraPositionUniform.Set(rendererShader); Engine.Game.CurrentScene.SetShadingUniforms(rendererShader); renderer.Render(); } } }
public MeshSurfaceInstance(Surface surface, Matrix4Uniform transformUniform) { this.surface = surface; this.transformUniform = transformUniform; }
public Graphics(Audio audio) { // Save a pointer to the audio class, for convenience this.Audio = audio; // Load Shader Programs. ShaderProgram uvShader = new ShaderProgram(VertexShader.FromFile("data/shaders/uvcolor_vs.glsl"), FragmentShader.FromFile("data/shaders/uvcolor_fs.glsl")); ShaderProgram objShader = new ShaderProgram(VertexShader.FromFile("data/shaders/obj_vs.glsl"), FragmentShader.FromFile("data/shaders/obj_fs.glsl")); ShaderProgram objShaderHax = new ShaderProgram(VertexShader.FromFile("data/shaders/obj_vs_hax.glsl"), FragmentShader.FromFile("data/shaders/obj_fs.glsl")); this.wallShader = objShader; // Create matrix uniforms used for rendering. this.view2D = new Matrix4Uniform("viewMatrix"); this.projection2D = new Matrix4Uniform("projectionMatrix"); this.view3D = new Matrix4Uniform("viewMatrix"); this.projection3D = new Matrix4Uniform("projectionMatrix"); this.createColorMatrices(); this.WallColorUniform = new Matrix4Uniform("mixColor", this.Blue); this.lightDirection = new Vector3(-1f, -0.5f, -0.4f); this.lightDirection.Normalize(); // Create the surfaces #region Font Surface Texture t = new Texture("data/fonts/freshman.png"); this.FontSurface = new IndexedSurface <UVColorVertexData>(); this.FontSurface.AddSettings( this.view2D, this.projection2D, new TextureUniform("diffuseTexture", t), SurfaceBlendSetting.Alpha, SurfaceDepthMaskSetting.DontMask ); this.FontSurface.SetShaderProgram(uvShader); // the following line loads the json file this.FontGeometry = new FontGeometry(this.FontSurface, amulware.Graphics.Font.FromJsonFile("data/fonts/freshman_monospaced_numbers.json")); // this.FontGeometry.SizeCoefficient = new Vector2(1, -1); // FLIP IT this.MenuFontGeometry = this.FontGeometry; #endregion #region 3D font Surface t = new Texture("data/fonts/freshman.png"); this.FontSurface3D = new IndexedSurface <UVColorVertexData>(); this.FontSurface3D.AddSettings( this.view3D, this.projection3D, new TextureUniform("diffuseTexture", t), SurfaceBlendSetting.Alpha, SurfaceDepthMaskSetting.DontMask ); this.FontSurface3D.SetShaderProgram(uvShader); // the following line loads the json file this.FontGeometry3D = new FontGeometry(this.FontSurface3D, amulware.Graphics.Font.FromJsonFile("data/fonts/freshman_monospaced_numbers.json")); this.FontGeometry3D.SizeCoefficient = new Vector2(1, -1); // FLIP IT #endregion #region PacmanSurface MeshData m = new MeshData("data/models/Pacman.obj"); t = new Texture("data/sprites/PacmanTexture.png"); this.pacManModel = new Matrix4Uniform("modelMatrix", Matrix4.Identity); this.PacmanSurface = m.ToIndexedSurface <NormalUVColorVertexData>(NormalUVColorVertexData.FromMesh); this.PacmanSurface.AddSettings( this.pacManModel, this.view3D, this.projection3D, new TextureUniform("diffuseTexture", t), new Vector3Uniform("lightDirection", this.lightDirection), new FloatUniform("ambientIntensity", 0.3f), new FloatUniform("diffuseIntensity", 0.8f), new Matrix4Uniform("mixColor", Matrix4.Identity) ); this.PacmanSurface.SetShaderProgram(objShader); #endregion #region PacmanMouthSurface (must be placed directly after the 'PacmanSurface') m = new MeshData("data/models/Pacman_mouth.obj"); this.PacManMouthTopTexture = new Texture("data/sprites/OrbTexture.png"); // 't' is the same texture as pacman, 't-top' is a plain white pixel (as used for the orbs). this.PacManMouthBottomTexture = t; this.PacManMouthTextureUniform = new TextureUniform("diffuseTexture", t); this.PacManMouthColorUniform = new Matrix4Uniform("mixColor", Matrix4.Identity); this.PacmanMouthSurface = m.ToIndexedSurface <NormalUVColorVertexData>(NormalUVColorVertexData.FromMesh); this.PacmanMouthSurface.AddSettings( this.pacManModel, this.view3D, this.projection3D, this.PacManMouthTextureUniform, new Vector3Uniform("lightDirection", this.lightDirection), new FloatUniform("ambientIntensity", 0.3f), new FloatUniform("diffuseIntensity", 0.8f), this.PacManMouthColorUniform ); this.PacmanMouthSurface.SetShaderProgram(objShaderHax); #endregion #region GhostSurface m = new MeshData("data/models/Ghost.obj"); t = new Texture("data/sprites/GhostTexture.png"); this.GhostColorUniform = new Matrix4Uniform("mixColor", this.Red); this.ghostModel = new Matrix4Uniform("modelMatrix", Matrix4.Identity); this.GhostSurface = m.ToIndexedSurface <NormalUVColorVertexData>(NormalUVColorVertexData.FromMesh); this.GhostSurface.AddSettings( this.ghostModel, this.view3D, this.projection3D, new TextureUniform("diffuseTexture", t), new Vector3Uniform("lightDirection", this.lightDirection), new FloatUniform("ambientIntensity", 0.3f), new FloatUniform("diffuseIntensity", 0.8f), this.GhostColorUniform ); this.GhostSurface.SetShaderProgram(objShader); #endregion #region OrbSurface m = new MeshData("data/models/Orb.obj"); t = new Texture("data/sprites/OrbTexture.png"); this.orbModel = new Matrix4Uniform("modelMatrix", Matrix4.Identity); this.OrbSurface = m.ToIndexedSurface <NormalUVColorVertexData>(NormalUVColorVertexData.FromMesh); this.OrbSurface.AddSettings( this.orbModel, this.view3D, this.projection3D, new TextureUniform("diffuseTexture", t), new Vector3Uniform("lightDirection", this.lightDirection), new FloatUniform("ambientIntensity", 0.3f), new FloatUniform("diffuseIntensity", 0.8f), new Matrix4Uniform("mixColor", Matrix4.Identity) ); this.OrbSurface.SetShaderProgram(objShader); #endregion }
public void OnLoad() { // create and fill a vertex buffer this.vertexBuffer = new VertexBuffer <ColouredVertex>(ColouredVertex.Size); this.vertexBuffer.AddVertex(new ColouredVertex(new Vector3(-1, -1, -1.5f), Color4.Lime)); this.vertexBuffer.AddVertex(new ColouredVertex(new Vector3(1, 1, -1.5f), Color4.Red)); this.vertexBuffer.AddVertex(new ColouredVertex(new Vector3(1, -1, -1.5f), Color4.Blue)); // load shaders #region Shaders var vertexShader = new Shader(ShaderType.VertexShader, @"#version 130 // a projection transformation to apply to the vertex' position uniform mat4 projectionMatrix; // attributes of our vertex in vec3 vPosition; in vec4 vColor; out vec4 fColor; // must match name in fragment shader void main() { // gl_Position is a special variable of OpenGL that must be set gl_Position = projectionMatrix * vec4(vPosition, 1.0); fColor = vColor; }" ); var fragmentShader = new Shader(ShaderType.FragmentShader, @"#version 130 in vec4 fColor; // must match name in vertex shader out vec4 fragColor; // first out variable is automatically written to the screen void main() { fragColor = fColor; }" ); #endregion // link shaders into shader program this.shaderProgram = new ShaderProgram(vertexShader, fragmentShader); // create vertex array to specify vertex layout this.vertexArray = new VertexArray <ColouredVertex>( this.vertexBuffer, shaderProgram, new VertexAttribute("vPosition", 3, VertexAttribPointerType.Float, ColouredVertex.Size, 0), new VertexAttribute("vColor", 4, VertexAttribPointerType.Float, ColouredVertex.Size, 12) ); // create projection matrix uniform this.projectionMatrix = new Matrix4Uniform("projectionMatrix"); this.projectionMatrix.Matrix = Matrix4.CreatePerspectiveFieldOfView( MathHelper.PiOver2, 16f / 9, 0.1f, 100f); Player = new Player(); Player.OutPutPlayerValuesToDebugger(); //This is where you load all of your content //Images, Models, Fonts, Sounds. }
public static void Main(string[] args) { using (var win = new GameWindow(1280, 720, GraphicsMode.Default, "OpenTK Intro", GameWindowFlags.Default, DisplayDevice.Default, // ask for an OpenGL 3.0 forward compatible context 3, 0, GraphicsContextFlags.ForwardCompatible)) { VertexBuffer <ColouredVertex> vertexBuffer = null; ShaderProgram shaderProgram = null; VertexArray <ColouredVertex> vertexArray = null; Matrix4Uniform projectionMatrix = null; win.Load += (s, e) => { var vertexShader = new Shader(ShaderType.VertexShader, File.ReadAllText(@"../../VertexShaders/vertex-shader.vs")); var fragmentShader = new Shader(ShaderType.FragmentShader, File.ReadAllText(@"../../FragmentShaders/fragment-shader.fs")); // link shaders into shader program shaderProgram = new ShaderProgram(vertexShader, fragmentShader); // create projection matrix uniform projectionMatrix = new Matrix4Uniform("projectionMatrix") { Matrix = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver2, 16f / 9, 0.1f, 100f) }; }; win.Unload += (s, e) => { /* dispose global GL resources here */ }; win.RenderFrame += (s, e) => { GL.ClearColor(128, 0, 1, 256); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); //http://genericgamedev.com/tutorials/opengl-in-csharp-an-object-oriented-introduction-to-opentk/4/ // activate shader program and set uniforms shaderProgram.Use(); projectionMatrix.Set(shaderProgram); // bind vertex buffer and array objects vertexBuffer.Bind(); vertexArray.Bind(); // upload vertices to GPU and draw them vertexBuffer.BufferData(); vertexBuffer.Draw(); // reset state for potential further draw calls (optional, but good practice) GL.BindVertexArray(0); GL.BindBuffer(BufferTarget.ArrayBuffer, 0); GL.UseProgram(0); // swap backbuffer win.SwapBuffers(); }; win.Resize += (s, e) => { GL.Viewport(0, 0, win.Width, win.Height); }; long updateCount = 0; var random = new Random(); var tetrahedron = new RegularTetrahedron(); win.UpdateFrame += (s, e) => { var keyboardState = win.Keyboard.GetState(); // create and fill a vertex buffer vertexBuffer = new VertexBuffer <ColouredVertex>(ColouredVertex.Size); //foreach (var i in Enumerable.Range(0, random.Next(100, 1000))) //{ // vertexBuffer.AddVertex(new ColouredVertex(new Vector3(-1f * (0.01f * updateCount), -1, -i), Color4.Lime)); // vertexBuffer.AddVertex(new ColouredVertex(new Vector3(1, 1, -i), Color4.Red)); // vertexBuffer.AddVertex(new ColouredVertex(new Vector3(1f * (0.01f * updateCount), -1, -i), Color4.Blue)); //} tetrahedron.Draw(vertexBuffer); // create vertex array to specify vertex layout vertexArray = new VertexArray <ColouredVertex>( vertexBuffer, shaderProgram, new VertexAttribute("vPosition", 3, VertexAttribPointerType.Float, ColouredVertex.Size, 0), new VertexAttribute("vColor", 4, VertexAttribPointerType.Float, ColouredVertex.Size, 12) ); ++updateCount; }; win.Run(); } }
public GraphicsManager() { // Load Shader Programs. ShaderProgram simpleShader = new ShaderProgram(VertexShader.FromFile("data/shaders/simple_vs.glsl"), FragmentShader.FromFile("data/shaders/simple_fs.glsl")); ShaderProgram uvShader = new ShaderProgram(VertexShader.FromFile("data/shaders/uvcolor_vs.glsl"), FragmentShader.FromFile("data/shaders/uvcolor_fs.glsl")); // Create matrix uniforms used for rendering. this.modelview = new Matrix4Uniform("modelviewMatrix"); this.projection = new Matrix4Uniform("projectionMatrix"); this.hudMatrix = new Matrix4Uniform("modelviewMatrix"); // Font Font quartz = Font.FromJsonFile("data/fonts/Quartz.json"); // Create the surfaces #region Background surface Texture t = new Texture("data/graphics/omega-nebula.jpg"); this.BackgroundSurface = new IndexedSurface <UVColorVertexData>(); this.BackgroundSurface.AddSettings( this.hudMatrix, this.projection, new TextureUniform("diffusetexture", t), SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.BackgroundSurface.SetShaderProgram(uvShader); this.BackgroundGeometry = new Sprite2DGeometry(this.BackgroundSurface); this.BackgroundGeometry.Size = new Vector2(1280, -720); this.BackgroundGeometry.Color.A = (byte)100; #endregion #region Planet Surface t = new Texture("data/graphics/planet.png"); this.PlanetSurface = new IndexedSurface <UVColorVertexData>(); this.PlanetSurface.AddSettings( this.modelview, this.projection, new TextureUniform("diffusetexture", t), SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.PlanetSurface.SetShaderProgram(uvShader); this.PlanetGeometry = new Sprite2DGeometry(this.PlanetSurface); this.PlanetGeometry.Size = new Vector2(2, 2); #endregion #region Asteroid Surface t = new Texture("data/graphics/asteroid.png"); this.AsteroidSurface = new IndexedSurface <UVColorVertexData>(); this.AsteroidSurface.AddSettings( this.modelview, this.projection, new TextureUniform("diffusetexture", t), SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.AsteroidSurface.SetShaderProgram(uvShader); this.AsteroidGeometry = new Sprite2DGeometry(this.AsteroidSurface); this.AsteroidGeometry.Size = new Vector2(2, 2); #endregion #region Space Core Surface t = new Texture("data/graphics/spacecore.png"); this.SpaceCoreSurface = new IndexedSurface <UVColorVertexData>(); this.SpaceCoreSurface.AddSettings( this.modelview, this.projection, new TextureUniform("diffusetexture", t), SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.SpaceCoreSurface.SetShaderProgram(uvShader); this.SpaceCoreGeometry = new Sprite2DGeometry(this.SpaceCoreSurface); this.SpaceCoreGeometry.Size = new Vector2(30, 30); #endregion #region Jumper Surface t = new Texture("data/graphics/jumper.png"); this.JumperSurface = new IndexedSurface <UVColorVertexData>(); this.JumperSurface.AddSettings( this.modelview, this.projection, new TextureUniform("diffusetexture", t), SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.JumperSurface.SetShaderProgram(uvShader); this.JumperGeometry = new Sprite2DGeometry(this.JumperSurface); this.JumperGeometry.Size = Jumper.Size; #endregion #region Trail Surface this.TrailSurface = new IndexedSurface <PrimitiveVertexData>(); this.TrailSurface.AddSettings( this.modelview, this.projection, SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.TrailSurface.SetShaderProgram(simpleShader); this.TrailGeometry = new PrimitiveGeometry(this.TrailSurface); this.TrailGeometry.Color = Color.Cyan; #endregion #region Score Surface t = new Texture("data/fonts/Quartz.png"); this.ScoreSurface = new IndexedSurface <UVColorVertexData>(); this.ScoreSurface.AddSettings( this.hudMatrix, this.projection, new TextureUniform("diffusetexture", t), SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.ScoreSurface.SetShaderProgram(uvShader); this.ScoreGeometry = new FontGeometry(this.ScoreSurface, quartz); this.ScoreGeometry.Height = 32; this.ScoreGeometry.SizeCoefficient = new Vector2(1, -1); #endregion #region Overlay surface this.OverlaySurface = new IndexedSurface <PrimitiveVertexData>(); this.OverlaySurface.AddSettings( this.hudMatrix, this.projection, SurfaceDepthMaskSetting.DontMask, SurfaceBlendSetting.Alpha ); this.OverlaySurface.SetShaderProgram(simpleShader); this.OverlayGeometry = new PrimitiveGeometry(this.OverlaySurface); this.OverlayGeometry.Color = Color.Cyan; this.OverlayGeometry.Color.A = (byte)150; #endregion }