static void Main(string[] args) { Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Glut.glutCloseFunc(OnClose); Glut.glutReshapeFunc(OnReshape); Gl.Disable(EnableCap.DepthTest); Gl.Enable(EnableCap.Blend); Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); program = new ShaderProgram(VertexShader, FragmentShader); program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up)); program["light_direction"].SetValue(new Vector3(0, 0, 1)); program["enable_lighting"].SetValue(lighting); glassTexture = new Texture("glass.bmp"); cube = new VBO<Vector3>(new Vector3[] { new Vector3(1, 1, -1), new Vector3(-1, 1, -1), new Vector3(-1, 1, 1), new Vector3(1, 1, 1), // top new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1), // bottom new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1), // front face new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1), // back face new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1), // left new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) }); // right cubeNormals = new VBO<Vector3>(new Vector3[] { new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, -1, 0), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(0, 0, -1), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(-1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0) }); cubeUV = new VBO<Vector2>(new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) }); cubeQuads = new VBO<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, BufferTarget.ElementArrayBuffer); watch = System.Diagnostics.Stopwatch.StartNew(); Glut.glutMainLoop(); }
static void Main(string[] args) { // create an OpenGL window Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); // provide the Glut callbacks that are necessary for running this tutorial Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutCloseFunc(OnClose); // compile the shader program program = new ShaderProgram(VertexShader, FragmentShader); // set the view and projection matrix, which are static throughout this tutorial program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up)); // create a triangle triangle = new VBO<Vector3>(new Vector3[] { new Vector3(0, 1, 0), new Vector3(-1, -1, 0), new Vector3(1, -1, 0) }); triangleElements = new VBO<int>(new int[] { 0, 1, 2 }, BufferTarget.ElementArrayBuffer); // create a square square = new VBO<Vector3>(new Vector3[] { new Vector3(-1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, -1, 0), new Vector3(-1, -1, 0) }); squareElements = new VBO<int>(new int[] { 0, 1, 2, 3 }, BufferTarget.ElementArrayBuffer); Glut.glutMainLoop(); }
static void Main(string[] args) { // create an OpenGL window Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); // provide the Glut callbacks that are necessary for running this tutorial Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutCloseFunc(OnClose); // enable depth testing to ensure correct z-ordering of our fragments Gl.Enable(EnableCap.DepthTest); // compile the shader program program = new ShaderProgram(VertexShader, FragmentShader); // set the view and projection matrix, which are static throughout this tutorial program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up)); // create a pyramid with vertices and colors pyramid = new VBO<Vector3>(new Vector3[] { new Vector3(0, 1, 0), new Vector3(-1, -1, 1), new Vector3(1, -1, 1), // front face new Vector3(0, 1, 0), new Vector3(1, -1, 1), new Vector3(1, -1, -1), // right face new Vector3(0, 1, 0), new Vector3(1, -1, -1), new Vector3(-1, -1, -1), // back face new Vector3(0, 1, 0), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1) }); // left face pyramidColor = new VBO<Vector3>(new Vector3[] { new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 1, 0), new Vector3(1, 0, 0), new Vector3(0, 1, 0), new Vector3(0, 0, 1), new Vector3(1, 0, 0), new Vector3(0, 0, 1), new Vector3(0, 1, 0) }); pyramidTriangles = new VBO<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }, BufferTarget.ElementArrayBuffer); // create a cube with vertices and colors cube = new VBO<Vector3>(new Vector3[] { new Vector3(1, 1, -1), new Vector3(-1, 1, -1), new Vector3(-1, 1, 1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1), new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1), new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1), new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) }); cubeColor = new VBO<Vector3>(new Vector3[] { new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(0, 1, 0), new Vector3(1, 0.5, 0), new Vector3(1, 0.5, 0), new Vector3(1, 0.5, 0), new Vector3(1, 0.5, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(1, 1, 0), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(0, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1), new Vector3(1, 0, 1) }); cubeQuads = new VBO<int>(new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 }, BufferTarget.ElementArrayBuffer); watch = System.Diagnostics.Stopwatch.StartNew(); Glut.glutMainLoop(); }
/// <summary> /// Gets the location of the parameter in a compiled OpenGL program. /// </summary> /// <param name="Program">Specifies the shader program that contains this parameter.</param> public void GetLocation(ShaderProgram Program) { Program.Use(); if (programid == 0) { programid = Program.ProgramID; location = (ptype == OpenGL.ParamType.Uniform ? Program.GetUniformLocation(name) : Program.GetAttributeLocation(name)); } }
static void Main(string[] args) { Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Glut.glutCloseFunc(OnClose); Glut.glutReshapeFunc(OnReshape); // enable blending and set to accumulate the star colors Gl.Disable(EnableCap.DepthTest); Gl.Enable(EnableCap.Blend); Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One); // create our shader program program = new ShaderProgram(VertexShader, FragmentShader); // set up the projection and view matrix program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 20), Vector3.Zero, Vector3.Up)); // load the star texture starTexture = new Texture("star.bmp"); // each star is simply a quad star = new VBO<Vector3>(new Vector3[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(1, 1, 0), new Vector3(-1, 1, 0) }); starUV = new VBO<Vector2>(new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) }); starQuads = new VBO<int>(new int[] { 0, 1, 2, 0, 2, 3 }, BufferTarget.ElementArrayBuffer); // create 50 stars for this tutorial int numStars = 50; for (int i = 0; i < numStars; i++) { stars.Add(new Star(0, (float)i / numStars * 4f, new Vector3(generator.NextDouble(), generator.NextDouble(), generator.NextDouble()))); } font = new BMFont("font24.fnt", "font24.png"); fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource); fontProgram.Use(); fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000)); fontProgram["color"].SetValue(new Vector3(1, 1, 1)); information = font.CreateString(fontProgram, "OpenGL C# Tutorial 10"); watch = System.Diagnostics.Stopwatch.StartNew(); Glut.glutMainLoop(); }
static void Main(string[] args) { Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH | Glut.GLUT_ALPHA | Glut.GLUT_STENCIL | Glut.GLUT_MULTISAMPLE); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); Gl.Enable(EnableCap.DepthTest); Gl.Enable(EnableCap.Blend); Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Glut.glutCloseFunc(OnClose); Glut.glutReshapeFunc(OnReshape); // add our mouse callbacks for this tutorial Glut.glutMouseFunc(OnMouse); Glut.glutMotionFunc(OnMove); // create our shader program program = new ShaderProgram(VertexShader, FragmentShader); // create our camera camera = new Camera(new Vector3(0, 0, 50), Quaternion.Identity); camera.SetDirection(new Vector3(0, 0, -1)); // set up the projection and view matrix program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); program["model_matrix"].SetValue(Matrix4.Identity); objectFile = new ObjLoader("enterprise/enterprise.obj", program); // load the bitmap font for this tutorial font = new BMFont("font24.fnt", "font24.png"); fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource); fontProgram.Use(); fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000)); fontProgram["color"].SetValue(new Vector3(1, 1, 1)); information = font.CreateString(fontProgram, "OpenGL C# Tutorial 16"); watch = System.Diagnostics.Stopwatch.StartNew(); Glut.glutMainLoop(); }
public void drawToPickBuffer(ShaderProgram shader, Matrix4 view) { shader.Use(); shader.setUniform("model", this.transform); shader.setUniform("view", view); shader.setUniform("pickColor", this.pickColor); Vao.Bind(); GL.DrawArrays(PrimitiveType.Triangles, 0, Vao.Lenth); Vao.Unbind(); }
public void draw(Camera cam) { GL.DepthFunc(DepthFunction.Lequal); GL.CullFace(CullFaceMode.Front); GL.BindTexture(TextureTarget.TextureCubeMap, Texture); Shader.Use(); Shader.setUniform("proj", cam.Projection); Shader.setUniform("view", cam.Transfrom); GL.BindVertexArray(vao); //GL.DrawArrays(PrimitiveType.Quads, 0, verts_f.Length); GL.DrawElements(PrimitiveType.Quads, elems_u.Length, DrawElementsType.UnsignedInt, 0); GL.BindVertexArray(0); GL.DepthFunc(DepthFunction.Less); GL.CullFace(CullFaceMode.Back); }
/// <summary> /// Get the index of a uniform block in the provided shader program. /// Note: This method will use the provided shader program, so make sure to /// store which program is currently active and reload it if required. /// </summary> /// <param name="program">The shader program that contains the uniform block.</param> /// <param name="uniformBlockName">The uniform block name.</param> /// <returns>The index of the uniform block.</returns> public static uint GetUniformBlockIndex(ShaderProgram program, string uniformBlockName) { program.Use(); // take care of a crash that can occur on NVIDIA drivers by using the program first return(GetUniformBlockIndex(program.ProgramID, uniformBlockName)); }
static void Main(string[] args) { Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH | Glut.GLUT_MULTISAMPLE); // multisampling makes things beautiful! Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Glut.glutCloseFunc(OnClose); Glut.glutReshapeFunc(OnReshape); // add our mouse callbacks for this tutorial Glut.glutMouseFunc(OnMouse); Glut.glutMotionFunc(OnMove); Gl.Enable(EnableCap.DepthTest); Gl.Enable(EnableCap.Blend); Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.OneMinusSrcAlpha); // create our shader program program = new ShaderProgram(VertexShader, FragmentShader); // create our camera camera = new Camera(new Vector3(0, 0, 10), Quaternion.Identity); camera.SetDirection(new Vector3(0, 0, -1)); // set up the projection and view matrix program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); //program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 10), Vector3.Zero, Vector3.Up)); program["light_direction"].SetValue(new Vector3(0, 0, 1)); program["enable_lighting"].SetValue(lighting); program["normalTexture"].SetValue(1); program["enable_mapping"].SetValue(normalMapping); brickDiffuse = new Texture("AlternatingBrick-ColorMap.png"); brickNormals = new Texture("AlternatingBrick-NormalMap.png"); Vector3[] vertices = new Vector3[] { new Vector3(1, 1, -1), new Vector3(-1, 1, -1), new Vector3(-1, 1, 1), new Vector3(1, 1, 1), // top new Vector3(1, -1, 1), new Vector3(-1, -1, 1), new Vector3(-1, -1, -1), new Vector3(1, -1, -1), // bottom new Vector3(1, 1, 1), new Vector3(-1, 1, 1), new Vector3(-1, -1, 1), new Vector3(1, -1, 1), // front face new Vector3(1, -1, -1), new Vector3(-1, -1, -1), new Vector3(-1, 1, -1), new Vector3(1, 1, -1), // back face new Vector3(-1, 1, 1), new Vector3(-1, 1, -1), new Vector3(-1, -1, -1), new Vector3(-1, -1, 1), // left new Vector3(1, 1, -1), new Vector3(1, 1, 1), new Vector3(1, -1, 1), new Vector3(1, -1, -1) }; cube = new VBO<Vector3>(vertices); Vector2[] uvs = new Vector2[] { new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1), new Vector2(0, 0), new Vector2(1, 0), new Vector2(1, 1), new Vector2(0, 1) }; cubeUV = new VBO<Vector2>(uvs); List<int> triangles = new List<int>(); for (int i = 0; i < 6; i++) { triangles.Add(i * 4); triangles.Add(i * 4 + 1); triangles.Add(i * 4 + 2); triangles.Add(i * 4); triangles.Add(i * 4 + 2); triangles.Add(i * 4 + 3); } cubeTriangles = new VBO<int>(triangles.ToArray(), BufferTarget.ElementArrayBuffer); Vector3[] normals = Geometry.CalculateNormals(vertices, triangles.ToArray()); cubeNormals = new VBO<Vector3>(normals); Vector3[] tangents = CalculateTangents(vertices, normals, triangles.ToArray(), uvs); cubeTangents = new VBO<Vector3>(tangents); // load the bitmap font for this tutorial font = new BMFont("font24.fnt", "font24.png"); fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource); fontProgram.Use(); fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000)); fontProgram["color"].SetValue(new Vector3(1, 1, 1)); information = font.CreateString(fontProgram, "OpenGL C# Tutorial 15"); watch = System.Diagnostics.Stopwatch.StartNew(); Glut.glutMainLoop(); }
static void Main(string[] args) { timefromstart = System.Diagnostics.Stopwatch.StartNew(); Console.WriteLine("Initializing GLUT - {0}s", getTimeFromStart().ToString()); Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("kVoxGame"); Console.WriteLine("GLUT window created - {0}s", getTimeFromStart().ToString()); //Callbacks Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); //Mouse Glut.glutMouseFunc(OnMouse); Glut.glutMotionFunc(OnMove); //Keyboard Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Console.WriteLine("GLUT callbacks binded - {0}s", getTimeFromStart().ToString()); Gl.Enable(EnableCap.DepthTest); Gl.Enable(EnableCap.Blend); Gl.Enable(EnableCap.Multisample); Gl.Enable(EnableCap.SampleAlphaToCoverage); Gl.Enable(EnableCap.FragmentLightingSgix); Gl.Enable(EnableCap.CullFace); Console.WriteLine("GL Enables enabled :D - {0}s", getTimeFromStart().ToString()); skydomeTexture = new Texture("skydome.jpg"); //Gl.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line); // create main shader program terrainSP = new ShaderProgram(terrainVS, terrainFS); terrainSP.Use(); terrainSP["color"].SetValue(new Vector3(0, 0.8, 0)); projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f); terrainSP["projection_matrix"].SetValue(projectionMatrix); terrainSP["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(0, 0, 0)) * Matrix4.CreateRotation(new Vector3(0, 0, 0), 0.0f)); Console.WriteLine("Shader program (main) compiled and data injected - {0}s", getTimeFromStart().ToString()); // create main skybox program skyboxSP = new ShaderProgram(skyboxVS, skyboxFS); skyboxSP.Use(); skyboxSP["color"].SetValue(new Vector3(0.2, 1.0, 1.0)); skyboxSP["resolution"].SetValue(new Vector2(width, height)); projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f); skyboxSP["projection_matrix"].SetValue(projectionMatrix); skyboxSP["model_matrix"].SetValue(Matrix4.CreateTranslation(new Vector3(0, 0, 0)) * Matrix4.CreateRotation(new Vector3(0, 0, 0), 0.0f)); Console.WriteLine("Shader program (skybox) compiled and data injected - {0}s", getTimeFromStart().ToString()); //Setup camera camera = new Camera(new Vector3(0, 0, 10), Quaternion.Identity); camera.SetDirection(new Vector3(0, 0, -1)); Console.WriteLine("Cam setup complete - {0}s", getTimeFromStart().ToString()); watch = System.Diagnostics.Stopwatch.StartNew(); chunks = new VoxelChunk[4]; //instance few voxel chunks chunks[0] = new VoxelChunk(0,0); chunks[1] = new VoxelChunk(0, 32); chunks[2] = new VoxelChunk(32, 0); chunks[3] = new VoxelChunk(32, 32); Console.WriteLine("Chunks added to array - {0}s", getTimeFromStart().ToString()); //init frustum frustum = new Frustum(); frustum.UpdateFrustum(projectionMatrix, camera.ViewMatrix); Console.WriteLine("starting main GLUT loop - {0}s", getTimeFromStart().ToString()); Glut.glutMainLoop(); }
public Scene(InputPanel ip) { //Stops GLUT trying to re-initialise if it has already been initialised. int glutTime = Glut.glutGet(Glut.GLUT_ELAPSED_TIME); if (glutTime <= 0) { Glut.glutInit(); } //Initialises GLUT Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("Scene"); Glut.glutSetOption(Glut.GLUT_ACTION_ON_WINDOW_CLOSE, 1); Glut.glutCloseFunc(closeFunc); Glut.glutIdleFunc(OnRenderFrame); Gl.Enable(EnableCap.Blend); //Creates the rendering ShaderProgram program = new ShaderProgram(VertexShader, FragmentShader); program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); resetCamera(); //Initialises colors colors[0] = new Vector3(1, 0.5f, 0); colors[1] = new Vector3(1, 1, 0); colors[2] = new Vector3(0.5f, 1, 0); colors[3] = new Vector3(0, 1, 0); colors[4] = new Vector3(0, 1, 0.5f); colors[5] = new Vector3(0, 0.5f, 1); colors[6] = new Vector3(0, 0, 1); colors[7] = new Vector3(0.5f, 0, 1); colors[8] = new Vector3(1, 0, 1); colors[9] = new Vector3(1, 0, 0.5f); //Starts the Stopwatch watch = System.Diagnostics.Stopwatch.StartNew(); inputPanel = ip; //Enters the main loop Glut.glutMainLoop(); }
static void Main(string[] args) { Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH); Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Glut.glutCloseFunc(OnClose); Glut.glutReshapeFunc(OnReshape); // enable blending and set to accumulate the star colors Gl.Enable(EnableCap.DepthTest); Gl.Enable(EnableCap.Blend); // create our shader program program = new ShaderProgram(VertexShader, FragmentShader); // set up the projection and view matrix program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 20), Vector3.Zero, Vector3.Up)); program["model_matrix"].SetValue(Matrix4.Identity); // load the flag texture flagTexture = new Texture("flag.png"); // create the flag, which is just a plane with a certain number of segments List<Vector3> vertices = new List<Vector3>(); List<Vector2> uvs = new List<Vector2>(); List<int> triangles = new List<int>(); for (int x = 0; x < 40; x++) { for (int y = 0; y < 40; y++) { vertices.Add(new Vector3((x - 20) / 5.0, (y - 20) / 10.0, 0)); uvs.Add(new Vector2(x / 39.0, 1 - y / 39.0)); if (y == 39 || x == 39) continue; triangles.Add(x * 40 + y); triangles.Add((x + 1) * 40 + y); triangles.Add((x + 1) * 40 + y + 1); triangles.Add(x * 40 + y); triangles.Add((x + 1) * 40 + y + 1); triangles.Add(x * 40 + y + 1); } } flagVertices = new VBO<Vector3>(vertices.ToArray()); flagUVs = new VBO<Vector2>(uvs.ToArray()); flagTriangles = new VBO<int>(triangles.ToArray(), BufferTarget.ElementArrayBuffer); // load the bitmap font for this tutorial font = new BMFont("font24.fnt", "font24.png"); fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource); fontProgram.Use(); fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000)); fontProgram["color"].SetValue(new Vector3(1, 1, 1)); information = font.CreateString(fontProgram, "OpenGL C# Tutorial 11"); watch = System.Diagnostics.Stopwatch.StartNew(); Glut.glutMainLoop(); }
static void Main(string[] args) { Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH | Glut.GLUT_MULTISAMPLE); Glut.glutInitWindowSize(width, height); int window = Glut.glutCreateWindow("VoxelEngine"); Console.WriteLine("OpenGL Version: {0}", Gl.Version()); Console.WriteLine("Loading shaders..."); orthoShader = LoadShader("ortho_vs", "ortho_fs"); if (orthoShader == null) { Glut.glutHideWindow(); Console.WriteLine("Error loading shaders."); Console.ReadKey(true); return; } Console.WriteLine("Loaded shaders successfully."); Console.WriteLine("Creating Glut Window..."); Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutCloseFunc(OnClose); Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Glut.glutMouseFunc(OnMouse); Glut.glutMotionFunc(OnMove); Glut.glutReshapeFunc(OnReshape); Glut.glutMouseWheelFunc(OnMouseWheel); Glut.glutPassiveMotionFunc(OnPassiveMove); gizmo = new Gizmo(); camera = new Camera(new Vector3(0, 48, 0), Quaternion.Identity); testViewMatrix = camera.ViewMatrix; VoxelChunkManager.Instance.projectionMatrix = Matrix4.CreatePerspectiveFieldOfView(fov, aspect, near, far); float nearTop = near * (float)Math.Tan(fov / 2f); float nearBottom = -nearTop; float nearRight = nearTop * aspect; float nearLeft = -nearRight; float farTop = far * (float)Math.Tan(fov / 2f); float farBottom = -farTop; float farRight = farTop * aspect; float farLeft = -farRight; frustumVertices = new VBO<Vector3>(new Vector3[] { new Vector3(nearLeft, nearTop, -near),new Vector3(nearRight, nearTop, -near),new Vector3(nearRight, nearBottom, -near),new Vector3(nearLeft, nearBottom, -near), new Vector3(farLeft, farTop, -far),new Vector3(farRight, farTop, -far),new Vector3(farRight, farBottom, -far),new Vector3(farLeft, farBottom, -far) }); frustumIndices = new VBO<int>(new int[] { 0, 1, 2, 3, 4, 5, 1, 0, 1, 5, 6, 2, 4, 0, 3, 7, 5, 4, 7, 6, 6, 7, 3, 2 }, BufferTarget.ElementArrayBuffer); int radius = 2; for (int i = -radius; i <= radius; i++) { for (int j = -radius; j <= radius; j++) { VoxelChunkManager.Instance.LoadChunk(i, j); } } //screenQuad = Geometry.CreateQuad(orthoShader, new Vector2(-0.5, 0.5), new Vector2(1, 1), new Vector2(0, 0), new Vector2(1, 1)); //fbo = new FBO(new System.Drawing.Size(width, height), // new FramebufferAttachment[] { // FramebufferAttachment.ColorAttachment0Ext }, // PixelInternalFormat.Rgb8, true); Gl.Enable(EnableCap.DepthTest); Gl.Enable(EnableCap.CullFace); Gl.FrontFace(FrontFaceDirection.Cw); Gl.CullFace(CullFaceMode.Back); //Gl.Enable(EnableCap.Multisample); Gl.ClearColor(0.53f, 0.8f, 0.92f, 1); font = new BMFont("font24.fnt", "font24.png"); fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource); fontProgram.Use(); fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000)); fontProgram["color"].SetValue(new Vector3(1, 1, 1)); information = font.CreateString(fontProgram, ""); watch = System.Diagnostics.Stopwatch.StartNew(); Console.WriteLine("Entering main loop..."); Glut.glutMainLoop(); }
static void Main(string[] args) { Glut.glutInit(); Glut.glutInitDisplayMode(Glut.GLUT_DOUBLE | Glut.GLUT_DEPTH | Glut.GLUT_MULTISAMPLE); // multisampling makes things beautiful! Glut.glutInitWindowSize(width, height); Glut.glutCreateWindow("OpenGL Tutorial"); Glut.glutIdleFunc(OnRenderFrame); Glut.glutDisplayFunc(OnDisplay); Glut.glutKeyboardFunc(OnKeyboardDown); Glut.glutKeyboardUpFunc(OnKeyboardUp); Glut.glutCloseFunc(OnClose); Glut.glutReshapeFunc(OnReshape); // enable blending and set to accumulate the star colors Gl.Enable(EnableCap.Blend); Gl.Enable(EnableCap.ProgramPointSize); Gl.Enable(EnableCap.Multisample); Gl.BlendFunc(BlendingFactorSrc.SrcAlpha, BlendingFactorDest.One); // create our shader program program = new ShaderProgram(VertexShader, FragmentShader); // set up the projection and view matrix program.Use(); program["projection_matrix"].SetValue(Matrix4.CreatePerspectiveFieldOfView(0.45f, (float)width / height, 0.1f, 1000f)); program["view_matrix"].SetValue(Matrix4.LookAt(new Vector3(0, 0, 20), Vector3.Zero, Vector3.Up)); program["model_matrix"].SetValue(Matrix4.Identity); program["static_colors"].SetValue(false); // load the particle texture particleTexture = new Texture("star.bmp"); // set up the particlePoints VBO, which will stay constant int[] points = new int[particleCount]; for (int i = 0; i < points.Length; i++) points[i] = i; particlePoints = new VBO<int>(points, BufferTarget.ElementArrayBuffer); // set up the particleColors, which we'll just keep static Vector3[] colors = new Vector3[particleCount]; for (int i = 0; i < colors.Length; i++) colors[i] = new Vector3(generator.NextDouble(), generator.NextDouble(), generator.NextDouble()); particleColors = new VBO<Vector3>(colors); // build up our first batch of 1000 particles and 1000 static colors for (int i = 0; i < particleCount; i++) particles.Add(new Particle(Vector3.Zero, 0)); // load the bitmap font for this tutorial font = new BMFont("font24.fnt", "font24.png"); fontProgram = new ShaderProgram(BMFont.FontVertexSource, BMFont.FontFragmentSource); fontProgram.Use(); fontProgram["ortho_matrix"].SetValue(Matrix4.CreateOrthographic(width, height, 0, 1000)); fontProgram["color"].SetValue(new Vector3(1, 1, 1)); information = font.CreateString(fontProgram, "OpenGL C# Tutorial 12"); watch = System.Diagnostics.Stopwatch.StartNew(); Glut.glutMainLoop(); }