Entry
BBCBASIC: Windows: Graphics: Microsoft: DirectX: Figure: Robot: Create: How to view a robot?
Feb 5th, 2006 16:46
Knud van Eeden,
----------------------------------------------------------------------
--- Knud van Eeden --- 05 February 2006 - 08:52 pm -------------------
BBCBASIC: Windows: Graphics: Microsoft: DirectX: Figure: Robot:
Create: How to view a robot?
---
Divide your figure in triangles.
---
Put the 3 points of each triangle,
together with a possible color,
in DATA
---
To quickly create a 3D effect, draw your figure
in 2D, and translate this figure in the direction
perpendicular to your 2D axes
(that comes down to adding e.g. z=0
then repeating that dataset and adding z=1)
---
To create the coordinates
I made a drawing on a piece of paper
(with squares on it preprinted),
made a drawing of the figure.
From this I got the 2D coordinates
(with the axes in the XZ plane), by
looking at the intersections of the lines
of the figure with the paper.
---
To create a 3D effect
To get a 3D representation, I translated
the 2D data in the direction perpendicular
to this 2D plane (as this was supposed
to be in the XZ plane, I translated the
figure in the Y direction).
---
Triangulation:
Triangles or thus 3 lines, in pairs of two, connected in 3 points, are
the simplest polygons possible (a 'mono'angle or 'bi'angle would not
create a closed figure, a 'tri'angle is the first that does).
Because of this being the simplest polygon possible, dividing the
contours of your figure in triangles (this basic units or 'atoms') is
the most often used technique in computer graphics.
To create the triangles, I drew manually connections between the
existing points on the contour of the figure, in order to create
triangles.
I chose a central point, in a symmetric position (e.g. the middle of
the figure), then drew straight lines from that point to each of the
other existing points on the contour.
(this seems a pretty effective algorithm to triangulize, generating not
too many triangles).
I numbered each triangle,
made a table
with columns
------------------------------------------------
| vertice number | x | y | z | triangle number |
------------------------------------------------
1 1 0 4 1
2 2 0 3 1
3 3 0 4 1
4 2 0 2 2
5 6 0 1 2
6 8 0 7 2
7 3 0 8 3
8 5 0 2 3
9 7 0 3 3
... ... ... ... ...
------------------------------------------------
Started with one of the points on the triangle
(usually the symmetric one in the middle)
and then followed consequently anti-clockwise
(because the order of the points is of importance
for some computer graphics algorithms)
the contour of that triangle, noting the
coordinates of it in that table.
Repeated the same data by changing the y value
from 0 to another value (so basically translating
the 2D figure in the perpendicular direction to it)
------------------------------------------------
| vertice number | x | y | z | triangle number |
------------------------------------------------
1 1 2 4 1
2 2 2 3 1
3 3 2 4 1
4 2 2 2 2
5 6 2 1 2
6 8 2 7 2
7 3 2 8 3
8 5 2 2 3
9 7 2 3 3
... ... ... ... ...
------------------------------------------------
---
Data
Finally I putted this data in DATA statements
of the render program.
DATA ...
===
Steps: Overview:
1. -Create a file containing the vertices of your figure in DATA
1. -This can by design only be triangles
2. -Run this file
===
Steps: Worked out:
1. -Create the following file
1. Put the information about triangles in your figure (one after
the other) in the DATA below
--- cut here: begin --------------------------------------------------
MODE 8
:
INSTALL @lib$ + "D3DLIB"
:
triangleTotalI% = 2 * ( 11 + 3 ) : REM total amount of triangles in
your figure
:
filename$ = "TRIANGLEN.B3D" : REM the file containing the data for
this triangles (in binary format)
:
dataB% = TRUE : REM set to true if you want to generate the binary
output file containing the data (should only be done once the first
time)
:
dimB% = TRUE : REM set to true if you want to dimension arrays
(should only be done once the first time)
:
IF dataB% THEN PROCGraphicsViewTriangleDataN( triangleTotalI%,
filename$ )
:
IF dimB% THEN PROCGraphicsViewTriangleDimN( triangleTotalI% )
:
IF HIMEM < PAGE + 9999 THEN HIMEM = PAGE + 4200
:
ON CLOSE PROCcleanup : QUIT
:
ON ERROR PROCcleanup : PRINT REPORT$ : END
:
d% = FN_initd3d( @hwnd%, 1, 0 )
IF d% = 0 THEN ERROR 100, "Can not initialise Direct3D"
b%(0) = FN_load3d( d%, @dir$ + filename$, n%( 0 ), f%( 0 ), s%( 0 ) )
IF b%( 0 ) = 0 ERROR 100, "Can not load" + filename$
e() = 0, 0, -6
a() = 0, 0, 0
REPEAT
p() = TIME / 100
r() = TIME / 80
X() = SIN( TIME / 200 )
PROC_render( d%, &FF7F7F7F, 0, l%(), 2, m%(), t%(), b%(), n%(), f%
(), s%(), y(), p(), r(), X(), Y(), Z(), e(), a(), PI/4, 5/4, 1, 1000 )
UNTIL INKEY( 1 ) = 0
END
:
:
:
DEF PROCGraphicsViewTriangleDataN( triangleTotalI%, filename$ )
LOCAL F%
:
LOCAL triangleI%
LOCAL triangleMinI%
LOCAL triangleMaxI%
:
LOCAL pointI%
LOCAL pointMinI%
LOCAL pointMaxI%
:
LOCAL xR
LOCAL yR
LOCAL zR
LOCAL colorR
:
triangleMinI% = 1
triangleMaxI% = triangleTotalI%
pointMinI% = 1
pointMaxI% = 3
F% = OPENOUT filename$
PROC4( triangleTotalI% * 3 ) : REM for each triangle exactly 3
points, thus totally N . 3 points
PROC4( &100042 ) : REM vertex size &10 and format &42 (possibly
change this)
FOR triangleI% = triangleMinI% TO triangleMaxI%
FOR pointI% = pointMinI% TO pointMaxI%
READ xR, yR, zR, colorR
xR = xR / 9 : REM scale down
yR = yR / 9 : REM scale down
zR = zR / 9 : REM scale down
PROC4( FN_f4( xR ) ) : PROC4( FN_f4( yR ) ) : PROC4( FN_f4(
zR ) ) : PROC4( colorR )
NEXT pointI%
NEXT triangleI%
CLOSE #F%
ENDPROC
:
DEF PROC4( A% )
BPUT# F%,A%
BPUT# F%, A% >> 8
BPUT# F%, A% >> 16
BPUT# F%, A% >> 24
ENDPROC
:
DEF PROCcleanup
t%(1) += 0 : IF t%(1) PROC_release(t%(1))
b%(0) += 0 : IF b%(0) PROC_release(b%(0))
b%(1) += 0 : IF b%(1) PROC_release(b%(1))
d% += 0 : IF d% PROC_release(d%)
ENDPROC
:
DEF PROCGraphicsViewTriangleDimN( triangleTotalI% )
DIM l%(0)
DIM b%(1)
DIM n%(10)
DIM f%(1)
DIM s%(1)
DIM m%(1)
DIM t%(1)
DIM y(1)
DIM p(1)
DIM r(1)
DIM X(1)
DIM Y(1)
DIM Z(1)
DIM e(2)
DIM a(2)
ENDPROC
:
REM put here your N triangles
:
REM The format is:
REM DATA <x of point 1>, <y of point 1>, <z of point 1>, <color of
point 1>
REM DATA <x of point 2>, <y of point 2>, <z of point 2>, <color of
point 2>
REM DATA <x of point 3>, <y of point 3>, <z of point 3>, <color of
point 3>
:
REM body: head: circumference: begin
:
REM triangle 001 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FF0000FF
DATA -4.0, 0.0, 7.0, &FF00FF10
DATA -3.0, 0.0, 9.0, &FFFF0020
:
REM triangle 002 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0030
DATA 3.0, 0.0, 9.0, &FFFF0040
DATA 4.0, 0.0, 7.0, &FFFF0050
:
REM triangle 003 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0060
DATA -4.0, 0.0, 1.0, &FFFF0070
DATA -4.0, 0.0, 7.0, &FFFF0080
:
REM triangle 004 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0090
DATA 4.0, 0.0, 7.0, &FFFF0100
DATA 4.0, 0.0, 1.0, &FFFF0110
:
REM triangle 005 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0000
DATA -2.0, 0.0, 3.0, &FFFF0000
DATA -4.0, 0.0, 1.0, &FFFF0000
:
REM triangle 006 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0000
DATA 4.0, 0.0, 1.0, &FFFF0000
DATA 2.0, 0.0, 3.0, &FFFF0000
:
REM triangle 007 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0000
DATA -2.0, 0.0, 0.0, &FFFF0000
DATA -2.0, 0.0, 3.0, &FFFF0000
:
REM triangle 008 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0000
DATA 2.0, 0.0, 3.0, &FFFF0000
DATA 2.0, 0.0, 0.0, &FFFF0000
:
REM triangle 009 (totally 3 points per triangle)
DATA 0.0, 0.0, 9.0, &FFFF0000
DATA 2.0, 0.0, 0.0, &FFFF0000
DATA -2.0, 0.0, 0.0, &FFFF0000
:
REM triangle 010 (totally 3 points per triangle)
DATA -4.0, 0.0, 1.0, &FFFF0000
DATA -2.0, 0.0, 3.0, &FFFF0000
DATA -2.0, 0.0, 1.0, &FFFF0000
:
REM triangle 011 (totally 3 points per triangle)
DATA 4.0, 0.0, 1.0, &FFFF0000
DATA 2.0, 0.0, 1.0, &FFFF0000
DATA 2.0, 0.0, 3.0, &FFFF0000
:
REM translated in y-direction
:
REM triangle 001 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FF0000FF
DATA -4.0, 4.0, 7.0, &FF00FF10
DATA -3.0, 4.0, 9.0, &FFFF0020
:
REM triangle 002 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0030
DATA 3.0, 4.0, 9.0, &FFFF0040
DATA 4.0, 4.0, 7.0, &FFFF0050
:
REM triangle 003 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0060
DATA -4.0, 4.0, 1.0, &FFFF0070
DATA -4.0, 4.0, 7.0, &FFFF0080
:
REM triangle 004 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0090
DATA 4.0, 4.0, 7.0, &FFFF0100
DATA 4.0, 4.0, 1.0, &FFFF0110
:
REM triangle 005 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0000
DATA -2.0, 4.0, 3.0, &FFFF0000
DATA -4.0, 4.0, 1.0, &FFFF0000
:
REM triangle 006 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0000
DATA 4.0, 4.0, 1.0, &FFFF0000
DATA 2.0, 4.0, 3.0, &FFFF0000
:
REM triangle 007 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0000
DATA -2.0, 4.0, 0.0, &FFFF0000
DATA -2.0, 4.0, 3.0, &FFFF0000
:
REM triangle 008 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0000
DATA 2.0, 4.0, 3.0, &FFFF0000
DATA 2.0, 4.0, 0.0, &FFFF0000
:
REM triangle 009 (totally 3 points per triangle)
DATA 0.0, 4.0, 9.0, &FFFF0000
DATA 2.0, 4.0, 0.0, &FFFF0000
DATA -2.0, 4.0, 0.0, &FFFF0000
:
REM triangle 010 (totally 3 points per triangle)
DATA -4.0, 4.0, 1.0, &FFFF0000
DATA -2.0, 4.0, 3.0, &FFFF0000
DATA -2.0, 4.0, 1.0, &FFFF0000
:
REM triangle 011 (totally 3 points per triangle)
DATA 4.0, 4.0, 1.0, &FFFF0000
DATA 2.0, 4.0, 1.0, &FFFF0000
DATA 2.0, 4.0, 3.0, &FFFF0000
:
REM body: head: circumference: end
:
REM body: head: chin: begin
:
REM triangle 01 (totally 3 points per triangle)
DATA 0.0, 0.0, 3.0, &FF0F0010
DATA -1.0, 0.0, 1.0, &FF0F0020
DATA -2.0, 0.0, 3.0, &FF0F0030
:
REM triangle 02 (totally 3 points per triangle)
DATA 0.0, 0.0, 3.0, &FF0F0040
DATA 1.0, 0.0, 1.0, &FF0F0050
DATA -1.0, 0.0, 1.0, &FF0F0060
:
REM triangle 03 (totally 3 points per triangle)
DATA 0.0, 0.0, 3.0, &FF0F0070
DATA 2.0, 0.0, 3.0, &FF0F0080
DATA 1.0, 0.0, 1.0, &FF0F0090
:
REM translated in y-direction
:
REM triangle 01 (totally 3 points per triangle)
DATA 0.0, 1.0, 3.0, &FF0F0010
DATA -1.0, 1.0, 1.0, &FF0F0020
DATA -2.0, 1.0, 3.0, &FF0F0030
:
REM triangle 02 (totally 3 points per triangle)
DATA 0.0, 1.0, 3.0, &FF0F0040
DATA 1.0, 1.0, 1.0, &FF0F0050
DATA -1.0, 1.0, 1.0, &FF0F0060
:
REM triangle 03 (totally 3 points per triangle)
DATA 0.0, 1.0, 3.0, &FF0F0070
DATA 2.0, 1.0, 3.0, &FF0F0080
DATA 1.0, 1.0, 1.0, &FF0F0090
:
REM body: head: chin: begin
:
--- cut here: end ----------------------------------------------------
2. -Save this file as
triangleN_FIGURE_ROBOT_1.bbc
3. -Run this file
7. -That will show N rotating 3D triangles
===
File: see also:
[file: 'pyramid.bbc' in the 'BBC BASIC for Windows' directory]
===
Help: see also:
[help: program: BBCBASIC for Windows v5.00a or higher: search
for 'DirectX']
===
Internet: see also:
---
Computer: Graphics: Object: 3D: Operation: Create: Simple: How to
quickly create 3D data?
http://www.faqts.com/knowledge_base/view.phtml/aid/39497/fid/818
---
BBCBASIC: Windows: Graphics: Microsoft: DirectX: Link: Overview: Can
you give an overview of links?
http://www.faqts.com/knowledge_base/view.phtml/aid/39482/fid/768
----------------------------------------------------------------------