private void GenerateMesh(int width, int height) { if (_initialized) { Dispose(); } _sampler = new Sampler(); _sampler.SetWrapMode(TextureWrapMode.Clamp); _program = ProgramFactory.Create <SpriteShaderProgram>(); var w = width / 2; var h = height / 2; var vertices = new[] { new Vertex(-w, h, 0, 0, 0), new Vertex(w, h, 0, 1, 0), new Vertex(-w, -h, 0, 0, 1), new Vertex(w, -h, 0, 1, 1) }; _vbo = new Buffer <Vertex>(); _vbo.Init(BufferTarget.ArrayBuffer, vertices); _vao = new VertexArray(); _vao.Bind(); _vao.BindAttribute(_program.InPosition, _vbo); _vao.BindAttribute(_program.InTexCoord, _vbo, 12); _initialized = true; }
protected override void OnRenderFrame(FrameEventArgs e) { // set up viewport GL.Viewport(0, 0, Size.X, Size.Y); // clear the back buffer GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); // set up modelview and perspective matrix SetupPerspective(); // set uniforms _program.CenterMass.Set(_centerMass); _program.TimeStep.Set((float)e.Time); _program.ModelViewProjectionMatrix.Set(ModelView * Projection); // set up binding of the shader variable to the buffer object _vao.BindAttribute(_program.InPosition, _buffers.Ping); _vao.BindAttribute(_program.InVelocity, _buffers.Ping, 16); // set target buffer for transform feedback // the two outputs are interleaved into the same buffer // so we only have to bind one of them as both binding indices are equal: // _program.OutPosition.Index == _program.OutVelocity.Index // when you choose to write into different buffers you need to bind all of them _feedback.BindOutput(_program.OutPosition, _buffers.Pong); // render the buffer and capture transform feedback outputs _feedback.Begin(TransformFeedbackPrimitiveType.Points); _vao.DrawArrays(PrimitiveType.Points, 0, _buffers.Ping.ElementCount); _feedback.End(); // swap all the buffers! _buffers.Swap(); SwapBuffers(); }
public static void InitializeBuffers() { VAO = new VertexArray(); VBO = new Buffer <VertVT>(); VAO.Bind(); VAO.BindAttribute(UIShader.Position, VBO); VAO.BindAttribute(UIShader.TexCoord, VBO, 12); }
public static void DrawStudConnector(Matrix4 transform, LDDModder.LDD.Primitives.Connectors.Custom2DFieldConnector connector) { bool wasTexEnabled = GL.IsEnabled(EnableCap.Texture2D); GL.Enable(EnableCap.Texture2D); //GL.DepthMask(false); StudConnectionShader.Use(); StudConnectionShader.ModelMatrix.Set(transform); var cellSize = new Vector2(0.8f) * new Vector2(connector.StudWidth, connector.StudHeight); cellSize.X /= connector.ArrayWidth; cellSize.Y /= connector.ArrayHeight; StudConnectionShader.CellSize.Set(cellSize); StudConnectionShader.IsMale.Set(connector.SubType == 23); TextureManager.StudGridTexture.Bind(TextureUnit.Texture5); StudConnectionShader.Texture.Set(TextureUnit.Texture5); var items = new List <StudGridCell>(); for (int y = 0; y < connector.ArrayHeight; y++) { for (int x = 0; x < connector.ArrayWidth; x++) { var node = connector[x, y]; items.Add(new StudGridCell() { Position = new Vector3(x, y, 0), Values = new Vector3(node.Value1, node.Value2, node.Value3) }); } } var gridBuffer = new Buffer <StudGridCell>(); gridBuffer.Init(BufferTarget.ArrayBuffer, items.ToArray()); var vao = new VertexArray(); vao.Bind(); vao.BindAttribute(StudConnectionShader.Position, gridBuffer, 0); vao.BindAttribute(StudConnectionShader.Values, gridBuffer, 12); vao.DrawArrays(PrimitiveType.Points, 0, items.Count); vao.UnbindAttribute(StudConnectionShader.Position); vao.UnbindAttribute(StudConnectionShader.Values); gridBuffer.Dispose(); vao.Dispose(); StudConnectionShader.Texture.Set(TextureUnit.Texture0); TextureManager.StudGridTexture.Bind(TextureUnit.Texture0); //GL.DepthMask(true); if (!wasTexEnabled) { GL.Disable(EnableCap.Texture2D); } }
private void OnLoad(object sender, EventArgs e) { // maximize window WindowState = WindowState.Maximized; // load program _programOdd = ProgramFactory.Create <GeodesicProgramOdd>(); _programEqual = ProgramFactory.Create <GeodesicProgram>(); _program = _programOdd; // create icosahedron and set model matrix _modelMatrix = Matrix4.CreateScale(1); _icosahedron = new Icosahedron(5); _icosahedron.UpdateBuffers(); // bind it to an vao _vao = new VertexArray(); _vao.Bind(); _vao.BindElementBuffer(_icosahedron.IndexBuffer); _vao.BindAttribute(_program.Position, _icosahedron.VertexBuffer); // set some reasonable default state GL.ClearColor(Color4.Black); GL.Enable(EnableCap.DepthTest); // backface culling is done in the tesselation control shader GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); GL.PatchParameter(PatchParameterInt.PatchVertices, 3); // lighting stuff _deferredRenderer = new DeferredRenderer(); // enable controls _variableHandler.Enable(this); }
private void OnLoad(object sender, EventArgs e) { // initialize shader _program = ProgramFactory.Create<SkyboxProgram>(); // initialize cube shape _cube = new Cube(); _cube.UpdateBuffers(); // initialize vertex array and attributes _vao = new VertexArray(); _vao.Bind(); _vao.BindAttribute(_program.InPosition, _cube.VertexBuffer); _vao.BindElementBuffer(_cube.IndexBuffer); // create cubemap texture and load all faces for (var i = 0; i < 6; i++) { using (var bitmap = new Bitmap(string.Format("Data/Textures/city{0}.jpg", i))) { bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); if (_skybox == null) BitmapTexture.CreateCompatible(bitmap, out _skybox, 1); _skybox.LoadBitmap(bitmap, i); } } // activate shader and bind texture to it _program.Use(); _program.Texture.BindTexture(TextureUnit.Texture0, _skybox); // enable seamless filtering to reduce artifacts at the edges of the cube faces GL.Enable(EnableCap.TextureCubeMapSeamless); // cull front faces because we are inside the cube // this is not really necessary but removes some artifacts when the user leaves the cube // which should be impossible for a real skybox, but in this demonstration it is possible by zooming out GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Front); // set a nice clear color GL.ClearColor(Color.MidnightBlue); }
private void OnLoad(object sender, EventArgs e) { // initialize shader (load sources, create/compile/link shader program, error checking) // when using the factory method the shader sources are retrieved from the ShaderSourceAttributes _program = ProgramFactory.Create<ExampleProgram>(); // this program will be used all the time so just activate it once and for all _program.Use(); // create vertices for a triangle var vertices = new[] { new Vector3(-1,-1,0), new Vector3(1,-1,0), new Vector3(0,1,0) }; // create buffer object and upload vertex data _vbo = new Buffer<Vector3>(); _vbo.Init(BufferTarget.ArrayBuffer, vertices); // create and bind a vertex array _vao = new VertexArray(); _vao.Bind(); // set up binding of the shader variable to the buffer object _vao.BindAttribute(_program.InPosition, _vbo); // set camera position Camera.DefaultState.Position = new Vector3(0,0,3); Camera.ResetToDefault(); // set a nice clear color GL.ClearColor(Color.MidnightBlue); }
private void OnLoad(object sender, EventArgs e) { // initialize shader (load sources, create/compile/link shader program, error checking) // when using the factory method the shader sources are retrieved from the ShaderSourceAttributes _program = ProgramFactory.Create <ExampleProgram>(); // this program will be used all the time so just activate it once and for all _program.Use(); // create vertices for a triangle var vertices = new[] { new Vector3(-1, -1, 0), new Vector3(1, -1, 0), new Vector3(0, 1, 0) }; // create buffer object and upload vertex data _vbo = new Buffer <Vector3>(); _vbo.Init(BufferTarget.ArrayBuffer, vertices); // create and bind a vertex array _vao = new VertexArray(); _vao.Bind(); // set up binding of the shader variable to the buffer object _vao.BindAttribute(_program.InPosition, _vbo); // set camera position Camera.DefaultState.Position = new Vector3(0, 0, 3); Camera.ResetToDefault(); // set a nice clear color GL.ClearColor(Color.MidnightBlue); }
private void OnLoad(object sender, EventArgs e) { // initialize and bind framebuffer _framebuffer = new Framebuffer(); _framebuffer.Bind(FramebufferTarget.Framebuffer); // initialize a renderbuffer and bind it to the depth attachment // to support depth testing while rendering to the texture _depthBuffer = new Renderbuffer(); _depthBuffer.Init(RenderbufferStorage.DepthComponent, FramebufferWidth, FramebufferHeight); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, _depthBuffer); // initialize texture and bind it to the color attachment _texture = new Texture2D(SizedInternalFormat.Rgba8, FramebufferWidth, FramebufferHeight, 1); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, _texture); Framebuffer.Unbind(FramebufferTarget.Framebuffer); // initialize demonstration geometry _cube = new ColorCube(); _cube.UpdateBuffers(); _quad = new TexturedQuad(); _quad.UpdateBuffers(); // initialize shaders _colorProgram = ProgramFactory.Create <SimpleColorProgram>(); _textureProgram = ProgramFactory.Create <SimpleTextureProgram>(); // set up vertex attributes for the cube _cubeVao = new VertexArray(); _cubeVao.Bind(); _cubeVao.BindAttribute(_colorProgram.InPosition, _cube.VertexBuffer); _cubeVao.BindAttribute(_colorProgram.InColor, _cube.ColorBuffer); _cubeVao.BindElementBuffer(_cube.IndexBuffer); // set up vertex attributes for the quad _quadVao = new VertexArray(); _quadVao.Bind(); _quadVao.BindAttribute(_textureProgram.InPosition, _quad.VertexBuffer); _quadVao.BindAttribute(_textureProgram.InTexCoord, _quad.TexCoordBuffer); // set camera position Camera.DefaultState.Position = new Vector3(0, 0, 3); Camera.ResetToDefault(); // enable depth testing GL.Enable(EnableCap.DepthTest); }
private void OnLoad(object sender, EventArgs e) { // initialize and bind framebuffer _framebuffer = new Framebuffer(); _framebuffer.Bind(FramebufferTarget.Framebuffer); // initialize a renderbuffer and bind it to the depth attachment // to support depth testing while rendering to the texture _depthBuffer = new Renderbuffer(); _depthBuffer.Init(RenderbufferStorage.DepthComponent, FramebufferWidth, FramebufferHeight); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, _depthBuffer); // initialize texture and bind it to the color attachment _texture = new Texture2D(SizedInternalFormat.Rgba8, FramebufferWidth, FramebufferHeight, 1); _framebuffer.Attach(FramebufferTarget.Framebuffer, FramebufferAttachment.ColorAttachment0, _texture); Framebuffer.Unbind(FramebufferTarget.Framebuffer); // initialize demonstration geometry _cube = new ColorCube(); _cube.UpdateBuffers(); _quad = new TexturedQuad(); _quad.UpdateBuffers(); // initialize shaders _colorProgram = ProgramFactory.Create<SimpleColorProgram>(); _textureProgram = ProgramFactory.Create<SimpleTextureProgram>(); // set up vertex attributes for the cube _cubeVao = new VertexArray(); _cubeVao.Bind(); _cubeVao.BindAttribute(_colorProgram.InPosition, _cube.VertexBuffer); _cubeVao.BindAttribute(_colorProgram.InColor, _cube.ColorBuffer); _cubeVao.BindElementBuffer(_cube.IndexBuffer); // set up vertex attributes for the quad _quadVao = new VertexArray(); _quadVao.Bind(); _quadVao.BindAttribute(_textureProgram.InPosition, _quad.VertexBuffer); _quadVao.BindAttribute(_textureProgram.InTexCoord, _quad.TexCoordBuffer); // set camera position Camera.DefaultState.Position = new Vector3(0,0,3); Camera.ResetToDefault(); // enable depth testing GL.Enable(EnableCap.DepthTest); }
public DeferredRenderer() { _directionalProgram = ProgramFactory.Create <DirectionalLightProgram>(); _pointProgram = ProgramFactory.Create <PointLightProgram>(); _quad = new Quad(); _quad.UpdateBuffers(); _vaoFullScreenQuad = new VertexArray(); _vaoFullScreenQuad.Bind(); _vaoFullScreenQuad.BindAttribute(_directionalProgram.Position, _quad.VertexBuffer); }
protected override void OnLoad() { // load textures into array for (var i = 0; i < _stateTextures.Length; i++) { using (var bitmap = new Bitmap(Path.Combine("Data/Textures/", _stateTextures[i]))) { bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); if (_textureArray == null) { BitmapTexture.CreateCompatible(bitmap, out _textureArray, _stateTextures.Length, 1); } _textureArray.LoadBitmap(bitmap, i); } } // initialize buffer var field = new Minefield[FieldWidth * FieldHeight]; for (var i = 0; i < field.Length; i++) { field[i] = new Minefield(i % FieldWidth, i / FieldHeight, i % _stateTextures.Length); } _buffer = new Buffer <Minefield>(); _buffer.Init(BufferTarget.ArrayBuffer, field); // load program _gridProgram = ProgramFactory.Create <TextureGridProgram>(); _gridProgram.Use(); // bind the texture and set uniform _gridProgram.TextureData.BindTexture(TextureUnit.Texture0, _textureArray); // set up vertex array and attributes _vao = new VertexArray(); _vao.Bind(); _vao.BindAttribute(_gridProgram.InPosition, _buffer); _vao.BindAttribute(_gridProgram.InTexture, _buffer, 8); // set nice clear color GL.ClearColor(Color.MidnightBlue); // initialize camera position Camera.DefaultState.Position = new Vector3(0, 5, 15); Camera.ResetToDefault(); }
protected override void OnLoad() { // load texture from file using (var bitmap = new Bitmap("Data/Textures/crate.png")) { BitmapTexture.CreateCompatible(bitmap, out _texture); _texture.LoadBitmap(bitmap); } // initialize shaders _textureProgram = ProgramFactory.Create <SimpleTextureProgram>(); // initialize cube object and base view matrix _objectView = _baseView = Matrix4.Identity; // initialize demonstration geometry _cube = new TexturedCube(); _cube.UpdateBuffers(); // set up vertex attributes for the quad _cubeVao = new VertexArray(); _cubeVao.Bind(); _cubeVao.BindAttribute(_textureProgram.InPosition, _cube.VertexBuffer); _cubeVao.BindAttribute(_textureProgram.InTexCoord, _cube.TexCoordBuffer); // Enable culling, our cube vertices are defined inside out, so we flip them GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Back); // initialize camera position Camera.DefaultState.Position = new Vector3(0, 0, 4); Camera.ResetToDefault(); // set nice clear color GL.ClearColor(Color.MidnightBlue); _stopwatch.Restart(); }
public static void InitializeResources() { NormalFont = new QFont("C:\\Windows\\Fonts\\segoeui.ttf", 10, new QFontBuilderConfiguration(true)); SmallFont = new QFont("C:\\Windows\\Fonts\\segoeui.ttf", 8, new QFontBuilderConfiguration(true)); MonoFont = new QFont("C:\\Windows\\Fonts\\consola.ttf", 10, new QFontBuilderConfiguration(true)); TextRenderer = new QFontDrawing(); UIShader = ProgramFactory.Create <UIShaderProgram>(); UIShader.Use(); UIShader.Opacity.Set(1f); VAO = new VertexArray(); VBO = new Buffer <VertVT>(); VAO.Bind(); VAO.BindAttribute(UIShader.Position, VBO); VAO.BindAttribute(UIShader.TexCoord, VBO, 12); }
protected void OnLoad(object sender, EventArgs e) { // load textures into array for (var i = 0; i < _stateTextures.Length; i++) { using (var bitmap = new Bitmap(Path.Combine("Data/Textures/", _stateTextures[i]))) { bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY); if (_textureArray == null) BitmapTexture.CreateCompatible(bitmap, out _textureArray, _stateTextures.Length, 1); _textureArray.LoadBitmap(bitmap, i); } } // initialize buffer var field = new Minefield[FieldWidth*FieldHeight]; for (var i = 0; i < field.Length; i++) { field[i] = new Minefield(i%FieldWidth, i/FieldHeight, i%_stateTextures.Length); } _buffer = new Buffer<Minefield>(); _buffer.Init(BufferTarget.ArrayBuffer, field); // load program _gridProgram = ProgramFactory.Create<TextureGridProgram>(); _gridProgram.Use(); // bind the texture and set uniform _gridProgram.TextureData.BindTexture(TextureUnit.Texture0, _textureArray); // set up vertex array and attributes _vao = new VertexArray(); _vao.Bind(); _vao.BindAttribute(_gridProgram.InPosition, _buffer); _vao.BindAttribute(_gridProgram.InTexture, _buffer, 8); // set nice clear color GL.ClearColor(Color.MidnightBlue); // initialize camera position Camera.DefaultState.Position = new Vector3(0, 5, 15); Camera.ResetToDefault(); }
protected override void OnLoad() { // initialize shader _program = ProgramFactory.Create <SkyboxProgram>(); // initialize cube shape _cube = new Cube(); _cube.UpdateBuffers(); // initialize vertex array and attributes _vao = new VertexArray(); _vao.Bind(); _vao.BindAttribute(_program.InPosition, _cube.VertexBuffer); _vao.BindElementBuffer(_cube.IndexBuffer); // create cubemap texture and load all faces for (var i = 0; i < 6; i++) { using (var bitmap = new Bitmap(string.Format("Data/Textures/city{0}.jpg", i))) { bitmap.RotateFlip(RotateFlipType.RotateNoneFlipX); if (_skybox == null) { BitmapTexture.CreateCompatible(bitmap, out _skybox, 1); } _skybox.LoadBitmap(bitmap, i); } } // activate shader and bind texture to it _program.Use(); _program.Texture.BindTexture(TextureUnit.Texture0, _skybox); // enable seamless filtering to reduce artifacts at the edges of the cube faces GL.Enable(EnableCap.TextureCubeMapSeamless); // cull front faces because we are inside the cube // this is not really necessary but removes some artifacts when the user leaves the cube // which should be impossible for a real skybox, but in this demonstration it is possible by zooming out GL.Enable(EnableCap.CullFace); GL.CullFace(CullFaceMode.Front); // set a nice clear color GL.ClearColor(Color.MidnightBlue); }
private void OnLoad(object sender, EventArgs e) { // load texture from file using (var bitmap = new Bitmap("Data/Textures/checker.jpg")) { BitmapTexture.CreateCompatible(bitmap, out _texture); _texture.LoadBitmap(bitmap); } _texture.GenerateMipMaps(); // initialize sampler _sampler = new Sampler(); _sampler.SetWrapMode(TextureWrapMode.Repeat); // create vertex data for a big plane const int a = 10; const int b = 10; var vertices = new[] { new Vertex(-a, 0,-a, 0, 0), new Vertex( a, 0,-a, b, 0), new Vertex(-a, 0, a, 0, b), new Vertex( a, 0, a, b, b) }; // create buffer object and upload vertex data _vbo = new Buffer<Vertex>(); _vbo.Init(BufferTarget.ArrayBuffer, vertices); // initialize shader _program = ProgramFactory.Create<SimpleTextureProgram>(); // activate shader program _program.Use(); // bind sampler _sampler.Bind(TextureUnit.Texture0); // bind texture _program.Texture.BindTexture(TextureUnit.Texture0, _texture); // which is equivalent to //_program.Texture.Set(TextureUnit.Texture0); //_texture.Bind(TextureUnit.Texture0); // set up vertex array and attributes _vao = new VertexArray(); _vao.Bind(); // memory layout of our data is XYZUVXYZUV... // the buffer abstraction knows the total size of one "pack" of vertex data // and if a vertex attribute is bound without further arguments the first N elements are taken from each pack // where N is provided via the VertexAttribAttribute on the program property: _vao.BindAttribute(_program.InPosition, _vbo); // if data should not be taken from the start of each pack, the offset must be given in bytes // to reach the texture coordinates UV the XYZ coordinates must be skipped, that is 3 floats, i.e. an offset of 12 bytes is needed _vao.BindAttribute(_program.InTexCoord, _vbo, 12); // if needed all the available arguments can be specified manually, e.g. //_vao.BindAttribute(_program.InTexCoord, _vbo, 2, VertexAttribPointerType.Float, Marshal.SizeOf(typeof(Vertex)), 12, false); // set default camera Camera.DefaultState.Position = new Vector3(0, 0.5f, 3); Camera.ResetToDefault(); // set a nice clear color GL.ClearColor(Color.MidnightBlue); }
protected void OnLoad(object sender, EventArgs eventArgs) { VSync = VSyncMode.Off; // Check for necessary capabilities: var extensions = GL.GetString(StringName.Extensions); if (!GL.GetString(StringName.Extensions).Contains("GL_ARB_shading_language")) { throw new NotSupportedException(String.Format("This example requires OpenGL 2.0. Found {0}. Aborting.", GL.GetString(StringName.Version).Substring(0, 3))); } if (!extensions.Contains("GL_ARB_texture_compression") || !extensions.Contains("GL_EXT_texture_compression_s3tc")) { throw new NotSupportedException("This example requires support for texture compression. Aborting."); } var temp = new int[1]; GL.GetInteger(GetPName.MaxTextureImageUnits, out temp[0]); Trace.WriteLine(temp[0] + " TMU's for Fragment Shaders found. (2 required)"); GL.GetInteger(GetPName.MaxVaryingFloats, out temp[0]); Trace.WriteLine(temp[0] + " varying floats between VS and FS allowed. (6 required)"); GL.GetInteger(GetPName.MaxVertexUniformComponents, out temp[0]); Trace.WriteLine(temp[0] + " uniform components allowed in Vertex Shader. (6 required)"); GL.GetInteger(GetPName.MaxFragmentUniformComponents, out temp[0]); Trace.WriteLine(temp[0] + " uniform components allowed in Fragment Shader. (11 required)"); Trace.WriteLine(""); // load textures TextureLoaderParameters.MagnificationFilter = TextureMagFilter.Linear; TextureLoaderParameters.MinificationFilter = TextureMinFilter.LinearMipmapLinear; TextureLoaderParameters.WrapModeS = TextureWrapMode.ClampToBorder; TextureLoaderParameters.WrapModeT = TextureWrapMode.ClampToBorder; TextureLoaderParameters.EnvMode = TextureEnvMode.Modulate; uint handle; TextureTarget target; ImageDDS.LoadFromDisk(TextureDiffuseHeightFilename, out handle, out target); _textureDiffuseHeight = TextureFactory.AquireTexture2D((int) handle); Trace.WriteLine("Loaded " + TextureDiffuseHeightFilename + " with handle " + handle + " as " + target); ImageDDS.LoadFromDisk(TextureNormalGlossFilename, out handle, out target); _textureNormalGloss = TextureFactory.AquireTexture2D((int) handle); Trace.WriteLine("Loaded " + TextureNormalGlossFilename + " with handle " + handle + " as " + target); Trace.WriteLine("End of Texture Loading. GL Error: " + GL.GetError()); Trace.WriteLine(""); // initialize buffer var normal = Vector3.UnitZ; var tangent = Vector3.UnitX; var vertices = new[] { new Vertex { Position = new Vector3(-1,-1,0), TexCoord = new Vector2(0,0), Normal = normal, Tangent = tangent }, new Vertex { Position = new Vector3(1,-1,0), TexCoord = new Vector2(1,0), Normal = normal, Tangent = tangent }, new Vertex { Position = new Vector3(-1,1,0), TexCoord = new Vector2(0,1), Normal = normal, Tangent = tangent }, new Vertex { Position = new Vector3(1,1,0), TexCoord = new Vector2(1,1), Normal = normal, Tangent = tangent } }; _buffer = new Buffer<Vertex>(); _buffer.Init(BufferTarget.ArrayBuffer, vertices); // load shader _program = ProgramFactory.Create<ParallaxProgram>(); _program.Use(); // set up vertex array _vao = new VertexArray(); _vao.Bind(); // bind vertex attributes // the buffer layout is defined by the Vertex struct: // data X Y Z NX NY NZ TX TY TZ U V *next vertex* // offset 0 4 8 12 16 20 24 28 32 36 40 44 // having to work with offsets could be prevented by using seperate buffer objects for each attribute, // but that might reduce performance and can get annoying as well. // performance increase could also be achieved by improving coherent read access // by padding the struct so that each attribute begins at a multiple of 16 bytes, i.e. 4-floats // because the GPU can then read all 4 floats at once. I did not do that here to keep it simple. _vao.BindAttribute(_program.InPosition, _buffer); _vao.BindAttribute(_program.InNormal, _buffer, 12); _vao.BindAttribute(_program.InTangent, _buffer, 24); _vao.BindAttribute(_program.InTexCoord, _buffer, 36); // set camera position Camera.DefaultState.Position = new Vector3(0,0,3); Camera.ResetToDefault(); // set state GL.ClearColor(0.2f, 0f, 0.4f, 0f); GL.PointSize(10f); GL.Disable(EnableCap.Dither); GL.FrontFace(FrontFaceDirection.Ccw); GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill); GL.PolygonMode(MaterialFace.Back, PolygonMode.Line); }
private void OnLoad(object sender, EventArgs e) { // load texture from file using (var bitmap = new Bitmap("Data/Textures/checker.jpg")) { BitmapTexture.CreateCompatible(bitmap, out _texture); _texture.LoadBitmap(bitmap); } _texture.GenerateMipMaps(); // initialize sampler _sampler = new Sampler(); _sampler.SetWrapMode(TextureWrapMode.Repeat); // create vertex data for a big plane const int a = 10; const int b = 10; var vertices = new[] { new Vertex(-a, 0, -a, 0, 0), new Vertex(a, 0, -a, b, 0), new Vertex(-a, 0, a, 0, b), new Vertex(a, 0, a, b, b) }; // create buffer object and upload vertex data _vbo = new Buffer <Vertex>(); _vbo.Init(BufferTarget.ArrayBuffer, vertices); // initialize shader _program = ProgramFactory.Create <SimpleTextureProgram>(); // activate shader program _program.Use(); // bind sampler _sampler.Bind(TextureUnit.Texture0); // bind texture _program.Texture.BindTexture(TextureUnit.Texture0, _texture); // which is equivalent to //_program.Texture.Set(TextureUnit.Texture0); //_texture.Bind(TextureUnit.Texture0); // set up vertex array and attributes _vao = new VertexArray(); _vao.Bind(); // memory layout of our data is XYZUVXYZUV... // the buffer abstraction knows the total size of one "pack" of vertex data // and if a vertex attribute is bound without further arguments the first N elements are taken from each pack // where N is provided via the VertexAttribAttribute on the program property: _vao.BindAttribute(_program.InPosition, _vbo); // if data should not be taken from the start of each pack, the offset must be given in bytes // to reach the texture coordinates UV the XYZ coordinates must be skipped, that is 3 floats, i.e. an offset of 12 bytes is needed _vao.BindAttribute(_program.InTexCoord, _vbo, 12); // if needed all the available arguments can be specified manually, e.g. //_vao.BindAttribute(_program.InTexCoord, _vbo, 2, VertexAttribPointerType.Float, Marshal.SizeOf(typeof(Vertex)), 12, false); // set default camera Camera.DefaultState.Position = new Vector3(0, 0.5f, 3); Camera.ResetToDefault(); // set a nice clear color GL.ClearColor(Color.MidnightBlue); }
protected override void OnLoad() { VSync = VSyncMode.Off; // Check for necessary capabilities: var extensions = GL.GetString(StringName.Extensions); if (!GL.GetString(StringName.Extensions).Contains("GL_ARB_shading_language")) { throw new NotSupportedException(String.Format("This example requires OpenGL 2.0. Found {0}. Aborting.", GL.GetString(StringName.Version).Substring(0, 3))); } if (!extensions.Contains("GL_ARB_texture_compression") || !extensions.Contains("GL_EXT_texture_compression_s3tc")) { throw new NotSupportedException("This example requires support for texture compression. Aborting."); } var temp = new int[1]; GL.GetInteger(GetPName.MaxTextureImageUnits, out temp[0]); Trace.WriteLine(temp[0] + " TMU's for Fragment Shaders found. (2 required)"); GL.GetInteger(GetPName.MaxVaryingFloats, out temp[0]); Trace.WriteLine(temp[0] + " varying floats between VS and FS allowed. (6 required)"); GL.GetInteger(GetPName.MaxVertexUniformComponents, out temp[0]); Trace.WriteLine(temp[0] + " uniform components allowed in Vertex Shader. (6 required)"); GL.GetInteger(GetPName.MaxFragmentUniformComponents, out temp[0]); Trace.WriteLine(temp[0] + " uniform components allowed in Fragment Shader. (11 required)"); Trace.WriteLine(""); // load textures TextureLoaderParameters.MagnificationFilter = TextureMagFilter.Linear; TextureLoaderParameters.MinificationFilter = TextureMinFilter.LinearMipmapLinear; TextureLoaderParameters.WrapModeS = TextureWrapMode.ClampToBorder; TextureLoaderParameters.WrapModeT = TextureWrapMode.ClampToBorder; TextureLoaderParameters.EnvMode = TextureEnvMode.Modulate; uint handle; TextureTarget target; ImageDDS.LoadFromDisk(TextureDiffuseHeightFilename, out handle, out target); _textureDiffuseHeight = TextureFactory.AquireTexture2D((int)handle); Trace.WriteLine("Loaded " + TextureDiffuseHeightFilename + " with handle " + handle + " as " + target); ImageDDS.LoadFromDisk(TextureNormalGlossFilename, out handle, out target); _textureNormalGloss = TextureFactory.AquireTexture2D((int)handle); Trace.WriteLine("Loaded " + TextureNormalGlossFilename + " with handle " + handle + " as " + target); Trace.WriteLine("End of Texture Loading. GL Error: " + GL.GetError()); Trace.WriteLine(""); // initialize buffer var normal = Vector3.UnitZ; var tangent = Vector3.UnitX; var vertices = new[] { new Vertex { Position = new Vector3(-1, -1, 0), TexCoord = new Vector2(0, 0), Normal = normal, Tangent = tangent }, new Vertex { Position = new Vector3(1, -1, 0), TexCoord = new Vector2(1, 0), Normal = normal, Tangent = tangent }, new Vertex { Position = new Vector3(-1, 1, 0), TexCoord = new Vector2(0, 1), Normal = normal, Tangent = tangent }, new Vertex { Position = new Vector3(1, 1, 0), TexCoord = new Vector2(1, 1), Normal = normal, Tangent = tangent } }; _buffer = new Buffer <Vertex>(); _buffer.Init(BufferTarget.ArrayBuffer, vertices); // load shader _program = ProgramFactory.Create <ParallaxProgram>(); _program.Use(); // set up vertex array _vao = new VertexArray(); _vao.Bind(); // bind vertex attributes // the buffer layout is defined by the Vertex struct: // data X Y Z NX NY NZ TX TY TZ U V *next vertex* // offset 0 4 8 12 16 20 24 28 32 36 40 44 // having to work with offsets could be prevented by using seperate buffer objects for each attribute, // but that might reduce performance and can get annoying as well. // performance increase could also be achieved by improving coherent read access // by padding the struct so that each attribute begins at a multiple of 16 bytes, i.e. 4-floats // because the GPU can then read all 4 floats at once. I did not do that here to keep it simple. _vao.BindAttribute(_program.InPosition, _buffer); _vao.BindAttribute(_program.InNormal, _buffer, 12); _vao.BindAttribute(_program.InTangent, _buffer, 24); _vao.BindAttribute(_program.InTexCoord, _buffer, 36); // set camera position Camera.DefaultState.Position = new Vector3(0, 0, 3); Camera.ResetToDefault(); // set state GL.ClearColor(0.2f, 0f, 0.4f, 0f); GL.PointSize(10f); GL.Disable(EnableCap.Dither); GL.FrontFace(FrontFaceDirection.Ccw); GL.PolygonMode(MaterialFace.Front, PolygonMode.Fill); GL.PolygonMode(MaterialFace.Back, PolygonMode.Line); }