Guest User

renderingEngine.cs

a guest
Apr 10th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.08 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using OpenTK;
  7. using OpenTK.Graphics;
  8. using OpenTK.Graphics.OpenGL;
  9. using System.Drawing;
  10. using OpenTK.Input;
  11.  
  12. namespace OpenTKProj
  13. {
  14.    
  15.  
  16.     class RenderingEngine
  17.     {
  18.         uint uiVAO;
  19.         VertexBufferObject vbo;
  20.         Static_Geometry geometryObjects;
  21.  
  22.         public float fRotationAngleCube = 0.0f, fRotationAnglePyramid = 0.0f;
  23.         public float fCubeRotationSpeed = 0.0f, fPyramidRotationSpeed = 0.0f;
  24.         const float PIover180 = 3.1415f / 180.0f;
  25.  
  26.         Shader vShader;
  27.         Shader fShader;
  28.         Shader fGroundShader;
  29.         ShaderProgram spScene;
  30.         //ShaderProgram spGround;
  31.  
  32.         Texture tGold, tSnow;
  33.  
  34.         public RenderingEngine()
  35.         {
  36.             vbo = new VertexBufferObject();
  37.             geometryObjects = new Static_Geometry();
  38.         }
  39.  
  40.         public void initScene()
  41.         {
  42.             GL.ClearColor(Color.CornflowerBlue);
  43.             vbo.createVBO();
  44.             GL.GenVertexArrays(1, out uiVAO);
  45.             GL.BindVertexArray(uiVAO);
  46.  
  47.             vbo.bindVBO();
  48.  
  49.             for(int i = 0; i < 36;i++)
  50.             {
  51.                 vbo.addData(geometryObjects.vCubeVertices[i]);
  52.                 vbo.addData(geometryObjects.vCubeTexCoords[i % 6]);
  53.             }
  54.  
  55.             for(int i = 0; i < 12; i++)
  56.             {
  57.                 vbo.addData(geometryObjects.vPyramidVertices[i]);
  58.                 vbo.addData(geometryObjects.vPyramidTexCoords[i % 3]);
  59.             }
  60.  
  61.             for(int i = 0; i < 6; i++)
  62.             {
  63.                 vbo.addData(geometryObjects.vGround[i]);
  64.                 geometryObjects.vCubeTexCoords[i] *= 5.0f;
  65.                 vbo.addData(geometryObjects.vCubeTexCoords[i % 6]);
  66.             }
  67.  
  68.             vbo.uploadDataToGPU();
  69.  
  70.             GL.EnableVertexAttribArray(0);
  71.             GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
  72.  
  73.             GL.EnableVertexAttribArray(1);
  74.             GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
  75.  
  76.             vShader = new Shader();
  77.             fShader = new Shader();
  78.             fGroundShader = new Shader();
  79.  
  80.             vShader.LoadShaderFromFile("Assets\\Shaders\\vTexShader.sh", ShaderType.VertexShader);
  81.             fShader.LoadShaderFromFile("Assets\\Shaders\\fTexShader.sh", ShaderType.FragmentShader);
  82.             //fGroundShader.LoadShaderFromFile("Assets\\Shaders\\fShaderGround.sh", ShaderType.FragmentShader);
  83.  
  84.             spScene = new ShaderProgram();
  85.             spScene.createProgram();
  86.             spScene.addShader(ref vShader);
  87.             spScene.addShader(ref fShader);
  88.             //spScene.addShader(ref fGroundShader);
  89.             spScene.linkProgram();
  90.             spScene.useProgram();
  91.  
  92.             //spGround = new ShaderProgram();
  93.             //spGround.createProgram();
  94.             //spGround.addShader(ref vShader);
  95.             ////spGround.addShader(ref fShader);
  96.             //spGround.addShader(ref fGroundShader);
  97.  
  98.             GL.Enable(EnableCap.DepthTest);
  99.             GL.ClearDepth(1f);
  100.  
  101.             tGold = new Texture();
  102.             tSnow = new Texture();
  103.  
  104.             tGold.loadTexture2D("Assets\\Textures\\golddiag.jpg");
  105.             tGold.setFiltering(TextureMagFilter.Linear, TextureMinFilter.LinearMipmapLinear);
  106.  
  107.             tSnow.loadTexture2D("Assets\\Textures\\snow.jpg");
  108.             tSnow.setFiltering(TextureMagFilter.Linear, TextureMinFilter.LinearMipmapLinear);
  109.  
  110.             GL.Enable(EnableCap.Texture2D);
  111.  
  112.         }
  113.  
  114.         public static float DegreesToRadians(float degrees)
  115.         {
  116.             const float degToRad = (float)System.Math.PI / 180.0f;
  117.             return degrees * degToRad;
  118.         }
  119.  
  120.         public static float RadiansToDegrees(float radians)
  121.         {
  122.             const float radToDeg = 180.0f / (float)System.Math.PI;
  123.             return radians * radToDeg;
  124.         }
  125.  
  126.         public Matrix4 GetViewMatrix()
  127.         {
  128.             Vector3 lookat = new Vector3();
  129.             Vector3 Position = Vector3.Zero + new Vector3(0f, 0f, 3f);
  130.             Vector3 Orientation = new Vector3((float)Math.PI, 0f, 0f);
  131.  
  132.             lookat.X = (float)(Math.Sin((float)Orientation.X) * Math.Cos((float)Orientation.Y));
  133.             lookat.Y = (float)Math.Sin((float)Orientation.Y);
  134.             lookat.Z = (float)(Math.Cos((float)Orientation.X) * Math.Cos((float)Orientation.Y));
  135.  
  136.             return Matrix4.LookAt(Position, Position + lookat, Vector3.UnitY);
  137.         }
  138.  
  139.         public void RenderScene(PointF size, float fInterval, PolygonMode mode = PolygonMode.Fill)
  140.         {
  141.  
  142.             GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit);
  143.  
  144.             int iModelViewLoc = GL.GetUniformLocation(spScene.ProgramID, "modelViewMatrix");
  145.             int iProjectionLoc = GL.GetUniformLocation(spScene.ProgramID, "projectionMatrix");
  146.             int iSamplerLoc = GL.GetUniformLocation(spScene.ProgramID, "gSampler");
  147.  
  148.             Matrix4 ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)(System.Math.PI / 2), (float)(size.X / size.Y), 0.01f, 1000f);
  149.             GL.UniformMatrix4(iProjectionLoc, false, ref ProjectionMatrix);
  150.  
  151.             Matrix4 ModelView = GetViewMatrix();
  152.             Matrix4 CurrentObjectMatrix = new Matrix4();
  153.  
  154.             GL.BindVertexArray(uiVAO);
  155.             GL.Uniform1(iSamplerLoc, 0);
  156.             tGold.bindTexture();
  157.  
  158.             CurrentObjectMatrix = Matrix4.CreateScale(1f) * Matrix4.CreateRotationX(fRotationAngleCube) * Matrix4.CreateTranslation(-1f, 0f, 0f) * ModelView;
  159.             //CurrentObjectMatrix = ModelView;
  160.             GL.UniformMatrix4(iModelViewLoc, false, ref CurrentObjectMatrix);
  161.             GL.PolygonMode(MaterialFace.FrontAndBack, mode);
  162.             GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
  163.  
  164.             CurrentObjectMatrix = Matrix4.CreateScale(1f) * Matrix4.CreateRotationY(fRotationAnglePyramid) * Matrix4.CreateTranslation(1f, 0f, 0f) * ModelView;
  165.  
  166.             GL.UniformMatrix4(iModelViewLoc, false, ref CurrentObjectMatrix);
  167.             GL.DrawArrays(PrimitiveType.Triangles, 36, 12);
  168.  
  169.             tSnow.bindTexture();
  170.             //GL.UniformMatrix4(iModelViewLoc, false, ref ModelView);
  171.             //GL.DrawArrays(PrimitiveType.Triangles, 48, 6);
  172.  
  173.             KeyboardState kb = Keyboard.GetState();
  174.  
  175.             if(kb.IsKeyDown(Key.W))
  176.             {
  177.                 fCubeRotationSpeed -= 60f * fInterval;
  178.             }
  179.             if(kb.IsKeyDown(Key.S))
  180.             {
  181.                 fCubeRotationSpeed += 60f * fInterval;
  182.             }
  183.             if (kb.IsKeyDown(Key.A))
  184.             {
  185.                 fPyramidRotationSpeed -= 60f * fInterval;
  186.             }
  187.             if (kb.IsKeyDown(Key.D))
  188.             {
  189.                 fPyramidRotationSpeed += 60f * fInterval;
  190.             }
  191.  
  192.             fRotationAngleCube += fCubeRotationSpeed / 100000;
  193.             fRotationAnglePyramid += fPyramidRotationSpeed / 100000;
  194.  
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment