-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathRenderScene.cs
137 lines (123 loc) · 5.4 KB
/
RenderScene.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using System;
using System.Globalization;
using System.Threading;
using OpenTK;
using OpenTK.Graphics.OpenGL;
namespace _056avatar
{
public partial class Form1
{
/// <summary>
/// Function called whenever the main application is idle..
/// </summary>
private void Application_Idle ( object sender, EventArgs e )
{
while ( glControl1.IsIdle )
{
glControl1.Invalidate();
Thread.Sleep( 5 );
long now = DateTime.Now.Ticks;
if ( now - lastFpsTime > 5000000 ) // more than 0.5 sec
{
lastFps = 0.5 * lastFps + 0.5 * (frameCounter * 1.0e7 / (now - lastFpsTime));
lastTps = 0.5 * lastTps + 0.5 * (triangleCounter * 1.0e7 / (now - lastFpsTime));
lastFpsTime = now;
frameCounter = 0;
triangleCounter = 0L;
if ( lastTps < 5.0e5 )
labelFps.Text = string.Format( CultureInfo.InvariantCulture, "Fps: {0:f1}, Tps: {1:f0}k",
lastFps, (lastTps * 1.0e-3) );
else
labelFps.Text = string.Format( CultureInfo.InvariantCulture, "Fps: {0:f1}, Tps: {1:f1}m",
lastFps, (lastTps * 1.0e-6) );
}
}
}
/// <summary>
/// Rendering code itself (separated for clarity).
/// </summary>
private void RenderScene ()
{
// Scene rendering:
if ( useVBO &&
scene != null &&
scene.Triangles > 0 ) // scene is nonempty => render it
{
// [colors] [normals] vertices
GL.BindBuffer( BufferTarget.ArrayBuffer, VBOid[ 0 ] );
IntPtr p = IntPtr.Zero;
if ( scene.HasColors() )
{
GL.ColorPointer( 3, ColorPointerType.Float, stride, p );
p += Vector3.SizeInBytes;
}
if ( scene.HasNormals() )
{
GL.NormalPointer( NormalPointerType.Float, stride, p );
p += Vector3.SizeInBytes;
}
GL.VertexPointer( 3, VertexPointerType.Float, stride, p );
// index buffer
GL.BindBuffer( BufferTarget.ElementArrayBuffer, VBOid[ 1 ] );
// Multiple instancing of the scene:
Vector3 center;
float delta = scene.GetDiameter( out center ) * 0.8f;
int n = (int)numericInstances.Value;
for ( int k = 0; k++ < n; )
{
GL.PushMatrix();
for ( int j = 0; j++ < n; )
{
GL.PushMatrix();
for ( int i = 0; i++ < n; )
{
triangleCounter += scene.Triangles;
GL.DrawElements( PrimitiveType.Triangles, scene.Triangles * 3, DrawElementsType.UnsignedInt, IntPtr.Zero );
GL.Translate( delta, 0.0f, 0.0f );
}
GL.PopMatrix();
GL.Translate( 0.0f, 0.0f, -delta );
}
GL.PopMatrix();
GL.Translate( 0.0f, delta, 0.0f );
}
}
else // color cube (JB)
{
GL.Begin( PrimitiveType.Quads );
GL.Color3( 0.0f, 1.0f, 0.0f ); // Set The Color To Green
GL.Vertex3( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Top)
GL.Vertex3( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Top)
GL.Vertex3( -1.0f, 1.0f, 1.0f ); // Bottom Left Of The Quad (Top)
GL.Vertex3( 1.0f, 1.0f, 1.0f ); // Bottom Right Of The Quad (Top)
GL.Color3( 1.0f, 0.5f, 0.0f ); // Set The Color To Orange
GL.Vertex3( 1.0f, -1.0f, 1.0f ); // Top Right Of The Quad (Bottom)
GL.Vertex3( -1.0f, -1.0f, 1.0f ); // Top Left Of The Quad (Bottom)
GL.Vertex3( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad (Bottom)
GL.Vertex3( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad (Bottom)
GL.Color3( 1.0f, 0.0f, 0.0f ); // Set The Color To Red
GL.Vertex3( 1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Front)
GL.Vertex3( -1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Front)
GL.Vertex3( -1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad (Front)
GL.Vertex3( 1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad (Front)
GL.Color3( 1.0f, 1.0f, 0.0f ); // Set The Color To Yellow
GL.Vertex3( 1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad (Back)
GL.Vertex3( -1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad (Back)
GL.Vertex3( -1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Back)
GL.Vertex3( 1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Back)
GL.Color3( 0.0f, 0.0f, 1.0f ); // Set The Color To Blue
GL.Vertex3( -1.0f, 1.0f, 1.0f ); // Top Right Of The Quad (Left)
GL.Vertex3( -1.0f, 1.0f, -1.0f ); // Top Left Of The Quad (Left)
GL.Vertex3( -1.0f, -1.0f, -1.0f ); // Bottom Left Of The Quad (Left)
GL.Vertex3( -1.0f, -1.0f, 1.0f ); // Bottom Right Of The Quad (Left)
GL.Color3( 1.0f, 0.0f, 1.0f ); // Set The Color To Violet
GL.Vertex3( 1.0f, 1.0f, -1.0f ); // Top Right Of The Quad (Right)
GL.Vertex3( 1.0f, 1.0f, 1.0f ); // Top Left Of The Quad (Right)
GL.Vertex3( 1.0f, -1.0f, 1.0f ); // Bottom Left Of The Quad (Right)
GL.Vertex3( 1.0f, -1.0f, -1.0f ); // Bottom Right Of The Quad (Right)
GL.End();
triangleCounter += 12;
}
}
}
}