Skip to content

Commit d908e47

Browse files
Merge pull request #11 from veeman/feature/example-textured-cube
Basic example with rotating textured cube added
2 parents 65b1338 + 858c19b commit d908e47

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
using System;
2+
using System.Diagnostics;
3+
using System.Drawing;
4+
using Examples.Shaders;
5+
using ObjectTK.Buffers;
6+
using ObjectTK.Shaders;
7+
using ObjectTK.Textures;
8+
using ObjectTK.Tools.Shapes;
9+
using OpenTK;
10+
using OpenTK.Graphics.OpenGL;
11+
using OpenTK.Input;
12+
13+
namespace Examples.BasicExamples
14+
{
15+
[ExampleProject("Rotating textured cube rendering")]
16+
public class RotatingTexturedCubeExample
17+
: ExampleWindow
18+
{
19+
private Texture2D _texture;
20+
21+
private SimpleTextureProgram _textureProgram;
22+
23+
private TexturedCube _cube;
24+
private VertexArray _cubeVao;
25+
26+
private Matrix4 _baseView;
27+
private Matrix4 _objectView;
28+
29+
private Vector3[] _rotateVectors = new[] { Vector3.Zero, Vector3.UnitX, Vector3.UnitY, Vector3.UnitZ, Vector3.One };
30+
private const int _defaultRotateIndex = 4;
31+
32+
private int _rotateIndex = _defaultRotateIndex;
33+
private readonly Stopwatch _stopwatch = new Stopwatch();
34+
35+
public RotatingTexturedCubeExample()
36+
{
37+
Load += OnLoad;
38+
RenderFrame += OnRenderFrame;
39+
KeyDown += OnKeyDown;
40+
}
41+
42+
private void OnKeyDown(object sender, KeyboardKeyEventArgs e)
43+
{
44+
switch (e.Key)
45+
{
46+
case Key.R:
47+
_objectView = _baseView = Matrix4.Identity;
48+
_rotateIndex = _defaultRotateIndex;
49+
_stopwatch.Restart();
50+
break;
51+
52+
case Key.Space:
53+
_baseView = _objectView;
54+
_rotateIndex = (_rotateIndex + 1) % _rotateVectors.Length;
55+
_stopwatch.Restart();
56+
break;
57+
58+
case Key.Number0:
59+
case Key.Number1:
60+
case Key.Number2:
61+
case Key.Number3:
62+
case Key.Number4:
63+
_baseView = _objectView;
64+
_rotateIndex = (e.Key - Key.Number0) % _rotateVectors.Length;
65+
_stopwatch.Restart();
66+
break;
67+
}
68+
}
69+
70+
private void OnLoad(object sender, EventArgs e)
71+
{
72+
// load texture from file
73+
using (var bitmap = new Bitmap("Data/Textures/crate.png"))
74+
{
75+
BitmapTexture.CreateCompatible(bitmap, out _texture);
76+
_texture.LoadBitmap(bitmap);
77+
}
78+
79+
// initialize shaders
80+
_textureProgram = ProgramFactory.Create<SimpleTextureProgram>();
81+
82+
// initialize cube object and base view matrix
83+
_objectView = _baseView = Matrix4.Identity;
84+
85+
// initialize demonstration geometry
86+
_cube = new TexturedCube();
87+
_cube.UpdateBuffers();
88+
89+
// set up vertex attributes for the quad
90+
_cubeVao = new VertexArray();
91+
_cubeVao.Bind();
92+
_cubeVao.BindAttribute(_textureProgram.InPosition, _cube.VertexBuffer);
93+
_cubeVao.BindAttribute(_textureProgram.InTexCoord, _cube.TexCoordBuffer);
94+
95+
// Enable culling, our cube vertices are defined inside out, so we flip them
96+
GL.Enable(EnableCap.CullFace);
97+
GL.CullFace(CullFaceMode.Back);
98+
99+
// initialize camera position
100+
Camera.DefaultState.Position = new Vector3(0, 0, 4);
101+
Camera.ResetToDefault();
102+
103+
// set nice clear color
104+
GL.ClearColor(Color.MidnightBlue);
105+
106+
_stopwatch.Restart();
107+
}
108+
109+
private void OnRenderFrame(object sender, OpenTK.FrameEventArgs e)
110+
{
111+
// set up viewport
112+
GL.Viewport(0, 0, Width, Height);
113+
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
114+
SetupPerspective();
115+
116+
// determinate object view rotation vectors and apply them
117+
_objectView = _baseView;
118+
var rotation = _rotateVectors[_rotateIndex];
119+
if (rotation != Vector3.Zero)
120+
_objectView *= Matrix4.CreateFromAxisAngle(_rotateVectors[_rotateIndex], (float)(_stopwatch.Elapsed.TotalSeconds * 1.0));
121+
122+
// set transformation matrix
123+
_textureProgram.Use();
124+
_textureProgram.ModelViewProjectionMatrix.Set(_objectView * ModelView * Projection);
125+
126+
// render cube with texture
127+
_cubeVao.Bind();
128+
_cubeVao.DrawArrays(_cube.DefaultMode, 0, _cube.VertexBuffer.ElementCount);
129+
130+
// swap buffers
131+
SwapBuffers();
132+
}
133+
}
134+
}

