using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Threading; using System.Threading.Tasks; using OpenTK; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using OpenTK.Input; namespace Brickon { public class Program : GameWindow { //<3 List vecbuffer; //Consider this more or less a pointer to your model for now private int vao; //Consider this a pointer to the buffer responsible for vertex positions private int vbov; //Consider this a pointer to the buffer responsible for vertex colors private int vbc; private int vbt; public static Player player = new Player();//new Vector3(1, 1, 1); List blocks = new List(); //private Shader shader; //should use debrecated stuff beacuse its easier keyboard doesnt work so going to rite that letter b string vertexShaderSource = @" #version 330 core layout(location = 0) in vec3 pos; layout(location = 1) in vec3 col; layout(location = 2) in vec2 tex; //we have to pass the vertex color to the fragment so out vec3 vertexColor; out vec2 texCoords; uniform mat4 view; uniform mat4 model; uniform mat4 projection; out vec3 frag_Position; void main(void) { vertexColor = col; frag_Position = vec3(model * vec4(pos, 1.0f)); gl_Position = projection* view*model *vec4(pos, 1.0) ;//* view * model texCoords = tex; //gl_Position = vec4(pos, 1.0)*model*view*projection; } "; string td = @" #version 330 core layout(location = 0) in vec3 pos; layout(location = 1) in vec3 col; layout(location = 2) in vec2 tex; //we have to pass the vertex color to the fragment so out vec3 vertexColor; out vec2 texCoords; uniform mat4 view; uniform mat4 model; uniform mat4 projection; out vec3 frag_Position; void main(void) { vertexColor = col; frag_Position=vec3(model * vec4(pos, 1.0f)); gl_Position=vec4(pos, 1.0) ;//* view * model texCoords = tex; //gl_Position = vec4(pos, 1.0)*model*view*projection; } "; string fragmentShaderSource = @" #version 330 in vec3 vertexColor; in vec2 texCoords; out vec4 outputColor; uniform float timedata; uniform sampler2D texture1; uniform vec3 player; in vec3 frag_Position; float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2(11.9898,78.233))) * 43758.5453); } void main() { vec3 xTangent = dFdx(frag_Position ); vec3 yTangent = dFdy(frag_Position ); vec3 faceNormal = normalize( cross( xTangent, yTangent ) ); float gray = dot(cos(faceNormal.xyz), vec3(0.299, 0.587, 0.111)); float lighting = dot(faceNormal,vec3(8))* .1 + .1; vec4 texel = texture(texture1,vec2(texCoords.x,-texCoords.y)); //texel.r =rand(faceNormal.xy); outputColor = texel*vec4(vec3(vertexColor*(lighting/2)),1.1); }"; string tdd = @" #version 330 in vec3 vertexColor; in vec2 texCoords; out vec4 outputColor; uniform float timedata; uniform sampler2D texture1; uniform vec3 player; in vec3 frag_Position; float rand(vec2 co){ return fract(sin(dot(co.xy ,vec2(11.9898,78.233))) * 43758.5453); } void main() { outputColor =vec4(vec3(vertexColor.x,vertexColor.y,vertexColor.z),1.1); }"; int tds; protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(0, 0, Width, Height); } protected override void OnClosing(CancelEventArgs e) { Environment.Exit(420); } float time; public static int vidth; protected override void OnLoad(EventArgs e) { //Ignore this base.OnLoad(e); //Set clear color GL.ClearColor(Color4.CornflowerBlue); //blocks.Add(new Vector3(888888, 88888, 888888)); //Create the vao and vbos, don't worry about it now //Initialize the shader there //The file names for the respective shader program components //shader = new Shader("vertex.vert", "fragment.frag"); //Bind the shader to set up some array pointers //shader.Use(); //Bind the vertex array to set the shader pointers //Files.Binary(Files.BinaryString(vertexShaderSource), "SHAD.BIN"); //vertexShaderSource = Files.FileToString("SHAD.BIN"); Stuff.ShaderGen(vertexShaderSource, fragmentShaderSource, new Vector3(0, 0, -5), out shader); Console.WriteLine(vertexShaderSource); Stuff.vertices = new List(); Stuff.colors = new List(); Stuff.texcoords = new List(); Random rnd = new Random(); Stuff.vertexGen(vao, vbov, vbc, vbt, out vao, out vbov, out vbc, out vbt); Stuff.vertexGen(tree.v, tree.vbov, tree.vb, tree.vbt, out tree.v, out tree.vbov, out tree.vb, out tree.vbt); MouseMove += mouseMovement; vbogen(tree.vbov, tree.vb, tree.vbt, treeModel.vertices, treeModel.col, treeModel.tex); Stuff.ShaderGen(td, tdd, new Vector3(0, 0, 0), out tds); Stuff.vertexGen(movingObject.v, movingObject.vbov, movingObject.vb, movingObject.vbt, out movingObject.v, out movingObject.vbov, out movingObject.vb, out movingObject.vbt); vbogen(movingObject.vbov, movingObject.vb, movingObject.vbt, vertModel.vertices, vertModel.col, vertModel.tex); //Task.Run(vbo); //Why create the buffer there when you can do it on render and load the cpu every frame for creating the triangle } public static Matrix4 model; VBO movingObject = new VBO(); VBO tree = new VBO(); Vertex vertModel = new Vertex(); Vertex treeModel = new Vertex(); Matrix4 view; public static int Height; public static Matrix4 projection; public List chunkdata = new List(); public void vbogen(int vbov, int vb, int vbt, List vertices, List col, List texCoords) { GL.BindVertexArray(0); //Some bad equivalent to GL.End //Bind the buffer which to be used GL.BindBuffer(BufferTarget.ArrayBuffer, vbov);// //Send data to bound buffer in the specified "slot" //The kind of buffer, The size of the array i(the array length * size of an element), The data(because we had a list we convert it to an array), Buffer Usage, doesn't matter that much nowadays GL.BufferData(BufferTarget.ArrayBuffer, vertices.Count * Vector3.SizeInBytes, vertices.ToArray(), BufferUsageHint.DynamicDraw);//StaticDraw //Same thing but for colors GL.BindBuffer(BufferTarget.ArrayBuffer, vb); GL.BufferData(BufferTarget.ArrayBuffer, col.Count * Vector3.SizeInBytes, col.ToArray(), BufferUsageHint.DynamicDraw);//StaticDraw GL.BindBuffer(BufferTarget.ArrayBuffer, vbt); GL.BufferData(BufferTarget.ArrayBuffer, texCoords.Count * Vector2.SizeInBytes, texCoords.ToArray(), BufferUsageHint.DynamicDraw);//StaticDraw } int shader; int dt; bool upd; float y_direction = -353; Controller testObject = new Controller(); List blockdat; protected override void OnRenderFrame(FrameEventArgs e) { //Ignore this too base.OnRenderFrame(e); //Mouse.SetPosition(Width / 1, Height / 1); //Clear the window vidth = Width; Height = this.Size.Height; GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Enable(EnableCap.DepthTest); int timeloc = GL.GetUniformLocation(shader, "timedata"); GL.Uniform1(timeloc, time); dt = (int)e.Time; GL.UseProgram(shader); view = Matrix4.CreateTranslation(new Vector3(0, 0, 0)) * Matrix4.CreateRotationY(MathHelper.DegreesToRadians(player.GetCamera().getYaw())) * Matrix4.CreateRotationX(MathHelper.DegreesToRadians(y_direction)); //3; int vl = GL.GetUniformLocation(shader, "view"); GL.UniformMatrix4(vl, false, ref view); int pl = GL.GetUniformLocation(shader, "player"); GL.Uniform3(pl, player.getPos()); int projectloc = GL.GetUniformLocation(shader, "projection"); projection = Matrix4.CreateOrthographicOffCenter(-1.0F, 1.0F, -1.0F, 1.0F, 0.0F, 4.0F); GL.UseProgram(tds); GL.Disable(EnableCap.DepthTest); GL.Enable(EnableCap.Blend); GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha); GL.BindVertexArray(movingObject.v); Stuff.transform(0, 0, 0, shader, out shader); GL.DrawArrays(PrimitiveType.Triangles, 0, vertModel.vertices.Count); GL.UseProgram(shader); GL.Disable(EnableCap.Blend); GL.Enable(EnableCap.DepthTest); player.ChunkNumber = 1; if (upd == true) { Stuff.vertices = new List(); Stuff.colors = new List(); Stuff.texcoords = new List(); } vbogen(vbov, vbc, vbt, Stuff.vertices, Stuff.colors, Stuff.texcoords); Console.WriteLine(Stuff.vertices.Count); CollisionDetection.plr = player; projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.DegreesToRadians(45f), Width / (float)Height, 0.1f, 100.0f); GL.BindVertexArray(vao); //Finally we DrawArrays to draw //We draw triangles, we start from 0 and we have0 3 vertices //model = Matrix4.Identity * Matrix4.CreateTranslation(1, 0, -5); Stuff.transform(0, 0, -5, shader, out shader); GL.DrawArrays(PrimitiveType.Triangles, 0, Stuff.vertices.Count); GL.BindVertexArray(tree.v); //transform(0, 18, -5, shader, out shader); //Matrix4.CreateRotationX(time) * //testObject.movement(); testObject.deltaTime = (float)e.Time; model = Matrix4.Identity * Matrix4.CreateRotationY(testObject.pos.X)*Matrix4.CreateTranslation(testObject.pos.X + player.getPos().X, 18+ testObject.pos.Y + player.getPos().Y, -5 + testObject.pos.Z + player.getPos().Z); int modelloc = GL.GetUniformLocation(shader, "model"); GL.UniformMatrix4(modelloc, false, ref model); GL.DrawArrays(PrimitiveType.Triangles, 0, treeModel.vertices.Count); Stuff.transform(8, 1, 1, shader, out shader); GL.DrawArrays(PrimitiveType.Triangles, 0, treeModel.vertices.Count); time += 0.1f; SwapBuffers(); } bool object_inst; bool IsClose(float a, float b, float tolerance) { return Math.Abs(a - b) < tolerance; } void mouseMovement(object sender, MouseMoveEventArgs e) { if (this.Focused == true) { player.GetCamera().setYaw(e.XDelta / 1.1f ); y_direction = (Mouse.GetState().Y/1.1f); } } protected override async void OnUpdateFrame(FrameEventArgs e) { var input = Keyboard.GetState(); if (this.Focused == true) { if (input.IsKeyDown(Key.Escape)) { Exit(); } if (input.IsKeyDown(Key.H)) { bool collision = false; for (int i = 0; i < chunkdata.Count; i++) { /**/ //if(CollisionDetection.collision(chunks[player.ChunkNumber].chunkdata[i])) if ((Sys.numBetween(-player.getPos().X, chunkdata[i].X, chunkdata[i].X + 1.5f)) && (Sys.numBetween(-player.getPos().Z, chunkdata[i].Z - 5, chunkdata[i].Z - 5 + 1.8f)) && (-player.getPos().Y <= 1.0f + (int)chunkdata[i].Y + 1 && 0.5f + -player.getPos().Y >= (int)chunkdata[i].Y + 1)) { collision = true; } } if (collision == false) player.GetCamera().walkForward(8 * (float)e.Time); } if (input.IsKeyDown(Key.S)) { player.GetCamera().walkBackwards(8 * (float)e.Time); } if (input.IsKeyDown(Key.D)) { player.GetCamera().walkRight(8 * (float)e.Time); } if (input.IsKeyDown(Key.A)) { player.GetCamera().walkLeft(8 * (float)e.Time); } if (input.IsKeyDown(Key.E)) { player.setY(player.getPos().Y - 0.1f); } if (input.IsKeyDown(Key.V)) { player.setY(player.getPos().Y + 0.1f); } if (input.IsKeyDown(Key.Space)) { for (int xj = 0; xj < 9; xj++) { player.setY(player.getPos().Y - 0.1f * (float)e.Time); await Sys.waitFunction(1); } } if (input.IsKeyDown(Key.Y)) { } if (input.IsKeyDown(Key.H)) { } base.OnUpdateFrame(e); } } [STAThread] public static void Main() { using (Program example = new Program()) { example.Run(); } } } }