Skip to content

Commit

Permalink
Merge pull request #11 from veeman/feature/example-textured-cube
Browse files Browse the repository at this point in the history
Basic example with rotating textured cube added
  • Loading branch information
frederikja163 authored Aug 28, 2020
2 parents 65b1338 + 858c19b commit d908e47
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 0 deletions.
134 changes: 134 additions & 0 deletions Examples/BasicExamples/RotatingTexturedCubeExample.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
using System;
using System.Diagnostics;
using System.Drawing;
using Examples.Shaders;
using ObjectTK.Buffers;
using ObjectTK.Shaders;
using ObjectTK.Textures;
using ObjectTK.Tools.Shapes;
using OpenTK;
using OpenTK.Graphics.OpenGL;
using OpenTK.Input;

namespace Examples.BasicExamples
{
[ExampleProject("Rotating textured cube rendering")]
public class RotatingTexturedCubeExample
: ExampleWindow
{
private Texture2D _texture;

private SimpleTextureProgram _textureProgram;

private TexturedCube _cube;
private VertexArray _cubeVao;

private Matrix4 _baseView;
private Matrix4 _objectView;

private Vector3[] _rotateVectors = new[] { Vector3.Zero, Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ, Vector3.One };
private const int _defaultRotateIndex = 4;

private int _rotateIndex = _defaultRotateIndex;
private readonly Stopwatch _stopwatch = new Stopwatch();

public RotatingTexturedCubeExample()
{
Load += OnLoad;
RenderFrame += OnRenderFrame;
KeyDown += OnKeyDown;
}

private void OnKeyDown(object sender, KeyboardKeyEventArgs e)
{
switch (e.Key)
{
case Key.R:
_objectView = _baseView = Matrix4.Identity;
_rotateIndex = _defaultRotateIndex;
_stopwatch.Restart();
break;

case Key.Space:
_baseView = _objectView;
_rotateIndex = (_rotateIndex + 1) % _rotateVectors.Length;
_stopwatch.Restart();
break;

case Key.Number0:
case Key.Number1:
case Key.Number2:
case Key.Number3:
case Key.Number4:
_baseView = _objectView;
_rotateIndex = (e.Key - Key.Number0) % _rotateVectors.Length;
_stopwatch.Restart();
break;
}
}

private void OnLoad(object sender, EventArgs e)
{
// 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();
}

private void OnRenderFrame(object sender, OpenTK.FrameEventArgs e)
{
// set up viewport
GL.Viewport(0, 0, Width, Height);
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
SetupPerspective();

// determinate object view rotation vectors and apply them
_objectView = _baseView;
var rotation = _rotateVectors[_rotateIndex];
if (rotation != Vector3.Zero)
_objectView *= Matrix4.CreateFromAxisAngle(_rotateVectors[_rotateIndex], (float)(_stopwatch.Elapsed.TotalSeconds * 1.0));

// set transformation matrix
_textureProgram.Use();
_textureProgram.ModelViewProjectionMatrix.Set(_objectView * ModelView * Projection);

// render cube with texture
_cubeVao.Bind();
_cubeVao.DrawArrays(_cube.DefaultMode, 0, _cube.VertexBuffer.ElementCount);

// swap buffers
SwapBuffers();
}
}
}
Binary file added Examples/Data/Textures/crate.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions Examples/Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<Compile Include="AdvancedExamples\LoaderDDS.cs" />
<Compile Include="AdvancedExamples\LoaderStatics.cs" />
<Compile Include="AdvancedExamples\ParallaxMappingExample.cs" />
<Compile Include="BasicExamples\RotatingTexturedCubeExample.cs" />
<Compile Include="BasicExamples\TextureGridExample.cs" />
<Compile Include="BasicExamples\SkyboxExample.cs" />
<Compile Include="ExampleProjectAttribute.cs" />
Expand Down Expand Up @@ -106,6 +107,7 @@
<Content Include="Data\Textures\city3.jpg" />
<Content Include="Data\Textures\city4.jpg" />
<Content Include="Data\Textures\city5.jpg" />
<Content Include="Data\Textures\crate.png" />
<Content Include="Data\Textures\empty.png" />
<Content Include="Data\Textures\flag.png" />
<Content Include="Data\Textures\mine.png" />
Expand Down
1 change: 1 addition & 0 deletions ObjectTK.Tools/ObjectTK.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
<Compile Include="Shapes\Quad.cs" />
<Compile Include="Shapes\Rect.cs" />
<Compile Include="Shapes\Shape.cs" />
<Compile Include="Shapes\TexturedCube.cs" />
<Compile Include="Shapes\TexturedQuad.cs" />
<Compile Include="Shapes\TexturedShape.cs" />
</ItemGroup>
Expand Down
45 changes: 45 additions & 0 deletions ObjectTK.Tools/Shapes/TexturedCube.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// TexturedCube.cs
//
// Copyright (C) 2018 OpenTK
//
// This software may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
//

using OpenTK;
using OpenTK.Graphics.OpenGL;
using System.Linq;

namespace ObjectTK.Tools.Shapes
{
public class TexturedCube
: TexturedShape
{
public TexturedCube()
{
DefaultMode = PrimitiveType.Triangles;

var quad_uv_map = new[]
{
new Vector2(0, 0),
new Vector2(0, 1),
new Vector2(1, 1),
new Vector2(1, 1),
new Vector2(1, 0),
new Vector2(0, 0),
};

// use default cube
using (var cube = new Cube())
{
// Cube uses indexed vertices, TexturedShape assumes a flat vertices array
// So we need to assemble the missing vertices ourself
Vertices = cube.Indices.Select(idx => cube.Vertices[idx]).ToArray();

// Use predefined uv texture mapping for vertices
TexCoords = Enumerable.Range(0, Vertices.Length).Select(i => quad_uv_map[i % quad_uv_map.Length]).ToArray();
}
}
}
}

0 comments on commit d908e47

Please sign in to comment.