Examples/Data/Textures/crate.png

336 KB
Loading

Examples/Examples.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
<Compile Include="AdvancedExamples\LoaderDDS.cs" />
5151
<Compile Include="AdvancedExamples\LoaderStatics.cs" />
5252
<Compile Include="AdvancedExamples\ParallaxMappingExample.cs" />
53+
<Compile Include="BasicExamples\RotatingTexturedCubeExample.cs" />
5354
<Compile Include="BasicExamples\TextureGridExample.cs" />
5455
<Compile Include="BasicExamples\SkyboxExample.cs" />
5556
<Compile Include="ExampleProjectAttribute.cs" />
@@ -106,6 +107,7 @@
106107
<Content Include="Data\Textures\city3.jpg" />
107108
<Content Include="Data\Textures\city4.jpg" />
108109
<Content Include="Data\Textures\city5.jpg" />
110+
<Content Include="Data\Textures\crate.png" />
109111
<Content Include="Data\Textures\empty.png" />
110112
<Content Include="Data\Textures\flag.png" />
111113
<Content Include="Data\Textures\mine.png" />

ObjectTK.Tools/ObjectTK.Tools.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<Compile Include="Shapes\Quad.cs" />
6262
<Compile Include="Shapes\Rect.cs" />
6363
<Compile Include="Shapes\Shape.cs" />
64+
<Compile Include="Shapes\TexturedCube.cs" />
6465
<Compile Include="Shapes\TexturedQuad.cs" />
6566
<Compile Include="Shapes\TexturedShape.cs" />
6667
</ItemGroup>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//
2+
// TexturedCube.cs
3+
//
4+
// Copyright (C) 2018 OpenTK
5+
//
6+
// This software may be modified and distributed under the terms
7+
// of the MIT license. See the LICENSE file for details.
8+
//
9+
10+
using OpenTK;
11+
using OpenTK.Graphics.OpenGL;
12+
using System.Linq;
13+
14+
namespace ObjectTK.Tools.Shapes
15+
{
16+
public class TexturedCube
17+
: TexturedShape
18+
{
19+
public TexturedCube()
20+
{
21+
DefaultMode = PrimitiveType.Triangles;
22+
23+
var quad_uv_map = new[]
24+
{
25+
new Vector2(0, 0),
26+
new Vector2(0, 1),
27+
new Vector2(1, 1),
28+
new Vector2(1, 1),
29+
new Vector2(1, 0),
30+
new Vector2(0, 0),
31+
};
32+
33+
// use default cube
34+
using (var cube = new Cube())
35+
{
36+
// Cube uses indexed vertices, TexturedShape assumes a flat vertices array
37+
// So we need to assemble the missing vertices ourself
38+
Vertices = cube.Indices.Select(idx => cube.Vertices[idx]).ToArray();
39+
40+
// Use predefined uv texture mapping for vertices
41+
TexCoords = Enumerable.Range(0, Vertices.Length).Select(i => quad_uv_map[i % quad_uv_map.Length]).ToArray();
42+
}
43+
}
44+
}
45+
}

0 commit comments

Comments
 (0)