-
Notifications
You must be signed in to change notification settings - Fork 9
/
Über
235 lines (196 loc) · 10.2 KB
/
Über
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
+-----------+
|+---------+|
||sparrow3D||
|+---------+|
+-----------+
Dieses Dokument dient der Veranschaulichung der Zusammenhänge und
Logik der Funktionen von sparrow3D. Erstmal in Deutsch. Gerade mir wird
es hoffentlich helfen die Bibliothek auch für außenstehende logisch
erscheinen zu lassen.
+----------+
|Einleitung|
+----------+
Sparrow3D ist ein Software Renderer und eine (Spiele)Engine. Sie bietet
dir Möglichkeiten für das Eventhandling, 2D und 3D Funktionen, Anzeige
von Meshs, eine simple Spritesteuerung und Darstellung von Schrift.
Dabei basiert es komplett auf SDL - welches neben SDL-Image und SDL-Font
die einzige Abhängigkeit von sparrow3D ist. So läuft sparrow3D (bisher)
auf folgenden Systemen:
- GP2X
- WIZ
- Dingoo
- Cannoo
- Pandora
- PC (Linux und Windows)
Es verbessert SDL dahingehend, dass es nochmal abstrahiert. Das
bedeutet, dass man sich nicht mehr um unterschiedliche Buttonnamen,
Buttonanzahl oder verschiedene Achsen (D-Pad oder Analogstick z.B.)
kümmern muss. Man programmiert einfach für ein generisches Gerät.
Die Vorteile/Features von sparrow3D sind im Groben:
- schnell, da komplett in C geschrieben
- Abstraktion der Hardware
- nochmal abstrahierte Surface-Funktionen für Blitting
- schnellerer Rotozoom als z.B. SDL-gfx
- Primitive wie Rechtecke oder Ellipsen
- 3D Software Renderer, der auch auf GP2X und Dingoo läuft
- Sprite Engine
- Laden von Meshs
- Darstellung von Schrift aus ttf-Dateien (damit beliebig skalierbar)
Es folgt eine kurze Vorstellung der einzelnen Teile von sparrow3D. Man
kann, muss jedoch nicht alle nutzen. Es ist ohne weiteres möglich nur
den 3D Software Renderer in ein bestehendes SDL Programm einzubinden.
+-----------+
|sparrowCore|
+-----------+
Obwohl der Name es vermuten lässt, ist dieser Part der Bibliothek nicht
zwingend essentiell. Wer rein am Sprite oder 3D Handling interessiert
ist, kann sie getrost ignorieren.
Der Cort Part bietet einem ein Fenster, Eventhandling - einhergehend mit
einer Abstraktion für die verschiedenen Targets - und das Laden von
optimalen Surfaces für das System (schneller).
Außerdem gibt es eine Feedbackfunktion für die Größenänderung des
Hauptfensters. Diese ist dafür gedacht, falls man in dem Fall z.B.
größere oder kleinen Surfaces laden oder erstellen möchte.
+-----------+
|sparrowMath|
+-----------+
Viele Teile, mindestens jedoch _alle_ 3D Funktionalitäten, basieren auf
Festkommazahlen. Für Leute, die noch nie damit gearbeitet haben: Vom
Prinzip her sind Festkommazahlen wie normale Integer. Nur wird im Falle
von Festkommazahlen die Zahl noch durch 65536 geteilt. Das bedeutet
65536 ist als Festkommazahl 65536/65536 = 1.0, 128 ist 128/65536 =
0.001953125 usw. .
Festkommazahlen sind (wesentlich) ungenauer als die klassichen Fließ-
kommazahlen, aber auf ARM-Systemen ohne FPU um _Längen_ schneller.
Für Addition und Subtraktion ändert sich hierbei nichts. Nur die
Multiplikation und Division braucht extra Funktionen, die sparrowMath
definiert. Auch Kosinus und Sinus sind langsam. Deshalb gibt einem
sparrow3D die Möglichkeit vorberechnete und schon in das eigene Zahlen-
system umgewandelte Funktionen zu nutzen.
Außerdem bietet einem sparrowMath noch andere Vereinfachungen und z.B.
eine Umrechnung vom HSV Raum in eine 16 Bit Farbe.
+-----------------+
|sparrowPrimitives|
+-----------------+
Für viele wird sparrowPrimitives der wichtigste Bestandteil von
sparrow3D sein. Er kümmert sich um das Zeichnen aller möglicher
Primitive in 2D. Ein wichtiger Aspekt von sparrow3D ist, dass es auf
SDL aufbaut, es verbessert und erweitert. Zuerst muss man ein
Zeichenziel wählen. Das kann der Bildschirm sein, aber auch jedes andere
billibige Surface. Jedoch ist GANZ wichtig, ich wiederhole mich: SUPER
WICHTIG zu beachten, dass ALLE Ziele 16 Bit Surfaces sein müssen!
Solltest du z.B. in ein 32 Bit Surface rendern wollen, musst du ein 16
Bit Surface erstellen, dort rein rendern und dieses dann auf das 32 Bit
Surface blitten (mit SDL Mitteln). Das war aus Vereinfachungs-, aber
vor allem auch Optimierungsgründen notwendig.
Außerdem gibt es bei sparrow3D kein "echtes" Alphablending. Per
Definition ist die Farbe Rosa (definiert als SP_ALPHA_COLOR) komplett
durchsichtig, der Rest komplett sichtbar. Bei Bedarf kann der Alphatest
jedoch auch ausgeschaltet werden.
Es gibt kein Standardziel. Am Anfang MUSS eines gewählt werden! Danach
arbeiten alle Zeichenoperationen in dieses Surface. Weiterhin wird für
das Surface ein zBuffer erstellt, der sich die Tiefenpositionen von
Primitiven merkt. Sowohl das Überprüfen (beim Zeichnen) als auch das
Setzen des zBuffers sind einstellbar. Prinzipiell ist das Schreiben mit
ohne Beides am schnellsten. Aber spätestens bei dreidimensionalen Szenen
ist der zBuffer sehr wichtig für eine korrekte Darstellung der
Primitivreihenfolge.
Es gibt grob folgende Bereiche, die sparrowPrimitives abdeckt:
- Zeichnen von Dreiecken und Vierecken mit oder ohne Textur
- Rotozoom Zeichnen von Surfaces oder Surfaceteilen (Tiling)
- Blitten von Surfaces (ohne Rotation und Zoom, dafür schneller) oder
Surfaceteilen (Tiling)
- Zeichnen von beliebig farbigen Linien, Rechtecken, Ellipsen und
Rechteck- oder Ellipsenrändern
Alle diese Funktionen können je nach Belieben in den zBuffer schreiben.
Dieser sollte im Beginn des Zeichenvorganges deshalb genau wie der
gesamte Bildschirm komplett gelöscht werden, was sparrowPrimitives
auch vermag.
Eine Besonderheit ist das Culling. Die Dreiecks- und Vierecksfunktionen
(NICHT die Rechtecksfunktionen!) besitzen Cullingfähigkeiten.
D.h. wenn man die einzelnen Punkte der Primitves in der falschen Reihen-
folge angibt, werden sie nicht gezeichnet. Bei dreidimensionalen,
geschlossenen Körpern ist das sehr wichtig zur Optimierung, da man die
Rückseite einer Fläche nicht sehen kann. Die notwendig Reihenfolge ist
immer GEGEN den Urzeigersinn (im Zweifel einfach ausprobieren).
+-------------+
|sparrowRender|
+-------------+
Dieser wichtige Part von sparrow3D gibt der Engine ihrem Namen: Er sorgt
für die Dreidimensionalität. Aufbauend auf sparrowPrimitive gibt es
verschiedene Funktionalitäten. Genauso wie OpenGL benutzt sparrow3D
für die Orientierung Matrizen. Ein kurzes OpenGL Tutorial sollte einem
genug Einblick geben, um auch mit sparrow3D arbeiten zu können. Es ist
trotzdem nicht kompatibel zu OpenGL! Aus Geschwindigkeitsgründen wurde
dieser Gedanke schnell fallen gelassen.
Wie bei OpenGL gibt es zwei Matrizen für die Orientierung im Raum
(Modelviewmatrix) und die Projektion (Projektionsmatrix). Am einfachsten
stellt man sich die Modelviewmatrix wie einen unsichtbaren Robotergreif-
arm vor, den man beliebig bewegen kann. Dieses Beispiel ist wichtig zu
verinnerlichen, weil alle Positions-, Rotations- und Skalierungsangaben
relativ zueinander sind. D.h. wenn du den "Roboterarm" um 2 auf der
z-Achse bewegst und den Befehl noch einmal aufrufst, ist er nun bei z=4.
Das gilt auch für die Rotation. Hier gilt: Learning by Doing!
Man kann die ModelViewMatrix resetten, Translatieren, Rotieren und
Skalieren. Außerdem kann man die Matrix auch von Hand bearbeiten, wenn
es einem beliebt oder durch auslesen (und sichern!) der Matrix glPush
bzw. glPop nachbilden (siehe auch die Beispielanwendung testsparrow.c).
Mithilfe der Projektionsmatrix wandelt sparrow3D die Raumkoordinaten in
Bildschirmkoordinaten um. Das geschieht unter Beachtung des z-Wertes.
Ein Objekt, was weiter weg ist, erscheint auch kleiner.
Desweiteren bietet einem sparrow3D auch sehr einfache, farbige
Lichtquellen, die man beliebig im Raum setzen kann.
Es gibt grob drei Arten von 3D Grafikfunktionen:
- "echte" 3D Funktionen, wo man ein Dreieck oder Viereck (mit oder ohne
Textur beliebig im Raum positionieren kann. Auf diese Objekte wirkt
auch Licht. Außerdem Funktionen, um komplette Flächengebilde (Meshs
oder Modell genannt) zu zeichnen. Diese Funktionen sind schneller als
jede Fläche einzeln zu zeichnen. Das Laden erfolgt mit sparrowMesh.
Nur bei diesen Funktionen wird Licht miteinberechnet!
- "Billboard" 3D Funktionen, die immer zum Bildschirm zeigen, aber in
der xy-Ebene durchaus noch rotiert oder skaliert sein können. Hier
gibt es z.B. wieder die Rechtecke, Ellipsen, Rechteck- oder
Ellipsenrahmen und Surfaces aus der sparrowPrimitives.
- "Blit" 3D Funktionen, die zwar die Position richtig aus der ModelView-
Matrix herleiten und projezieren, jedoch weder Größe noch Rotation
der 3D Welt anpassen. Diese Funktionen sind dafür sehr schnell und
z.B. für Partikeleffekte geeignet, wo die Größe der Partikel nicht
so wichtig ist.
+-----------+
|sparrowMesh|
+-----------+
sparrowMesh füllt die Meshstrukturen, die sparrowRender zeichnen kann
mit Leben. Es kann Meshes aus obj-files laden und löschen.
+-----------+
|sparrowFont|
+-----------+
Keine Anwendung ohne Schrift. Naja, fast keine. Aber die meisten werden
auf Schriftunterstützung nicht verzichten wollen. Diesen Part übernimmt
sparrowFont. Dieser Teil kann mithilfe von SDL_TTF ttf-Dateien öffnen
und einzelne Buchstaben (z.B. das gesamte deutsche Alphabet) speichern,
sodass diese dann sehr schnell gezeichnet werden können. Durch Caching
und eine schnelle Baumstruktur ist keine Geschwindigkeitseinbuße zu
bemerken.
Im Moment kann man (mit sparrow3D-Standard-Mitteln) Text weder im
3D-Raum unterbringen noch Rotieren oder Zoomen.
+-------------+
|sparrowSprite|
+-------------+
Für die meisten Spiele sind Sprites mehr oder minder essentiell. Bei
einem 2D Jump and Run wird man ohne gar nicht auskommen, aber auch bei
einem 3D-Spiel sind diese sehr gut für "Head-up-Display" (HUD) Anzeigen
geeignet.
Ein Sprite von sparrow3D besteht im Prinzip nur aus beliebig vielen
Surfaces oder nur Teilen davon (Tiling). Jede dieser Untereinheiten
hat eine fixe Zeit, die sie angezeigt wird ehe die nachfolgende
gezeichnet wird. Wenn ein Sprite komplett angezeigt wurde, beginnt es
von vorne. So sind sehr einfache Animationen möglich.
Sprites können zudem noch rotiert oder skaliert werden. Das ist zwar
langsamer, aber geschickt und einzeln eingesetzt kann es dem Spiel viel
Dynamik geben. Ein schönes Beispiel wäre hier z.B. das allseits bekannte
und beliebte Spiel Worms, wo meist nur die Projektile rotieren.
+---------+
|Abschluss|
+---------+
sparrow3D ist in ständiger Weiterentwicklung. Wenn du Fragen,
Anregungen oder Featurewünsche hast, fühl dich frei sie näherzubringen!