Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using OpenTK;
- using OpenTK.Graphics;
- using OpenTK.Graphics.OpenGL;
- using System.Drawing;
- using OpenTK.Input;
- namespace OpenTKProj
- {
- class RenderingEngine
- {
- uint uiVAO;
- VertexBufferObject vbo;
- Static_Geometry geometryObjects;
- public float fRotationAngleCube = 0.0f, fRotationAnglePyramid = 0.0f;
- public float fCubeRotationSpeed = 0.0f, fPyramidRotationSpeed = 0.0f;
- const float PIover180 = 3.1415f / 180.0f;
- Shader vShader;
- Shader fShader;
- Shader fGroundShader;
- ShaderProgram spScene;
- //ShaderProgram spGround;
- Texture tGold, tSnow;
- public RenderingEngine()
- {
- vbo = new VertexBufferObject();
- geometryObjects = new Static_Geometry();
- }
- public void initScene()
- {
- GL.ClearColor(Color.CornflowerBlue);
- vbo.createVBO();
- GL.GenVertexArrays(1, out uiVAO);
- GL.BindVertexArray(uiVAO);
- vbo.bindVBO();
- for(int i = 0; i < 36;i++)
- {
- vbo.addData(geometryObjects.vCubeVertices[i]);
- vbo.addData(geometryObjects.vCubeTexCoords[i % 6]);
- }
- for(int i = 0; i < 12; i++)
- {
- vbo.addData(geometryObjects.vPyramidVertices[i]);
- vbo.addData(geometryObjects.vPyramidTexCoords[i % 3]);
- }
- for(int i = 0; i < 6; i++)
- {
- vbo.addData(geometryObjects.vGround[i]);
- geometryObjects.vCubeTexCoords[i] *= 5.0f;
- vbo.addData(geometryObjects.vCubeTexCoords[i % 6]);
- }
- vbo.uploadDataToGPU();
- GL.EnableVertexAttribArray(0);
- GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 5 * sizeof(float), 0);
- GL.EnableVertexAttribArray(1);
- GL.VertexAttribPointer(1, 2, VertexAttribPointerType.Float, false, 5 * sizeof(float), 3 * sizeof(float));
- vShader = new Shader();
- fShader = new Shader();
- fGroundShader = new Shader();
- vShader.LoadShaderFromFile("Assets\\Shaders\\vTexShader.sh", ShaderType.VertexShader);
- fShader.LoadShaderFromFile("Assets\\Shaders\\fTexShader.sh", ShaderType.FragmentShader);
- //fGroundShader.LoadShaderFromFile("Assets\\Shaders\\fShaderGround.sh", ShaderType.FragmentShader);
- spScene = new ShaderProgram();
- spScene.createProgram();
- spScene.addShader(ref vShader);
- spScene.addShader(ref fShader);
- //spScene.addShader(ref fGroundShader);
- spScene.linkProgram();
- spScene.useProgram();
- //spGround = new ShaderProgram();
- //spGround.createProgram();
- //spGround.addShader(ref vShader);
- ////spGround.addShader(ref fShader);
- //spGround.addShader(ref fGroundShader);
- GL.Enable(EnableCap.DepthTest);
- GL.ClearDepth(1f);
- tGold = new Texture();
- tSnow = new Texture();
- tGold.loadTexture2D("Assets\\Textures\\golddiag.jpg");
- tGold.setFiltering(TextureMagFilter.Linear, TextureMinFilter.LinearMipmapLinear);
- tSnow.loadTexture2D("Assets\\Textures\\snow.jpg");
- tSnow.setFiltering(TextureMagFilter.Linear, TextureMinFilter.LinearMipmapLinear);
- GL.Enable(EnableCap.Texture2D);
- }
- public static float DegreesToRadians(float degrees)
- {
- const float degToRad = (float)System.Math.PI / 180.0f;
- return degrees * degToRad;
- }
- public static float RadiansToDegrees(float radians)
- {
- const float radToDeg = 180.0f / (float)System.Math.PI;
- return radians * radToDeg;
- }
- public Matrix4 GetViewMatrix()
- {
- Vector3 lookat = new Vector3();
- Vector3 Position = Vector3.Zero + new Vector3(0f, 0f, 3f);
- Vector3 Orientation = new Vector3((float)Math.PI, 0f, 0f);
- lookat.X = (float)(Math.Sin((float)Orientation.X) * Math.Cos((float)Orientation.Y));
- lookat.Y = (float)Math.Sin((float)Orientation.Y);
- lookat.Z = (float)(Math.Cos((float)Orientation.X) * Math.Cos((float)Orientation.Y));
- return Matrix4.LookAt(Position, Position + lookat, Vector3.UnitY);
- }
- public void RenderScene(PointF size, float fInterval, PolygonMode mode = PolygonMode.Fill)
- {
- GL.Clear(ClearBufferMask.DepthBufferBit | ClearBufferMask.ColorBufferBit);
- int iModelViewLoc = GL.GetUniformLocation(spScene.ProgramID, "modelViewMatrix");
- int iProjectionLoc = GL.GetUniformLocation(spScene.ProgramID, "projectionMatrix");
- int iSamplerLoc = GL.GetUniformLocation(spScene.ProgramID, "gSampler");
- Matrix4 ProjectionMatrix = Matrix4.CreatePerspectiveFieldOfView((float)(System.Math.PI / 2), (float)(size.X / size.Y), 0.01f, 1000f);
- GL.UniformMatrix4(iProjectionLoc, false, ref ProjectionMatrix);
- Matrix4 ModelView = GetViewMatrix();
- Matrix4 CurrentObjectMatrix = new Matrix4();
- GL.BindVertexArray(uiVAO);
- GL.Uniform1(iSamplerLoc, 0);
- tGold.bindTexture();
- CurrentObjectMatrix = Matrix4.CreateScale(1f) * Matrix4.CreateRotationX(fRotationAngleCube) * Matrix4.CreateTranslation(-1f, 0f, 0f) * ModelView;
- //CurrentObjectMatrix = ModelView;
- GL.UniformMatrix4(iModelViewLoc, false, ref CurrentObjectMatrix);
- GL.PolygonMode(MaterialFace.FrontAndBack, mode);
- GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
- CurrentObjectMatrix = Matrix4.CreateScale(1f) * Matrix4.CreateRotationY(fRotationAnglePyramid) * Matrix4.CreateTranslation(1f, 0f, 0f) * ModelView;
- GL.UniformMatrix4(iModelViewLoc, false, ref CurrentObjectMatrix);
- GL.DrawArrays(PrimitiveType.Triangles, 36, 12);
- tSnow.bindTexture();
- //GL.UniformMatrix4(iModelViewLoc, false, ref ModelView);
- //GL.DrawArrays(PrimitiveType.Triangles, 48, 6);
- KeyboardState kb = Keyboard.GetState();
- if(kb.IsKeyDown(Key.W))
- {
- fCubeRotationSpeed -= 60f * fInterval;
- }
- if(kb.IsKeyDown(Key.S))
- {
- fCubeRotationSpeed += 60f * fInterval;
- }
- if (kb.IsKeyDown(Key.A))
- {
- fPyramidRotationSpeed -= 60f * fInterval;
- }
- if (kb.IsKeyDown(Key.D))
- {
- fPyramidRotationSpeed += 60f * fInterval;
- }
- fRotationAngleCube += fCubeRotationSpeed / 100000;
- fRotationAnglePyramid += fPyramidRotationSpeed / 100000;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment