Initial. Don't... just don't ask.

This commit is contained in:
Ryan McGrath 2011-02-24 00:22:00 -08:00
commit 00bae13bba
586 changed files with 129057 additions and 0 deletions

35
threeDKit/0-CHANGES Normal file
View file

@ -0,0 +1,35 @@
v1.3
(2 June 1997) Reorganised triangle code. Triangle code
now works with any bpp.
v1.2
(1 feb 1996) Added surface wrapping demo.
added swtriangle
v1.1
(Jan 1996) Added wtriangle routine to draw from bitmap.
added striangle.
v1.0
(Jan 1996) Plane demo fully working in all 256c modes.
3dkit.c extensively rewritten with many more features.
triangle.c optimised to write to frame buffer directly.
v0.9
(Jan 1996) Ported plane demo from DOS compiler to Linux.
triangle.c written to replace 8088 assembly code.
Prior to 0.9:
(1994) Created plane demo with page flipping.
(+-1993) 3dkit written in C for DOS. Triangle and line drawing
routines written in assembly language in 320x400x256.

482
threeDKit/0-COPYING Normal file
View file

@ -0,0 +1,482 @@
GNU LIBRARY GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1991 Free Software Foundation, Inc.
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the library GPL. It is
numbered 2 because it goes with version 2 of the ordinary GPL.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Library General Public License, applies to some
specially designated Free Software Foundation software, and to any
other libraries whose authors decide to use it. You can use it for
your libraries, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if
you distribute copies of the library, or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link a program with the library, you must provide
complete object files to the recipients so that they can relink them
with the library, after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
Our method of protecting your rights has two steps: (1) copyright
the library, and (2) offer you this license which gives you legal
permission to copy, distribute and/or modify the library.
Also, for each distributor's protection, we want to make certain
that everyone understands that there is no warranty for this free
library. If the library is modified by someone else and passed on, we
want its recipients to know that what they have is not the original
version, so that any problems introduced by others will not reflect on
the original authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that companies distributing free
software will individually obtain patent licenses, thus in effect
transforming the program into proprietary software. To prevent this,
we have made it clear that any patent must be licensed for everyone's
free use or not licensed at all.
Most GNU software, including some libraries, is covered by the ordinary
GNU General Public License, which was designed for utility programs. This
license, the GNU Library General Public License, applies to certain
designated libraries. This license is quite different from the ordinary
one; be sure to read it in full, and don't assume that anything in it is
the same as in the ordinary license.
The reason we have a separate public license for some libraries is that
they blur the distinction we usually make between modifying or adding to a
program and simply using it. Linking a program with a library, without
changing the library, is in some sense simply using the library, and is
analogous to running a utility program or application program. However, in
a textual and legal sense, the linked executable is a combined work, a
derivative of the original library, and the ordinary General Public License
treats it as such.
Because of this blurred distinction, using the ordinary General
Public License for libraries did not effectively promote software
sharing, because most developers did not use the libraries. We
concluded that weaker conditions might promote sharing better.
However, unrestricted linking of non-free programs would deprive the
users of those programs of all benefit from the free status of the
libraries themselves. This Library General Public License is intended to
permit developers of non-free programs to use free libraries, while
preserving your freedom as a user of such programs to change the free
libraries that are incorporated in them. (We have not seen how to achieve
this as regards changes in header files, but we have achieved it as regards
changes in the actual functions of the Library.) The hope is that this
will lead to faster development of free libraries.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, while the latter only
works together with the library.
Note that it is possible for a library to be covered by the ordinary
General Public License rather than by this special one.
GNU LIBRARY GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library which
contains a notice placed by the copyright holder or other authorized
party saying it may be distributed under the terms of this Library
General Public License (also called "this License"). Each licensee is
addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also compile or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
c) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
d) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the source code distributed need not include anything that is normally
distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Library General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS
Appendix: How to Apply These Terms to Your New Libraries
If you develop a new library, and you want it to be of the greatest
possible use to the public, we recommend making it free software that
everyone can redistribute and change. You can do so by permitting
redistribution under these terms (or, alternatively, under the terms of the
ordinary General Public License).
To apply these terms, attach the following notices to the library. It is
safest to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least the
"copyright" line and a pointer to where the full notice is found.
<one line to give the library's name and a brief idea of what it does.>
Copyright (C) <year> <name of author>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
Also add information on how to contact you by electronic and paper mail.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the library, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the
library `Frob' (a library for tweaking knobs) written by James Random Hacker.
<signature of Ty Coon>, 1 April 1990
Ty Coon, President of Vice
That's all there is to it!

58
threeDKit/0-README Normal file
View file

@ -0,0 +1,58 @@
This is version 1.3 of 3DKIT, a super fast on-the-fly rendering library for
living 3D animation, written in C by Paul Sheer.
The 3DKIT library is free software. See the file 0-COPYING for copying
permission.
The library consists of the following files:
3dinit.h
3dinit.c
3dkit.h
3dkit.c
wrapsurf.c
triangle.h
striangle.c
swtriangle.c
triangle.c
wtriangle.c
trisetpixel.c
tri.c
triangl.c
quickmath.c
quickmath.h
For demonstration, two additional programs are included:
planukit.c:
A greyscale-shaded rendered-on-the-fly turbo-prop that you can rotate
and scale however you like.
wrapdemo.c:
Demonstrates surface wrapping of bitmaps in a similar fashion.
For details, please read the manual pages:
man 6 planukit
man 6 wrapdemo
man 3 triangle
man 3 striangle
man 3 wtriangle
man 3 swtriangle
man 3 trisetcolorlookup
man 3 trigetcolorlookup
man 3 trisetdrawpoint
man 7 threedkit
CONTACTING THE AUTHOR
---------------------
email: psheer@icon.co.za
paper mail: P O BOX 890507
Lyndhurst
Johannesburg 2106
South Africa
Donations (by check or postal order) will be appreciated and will encourage
further development of this software. However this is strictly on a voluntary
basis where this software falls under the GNU LIBRARY GENERAL PUBLIC LICENSE.

334
threeDKit/3dinit.c Normal file
View file

@ -0,0 +1,334 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
/*
File: 3dinit.c
Contains the utility function initcolor for initialising the normal color
vectors of a surface, and the a function to initialise a 3D ellipse.
This file is incomplete and should contain a number of useful
tools to initialise different 3D primitives.
*/
#include <config.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef DO_NOT_USE_VGALIB
#include <vga.h>
#endif
#include <vgagl.h>
#include "./3dkit.h"
#include "./3dinit.h"
double mag (Vec v)
{
double r;
if ((r = sqrt (v.x * v.x + v.y * v.y + v.z * v.z)) == 0)
return 1;
return r;
}
/* adds the normal vector to v at point (i,j), calculated from the
panel d. d is one of the four panels at (i,j).
i
-->
0|3 |
-+- | j
1|2 v
*/
void norm_vec (TD_Surface * surf, int i, int j, Vec * v, int d)
{
int i1 = 0, i2 = 0, j1 = 0, j2 = 0, w = surf->w;
double x, y, z, r;
double x1, y1, z1;
double x2, y2, z2;
Vec u;
switch (d & 3) {
case 0:
j1 = -1;
i2 = -1;
break;
case 1:
i1 = -1;
j2 = 1;
break;
case 2:
j1 = 1;
i2 = 1;
break;
case 3:
i1 = 1;
j2 = -1;
break;
}
x = surf->point[i + j * w].x;
y = surf->point[i + j * w].y;
z = surf->point[i + j * w].z;
x1 = surf->point[i + i1 + (j + j1) * w].x - x;
y1 = surf->point[i + i1 + (j + j1) * w].y - y;
z1 = surf->point[i + i1 + (j + j1) * w].z - z;
x2 = surf->point[i + i2 + (j + j2) * w].x - x;
y2 = surf->point[i + i2 + (j + j2) * w].y - y;
z2 = surf->point[i + i2 + (j + j2) * w].z - z;
u.x = y1 * z2 - z1 * y2;
u.y = z1 * x2 - x1 * z2;
u.z = x1 * y2 - y1 * x2;
r = mag(u);
v->x += u.x / r;
v->y += u.y / r;
v->z += u.z / r;
}
/*Following routine initialise a surface's normal vectors*/
/*(FIXME: this doesn't work 100% at the edges, I think it
needs Frenet-Sneret (spelling?) formula) */
/* n gives the brightness of the surface and the direction of the normal.
normally +256 or -256 (can be less to give a darker surface) */
void TD_initcolor (TD_Surface * surf, int n)
{
int i, j, k, w = surf->w, l = surf->l, m;
double r, ru;
int w0, ww;
int l0, ll;
Vec v, u;
if (w > 2) {
w0 = 1;
ww = w - 1;
} else {
w0 = 0;
ww = w;
}
if (l > 2) {
l0 = 1;
ll = l - 1;
} else {
l0 = 0;
ll = l;
}
for (j = 0; j < l; j++)
for (i = 0; i < w; i++) {
/* normal at a point is the average of the four cross products
except at the edge points where the gradient of the normal near
the edge is considered as well */
v.x = v.y = v.z = 0;
u.x = u.y = u.z = 0;
m = 0;
if (i == 0) {
m = 1;
if (j != 0) {
norm_vec (surf, i, j, &v, 3);
norm_vec (surf, w0, j, &u, 3);
}
if (j != (l - 1)) {
norm_vec (surf, i, j, &v, 2);
norm_vec (surf, w0, j, &u, 2);
}
}
if (i == (w - 1)) {
m = 1;
if (j != 0) {
norm_vec (surf, i, j, &v, 0);
norm_vec (surf, ww, j, &u, 0);
}
if (j != (l - 1)) {
norm_vec (surf, i, j, &v, 1);
norm_vec (surf, ww, j, &u, 1);
}
}
if (j == 0) {
m = 1;
if (i != 0) {
norm_vec (surf, i, j, &v, 1);
norm_vec (surf, i, l0, &u, 1);
}
if (i != (w - 1)) {
norm_vec (surf, i, j, &v, 2);
norm_vec (surf, i, l0, &u, 2);
}
}
if (j == (l - 1)) {
m = 1;
if (i != 0) {
norm_vec (surf, i, j, &v, 0);
norm_vec (surf, i, ll, &u, 0);
}
if (i != (w - 1)) {
norm_vec (surf, i, j, &v, 3);
norm_vec (surf, i, ll, &u, 3);
}
}
if (m) {
r = mag (v);
ru = mag (u);
v.x = (float) 3 * v.x / (2 * r) - u.x / (2 * ru);
v.y = (float) 3 * v.y / (2 * r) - u.y / (2 * ru);
v.z = (float) 3 * v.z / (2 * r) - u.z / (2 * ru);
} else {
for (k = 0; k < 4; k++)
norm_vec (surf, i, j, &v, k);
}
r = mag (v);
surf->point[i + j * w].dirx = (double) v.x * n / r;
surf->point[i + j * w].diry = (double) v.y * n / r;
surf->point[i + j * w].dirz = (double) v.z * n / r;
}
}
static inline void fxchg (double *a, double *b)
{
double t = *a;
*a = *b;
*b = t;
}
void TD_initellipsoidpart (TD_Surface * surf, int x, int y, int z,
int a, int b, int c, int w, int dir, int col)
{
int i, j;
Vec v;
float r;
surf->w = surf->l = 2 * w + 1;
for (i = -w; i <= w; i++)
for (j = -w; j <= w; j++) {
v.x = (float) j / w;
v.y = (float) i / w;
v.z = 1;
switch (dir) {
case 0:
v.z = -v.z;
fxchg (&v.x, &v.y);
break;
case 1:
v.y = -v.y;
fxchg (&v.x, &v.z);
break;
case 2:
v.z = -v.z;
fxchg (&v.x, &v.z);
break;
case 3:
v.y = -v.y;
fxchg (&v.y, &v.z);
break;
case 4:
v.z = -v.z;
fxchg (&v.y, &v.z);
break;
}
r = mag (v);
v.x *= (float) a / r;
v.y *= (float) b / r;
v.z *= (float) c / r;
surf->point[i + w + (j + w) * surf->w].x = v.x + x;
surf->point[i + w + (j + w) * surf->w].y = v.y + y;
surf->point[i + w + (j + w) * surf->w].z = v.z + z;
v.x /= (float) a * a; /*normal vector*/
v.y /= (float) b * b;
v.z /= (float) c * c;
r = mag (v);
surf->point[i + w + (j + w) * surf->w].dirx = (float) col * v.x / r;
surf->point[i + w + (j + w) * surf->w].diry = (float) col * v.y / r;
surf->point[i + w + (j + w) * surf->w].dirz = (float) col * v.z / r;
}
}
void TD_initellipsoid (TD_Surface * surf1, TD_Surface * surf2, TD_Surface * surf3,
TD_Surface * surf4, TD_Surface * surf5, TD_Surface * surf6, int x,
int y, int z, int a, int b, int c, int w, int col)
{
TD_initellipsoidpart (surf1, x, y, z, a, b, c, w, 0, col);
TD_initellipsoidpart (surf2, x, y, z, a, b, c, w, 1, col);
TD_initellipsoidpart (surf3, x, y, z, a, b, c, w, 2, col);
TD_initellipsoidpart (surf4, x, y, z, a, b, c, w, 3, col);
TD_initellipsoidpart (surf5, x, y, z, a, b, c, w, 4, col);
TD_initellipsoidpart (surf6, x, y, z, a, b, c, w, 5, col);
}
void TD_initsellipsoid (TD_Solid *s, int n, int x,
int y, int z, int a, int b, int c, int w, int col)
{
TD_initellipsoid(&s->surf[n], &s->surf[n+1], &s->surf[n+2],
&s->surf[n+3], &s->surf[n+4], &s->surf[n+5], x, y, z,
a, b, c, w, col);
}

43
threeDKit/3dinit.h Normal file
View file

@ -0,0 +1,43 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
/*
File: 3dinit.h
*/
#include "quickmath.h"
void TD_initcolor (TD_Surface * surf, int n);
void TD_initellipsoidpart (TD_Surface * surf, int x, int y, int z,
int a, int b, int c, int w, int dir, int col);
void TD_initellipsoid (TD_Surface * surf1, TD_Surface * surf2, TD_Surface * surf3,
TD_Surface * surf4, TD_Surface * surf5, TD_Surface * surf6, int x,
int y, int z, int a, int b, int c, int w, int col);
void TD_initsellipsoid (TD_Solid *s, int n, int x,
int y, int z, int a, int b, int c, int w, int col);

670
threeDKit/3dkit.c Normal file
View file

@ -0,0 +1,670 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
/*
File: 3dkit.c
Comments or suggestions welcome.
This 3D graphics tool prints an object in three dimensions on the screen.
The object must be made up of one or more surfaces passed in a structure.
The algorithm calculates the light intensity at each point and does a color
interpolation so that surfaces appear uniform with smooth colour
graduations.
The TD_Object structure contains an array of surfaces comprising the object.
When printing, the surfaces are sorted from furthest to closest by
determining the distance from the eye point of their respective centres.
This removes hidden features.
The points of a surface are assumed to form a contorted rectangular mesh
having a length and a width - the number of points along the longitudinal
edges and lateral edges respectively. Although the surfaces are restricted
to rectangles, they can be infinitely contorted into spheres, triangles
etc., possibly with a whole side compressed into a single point.
It is advisable however to make up complex surfaces out of several less
contorted surfaces so that the sorting routine can place the correct parts
of the surface in front of one another. A sphere for example can be
defined as eight surfaces, each a triangular octant.
Besides defining each 3D coord point of each surface array, the user must
also define the unit normal at each point. so that shading can be calculated.
The function TD_initcolor may be called to do this for you.
The surfaces are drawn on the screen using one of the following methods.
The integer surf.render determines the method.
0 : Interpolated trangles are drawn with each rectangle outlined.
1 : A wire frame is drawn of the edges of the surface only.
2 : Interpolated triangles only.
3 : Mesh - each rectangle outlined only.
The demo planukit.c demostrates usage in detail.
This code represents a complete re-write of the previous version, which
I wrote when I was first learning C (an excuse). It is far more structured,
efficient and readable. An important additional feature is that the 3D
camera position can now be defined, so that this code can be used as a
VR tool. Hence an object can be displayed as an object at the screen
centre, or as a 3D world. (See plane.h for how to modify the demo).
*/
#define TD_MULCONSTANT 4096
#include <config.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <vgagl.h>
#include "3dkit.h"
#define max(x,y) (((x) > (y)) ? (x) : (y))
#define min(x,y) (((x) < (y)) ? (x) : (y))
/*global for holding a surface temporarily:*/
TD_Short_Point *temp;
#if defined(__GNUC__) && !defined(__STRICT_ANSI__)
/* The optimisation comes from svgalib-1.2.9/gl/line.c: */
/* Framebuffer Graphics Libary for Linux, Copyright 1993 Harm Hanemaayer */
/* line.c Line drawing */
#ifdef __alpha__
static inline int muldiv64 (int m1, int m2, int d)
{
return (int) m1 *(int) m2 / (int) d;
}
#else
#ifdef __i386__
/* We use the 32-bit to 64-bit multiply and 64-bit to 32-bit divide of the */
/* 386 (which gcc doesn't know well enough) to efficiently perform integer */
/* scaling without having to worry about overflows. */
static inline int muldiv64 (int m1, int m2, int d)
{
/* int32 * int32 -> int64 / int32 -> int32 */
int result;
int dummy;
__asm__ (
"imull %%edx\n\t"
"idivl %4\n\t"
: "=a" (result), "=d"(dummy) /* out */
: "0" (m1), "1" (m2), "g" (d) /* in */
/***rjr***: "ax", "dx"*/ /* mod */
);
return result;
}
#else
static inline int muldiv64(int m1, int m2, int d)
{
return (double) m1 * (double) m2 / ((double) d);
}
#endif /* !__i386__ */
#endif /* !__alpha__ */
#else
#define muldiv64(a,b,c) ((int) ((double) a * (double) b / ((double) c)))
#endif
void TD_translate (TD_Solid * s, TD_Point * p, TD_Short_Point * scr)
{
/* the following rotational transformation avoids floating point
calculations entirely */
if (s->option_flags & TDOPTION_32BIT_SURFACES) {
/* for super accuracy */
double x = p->x + s->x_cam;
double y = p->y + s->y_cam;
double z = p->z + s->z_cam;
double yt = x * s->a21 + y * s->a22 + z * s->a23 + s->s_cam;
if (yt < 1) {
scr->x = scr->y = 32767;
return;
} else {
double xt = x * s->a11 + y * s->a12 + z * s->a13;
double zt = x * s->a31 + y * s->a32 + z * s->a33;
scr->x = ((int) ((double) s->posx + xt * s->xscale / yt)) >> 16;
scr->y = ((int) ((double) s->posy - zt * s->yscale / yt)) >> 16;
return;
}
} else {
int x = p->x + s->x_cam;
int y = p->y + s->y_cam;
int z = p->z + s->z_cam;
int yt = x * s->a21 + y * s->a22 + z * s->a23 + s->s_cam;
/*(FIXME:) There may be problems if yt overflows, this just checks if the point
is behind the cam: */
if (yt < 1) {
scr->x = scr->y = 32767; /*line and triangle routines must
reject these values. */
return;
} else {
int xt = x * s->a11 + y * s->a12 + z * s->a13;
int zt = x * s->a31 + y * s->a32 + z * s->a33;
scr->x = s->posx + muldiv64 (xt, s->xscale, yt);
scr->y = s->posy - muldiv64 (zt, s->yscale, yt);
return;
}
}
}
int TD_finddistance (TD_Solid * s, TD_Point * p)
{
/* the following rotational transformation avoids floating point
calculations entirely */
if (s->option_flags & TDOPTION_32BIT_SURFACES) {
/* for super accuracy */
double x = p->x + s->x_cam;
double y = p->y + s->y_cam;
double z = p->z + s->z_cam;
return ((int) ((double) x * s->a21 + y * s->a22 + z * s->a23 + s->s_cam)) >> 16;
} else {
int x = p->x + s->x_cam;
int y = p->y + s->y_cam;
int z = p->z + s->z_cam;
return (x * s->a21 + y * s->a22 + z * s->a23 + s->s_cam);
}
}
int TD_findcolor (TD_Solid * s, TD_Point * p, int which)
{
int c, shadow = s->surf[which].shadow;
/*this you can fool around with to get different shadowing effects. */
/*c starts off as a signed 28 bit integer. Brightest = -2^28, darkest = +2^28 */
if (s->option_flags & TDOPTION_LIGHT_SOURCE_CAM) {
/* do product of translated normal vector with lighting vector: */
c = ((p->dirx * s->a11 + p->diry * s->a12 + p->dirz * s->a13) * s->xlight +
(p->dirx * s->a21 + p->diry * s->a22 + p->dirz * s->a23) * s->ylight +
(p->dirx * s->a31 + p->diry * s->a32 + p->dirz * s->a33) * s->zlight);
c = (c >> 20) + 256;
} else {
c = p->dirx * s->xlight +
p->diry * s->ylight +
p->dirz * s->zlight;
c = (c >> 8) + 256;
}
/*c now 9 bits */
/*
c = s->surf[which].maxcolor
- ((c * c) >> (16 - s->surf[which].depth_per_color));
*/
/*:responds quadratically to light or.*/
c = s->surf[which].maxcolor - (c >> (8 - s->surf[which].depth_per_color));
/*:responds linearly to light.*/
if (c < shadow)
return shadow;
else
return c;
}
void TD_calc_rotation_matrix (TD_Solid * s)
{
/* This matrix comes from "Dynamics of Atmospheric Flight" by Bernard Etkin,
John Wiley & Sons, Inc., and is much easier to copy down than to
derive yourself. */
float tsi = s->alpha, theta = s->beta, phi = s->gamma;
s->a22 = (float) TD_MULCONSTANT * (cos (theta) * cos (tsi));
s->a21 = (float) TD_MULCONSTANT * (cos (theta) * sin (tsi));
s->a23 = (float) TD_MULCONSTANT * (-sin (theta));
s->a12 = (float) TD_MULCONSTANT * (sin (phi) * sin (theta) * cos (tsi) - cos (phi) * sin (tsi));
s->a11 = (float) TD_MULCONSTANT * (sin (phi) * sin (theta) * sin (tsi) + cos (phi) * cos (tsi));
s->a13 = (float) TD_MULCONSTANT * (sin (phi) * cos (theta));
s->a32 = (float) TD_MULCONSTANT * (cos (phi) * sin (theta) * cos (tsi) + sin (phi) * sin (tsi));
s->a31 = (float) TD_MULCONSTANT * (cos (phi) * sin (theta) * sin (tsi) - sin (phi) * cos (tsi));
s->a33 = (float) TD_MULCONSTANT * (cos (phi) * cos (theta));
/* this is the classical rotations matrix of aerodynamics */
/*
s->a11 = (float) TD_MULCONSTANT * (cos (s->alpha) * cos (s->gamma));
s->a12 = (float) TD_MULCONSTANT * (cos (s->alpha) * sin (s->gamma));
s->a13 = (float) TD_MULCONSTANT * (-sin (s->alpha));
s->a21 = (float) TD_MULCONSTANT * (sin (s->beta) * sin (s->alpha) * cos (s->gamma) - cos (s->beta) * sin (s->gamma));
s->a22 = (float) TD_MULCONSTANT * (sin (s->beta) * sin (s->alpha) * sin (s->gamma) - cos (s->beta) * cos (s->gamma));
s->a23 = (float) TD_MULCONSTANT * (sin (s->beta) * cos (s->alpha));
s->a31 = (float) TD_MULCONSTANT * (cos (s->beta) * sin (s->alpha) * cos (s->gamma) + sin (s->beta) * sin (s->gamma));
s->a32 = (float) TD_MULCONSTANT * (cos (s->beta) * sin (s->alpha) * sin (s->gamma) + sin (s->beta) * cos (s->gamma));
s->a33 = (float) TD_MULCONSTANT * (cos (s->beta) * cos (s->alpha));
*/
/*results are 14 bit + sign integers*/
}
void TD_drawwire (TD_Solid * s, int which)
{
TD_Surface *surf = &s->surf[which];
int w = surf->w;
int l = surf->l;
int i = 0, j = 0, c = surf->mesh_color;
void (*dl) (int, int, int, int, int) = s->draw_line;
while (j < w - 1)
TD_translate (s, &surf->point[j++], &temp[i++]);
while (j < (w * l - 1)) {
TD_translate (s, &surf->point[j], &temp[i++]);
j += w;
}
while (j > w * (l - 1))
TD_translate (s, &surf->point[j--], &temp[i++]);
while (j >= 0) {
TD_translate (s, &surf->point[j], &temp[i++]);
j -= w;
}
for (j = 0; j < i - 1; j++) {
(*dl) (temp[j].x, temp[j].y, temp[j + 1].x, temp[j + 1].y, c);
}
}
void TD_drawmesh (TD_Solid * s, int which)
{
TD_Surface *surf = &s->surf[which];
int w = surf->w;
int l = surf->l;
int i = 0, j = 0, k = 0, c = surf->mesh_color;
void (*dl) (int, int, int, int, int) = s->draw_line;
while (j < l * w) {
TD_translate (s, &surf->point[j], &temp[j]);
j++;
}
for (j = 0; j < l - 1; j++, k++) {
for (i = 0; i < w - 1; i++, k++) {
(*dl) (temp[k + 1].x, temp[k + 1].y, temp[k].x, temp[k].y, c);
(*dl) (temp[k + w].x, temp[k + w].y, temp[k].x, temp[k].y, c);
}
(*dl) (temp[k + w].x, temp[k + w].y, temp[k].x, temp[k].y, c);
}
for (i = 0; i < w - 1; i++, k++)
(*dl) (temp[k + 1].x, temp[k + 1].y, temp[k].x, temp[k].y, c);
}
void xchg (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void TD_drawsurface (TD_Solid * s, int which)
{
TD_Surface *surf = &s->surf[which];
int w = surf->w;
int l = surf->l;
int i = 0, j = 0, k = 0, c = surf->mesh_color;
void (*dl) (int, int, int, int, int) = s->draw_line;
void (*dt) (int, int, int, int, int, int, int, int, int, int) = s->draw_triangle;
void (*ds) (int, int, int, int, int, int, int, int) = s->draw_striangle;
int mesh;
int d1, d2, d3, d4, d;
int x1, y1, c1;
int x2, y2, c2;
int x3, y3, c3;
int x4, y4, c4;
int furthest, clockwise = 0;
TD_tridata tri;
tri.bitmap1 = surf->bitmap1;
tri.bitmap2 = surf->bitmap2;
if (s->option_flags & TDOPTION_ALL_SAME_RENDER)
mesh = (s->render == TD_MESH_AND_SOLID);
else
mesh = (surf->render == TD_MESH_AND_SOLID);
/*distance of four corners (numbered clockwise): */
d1 = TD_finddistance (s, &surf->point[0]);
d2 = TD_finddistance (s, &surf->point[w - 1]);
d3 = TD_finddistance (s, &surf->point[w * l - 1]);
d4 = TD_finddistance (s, &surf->point[w * (l - 1)]);
/*find furthest point */
furthest = 1;
d = d1;
if (d2 > d) {
furthest = 2;
d = d2;
}
if (d3 > d) {
furthest = 3;
d = d3;
}
if (d4 > d)
furthest = 4;
/*draw scanning from the furthest point to the second furthest point */
/*there are eight possibilities: */
switch (furthest) {
case 1:
if (d2 > d4) {
clockwise = 0;
for (j = 0; j < l; j++)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 1;
for (j = 0; j < l; j++)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
case 2:
if (d1 > d3) {
clockwise = 1;
for (j = 0; j < l; j++)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 0;
for (j = l - 1; j >= 0; j--)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
case 3:
if (d4 > d2) {
clockwise = 0;
for (j = l - 1; j >= 0; j--)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 1;
for (j = l - 1; j >= 0; j--)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
case 4:
if (d3 > d1) {
clockwise = 1;
for (j = l - 1; j >= 0; j--)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 0;
for (j = 0; j < l; j++)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
}
if (!surf->backfacing)
clockwise = 2;
for (k = 0, j = 0; j < l - 1; j++, k++) {
for (i = 0; i < w - 1; i++, k++) {
/*define the grid square we are currently drawing: */
x1 = temp[k].x;
y1 = temp[k].y;
c1 = temp[k].c;
x2 = temp[k + 1].x;
y2 = temp[k + 1].y;
c2 = temp[k + 1].c;
x3 = temp[k + w + 1].x;
y3 = temp[k + w + 1].y;
c3 = temp[k + w + 1].c;
x4 = temp[k + w].x;
y4 = temp[k + w].y;
c4 = temp[k + w].c;
/*draw with two triangles */
if (furthest & 1) { /*draw with hypotenuse from point 1 to point 3 */
if (s->option_flags & TDOPTION_FLAT_TRIANGLE) {
c1 = (c1 + c2 + c3 + c4) >> 2;
(*ds) (x1, y1, x2, y2, x3, y3, c1, clockwise);
(*ds) (x1, y1, x3, y3, x4, y4, c1, clockwise);
} else {
(*dt) (x1, y1, c1, x2, y2, c2, x3, y3, c3, clockwise);
(*dt) (x1, y1, c1, x3, y3, c3, x4, y4, c4, clockwise);
}
} else { /*draw with hypotenuse from point 2 to point 4 */
if (s->option_flags & TDOPTION_FLAT_TRIANGLE) {
c1 = (c1 + c2 + c3 + c4) >> 2;
(*ds) (x1, y1, x2, y2, x4, y4, c1, clockwise);
(*ds) (x2, y2, x3, y3, x4, y4, c1, clockwise);
} else {
(*dt) (x1, y1, c1, x2, y2, c2, x4, y4, c4, clockwise);
(*dt) (x2, y2, c2, x3, y3, c3, x4, y4, c4, clockwise);
}
}
if (mesh) {
(*dl) (x1, y1, x2, y2, c);
(*dl) (x1, y1, x4, y4, c);
}
}
if (mesh)
(*dl) (temp[k + w].x, temp[k + w].y, temp[k].x, temp[k].y, c);
}
if (mesh) {
for (i = 0; i < w - 1; i++, k++)
(*dl) (temp[k + 1].x, temp[k + 1].y, temp[k].x, temp[k].y, c);
}
}
int compare (const void *vp, const void *vq)
{
const int *p = vp;
const int *q = vq;
int diff = *p - *q;
return ((diff >= 0) ? ((diff > 0) ? -1 : 0) : +1);
}
struct disttype {
int distance;
int number;
};
void TD_draw_solid (TD_Solid * s)
{
int n = s->num_surfaces, w, l, i, j, render, num_existing_surfaces;
int max = 0;
struct disttype *sortarray = NULL;
temp = NULL;
gl_trisetdrawpoint(s->draw_point);
if ((sortarray = malloc (s->num_surfaces * sizeof (struct disttype))) == NULL) {
fprintf (stderr, "1. Error allocating memory.\n");
goto fin;
}
if (s->option_flags & TDOPTION_INIT_ROTATION_MATRIX)
TD_calc_rotation_matrix (s);
for (j = 0, i = 0; i < n; i++) {
if((s->surf[i].point)) {
sortarray[j++].number = i;
w = s->surf[i].w;
if (max < w)
max = w;
l = s->surf[i].l; /*find the largest surface */
if (max < l)
max = l;
}
}
num_existing_surfaces = j;
if(!num_existing_surfaces) goto fin;
if (s->option_flags & TDOPTION_SORT_SURFACES) {
for (j = 0, i = 0; i < n; i++) {
if((s->surf[i].point)) {
sortarray[j++].distance =
TD_finddistance (s, &s->surf[i].point[s->surf[i].w / 2
+ s->surf[i].w * (s->surf[i].l / 2)]);
/*the distance of the middle point of the surface */
}
}
qsort (sortarray, num_existing_surfaces, sizeof (struct disttype), compare);
}
max++;
if ((temp = malloc (max * max * sizeof (TD_Short_Point))) == NULL) {
fprintf (stderr, "2. Error allocating memory.\n");
goto fin;
}
if (s->option_flags & TDOPTION_ROTATE_OBJECT) {
s->x_cam = 0;
s->y_cam = 0;
s->z_cam = 0;
s->s_cam = s->distance * TD_MULCONSTANT;
} else {
s->s_cam = 0;
}
for (i = 0; i < num_existing_surfaces; i++) {
if (s->option_flags & TDOPTION_ALL_SAME_RENDER)
render = s->render;
else
render = s->surf[sortarray[i].number].render;
switch (render) {
case TD_SOLID:
case TD_MESH_AND_SOLID:
if ((long) s->surf[sortarray[i].number].bitmap1
| (long) s->surf[sortarray[i].number].bitmap2)
TD_drawwrapsurface (s, sortarray[i].number);
else
TD_drawsurface (s, sortarray[i].number);
break;
case TD_EDGES_ONLY:
TD_drawwire (s, sortarray[i].number);
break;
case TD_MESH:
TD_drawmesh (s, sortarray[i].number);
break;
default:
TD_drawmesh (s, sortarray[i].number);
}
}
fin:
if(temp)
free (temp);
if(sortarray)
free (sortarray);
}

193
threeDKit/3dkit.h Normal file
View file

@ -0,0 +1,193 @@
/*
3DKIT version 1.2
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
/*
File 3dkit.h
*/
#ifndef THREEDKIT_H
#define THREEDKIT_H 1
#include "triangle.h"
#define TD_DEFAULT_MAXCOLOR 63
#define TD_DEFAULT_COLOR 0
#define TD_DEFAULT_SHADOW 7
#define TD_MESH 1
#define TD_MESH_AND_SOLID 2
#define TD_SOLID 3
#define TD_EDGES_ONLY 4
#define TD_PI 3.14159
#define TDOPTION_INIT_ROTATION_MATRIX 1
#define TDOPTION_ALL_SAME_RENDER 2
#define TDOPTION_SORT_SURFACES 4
/*Two ways to display the object:
1. Angles refer to camera view; camera position is specified
in x_cam, y_cam, z_cam.
2. Origin at screen centre; object s_cam away; angles refer to
rotation of object:*/
#define TDOPTION_ROTATE_OBJECT 8
/* Tells that the surface data are signed 32 bit values.
otherwise ussumes 16 bit values.
This can be used to avoid working with cumbersome 32 bits
unless the surface arrays have some other use and need the
accuracy, eg. CAD.
If set, distance x_cam, y_cam and z_cam are also treated as 32 bit.
(32/16 bit has nothing to do with the code itself) */
#define TDOPTION_32BIT_SURFACES 16
/* The light source is relative to the angle of the camera: */
#define TDOPTION_LIGHT_SOURCE_CAM 32
/* Otherwise it is fixed relative to the object. */
/*use flat triangle instead of interpolated triangles (slight speed increase)*/
#define TDOPTION_FLAT_TRIANGLE 64
typedef struct {
int x;
int y;
int z;
int dirx;
int diry;
int dirz;
} TD_Point;
typedef struct {
int w; /*grid width and length*/
int l;
int bitmapwidth; /*bitmap width and length*/
int bitmaplength;
int maxcolor; /*There 256 colors divided into n scales.
maxcolor must point to the top of the scale you want
less a few for roundoff*/
int shadow; /*must point to the bottom of the scale plus a few for roundoff
so that none of the previous scale is printed. */
int depth_per_color; /*number of colors in a scale = depth_per_color ^ 2*/
int mesh_color; /*color of mesh if mesh is drawn*/
int render; /*how it must be rendered*/
int backfacing; /*enable backfacing*/
unsigned char *bitmap1; /*1 byte per pixel bitmap data: triangle front side*/
unsigned char *bitmap2; /*1 byte per pixel bitmap data: triangle back side*/
TD_Point *point; /*3D data and normals*/
} TD_Surface;
typedef struct {
int num_surfaces; /*number of surfaces*/
TD_Surface *surf; /*array of surfaces*/
int a11, a12, a13; /*rotation matrix*/
int a21, a22, a23;
int a31, a32, a33;
float alpha, beta, gamma; /*eulerian rotation angles in radians*/
int xlight, ylight, zlight; /*lighting vector. Magnitude of this
vector must be less than 255*/
int xscale; /*determines the size of the object*/
int yscale;
int distance; /* distance of the camera from origin (always 16 bit)*/
int x_cam; /* position of the camera */
int y_cam;
int z_cam;
int s_cam;
int posx; /*position of camera optical axis on screen*/
int posy;
int option_flags;
int render; /*if option ALL_SAME_RENDER is set then all surfaces are rendered
using this var. Else render is checked on each surface.*/
void (*draw_point) (int, int, int);
void (*draw_wtriangle) (int, int, int, int, int, \
int, int, int, int, int, \
int, int, int, int, int, \
TD_tridata *);
void (*draw_swtriangle) (int, int, int, int, \
int, int, int, int, \
int, int, int, int, int, \
TD_tridata *);
void (*draw_striangle) (int, int, int, int, int, int, int, int);
void (*draw_triangle) (int, int, int, int, int, int, int, int, int, int);
void (*draw_line) (int, int, int, int, int );
} TD_Solid;
typedef struct {
int x;
int y;
int color;
} TD_temppoint;
/*used internally*/
typedef struct {
int x, y, c, u, v;
} TD_Short_Point;
/*used internally*/
void TD_translate (TD_Solid * s, TD_Point * p, TD_Short_Point * scr);
int TD_finddistance (TD_Solid * s, TD_Point * p);
int TD_findcolor (TD_Solid * s, TD_Point * p, int which);
void TD_calc_rotation_matrix (TD_Solid * s);
void TD_drawwire (TD_Solid * s, int which);
void TD_drawmesh (TD_Solid * s, int which);
void TD_drawsurface (TD_Solid * s, int which);
void TD_drawwrapsurface (TD_Solid * s, int which);
/*Draws a 3D solid object composed of a number of surfaces, with
hidden surface elimination*/
void TD_draw_solid (TD_Solid * s);
/*initialises the color normal vectors to a surface*/
void TD_initcolor (TD_Surface * surf, int n);
#endif

482
threeDKit/3dtext.c Normal file
View file

@ -0,0 +1,482 @@
/*
Copyright (C) 1996, 1997 Paul Sheer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/* 3dtext */
#include <config.h>
#include <stdlib.h>
#include <math.h>
#include <my_string.h>
#include <stdio.h>
#include <stdarg.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/Xresource.h>
#include "app_glob.c"
#include "coolwidget.h"
#include "widget3d.h"
#include "quickmath.h"
#include "dialog.h"
/*
this processes a text file into a 3d world
the text file contains the following commands seperated
by zero or more newlines. Charaacters after a # at the
beginning of a line are ignored.
# x, y, z, a, b, h, w, and c are floats. (x,y,z) is a vector.
scale a
# specifies the absolute value of the maximum extent of the scene in 3D space
# (after offset has been subtracted (see next)). This must come first.
offset x y z
# specifies the a vector that is to be subtracted from the given position of
# forthcoming object. Must also come before any drawing commands.
cylinder x y z a b c r
# draws a cylinder beginning at (a,b,c) ending at (a,b,c)+(x,y,z) of radius r
cappedcylinder x y z a b c r
# draws a cylinder beginning at (a,b,c) ending at (a,b,c)+(x,y,z) of radius r
# with closed ends.
surface a b x y z x y z x y z ... x y z
# draws a surface of grid size a by b there must be a*b (x, y, z) points.
trapezium x y z a b c u v w p q r
# draws a trapezium with one corner at (x,y,z) and the other three at (x,y,z)+(a,b,c) etc.
pipe r a x y z x y z x y z x y z ... x y z
# draw a pipe with corners at (x,y,z) the pipe diameter is r and the corner radii are a
* the first (x,y,z) is the start the last is the finish. Points mus be more than 2a appart
cappedpipe r a x y z x y z x y z x y z ... x y z
# same with closed ends
rectangle a b c x y z
# rectangle with (height,width,depth) = (x,y,z), corner at (a,b,c)
ellipse a b c x y z
# an ellipse with (height,width,depth) = (x,y,z), centre at (a,b,c)
density a
# will set the density of the grid making up any of the specific surfaces above.
# can be called before each surface command.
*/
/* globals: */
int GridDensity = 6;
double DimensionScale = 1;
Vec DimensionOffset = {0, 0, 0};
static inline void assignTD (TD_Point *p, Vec v)
{
p->x = (double) (v.x + DimensionOffset.x) * DimensionScale;
p->y = (double) (v.y + DimensionOffset.y) * DimensionScale;
p->z = (double) (v.z + DimensionOffset.z) * DimensionScale;
}
static void third_cyl (double t, TD_Point * p, Vec A, Vec X, Vec r1, Vec r2, int g, double f)
{
int i = 0;
double h;
double alpha = t;
Vec rv;
while (alpha < (2 * PI / 3 + t + 0.001)) {
for (h = 0; h <= 1; h += 0.5) {
rv = plus (plus (plus (times (r1, cos (alpha) * (1 + h * (f - 1))), times (r2, sin (alpha) * (1 + h * (f - 1)))), A), times (X, h));
assignTD(&(p[i]), rv);
i++;
}
alpha += (2 * PI / 3) / g;
}
}
void Cdraw3d_cone (const char *ident, double x, double y, double z, double a, double b, double c, double ra, double rb)
{
int g = 4 * GridDensity / 3;
TD_Point *p = Cmalloc ((g + 1) * 3 * sizeof (TD_Point));
Vec r1;
Vec r2;
Vec A, X;
double f = rb / ra;
A.x = a;
A.y = b;
A.z = c;
X.x = x;
X.y = y;
X.z = z;
orth_vectors (X, &r1, &r2, ra);
third_cyl (0, p, A, X, r1, r2, g, f);
Cinit_surf_points (ident, 3, g + 1, p);
third_cyl (2 * PI / 3, p, A, X, r1, r2, g, f);
Cinit_surf_points (ident, 3, g + 1, p);
third_cyl (4 * PI / 3, p, A, X, r1, r2, g, f);
Cinit_surf_points (ident, 3, g + 1, p);
free (p);
}
void Cdraw3d_cylinder (const char *ident, double x, double y, double z, double a, double b, double c, double r)
{
Cdraw3d_cone (ident, x, y, z, a, b, c, r, r);
}
void Cdraw3d_roundplate (const char *ident, double x, double y, double z, double a, double b, double c, double r)
{
TD_Point *p = Cmalloc ((GridDensity * 4 + 1) * 2 * sizeof (TD_Point));
double alpha = 0;
Vec r1;
Vec r2;
Vec rv;
Vec A;
Vec X;
int i = 0;
A.x = a;
A.y = b;
A.z = c;
X.x = x;
X.y = y;
X.z = z;
orth_vectors (X, &r1, &r2, r);
while (alpha < (2 * PI + 0.001)) {
rv = plus (plus (times (r1, cos (alpha)), times (r2, sin (alpha))), A);
assignTD (&p[i], rv);
i++;
assignTD (&p[i], A);
i++;
alpha += (2 * PI) / (GridDensity * 4);
}
Cinit_surf_points (ident, 2, GridDensity * 4 + 1, p);
free (p);
}
void Cdraw3d_cappedcylinder (const char *ident, double x, double y, double z, double a, double b, double c, double r)
{
Cdraw3d_cylinder (ident, x, y, z, a, b, c, r);
Cdraw3d_roundplate (ident, -x, -y, -z, a, b, c, r);
Cdraw3d_roundplate (ident, x, y, z, x + a, y + b, z + c, r);
}
void textformaterror (int line, const char *ident)
{
Cerrordialog (CMain, 20, 20, " Compile text to 3D ", " A text format error was encounted at line %d,\nwhile trying to draw 3d item to widget %s.\n ", line, ident);
}
void Cdraw3d_scale (const char *ident, double a)
{
DimensionScale = 32767 / a;
}
void Cdraw3d_offset (const char *ident, double x, double y, double z)
{
DimensionOffset.x = x;
DimensionOffset.y = y;
DimensionOffset.z = z;
}
void Cdraw3d_density (const char *ident, double a)
{
GridDensity = a;
}
void draw3d_surface(const char *ident, int w, int h, Vec *v)
{
int i;
TD_Point *p = Cmalloc (w * h * sizeof (TD_Point));
for(i=0;i<w*h;i++)
assignTD(&p[i], v[i]);
Cinit_surf_points (ident, w, h, p);
free (p);
}
void Cdraw3d_surface(const char *ident, int w, int h,...)
{
va_list pa;
int i;
TD_Point *p = Cmalloc (w * h * sizeof (TD_Point));
va_start(pa, h);
for(i = 0; i < w * h; i++) {
p[i].x = va_arg(pa, double);
p[i].y = va_arg(pa, double);
p[i].z = va_arg(pa, double);
p[i].dirx = 0;
p[i].diry = 0;
p[i].dirz = 0;
}
va_end(pa);
Cinit_surf_points (ident, w, h, p);
free (p);
}
static void fxchg (double *a, double *b)
{
double t = *a;
*a = *b;
*b = t;
}
void initellipsoidpart (TD_Point *p, double x, double y, double z,
double a, double b, double c, int w, int dir, double f)
{
int i, j;
Vec v;
double r;
int d = 2 * w + 1;
Vec X;
X.x = x;
X.y = y;
X.z = z;
for (i = -w; i <= w; i++)
for (j = -w; j <= w; j++) {
v.x = (double) j / w;
v.y = (double) i / w;
v.z = 1;
switch (dir) {
case 0:
v.z = -v.z;
fxchg (&v.x, &v.y);
break;
case 1:
v.y = -v.y;
fxchg (&v.x, &v.z);
break;
case 2:
v.z = -v.z;
fxchg (&v.x, &v.z);
break;
case 3:
v.y = -v.y;
fxchg (&v.y, &v.z);
break;
case 4:
v.z = -v.z;
fxchg (&v.y, &v.z);
break;
}
r = norm (v);
v.x *= (f + (1 - f) / r) * a;
v.y *= (f + (1 - f) / r) * b;
v.z *= (f + (1 - f) / r) * c;
assignTD(&p[i + w + (j + w) * d], plus(v, X));
}
}
void Cdraw3d_ellipsoid (const char *ident, double x, double y, double z, double a, double b, double c, double f)
{
int w = GridDensity / 2;
int g = 2 * w + 1;
TD_Point *p = Cmalloc (g * g * sizeof (TD_Point));
initellipsoidpart (p, x, y, z, a, b, c, w, 0, f);
Cinit_surf_points (ident, g, g, p);
initellipsoidpart (p, x, y, z, a, b, c, w, 1, f);
Cinit_surf_points (ident, g, g, p);
initellipsoidpart (p, x, y, z, a, b, c, w, 2, f);
Cinit_surf_points (ident, g, g, p);
initellipsoidpart (p, x, y, z, a, b, c, w, 3, f);
Cinit_surf_points (ident, g, g, p);
initellipsoidpart (p, x, y, z, a, b, c, w, 4, f);
Cinit_surf_points (ident, g, g, p);
initellipsoidpart (p, x, y, z, a, b, c, w, 5, f);
Cinit_surf_points (ident, g, g, p);
free (p);
}
void Cdraw3d_cappedcone (const char *ident, double x, double y, double z, double a, double b, double c, double ra, double rb)
{
Cdraw3d_cone (ident, x, y, z, a, b, c, ra, rb);
Cdraw3d_roundplate (ident, -x, -y, -z, a, b, c, ra);
Cdraw3d_roundplate (ident, x, y, z, x + a, y + b, z + c, rb);
}
void Cdraw3d_rectangle (const char *ident, double x, double y, double z, double a, double b, double c)
{
Cdraw3d_ellipsoid (ident, x, y, z, a, b, c, 1);
}
void Cdraw3d_sphere (const char *ident, double x, double y, double z, double r)
{
Cdraw3d_ellipsoid (ident, x, y, z, r, r, r, 0);
}
/* returns -1 on error, zero on success */
int Cdraw3d_from_text (const char *ident, const char *text)
{
char *p = (char *) text;
int line = 1;
double x, y, z, a, b, c, r, r2;
Vec *v;
int w, h, i, k;
do {
p += strspn(p, " \t\r");
if(!*p)
break;
if (*p == '#' || *p == '\n') {
/* comment, do nothing */ ;
} else if (!strncmp (p, "scale ", 6)) {
if (sscanf (p, "scale %lf", &x) == 1)
Cdraw3d_scale (ident, x);
else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "offset ", 7)) {
if (sscanf (p, "offset %lf %lf %lf", &x, &y, &z) == 3)
Cdraw3d_offset (ident, x, y, z);
else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "density ", 7)) {
if (sscanf (p, "density %lf", &x) == 1)
Cdraw3d_density (ident, x);
else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "cylinder ", 8)) {
if (sscanf (p, "cylinder %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7)
Cdraw3d_cylinder (ident, x, y, z, a, b, c, r);
else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "roundplate ", 11)) {
if (sscanf (p, "roundplate %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7) {
Cdraw3d_roundplate (ident, x, y, z, a, b, c, r);
Cdraw3d_roundplate (ident, -x, -y, -z, a, b, c, r);
} else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "cone ", 5)) {
if (sscanf (p, "cone %lf %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r, &r2) == 8)
Cdraw3d_cone (ident, x, y, z, a, b, c, r, r2);
else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "cappedcone ", 11)) {
if (sscanf (p, "cappedcone %lf %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r, &r2) == 8)
Cdraw3d_cappedcone (ident, x, y, z, a, b, c, r, r2);
else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "cappedcylinder ", 15)) {
if (sscanf (p, "cappedcylinder %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7) {
Cdraw3d_cappedcylinder (ident, x, y, z, a, b, c, r);
} else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "ellipsoid ", 10)) {
if (sscanf (p, "ellipsoid %lf %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c, &r) == 7) {
Cdraw3d_ellipsoid (ident, x, y, z, a, b, c, r);
} else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "rectangle ", 10)) {
if (sscanf (p, "rectangle %lf %lf %lf %lf %lf %lf", &x, &y, &z, &a, &b, &c) == 6) {
Cdraw3d_rectangle (ident, x, y, z, a, b, c);
} else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "sphere ", 7)) {
if (sscanf (p, "sphere %lf %lf %lf %lf ", &x, &y, &z, &r) == 4) {
Cdraw3d_sphere (ident, x, y, z, r);
} else {
textformaterror (line, ident);
return -1;
}
} else if (!strncmp (p, "surface ", 8)) {
if (sscanf (p, "surface %d %d %n", &w, &h, &i) == 2) {
v = Cmalloc(w * h * sizeof(Vec));
for(k = 0; k < w * h; k++) {
p += i;
if(sscanf(p, "%lf %lf %lf %n", &(v[k].x), &(v[k].y), &(v[k].z), &i) != 3) {
textformaterror (line, ident);
free(v);
return -1;
}
}
draw3d_surface(ident, w, h, v);
free(v);
} else {
textformaterror (line, ident);
return -1;
}
} else {
textformaterror (line, ident);
return -1;
}
while (*p != '\n' && *p)
p++;
line++;
} while (*(p++));
Credraw3dobject(ident, 1);
return 0;
}

91
threeDKit/Makefile Normal file
View file

@ -0,0 +1,91 @@
#----------------------------------------------------------------------
# Makefile for threeDKit
#----------------------------------------------------------------------
# *** NO SERVICIBLE PARTS HERE!
# All options are in Makefile.cfg.
include ../Makefile.cfg
srcdir = ..
VPATH = $(srcdir)/src
#----------------------------------------------------------------------
# Compiler Section (overrides Makefile.cfg)
#----------------------------------------------------------------------
INCLUDES += -I$(srcdir)/src
#----------------------------------------------------------------------
# Rules Section
#----------------------------------------------------------------------
CFILES = 3dinit.c 3dkit.c quickmath.c swtriangle.c triangl.c triangle.c wrapsurf.c wtriangle.c
OBJECTS = $(CFILES:.c=.o)
#
# Determine what library (static or shared) we will be linking programs with
ifdef INSTALLSHAREDLIB
LIBS = -lm -lvgagl -lvga
endif
ifndef LIBS
LIBS = -lm $(srcdir)/staticlib/libvgagl.a $(srcdir)/staticlib/libvga.a
LVGADEP = $(srcdir)/staticlib/libvgagl.a $(srcdir)/staticlib/libvga.a
endif
.c.o:
$(CC) $(CFLAGS) -c -o $*.o $<
.c.s:
$(CC) $(CFLAGS) -S -o $*.s $<
.PHONY: all clean install installheaders
ifeq (a.out, $(TARGET_FORMAT))
all: lib3dkit.a plane wrapdemo
else
all: lib3dkit.so.$(VERSION) plane wrapdemo
# These rules are for ELF only.
lib3dkit.so.$(VERSION): $(OBJECTS)
$(CC) -shared -Wl,-soname,lib3dkit.so.$(MAJOR_VER) \
-o lib3dkit.so.$(VERSION) $(OBJECTS) -lm
$(sharedlibdir)/lib3dkit.so.$(VERSION): lib3dkit.so.$(VERSION)
$(INSTALL_SHLIB) $< $(sharedlibdir)/$<
(cd $(sharedlibdir); ln -sf lib3dkit.so.$(VERSION) `echo lib3dkit.so.$(VERSION) | sed 's/\.so\..*/.so/'` )
-ldconfig
install: $(sharedlibdir)/lib3dkit.so.$(VERSION) installheaders
installheaders:
@cp 3dkit.h $(includedir)/3dkit.h
@chmod a+r $(includedir)/3dkit.h
@cp triangle.h $(includedir)/triangle.h
@chmod a+r $(includedir)/triangle.h
endif
lib3dkit.a: $(ALLOBJS)
rm -f lib3dkit.a
$(AR) rcs lib3dkit.a $(ALLOBJS)
plane: planukit.o planinit.o $(OBJECTS) $(LVGADEP)
$(CC) $(LDFLAGS) -o plane planukit.o planinit.o $(OBJECTS) $(LIBS)
# chown root plane
# chmod u+s plane
wrapdemo: wrapdemo.o $(OBJECTS) $(LVGADEP)
$(CC) $(CFLAGS) $(LDFLAGS) -o wrapdemo wrapdemo.c $(OBJECTS) $(LIBS)
# chown root wrapdemo
# chmod u+s wrapdemo
clean:
rm -f *.o core lib3dkit.a lib3dkit.so.* plane wrapdemo *.bak

1
threeDKit/config.h Normal file
View file

@ -0,0 +1 @@
/* autoconf type defines can come here */

40
threeDKit/plane.h Normal file
View file

@ -0,0 +1,40 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
1996 Paul Sheer psheer@icon.co.za
This file is an example program demonstrating the use of the
3dkit library. It is not part of the library and is not copyright.
*/
/*
File: plane.h
*/
/*choose method (see 3dkit.h for explanation)*/
/*comment out for real world-like view:*/
/*#define WORLD_VIEW 0*/
#ifdef WORLD_VIEW
#define PL_METER 1200
/* In this demo PL_METER is about a meter
so the wing is 20 * 1200 = 24000 units across,
within the 65536 limit. */
#else
#define PL_METER 2400
#endif
void initwing (TD_Surface * surf, int lsf, int usd, int half);
void inittips (TD_Surface * surf, int lsf);
void initstab (TD_Surface * surf, int lsf, int usd);
void initfin (TD_Surface * surf, int usd);
void initfus (TD_Surface * surf, float quart);
void initfus1 (TD_Surface * surf, float quart);
void initfus2 (TD_Surface * surf, float quart);
void initfus3 (TD_Surface * surf, float quart);
void initnacelle (TD_Surface * surf, float quart, int lor);
void initnacelle2 (TD_Surface * surf, float quart, int lor);

332
threeDKit/planinit.c Normal file
View file

@ -0,0 +1,332 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
1996 Paul Sheer psheer@icon.co.za
This file is an example program demonstrating the use of the
3dkit library. It is not part of the library and is not copyright.
The author take no responsibility, for the results
of compilation, execution or other usage of this program.
*/
/*
File: planinit.c
*/
/* The following define the plane's geometry:
I once had a list of what every variable did,
not any more, but I'll label those that I know. */
/*fuselage radius*/
#define FRAD (float) 1.1
/*length of tapered portion of fuselage*/
#define FD 8
/*length of various other portions*/
#define FB (float)2.65
#define FA (float)2.78
#define FC (float)3.80
#define FE (float)1.75
#define QM (float).4
#define QM2 (float).15
#define WH (float).5
/*wing span*/
#define SPAN 20
/*root chord length*/
#define CHORD 3
/*wing dihedral*/
#define DIHEDRAL (float).12
/*wing taper*/
#define TAPER .4
#define PRAT (float)M_PI/3
/*elevation of stabiliser*/
#define TAILHEIGHT (float)2.8
/*stabiliser chord, dihedral, taper and span*/
#define TCHORD (float)1.2
#define TDIHEDRAL (float).02
#define TTAPER (float).7
#define TSPAN 5
/*fin length*/
#define VLENGTH (float)2.3
/*fin base elevation, taper, base chord*/
#define VHEIGHT (float).5
#define VTAPER (float).7
#define VCHORD (float)1.7
/*distance between propeller centres*/
#define PROPSPAN 4
/*nacelle radius, length, and two other shape params*/
#define NACRAD (float).4
#define NACLEN (float)1.6
#define RNACLEN (float)2.4
#define NACHEIGHT (float).4
#include <stdarg.h>
#include <stdlib.h>
#include <math.h>
#include "vga.h"
#include "vgagl.h"
#include "./3dkit.h"
#include "./plane.h"
extern int DENS;
extern int DENS2;
float rib[20][3] =
{{-29.7, 0, 0}, {-29.5, 1, 0}, {-29, 2, 0},
{-28, 3, 0}, {-25, 5.3, 0}, {-20, 6.7, 0}, {-12, 8, 0},
{1, 9, 0}, {19, 9, 0}, {39, 7.5, 0}, {59, 4.5, 0}, {82, 0, 0}};
void initwing (TD_Surface * surf, int lsf, int usd, int half)
{
int i, k;
float j;
int LSC = 110;
int widtth, length;
surf->l = widtth = 12;
surf->w = length = DENS2 + 1;
for (k = 0; k < length; k++)
for (i = 0; i < widtth; i++) {
j = k;
if (lsf * usd == -1)
j = length - k - 1;
j = j / 4 + (float) DENS2 *half / 4;
surf->point[i * length + k].x = (float) ((float) j / DENS2 * (SPAN / 2 - FRAD) + FRAD) * lsf * PL_METER;
surf->point[i * length + k].y = -(float) rib[i][0] / LSC * CHORD * (1 - (float) j / DENS2 * (1 - TAPER)) * PL_METER;
surf->point[i * length + k].z = ((float) rib[i][1] / LSC * CHORD * usd * (1 - (float) j / DENS2 * (1 - TAPER))
/ ((float) 1.5 - (float) usd / 2) - WH + (float) j / DENS2 * DIHEDRAL * SPAN / 2) * PL_METER;
}
}
void inittips (TD_Surface * surf, int lsf)
{
int i, k, j, usd;
int LSC = 110;
int widtth, length;
surf->l = widtth = 12;
surf->w = length = 2;
for (j = 0, k = -1; k < 2; k += 2, j++)
for (i = 0; i < widtth; i++) {
usd = -k * lsf;
surf->point[i * length + j].x = (float) ((float) (SPAN / 2 - FRAD) + FRAD) * lsf * PL_METER;
surf->point[i * length + j].y = -(float) rib[i][0] / LSC * CHORD * (1 - (float) (1 - TAPER)) * PL_METER;
surf->point[i * length + j].z = ((float) rib[i][1] / LSC * CHORD * usd * (1 - (float) (1 - TAPER)) /
((float) 1.5 - (float) usd / 2) - WH + (float) DIHEDRAL * SPAN / 2) * PL_METER;
}
}
void initstab (TD_Surface * surf, int lsf, int usd)
{
int i, j, k;
int LSC = 110;
int widtth, length;
surf->l = widtth = 12;
surf->w = length = DENS2 + 1;
for (k = 0; k < length; k++)
for (i = 0; i < widtth; i++) {
j = k;
if (lsf * usd == -1)
j = length - k - 1;
surf->point[i * length + k].x = (float) j / DENS2 * TSPAN / 2 * lsf * PL_METER;
surf->point[i * length + k].y = (-(float) rib[i][0] / LSC * TCHORD * (1 - (float) j / DENS2 * (1 - TTAPER)) - FB - FD) * PL_METER;
surf->point[i * length + k].z = ((float) rib[i][1] / LSC * TCHORD * usd * (1 - (float) j / DENS2 * (1 - TTAPER)) / 2 + TAILHEIGHT + (float) j / DENS2 * TDIHEDRAL * TSPAN / 2) * PL_METER;
}
}
void initfin (TD_Surface * surf, int usd)
{
int i, j, k;
int LSC = 110;
int widtth, length;
float locrad;
surf->l = widtth = 12;
surf->w = length = DENS2 + 1;
locrad = (sin ((float) M_PI / 2 * VCHORD / FD)) * FRAD;
for (k = 0; k < length; k++)
for (i = 0; i < widtth; i++) {
j = k;
if (usd == 1)
j = length - k - 1;
surf->point[i * length + k].x = ((float) rib[i][1] / LSC * VCHORD * usd * (1 - (float) j / DENS2 * (1 - VTAPER)) / 2) * PL_METER;
surf->point[i * length + k].y = (-(float) rib[i][0] / LSC * VCHORD * (1 - (float) j / DENS2 * (1 - VTAPER)) - FB - FD - (float) VCHORD / 2 * ((float) j / DENS2 - 1)) * PL_METER;
surf->point[i * length + k].z = ((float) j / DENS2 * (TAILHEIGHT - locrad) + locrad) * PL_METER;
}
}
void initfus (TD_Surface * surf, float quart)
{
int i, j;
int widtth, length;
float locrad;
surf->w = widtth = surf->l = length = DENS + 1;
for (j = length - 1; j >= 0; j--)
for (i = 0; i < widtth; i++) {
locrad = (sin ((float) M_PI / 2 * j / DENS + .02)) * FRAD;
surf->point[i + j * widtth].x = (float) cos ((float) i / DENS * M_PI / 2 + quart) * PL_METER * locrad;
surf->point[i + j * widtth].y = ((float) j / DENS * FD - FB - FD) * PL_METER;
surf->point[i + j * widtth].z = (float) sin ((float) i / DENS * M_PI / 2 + quart) * PL_METER * locrad;
}
}
void initfus1 (TD_Surface * surf, float quart)
{
int i, j;
int widtth, length;
float locrad;
surf->w = widtth = surf->l = length = DENS + 1;
for (j = length - 1; j >= 0; j--)
for (i = 0; i < widtth; i++) {
locrad = FRAD;
surf->point[i + j * widtth].x = (float) cos ((float) i / DENS * M_PI / 2 + quart) * PL_METER * locrad;
surf->point[i + j * widtth].y = ((float) j / DENS * (FA + FB) - FB) * PL_METER;
surf->point[i + j * widtth].z = (float) sin ((float) i / DENS * M_PI / 2 + quart) * PL_METER * locrad;
}
}
void initfus2 (TD_Surface * surf, float quart)
{
int i, j;
int widtth, length;
float locrad, ya, q;
surf->w = widtth = surf->l = length = DENS + 1;
for (j = length - 1; j >= 0; j--)
for (i = 0; i < widtth; i++) {
ya = ((float) j / DENS * FC + FA);
locrad = (cos ((float) PRAT * j / DENS)) * FRAD;
q = (ya - FA) / FC * QM;
surf->point[i + j * widtth].x = ((float) cos ((float) i / DENS * M_PI / 2 + quart) * locrad) * PL_METER;
surf->point[i + j * widtth].y = ya * PL_METER;
surf->point[i + j * widtth].z = ((float) sin ((float) i / DENS * M_PI / 2 + quart) * locrad - q) * PL_METER;
}
}
void initfus3 (TD_Surface * surf, float quart)
{
int i, j;
int widtth, length;
float ya, locrad, q;
surf->w = widtth = surf->l = length = DENS + 1;
for (j = length - 1; j >= 0; j--)
for (i = 0; i < widtth; i++) {
ya = ((float) j / DENS * FE + (float) FA + (float) FC);
locrad = (cos ((float) PRAT)) * FRAD * sqrt ((float) ((float) FE - ya + (float) FA + (float) FC + .01) / FE);
q = QM + (ya - FA - FC) / FE * QM2;
surf->point[i + j * widtth].x = ((float) cos ((float) i / DENS * M_PI / 2 + quart) * locrad) * PL_METER;
surf->point[i + j * widtth].y = ya * PL_METER;
surf->point[i + j * widtth].z = ((float) sin ((float) i / DENS * M_PI / 2 + quart) * locrad - q) * PL_METER;
}
}
void initnacelle (TD_Surface * surf, float quart, int lor)
{
int i, j;
int widtth, length;
float xa, ya, za, locrad, q, nz, ny;
surf->w = widtth = surf->l = length = DENS + 1;
nz = (float) DIHEDRAL *PROPSPAN - WH + NACHEIGHT;
ny = (float) .27 *CHORD - (.27 * CHORD * PROPSPAN / SPAN / 2);
for (j = length - 1; j >= 0; j--)
for (i = 0; i < widtth; i++) {
ya = ((float) j / DENS * NACLEN + ny);
locrad = (float) NACRAD *sqrt ((float) ((float) NACLEN + ny - ya + .01) / NACLEN);
q = 0;
xa = (float) cos ((float) i / DENS * M_PI / 2 + quart) * locrad;
surf->point[i + j * widtth].x = (xa + lor * PROPSPAN) * PL_METER;
surf->point[i + j * widtth].y = ya * PL_METER;
if (quart > 1.6) {
za = (float) sin ((float) i / DENS * M_PI / 2 + quart) * locrad * 2.5 - q; /*%%%%%%%%%% */
} else {
za = (float) sin ((float) i / DENS * M_PI / 2 + quart) * locrad - q; /*%%%%%%%%%% */
}
surf->point[i + j * widtth].z = (za + nz) * PL_METER;
}
}
void initnacelle2 (TD_Surface * surf, float quart, int lor)
{
int i, j;
int widtth, length;
float xa, ya, za, locrad, q, nz, ny;
surf->w = widtth = surf->l = length = DENS + 1;
nz = (float) DIHEDRAL *PROPSPAN - WH + NACHEIGHT;
ny = (float) .27 *CHORD - (.27 * CHORD * PROPSPAN / SPAN / 2);
for (j = 0; j < length; j++)
for (i = 0; i < widtth; i++) {
ya = ((float) -j / DENS * RNACLEN + ny);
locrad = (float) NACRAD *sqrt ((float) -((float) -RNACLEN + ny - ya - .01) / RNACLEN);
q = 0;
xa = (float) cos ((float) i / DENS * M_PI / 2 + quart) * locrad;
surf->point[i * length + j].x = (xa + lor * PROPSPAN) * PL_METER;
surf->point[i * length + j].y = ya * PL_METER;
za = (float) sin ((float) i / DENS * M_PI / 2 + quart) * locrad - q;
surf->point[i * length + j].z = (za + nz) * PL_METER;
}
}

644
threeDKit/planukit.c Normal file
View file

@ -0,0 +1,644 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
1996 Paul Sheer psheer@icon.co.za
This file is an example program demonstrating the use of the
3dkit library. It is not part of the library and is not copyright.
The author takes no responsibility, for the results
of compilation, execution or other usage of this program.
*/
/*
File: planukit.c
comments or suggestions welcome, send to: psheer@icon.co.za
Demo of 3D graphics tool for drawing shaded 3D surfaces with a light source.
This demo sets up the surfaces (in a crude fashion) and the function
drawobject from 3dkit.c draws them at the specified angle of azimuth,
rotation and elevation. The surfaces are sorted from furthest to closest
and drawn from their furthest corner forward toward their second furthest
corner. So any object made up of reasonable surfaces will be drawn solid
with hidden surfaces properly removed. Backfaced triangles are not drawn
to improve speed.
This demo draws a turbo-prop aeroplane (done originally for a 3rd year
aeronautical engineering design project).
see the handle_key function below for what all the keys do.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h> /*for stderr */
#include <math.h>
#include <string.h>
#include <vga.h>
#include <vgagl.h>
#include "3dkit.h"
#include "3dinit.h"
#include "plane.h"
#ifdef WORLD_VIEW
#define PL_TDOPTION_ROTATE_OBJECT 0
#define PL_TDOPTION_LIGHT_SOURCE_CAM 0
#else
#define PL_TDOPTION_ROTATE_OBJECT TDOPTION_ROTATE_OBJECT
/*Lighting vector follows camera: */
#define PL_TDOPTION_LIGHT_SOURCE_CAM TDOPTION_LIGHT_SOURCE_CAM
#endif
/*closer to 1.25 on my screen: */
#define PL_SCREEN_ASPECT 1.333
/*Number of surfaces in ths plane */
#ifdef WORLD_VIEW
#define PL_NUMSURFACES 62
#else
#define PL_NUMSURFACES 52
/*52 */
#endif
/*maximum width or length of a surface (for malloc) */
#define PL_SURF_SIZE 20
/*globals used for initialisation of surfaces */
/*width and breadth of body surfaces (in grid points) */
int DENS = 2;
/*width of wing surfaces (in grid points). */
int DENS2 = 2;
/* length of wing surfaces is inherent in the following
made-up aerofoil: */
int gmode;
int PL_screen_width;
int PL_screen_height;
/*A trivial example of how to initialise a surface: */
void initplate (TD_Surface * surf, float xstart, float ystart, float zstart, float x, float y, int w, int l)
{
int i, k, j;
/*setup width and length */
surf->w = w + 1;
surf->l = l + 1;
/*initialise a 6 meter square plate with its centre at the origin */
for (k = 0; k < w + 1; k++)
for (i = 0; i < l + 1; i++) {
j = l - i;
surf->point[i * (w + 1) + k].x = (float) PL_METER *(xstart + (float) x * k / w);
surf->point[i * (w + 1) + k].y = (float) PL_METER *(ystart + (float) y * j / l);
surf->point[i * (w + 1) + k].z = (float) PL_METER *zstart;
}
}
/*exchanges the x and y values of a surface, making y negative */
/* This is a patch to get the coords aligned with flight-dynamic's
axes. */
void xchgxy (TD_Surface * surf)
{
int j;
int t;
for (j = 0; j < surf->l * surf->w; j++) {
t = surf->point[j].x;
surf->point[j].x = surf->point[j].y;
surf->point[j].y = -t;
}
}
/*
void gl_triangle (int x1, int y1, int z1, int x2, int y2, int z2,
int x3, int y3, int z3, int bf);
void gl_striangle (int x1, int y1, int x2, int y2, int x3, int y3,
int c, int bf);
*/
/*returns 0 on error */
TD_Solid *PL_init_solid (void)
{
TD_Solid *plane_demo;
int i;
int n = PL_NUMSURFACES;
if ((plane_demo = malloc (sizeof (TD_Solid))) == NULL)
return 0;
memset (plane_demo, 0, sizeof (TD_Solid));
plane_demo->num_surfaces = n;
if ((plane_demo->surf = calloc (n , sizeof (TD_Surface))) == NULL)
return 0;
for (i = 0; i < n; i++) {
if ((plane_demo->surf[i].point
= malloc (PL_SURF_SIZE * PL_SURF_SIZE * sizeof (TD_Point))) == NULL)
return 0;
/* plane_demo->surf[i].render = TD_MESH_AND_SOLID; *//*can leave out and set option ALL_SAME_RENDER */
plane_demo->surf[i].shadow = TD_DEFAULT_COLOR + TD_DEFAULT_SHADOW;
plane_demo->surf[i].maxcolor = TD_DEFAULT_COLOR + TD_DEFAULT_MAXCOLOR;
plane_demo->surf[i].mesh_color = 191; /*navy blue in from the palette set */
plane_demo->surf[i].backfacing = 1; /*don't draw any of surface that faces away */
plane_demo->surf[i].depth_per_color = 6; /*2^6 = 64 colors in the grey scale */
}
plane_demo->alpha = 0; /* begin all at zero (flight dynamics */
plane_demo->beta = 0; /* says plane is level */
plane_demo->gamma = 0;
plane_demo->xlight = -147; /* lighting out of the screen,... */
plane_demo->ylight = -147; /* ...to the right,... */
plane_demo->zlight = 147; /* ...and from the top. */
plane_demo->distance = PL_METER * 35; /* distance of the camera from the */
/* origin, PL_METER * meters. */
/*if PL_TDOPTION_ROTATE_OBJECT is set to zero then we need to
define the full camera position instead: */
plane_demo->x_cam = PL_METER * 35;
plane_demo->y_cam = PL_METER * 0;
plane_demo->z_cam = PL_METER * 0;
/* These two are scale factors for the screen: */
/* xscale is now calculated so that the maximum volume (-2^15 to 2^15 or
-2^31 to 2^31) will just fit inside the screen width at this distance: */
plane_demo->xscale = (int) plane_demo->distance * PL_screen_width / (32768 * 2);
plane_demo->yscale = (float) plane_demo->xscale * PL_SCREEN_ASPECT
* PL_screen_height / PL_screen_width; /*to get display aspect square */
/*The above gives an average (not to telescopic, and not to wide angle) view */
/*use any triangle or linedrawing routine: */
plane_demo->draw_triangle = gl_triangle;
plane_demo->draw_striangle = gl_striangle;
plane_demo->draw_line = gl_line;
/* very important to set TDOPTION_INIT_ROTATION_MATRIX if you don't
calculate the rotation matrix yourself. */
plane_demo->option_flags = TDOPTION_INIT_ROTATION_MATRIX
| TDOPTION_ALL_SAME_RENDER | TDOPTION_SORT_SURFACES
| PL_TDOPTION_ROTATE_OBJECT | PL_TDOPTION_LIGHT_SOURCE_CAM;
plane_demo->render = TD_MESH_AND_SOLID; /*how we want to render it */
return plane_demo;
}
void PL_init_surfaces (TD_Solid * plane)
{
int i;
/* To see what an example of the ellipsoid initialisation: */
/*
TD_initsellipsoid (plane, 0, 0, 0, 0,
PL_METER * 8, PL_METER * 4, PL_METER * 4, 3);
for(i=0;i<6;i++)
TD_initcolor (&plane->surf[i], -256);
return;
*/
for (i = 0; i < 4; i++) {
initfus (&plane->surf[i], i * TD_PI / 2);
}
for (i = 0; i < 4; i++) {
initfus1 (&plane->surf[i + 4], i * TD_PI / 2);
}
for (i = 0; i < 4; i++) {
initfus2 (&plane->surf[i + 8], i * TD_PI / 2);
}
initwing (&plane->surf[12], 1, 1, 0);
initwing (&plane->surf[13], -1, 1, 0);
initwing (&plane->surf[14], 1, -1, 0);
initwing (&plane->surf[15], -1, -1, 0);
initwing (&plane->surf[16], 1, 1, 1);
initwing (&plane->surf[17], -1, 1, 1);
initwing (&plane->surf[18], 1, -1, 1);
initwing (&plane->surf[19], -1, -1, 1);
initwing (&plane->surf[20], 1, 1, 2);
initwing (&plane->surf[21], -1, 1, 2);
initwing (&plane->surf[22], 1, -1, 2);
initwing (&plane->surf[23], -1, -1, 2);
initwing (&plane->surf[24], 1, 1, 3);
initwing (&plane->surf[25], -1, 1, 3);
initwing (&plane->surf[26], 1, -1, 3);
initwing (&plane->surf[27], -1, -1, 3);
initstab (&plane->surf[28], 1, 1);
initstab (&plane->surf[29], -1, 1);
initstab (&plane->surf[30], 1, -1);
initstab (&plane->surf[31], -1, -1);
initfin (&plane->surf[32], 1);
initfin (&plane->surf[33], -1);
for (i = 0; i < 4; i++) {
initfus3 (&plane->surf[i + 34], i * TD_PI / 2);
}
for (i = 0; i < 4; i++) {
initnacelle (&plane->surf[i + 38], i * TD_PI / 2, -1);
}
for (i = 0; i < 4; i++) {
initnacelle (&plane->surf[i + 42], i * TD_PI / 2, 1);
}
for (i = 0; i < 2; i++) {
initnacelle2 (&plane->surf[i + 46], i * TD_PI / 2, -1);
}
for (i = 0; i < 2; i++) {
initnacelle2 (&plane->surf[i + 48], i * TD_PI / 2, 1);
}
inittips (&plane->surf[50], 1);
inittips (&plane->surf[51], -1);
#ifdef WORLD_VIEW
for (i = 0; i < 10; i++)
initplate (&plane->surf[i + 52], -20 + (float) i * 4.44, -20, -3.5, 0.4, 40, 1, 10);
#endif
for (i = 0; i < PL_NUMSURFACES; i++) {
xchgxy (&plane->surf[i]);
TD_initcolor (&plane->surf[i], -256);
/*initialises the color vector (vector normal to each point) */
}
}
/*returns 1 on error */
int PL_init_plane (TD_Solid ** plane)
{
if (!(*plane = PL_init_solid ()))
return 1;
PL_init_surfaces (*plane);
return 0;
}
void PL_init_palette (void)
{
/* Here the depth_per_color is 5 (for 64 colors).
256 / 64 gives 4 colors so TD_Surface->color
can be 0, 64, 128, OR 192 */
int i;
unsigned char palette[768];
for (i = 0; i < 64; i++) {
palette[i * 3] = i;
palette[i * 3 + 1] = i;
palette[i * 3 + 2] = 16 + i / 2;
}
for (i = 0; i < 64; i++) {
palette[(i + 64) * 3 + 0] = i;
palette[(i + 64) * 3 + 1] = 0;
palette[(i + 64) * 3 + 2] = 0;
}
for (i = 0; i < 64; i++) {
palette[(i + 128) * 3 + 0] = 0;
palette[(i + 128) * 3 + 1] = i;
palette[(i + 128) * 3 + 2] = 0;
}
for (i = 0; i < 64; i++) {
palette[(i + 192) * 3 + 0] = 0;
palette[(i + 192) * 3 + 1] = 0;
palette[(i + 192) * 3 + 2] = i;
}
gl_setpalette (&palette);
}
/*returns 1 if exit key is pressed */
int PL_handle_key (TD_Solid * plane)
{
static float incr = 0.1047198;
int finished = 0;
int c;
/*plane->gamma += incr;
plane->beta = -0.5;
plane->render = TD_SOLID;
return 0; *//*---> a screen saver*/
switch (c = getchar ()) {
case 'q':
plane->alpha += incr;
break;
case 'a':
plane->alpha -= incr;
break;
case 'o':
plane->beta += incr;
break;
case 'p':
plane->beta -= incr;
break;
case 'z':
plane->gamma += incr;
break;
case 'x':
plane->gamma -= incr;
break;
case 't':
plane->z_cam += PL_METER;
break;
case 'v':
plane->z_cam -= PL_METER;
break;
case 'g':
plane->x_cam += PL_METER;
break;
case 'f':
plane->x_cam -= PL_METER;
break;
case 'w':
plane->distance += PL_METER;
plane->y_cam += PL_METER;
break;
case 's':
plane->distance -= PL_METER;
plane->y_cam -= PL_METER;
break;
case 'c':
finished = 1;
break;
case 'i':
plane->gamma = 0;
plane->alpha = 0;
plane->beta = 0;
break;
case ' ':
switch (plane->render) {
case TD_MESH:
plane->render = TD_MESH_AND_SOLID;
break;
case TD_MESH_AND_SOLID:
plane->render = TD_SOLID;
break;
case TD_SOLID:
plane->render = TD_EDGES_ONLY;
break;
case TD_EDGES_ONLY:
plane->render = TD_MESH;
break;
}
break;
case 'r':
if (plane->option_flags & TDOPTION_FLAT_TRIANGLE)
plane->option_flags &= 0xFFFFFFFF - TDOPTION_FLAT_TRIANGLE;
else
plane->option_flags |= TDOPTION_FLAT_TRIANGLE;
break;
case '1':
incr += .01047198;
break;
case '2':
incr -= .01047198;
break;
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
DENS = c - '2';
PL_init_surfaces (plane);
}
if (incr < 0)
incr = 0;
return (finished);
}
/*WRITE-PAGE FLIPPING*/
/*
The following routines redirect the setpage functions to take advantage
of the vga memory: writing graphics functions to one half of the memory
while viewing
the other. This saves us a copyscreen, while costing the extra
time it takes to draw to vga memory instead of linear (i.e. the
virtual screen) memory. vga.c should have the minor modification
that would allow this to be done more simply. The following was the
only way that seemed to work without altering vga.c. We will
call the method "Write-page Flipping", as apposed to "Page Flipping"
where pages are flipped, but writing is done to a virtual screen
which is then copied to the vga memory not being viewed. Write-page
Flipping writes directly to the vga memory not being viewed.
The method even works on my TVGA8900CL/D in 320x200 (though it's not
supposed to), and doesn't work in 640x480 (where it is supposed to)
so I have given both options at startup. Note that Write-page flipping
can only work on linear or paged modes (320x200, 640x480, 800x600,
1024x768) since graphics functions to write directly to planar
modes are not supported by svgalib.
*/
GraphicsContext physcr, virscr;
int winflipping, vgawindow = 0;
int Startpage[2];
int gmode, chipset;
void PL_redraw (TD_Solid * plane)
{
gl_clearscreen (64);
TD_draw_solid (plane);
}
void PL_cleanup (TD_Solid * plane)
{
/*this function should free all allocated memory*/
return;
}
void winpointto (int win)
{
if (chipset == TVGA8900 && gmode == G320x200x256) {
/*trident has 4 bpp in this mode */
vga_ext_set(VGA_EXT_PAGE_OFFSET, (Startpage[win] * 4) >> 16);
} else {
vga_ext_set(VGA_EXT_PAGE_OFFSET, Startpage[win] >> 16);
}
vga_setpage (0);
}
void winview (int win)
{
vga_waitretrace ();
vga_setdisplaystart (Startpage[win] * win);
}
void winflip (void)
{
winview (vgawindow);
vgawindow = 1 - vgawindow;
winpointto (vgawindow);
}
void PL_animate (TD_Solid * plane, void (*PL_redraw_callback) (TD_Solid *),
int (*PL_key_callback) (TD_Solid *))
{
do {
PL_redraw_callback (plane);
if(winflipping) {
winflip ();
} else {
gl_setscreenoffset( HEIGHT * WIDTH * currentcontext.flippage );
gl_copyscreen (&physcr);
}
} while (!(int *) PL_key_callback (plane));
}
int pl_getchar (void)
{
int c = 0;
while (c == 0 || c == '\n') {
c = vga_getkey ();
}
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
return c;
}
int main (void)
{
int mode[7] =
{5, 6, 7, 8, 10, 11, 12};
int Winflipping[7] =
{1, 0, 0, 0, 1, 1, 1};
int Winflippages[7] =
{65536, 0, 0, 0, 8 * 65536, 8 * 65536, 16 * 256};
int c, c2;
vga_modeinfo *ginfo;
TD_Solid *plane;
/* Note that in this demo, graphics are written to all modes as
virtual modes, so that the triangle routine optimisations will
operate all the time (see triangle.c). */
vga_init ();
if (!(vga_ext_set(VGA_EXT_AVAILABLE, VGA_AVAIL_SET) & (1 << VGA_EXT_PAGE_OFFSET))) {
puts("You need at least svgalib 1.2.10 to run this program!\n");
exit(1);
}
do {
printf ("\n256 color modes:\n\n1: 320x200\n2: 320x240\n3: 320x400\n");
printf ("4: 360x480\n5: 640x480\n6: 800x600\n7: 1024x768\n");
printf ("\nWhich? ");
c = pl_getchar () - '1';
printf ("\n");
} while (c < 0 || c > 6);
printf("Want (W)rite-page flipping, normal (P)age flipping\n");
printf("using copyscreen, or (N)o page flipping (W/F/N)\n");
printf("(W is faster but may not work, N will always work\n");
printf("but sometimes looks tacky) ?\n");
c2 = pl_getchar();
printf ("\n");
gmode = mode[c];
winflipping = Winflipping[c];
if (!vga_hasmode (gmode)) {
fprintf (stderr, "Mode not available.\n");
exit (-1);
}
vga_setmode (gmode);
gl_setcontextvga (gmode);
ginfo = vga_getmodeinfo (gmode);
PL_screen_width = ginfo->width;
PL_screen_height = ginfo->height;
if (PL_init_plane (&plane)) {
fprintf (stderr, "Unable to intialise data structures.\n");
}
plane->posx = PL_screen_width / 2; /*Where origin will be printed */
plane->posy = PL_screen_height / 2;
PL_init_palette ();
/* to see what the palette looks like: */
/* for(i=0;i<256;i++) gl_line(0,i,PL_screen_width,i,i); getchar(); */
/* Allow write flipping
on 320x200 even though ginfo doesn't report more
than 64k of memory:*/
if ((PL_screen_width * PL_screen_height * 2 > ginfo->maxpixels
&& gmode != G320x200x256) || c2 != 'W')
winflipping = 0;
if (winflipping) {
printf("Using Write-page Flipping.\n");
Startpage[0] = 0; /*define pages offsets into memory*/
Startpage[1] = Winflippages[c];
winflip ();
} else {
gl_getcontext (&physcr);
gl_setcontextvgavirtual (gmode);
gl_getcontext (&virscr);
if(c2 != 'N') {
if(gl_enablepageflipping (&physcr))
printf("Using Page Flipping.\n");
}
}
gl_enableclipping ();
PL_animate (plane, PL_redraw, PL_handle_key);
PL_cleanup (plane);
vga_setmode (TEXT);
return 0;
}

118
threeDKit/quickmath.c Normal file
View file

@ -0,0 +1,118 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#include <math.h>
#include "quickmath.h"
inline double fsqr (double x)
{
return x * x;
}
inline int lsqr (int x)
{
return (int) x * x;
}
inline double fmax (double a, double b)
{
return max(a, b);
}
inline double fmin (double a, double b)
{
return min(a, b);
}
inline double fsgn (double a)
{
return (a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0));
}
inline double dot (Vec a, Vec b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
Vec cross (Vec a, Vec b)
{
Vec c;
c.x = a.y * b.z - a.z * b.y;
c.y = a.z * b.x - a.x * b.z;
c.z = a.x * b.y - a.y * b.x;
return c;
}
Vec plus (Vec a, Vec b)
{
Vec c;
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
return c;
}
Vec minus (Vec a, Vec b)
{
Vec c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return c;
}
Vec times (Vec a, double f)
{
Vec c;
c.x = a.x * f;
c.y = a.y * f;
c.z = a.z * f;
return c;
}
double norm (Vec a)
{
return sqrt (sqr(a.x) + sqr(a.y) + sqr(a.z));
}
void orth_vectors(Vec X, Vec *r1, Vec *r2, double r)
{
if (X.x == 0 && X.y == 0) {
r1->x = 1;
r1->y = 0;
r1->z = 0;
} else {
r1->x = X.y / sqrt (X.x * X.x + X.y * X.y);
r1->y = -X.x / sqrt (X.x * X.x + X.y * X.y);
r1->z = 0;
}
*r1 = times (*r1, r); /* r1 now has length r */
*r2 = cross (X, *r1);
*r2 = times (*r2, r / norm (*r2)); /* r2 now has length r */
/* r1 and r2 are now two vectors prependicular to each other and to (x,y,z) */
}

155
threeDKit/quickmath.h Normal file
View file

@ -0,0 +1,155 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#ifndef QUICK_MATH_H
#define QUICK_MATH_H
#if 0
#ifndef SVGALIB
#include "../config.h"
#endif
#endif
#ifndef PI
#define PI 3.14159265358979323846
#endif
typedef struct {
double x, y, z;
} Vec;
#define sqr(x) ((x)*(x))
#define fswap(a, b) \
{ \
double __t_var = (a); \
(a) = (b); \
(b) = __t_var; \
}
#define swap(a, b) \
{ \
int __t_var = (a); \
(a) = (b); \
(b) = __t_var; \
}
#define max(x,y) (((x) > (y)) ? (x) : (y))
#define min(x,y) (((x) < (y)) ? (x) : (y))
#ifndef __GNUC__
double fsqr (double x);
int lsqr (int x);
double fmax (double a, double b);
double fmin (double a, double b);
double fsgn (double a);
double dot (Vec a, Vec b);
Vec cross (Vec a, Vec b);
Vec plus (Vec a, Vec b);
Vec minus (Vec a, Vec b);
Vec times (Vec a, double f);
double norm (Vec a);
#else
extern inline double fsqr (double x)
{
return x * x;
}
extern inline int lsqr (int x)
{
return (int) x *x;
}
extern inline double fmax (double a, double b)
{
return max (a, b);
}
extern inline double fmin (double a, double b)
{
return min (a, b);
}
extern inline double fsgn (double a)
{
return (a == 0.0 ? 0.0 : (a > 0.0 ? 1.0 : -1.0));
}
extern inline double dot (Vec a, Vec b)
{
return a.x * b.x + a.y * b.y + a.z * b.z;
}
extern inline Vec cross (Vec a, Vec b)
{
Vec c;
c.x = a.y * b.z - a.z * b.y;
c.y = a.z * b.x - a.x * b.z;
c.z = a.x * b.y - a.y * b.x;
return c;
}
extern inline Vec plus (Vec a, Vec b)
{
Vec c;
c.x = a.x + b.x;
c.y = a.y + b.y;
c.z = a.z + b.z;
return c;
}
extern inline Vec minus (Vec a, Vec b)
{
Vec c;
c.x = a.x - b.x;
c.y = a.y - b.y;
c.z = a.z - b.z;
return c;
}
extern inline Vec times (Vec a, double f)
{
Vec c;
c.x = a.x * f;
c.y = a.y * f;
c.z = a.z * f;
return c;
}
extern inline double norm (Vec a)
{
return sqrt (sqr (a.x) + sqr (a.y) + sqr (a.z));
}
#endif
void orth_vectors (Vec X, Vec * r1, Vec * r2, double r);
#endif

3
threeDKit/striangle.c Normal file
View file

@ -0,0 +1,3 @@
#define SOLID
#include "triangl.c"

BIN
threeDKit/susannaRUBENS.bmp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 52 KiB

3
threeDKit/swtriangle.c Normal file
View file

@ -0,0 +1,3 @@
#define SOLID
#define WRAP
#include "triangl.c"

366
threeDKit/tri.c Normal file
View file

@ -0,0 +1,366 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#include <stdlib.h>
#ifdef WRAP
#ifdef INTERP
void gl_wtriangle (int x0, int y0, int xd0, int yd0, int z0,
int x1, int y1, int xd1, int yd1, int z1,
int x2, int y2, int xd2, int yd2, int z2,
TD_tridata * tri)
#else
void gl_swtriangle (int x0, int y0, int xd0, int yd0,
int x1, int y1, int xd1, int yd1,
int x2, int y2, int xd2, int yd2, int z0,
TD_tridata * tri)
#endif
#else
#ifdef INTERP
void gl_triangle (int x0, int y0, int z0,
int x1, int y1, int z1,
int x2, int y2, int z2, int bf)
#else
void gl_striangle (int x0, int y0,
int x1, int y1,
int x2, int y2, int z0, int bf)
#endif
#endif
{
void (*colhline_pos) (void);
void (*colhline_neg) (void);
int dir;
int X;
int nz;
int g0, g1h = 0, g1l = 0;
#ifdef INTERP
int c0;
int c_y;
#endif
#ifdef WRAP
int X0, Y0;
int bf = tri->bf;
int xd_y, yd_y;
#endif
dir = 1;
/*Max triangle size in the order of (2^31) >> SHLB)^(.5) : */
if ((nz = (x0 - x1) * (y0 - y2) - (y0 - y1) * (x0 - x2)) == 0)
return; /*the points are collinear. */
#ifdef INTERP
c_x = -(((y0 - y1) * (z0 - z2) - (z0 - z1) * (y0 - y2)) << SHLB) / nz;
c_y = -(((z0 - z1) * (x0 - x2) - (x0 - x1) * (z0 - z2)) << SHLB) / nz;
#endif
#ifdef WRAP
xd_x = -(((y0 - y1) * (xd0 - xd2) - (xd0 - xd1) * (y0 - y2)) << SHLB) / nz;
xd_y = -(((xd0 - xd1) * (x0 - x2) - (x0 - x1) * (xd0 - xd2)) << SHLB) / nz;
yd_x = -(((y0 - y1) * (yd0 - yd2) - (yd0 - yd1) * (y0 - y2)) << SHLB) / nz;
yd_y = -(((yd0 - yd1) * (x0 - x2) - (x0 - x1) * (yd0 - yd2)) << SHLB) / nz;
#endif
#ifdef INTERP
if ((abs (c_x) > (6 << SHLB)) || (abs (c_y) > (6 << SHLB))) {
int tz0, tz1;
/*so that high colour gradients don't screw up at the edges. */
/*4 is the maximum gradient per pixel. */
c_x >>= 2;
c_y >>= 2;
tz0 = ((2 * z0 + z1 + z2) << SHLB) / 4;
tz1 = ((z0 + 2 * z1 + z2) << SHLB) / 4;
z2 = ((z0 + z1 + 2 * z2) << SHLB) / 4;
z0 = tz0;
z1 = tz1;
} else {
z0 <<= SHLB;
z1 <<= SHLB;
z2 <<= SHLB;
}
#endif
/************** BOOLEAN LOGIC HERE ************/
/* The following allows a triangle to have a different picture on either side */
/* To print triangles that don't appear when viewed from behind use bf = 0|1 */
/* To print triangles that appear with a different picture when viewed from */
/* behind use bf = 2|3 */
#ifdef WRAP
dat = tri->bitmap1;
if (nz > 0) { /* nz is the cross product of the vectors of the two sides
it indicates whether the points were ordered clockwise
or anti-clockwise (you can find out which way by testing) */
if (bf == 1)
return;
if (bf == 3)
dat = tri->bitmap1;
dir++;
} else {
if (!bf)
return;
if (bf == 2)
dat = tri->bitmap2;
}
#else
if (nz > 0) {
if (bf == 1)
return;
dir++;
} else {
if (!bf)
return;
}
#endif
#define Xchg(a,b) {X=(a);(a)=(b);(b)=X;}
if (y1 < y0) {
Xchg (y0, y1);
Xchg (x0, x1);
#ifdef INTERP
Xchg (z0, z1);
#endif
#ifdef WRAP
Xchg (xd0, xd1);
Xchg (yd0, yd1);
#endif
dir++;
}
if (y2 < y1) {
Xchg (y2, y1);
Xchg (x2, x1);
#ifdef INTERP
Xchg (z2, z1);
#endif
#ifdef WRAP
Xchg (xd2, xd1);
Xchg (yd2, yd1);
#endif
dir++;
}
if (y1 < y0) {
Xchg (y0, y1);
Xchg (x0, x1);
#ifdef INTERP
Xchg (z0, z1);
#endif
#ifdef WRAP
Xchg (xd0, xd1);
Xchg (yd0, yd1);
#endif
dir++;
}
_color_lookup = color_lookup;
#ifdef INTERP
c0 = z0;
#else
if(BYTESPERPIXEL == 1) {
c = z0;
} else {
c = _color_lookup[z0];
}
#endif
#ifdef WRAP
X0 = xd0 << SHLB;
Y0 = yd0 << SHLB;
#endif
if (y2 == y0)
return;
g0 = ((int) (x2 - x0) << SHLB) / (y2 - y0);
if (y1 != y0)
g1h = ((int) (x1 - x0) << SHLB) / (y1 - y0);
if (y2 != y1)
g1l = ((int) (x2 - x1) << SHLB) / (y2 - y1);
dir = dir & 1;
/* Very large triangles (larger than the screen) sometimes become a problem,
if so: */
if (__clip) {
if (((abs (x0 - x1) + abs (x1 - x2) + abs (x0 - x2)) >
((__clipx2 - __clipx1) * 2)) || ((y2 - y0) > (__clipy2 - __clipy1)))
return;
if (y2 < __clipy1 || y0 > __clipy2 ||
(x0 < __clipx1 && x1 < __clipx1 && x2 < __clipx1) ||
(x0 > __clipx2 && x1 > __clipx2 && x2 > __clipx2))
return;
}
dx0 = x0;
dy0 = y0;
if(tri_drawpoint) {
_tri_drawpoint = tri_drawpoint;
colhline_pos = linefuncs[(7 * 2) + (BYTESPERPIXEL - 1) * 16];
colhline_neg = linefuncs[(7 * 2) + (BYTESPERPIXEL - 1) * 16 + 1];
} else {
_tri_drawpoint = gl_setpixel;
colhline_pos = linefuncs[(MODETYPE * 2) + (BYTESPERPIXEL - 1) * 16];
colhline_neg = linefuncs[(MODETYPE * 2) + (BYTESPERPIXEL - 1) * 16 + 1];
}
#ifdef tri_set_color
#undef tri_set_color
#endif
#ifdef WRAP
#ifdef INTERP
#define tri_set_color \
xd = X0 + xd_x * px1 + xd_y * py; \
yd = Y0 + yd_x * px1 + yd_y * py; \
c = c0 + c_x * px1 + c_y * py;
#else
#define tri_set_color \
xd = X0 + xd_x * px1 + xd_y * py; \
yd = Y0 + yd_x * px1 + yd_y * py;
#endif
#else
#ifdef INTERP
#define tri_set_color \
c = c0 + c_x * px1 + c_y * py;
#else
#define tri_set_color
#endif
#endif
if (dir == 1) {
if (y1 != y0) {
py = 0;
if (x1 < x0) {
px1 = 0;
px2 = -((abs (g1h) >> 1)) >> SHLB;
} else {
px1 = ((abs (g0) >> 1)) >> SHLB;
px2 = 0;
}
tri_set_color;
colhline_neg ();
if ((py = 1) < y1 - y0)
for (; py < y1 - y0; py++) {
px1 = ((g0 * py) + (abs (g0) >> 1)) >> SHLB;
px2 = ((g1h * py) - (abs (g1h) >> 1)) >> SHLB;
tri_set_color;
colhline_neg ();
}
px1 = min (((g0 * py) + (abs (g0) >> 1)) >> SHLB, max (x2, x0) - x0);
px2 = x1 - x0;
tri_set_color;
colhline_neg ();
} else {
py = 0;
px1 = 0;
px2 = x1 - x0;
tri_set_color;
colhline_neg ();
}
if (y1 != y2) {
if ((py = y1 - y0 + 1) < y2 - y0)
for (; py < y2 - y0; py++) {
px1 = ((g0 * py) + (abs (g0) >> 1)) >> SHLB;
px2 = (((g1l * (py - y1 + y0)) - (abs (g1l) >> 1)) >> SHLB) + x1 - x0;
tri_set_color;
colhline_neg ();
}
if (x1 < x2) {
px1 = x2 - x0;
px2 = x2 - x0 - ((abs (g1l) >> 1) >> SHLB);
} else {
px1 = x2 - x0 + ((abs (g0) >> 1) >> SHLB);
px2 = x2 - x0;
}
tri_set_color;
colhline_neg ();
}
} else {
if (y1 != y0) {
py = 0;
if (x1 > x0) {
px1 = 0;
px2 = ((abs (g1h) >> 1)) >> SHLB;
} else {
px1 = -((abs (g0) >> 1)) >> SHLB;
px2 = 0;
}
tri_set_color;
colhline_pos ();
if ((py = 1) < y1 - y0)
for (; py < y1 - y0; py++) {
px1 = ((g0 * py) - (abs (g0) >> 1)) >> SHLB;
px2 = ((g1h * py) + (abs (g1h) >> 1)) >> SHLB;
tri_set_color;
colhline_pos ();
}
px1 = max (((g0 * py) - (abs (g0) >> 1)) >> SHLB, min (x2, x0) - x0);
px2 = x1 - x0;
tri_set_color;
colhline_pos ();
} else {
py = 0;
px1 = 0;
px2 = x1 - x0;
tri_set_color;
colhline_pos ();
}
if (y1 != y2) {
if ((py = y1 - y0 + 1) < y2 - y0)
for (; py < y2 - y0; py++) {
px1 = ((g0 * py) - (abs (g0) >> 1)) >> SHLB;
px2 = (((g1l * (py - y1 + y0)) + (abs (g1l) >> 1)) >> SHLB) + x1 - x0;
tri_set_color;
colhline_pos ();
}
if (x1 > x2) {
px1 = x2 - x0;
px2 = x2 - x0 + ((abs (g1l) >> 1) >> SHLB);
} else {
px1 = x2 - x0 - ((abs (g0) >> 1) >> SHLB);
px2 = x2 - x0;
}
tri_set_color;
colhline_pos ();
}
}
}

298
threeDKit/triangl.c Normal file
View file

@ -0,0 +1,298 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#include <config.h>
#ifndef DO_NOT_USE_VGALIB
#include <vga.h>
#endif
#include <vgagl.h>
#include "triangle.h"
#include <stdio.h>
#define SHLB 8
#define SHC 0
#ifdef WRAP
#define S_MASK 0x01ff00
#endif
#ifdef byte
#undef byte
#endif
#define byte unsigned char
#ifdef word
#undef word
#endif
#define word unsigned short
#ifdef quad_t
#undef quad_t
#endif
#ifdef INT_IS_16_BITS
#define quad_t unsigned long
#else
#define quad_t unsigned int
#endif
/* this assumes that BYTEWIDTH is not necessarily equal to bytes-per-pixel times WIDTH */
#define assignvpoffset8(x, y, vp) vp = (y) * BYTEWIDTH + (x);
#define assignvpoffset16(x, y, vp) vp = (y) * BYTEWIDTH + ((x) << 1);
#define assignvpoffset24(x, y, vp) vp = (y) * BYTEWIDTH + (x) * 3;
#define assignvpoffset32(x, y, vp) vp = (y) * BYTEWIDTH + ((x) << 2);
#define declarevp8 byte *vpbyte = (byte *) VBUF
#define declarevp16 word *vpword = (word *) VBUF
#define declarevp24 byte *vpbyte = (byte *) VBUF
#define declarevp32 quad_t *vpquad = (quad_t *) VBUF
#define assignvp8(x, y, vp) vpbyte = (byte *) VBUF + (y) * BYTEWIDTH + (x);
#define assignvp16(x, y, vp) vpword = (word *) ((byte *) VBUF + (y) * BYTEWIDTH) + (x);
#define assignvp24(x, y, vp) vpbyte = (byte *) VBUF + (y) * BYTEWIDTH + (x) * 3;
#define assignvp32(x, y, vp) vpquad = (quad_t *) ((byte *) VBUF + (y) * BYTEWIDTH) + (x);
/* here we would like to have a single void pointer and cast it to byte, word or
quad_t, but ansi does not allow casts on LHS :( */
#define decvp8 *(--(vpbyte)) = lookup(color)
#define incvp8 *((vpbyte)++) = lookup(color)
#define decvp16 *(--(vpword)) = lookup(color)
#define incvp16 *((vpword)++) = lookup(color)
#define decvp24 *(--(vpbyte)) = lookup(color) >> 16; \
*(--(vpbyte)) = lookup(color) >> 8; \
*(--(vpbyte)) = lookup(color);
#define incvp24 *((vpbyte)++) = lookup(color); \
*((vpbyte)++) = lookup(color) >> 8; \
*((vpbyte)++) = lookup(color) >> 16;
#define decvp32 *(--(vpquad)) = lookup(color)
#define incvp32 *((vpquad)++) = lookup(color)
#define decvpoffset8 \
if (!offst--) \
vga_setpage (--pg); \
*(vpbyte + offst) = lookup(color);
#define incvpoffset8 \
*(vpbyte + offst) = lookup(color); \
if (!(++offst)) \
vga_setpage (++pg);
#define decvpoffset16 \
if (!offst) \
vga_setpage (--pg); \
offst -= 2; \
*(vpword + offst) = lookup(color);
#define incvpoffset16 \
*(vpword + offst) = lookup(color); \
offst += 2; \
if (!offst) \
vga_setpage (++pg);
#define decvpoffset24 \
if (!offst--) \
vga_setpage (--pg); \
*(vpbyte + offst) = lookup(color) >> 16; \
if (!offst--) \
vga_setpage (--pg); \
*(vpbyte + offst) = lookup(color) >> 8; \
if (!offst--) \
vga_setpage (--pg); \
*(vpbyte + offst) = lookup(color);
#define incvpoffset24 \
*(vpbyte + offst) = lookup(color); \
if (!(++offst)) \
vga_setpage (++pg); \
*(vpbyte + offst) = lookup(color) >> 8; \
if (!(++offst)) \
vga_setpage (++pg); \
*(vpbyte + offst) = lookup(color) >> 16; \
if (!(++offst)) \
vga_setpage (++pg);
#define decvpoffset32 \
if (!offst) \
vga_setpage (--pg); \
offst -= 4; \
*(vpquad + offst) = lookup(color);
#define incvpoffset32 \
*(vpquad + offst) = lookup(color); \
offst += 4; \
if (!offst) \
vga_setpage (++pg);
static int px1, px2, py;
static int c;
#ifdef INTERP
static int c_x;
#endif
#ifdef WRAP
static int xd, xd_x, yd, yd_x;
static unsigned char *dat;
#endif
static int dx0, dy0;
#if defined(WRAP) && defined(INTERP)
/* this must only occur once */
int color_lookup[TRIANGLE_COLOR_LOOKUP_TABLE_SIZE];
static int *_color_lookup;
void gl_trisetcolorlookup (int i, int c)
{
if(i < TRIANGLE_COLOR_LOOKUP_TABLE_SIZE)
color_lookup[i] = c;
}
int gl_trigetcolorlookup (int i)
{
if(i < TRIANGLE_COLOR_LOOKUP_TABLE_SIZE)
return color_lookup[i];
return 0;
}
void (*tri_drawpoint) (int, int, int);
static void (*_tri_drawpoint) (int, int, int);
void gl_trisetdrawpoint(void (*draw_point) (int, int, int))
{
tri_drawpoint = draw_point;
}
#else
extern int color_lookup[TRIANGLE_COLOR_LOOKUP_TABLE_SIZE];
static int *_color_lookup;
extern void (*tri_drawpoint) (int, int, int);
static void (*_tri_drawpoint) (int, int, int);
#endif /* this static is just because static is faster than ordinary array (so I hear) in DLL's */
#define TRI_BPP 8
#include "trisetpixel.c"
#undef TRI_BPP
#define TRI_BPP 16
#include "trisetpixel.c"
#undef TRI_BPP
#define TRI_BPP 24
#include "trisetpixel.c"
#undef TRI_BPP
#define TRI_BPP 32
#include "trisetpixel.c"
#undef TRI_BPP
/*
#define CONTEXT_VIRTUAL 0x0
#define CONTEXT_PAGED 0x1
#define CONTEXT_LINEAR 0x2
#define CONTEXT_MODEX 0x3
#define CONTEXT_PLANAR16 0x4
*/
static void (*linefuncs[64]) (void) =
{
colhline_pos_direct8,
colhline_neg_direct8,
colhline_pos_paged8,
colhline_neg_paged8, /*2 */
colhline_pos_direct8,
colhline_neg_direct8,
colhline_pos_setpixel8,
colhline_neg_setpixel8, /*4 */
colhline_pos_setpixel8,
colhline_neg_setpixel8,
colhline_pos_setpixel8,
colhline_neg_setpixel8, /*6 */
colhline_pos_setpixel8,
colhline_neg_setpixel8,
colhline_pos_setpixel8,
colhline_neg_setpixel8, /*8 */
colhline_pos_direct16,
colhline_neg_direct16,
colhline_pos_paged16,
colhline_neg_paged16, /*2 */
colhline_pos_direct16,
colhline_neg_direct16,
colhline_pos_setpixel16,
colhline_neg_setpixel16, /*4 */
colhline_pos_setpixel16,
colhline_neg_setpixel16,
colhline_pos_setpixel16,
colhline_neg_setpixel16, /*6 */
colhline_pos_setpixel16,
colhline_neg_setpixel16,
colhline_pos_setpixel16,
colhline_neg_setpixel16, /*8 */
colhline_pos_direct24,
colhline_neg_direct24,
colhline_pos_paged24,
colhline_neg_paged24, /*2 */
colhline_pos_direct24,
colhline_neg_direct24,
colhline_pos_setpixel24,
colhline_neg_setpixel24, /*4 */
colhline_pos_setpixel24,
colhline_neg_setpixel24,
colhline_pos_setpixel24,
colhline_neg_setpixel24, /*6 */
colhline_pos_setpixel24,
colhline_neg_setpixel24,
colhline_pos_setpixel24,
colhline_neg_setpixel24, /*8 */
colhline_pos_direct32,
colhline_neg_direct32,
colhline_pos_paged32,
colhline_neg_paged32, /*2 */
colhline_pos_direct32,
colhline_neg_direct32,
colhline_pos_setpixel32,
colhline_neg_setpixel32, /*4 */
colhline_pos_setpixel32,
colhline_neg_setpixel32,
colhline_pos_setpixel32,
colhline_neg_setpixel32, /*6 */
colhline_pos_setpixel32,
colhline_neg_setpixel32,
colhline_pos_setpixel32,
colhline_neg_setpixel32, /*8 */
};
#include "tri.c"

3
threeDKit/triangle.c Normal file
View file

@ -0,0 +1,3 @@
#define INTERP
#include "triangl.c"

29
threeDKit/triangle.h Normal file
View file

@ -0,0 +1,29 @@
#define max(x,y) (((x) > (y)) ? (x) : (y))
#define min(x,y) (((x) < (y)) ? (x) : (y))
#define TRIANGLE_COLOR_LOOKUP_TABLE_SIZE 4096
/* triangle interpolation definitions: */
typedef struct {
unsigned char *bitmap1;
unsigned char *bitmap2;
int bf;
} TD_tridata;
void gl_triangle (int x0, int y0, int z0, int x1, int y1, int z1, int x2, int y2, int z2, int bf);
void gl_wtriangle (int x0, int y0, int xd0, int yd0, int z0, \
int x1, int y1, int xd1, int yd1, int z1, \
int x2, int y2, int xd2, int yd2, int z2, \
TD_tridata * tri); /* This does not alter tri structure. */
void gl_swtriangle (int x0, int y0, int xd0, int yd0, \
int x1, int y1, int xd1, int yd1, \
int x2, int y2, int xd2, int yd2, int c, \
TD_tridata * tri); /* This does not alter tri structure. */
void gl_striangle (int x0, int y0, int x1, int y1, int x2, int y2, int color, int bf);
void gl_trisetcolorlookup (int i, int c);
int gl_trigetcolorlookup (int i);
void gl_trisetdrawpoint (void (setpixelfunc) (int, int, int));

462
threeDKit/trisetpixel.c Normal file
View file

@ -0,0 +1,462 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
#ifdef lookup
#undef lookup
#endif
#if (TRI_BPP==8) || !defined(INTERP)
#define lookup(x) x
#else
#define lookup(x) _color_lookup[x]
#endif
#ifdef assignvp
#undef declarevp
#undef assignvp
#undef assignvpoffset
#undef incvp
#undef incvpoffset
#undef decvp
#undef decvpoffset
#endif
#if TRI_BPP==8
#define declarevp declarevp8
#define assignvp assignvp8
#define assignvpoffset assignvpoffset8
#define incvp incvp8
#define incvpoffset incvpoffset8
#define decvp decvp8
#define decvpoffset decvpoffset8
#endif
#if TRI_BPP==16
#define declarevp declarevp16
#define assignvp assignvp16
#define assignvpoffset assignvpoffset16
#define incvp incvp16
#define incvpoffset incvpoffset16
#define decvp decvp16
#define decvpoffset decvpoffset16
#endif
#if TRI_BPP==24
#define declarevp declarevp24
#define assignvp assignvp24
#define assignvpoffset assignvpoffset24
#define incvp incvp24
#define incvpoffset incvpoffset24
#define decvp decvp24
#define decvpoffset decvpoffset24
#endif
#if TRI_BPP==32
#define declarevp declarevp32
#define assignvp assignvp32
#define assignvpoffset assignvpoffset32
#define incvp incvp32
#define incvpoffset incvpoffset32
#define decvp decvp32
#define decvpoffset decvpoffset32
#endif
#ifdef color
#undef color
#endif
#ifdef WRAP
#ifdef INTERP
#define color \
(dat[(xd >> SHLB) + (yd & S_MASK)] + (c >> (SHLB + SHC)))
#else
#define color \
(dat[(xd >> SHLB) + (yd & S_MASK)] + c)
#endif
#else
#ifdef INTERP
#define color \
(c >> (SHLB + SHC))
#else
#define color \
c
#endif
#endif
/* these are actually all the same except for 8 */
#if TRI_BPP==8
static void colhline_neg_setpixel8 (void)
#endif
#if TRI_BPP==16
static void colhline_neg_setpixel16 (void)
#endif
#if TRI_BPP==24
static void colhline_neg_setpixel24 (void)
#endif
#if TRI_BPP==32
static void colhline_neg_setpixel32 (void)
#endif
{
int count, y = py + dy0, x1 = px1 + dx0, x2 = px2 + dx0;
if (__clip) {
if (y < __clipy1 || y > __clipy2)
return;
if (x1 > __clipx2 + 1) {
#ifdef WRAP
xd -= (x1 - __clipx2 - 1) * xd_x;
yd -= (x1 - __clipx2 - 1) * yd_x;
#endif
#ifdef INTERP
c -= (x1 - __clipx2 - 1) * c_x;
#endif
x1 = __clipx2 + 1;
}
if (x2 < __clipx1) {
x2 = __clipx1;
}
}
count = x1 - x2;
if (count > 0) {
do {
_tri_drawpoint (--x1, y, lookup(color));
#ifdef WRAP
yd -= yd_x;
xd -= xd_x;
#endif
#ifdef INTERP
c -= c_x;
#endif
} while (--count);
}
}
#if TRI_BPP==8
static void colhline_pos_setpixel8 (void)
#endif
#if TRI_BPP==16
static void colhline_pos_setpixel16 (void)
#endif
#if TRI_BPP==24
static void colhline_pos_setpixel24 (void)
#endif
#if TRI_BPP==32
static void colhline_pos_setpixel32 (void)
#endif
{
int count, y = py + dy0, x1 = px1 + dx0, x2 = px2 + dx0;
if (__clip) {
if (y < __clipy1 || y > __clipy2)
return;
if (x1 < __clipx1) {
#ifdef WRAP
xd += (__clipx1 - x1) * xd_x;
yd += (__clipx1 - x1) * yd_x;
#endif
#ifdef INTERP
c += (__clipx1 - x1) * c_x;
#endif
x1 = __clipx1;
}
if (x2 > __clipx2 + 1) {
x2 = __clipx2 + 1;
}
}
count = x2 - x1;
if (count > 0) {
do {
_tri_drawpoint (x1++, y, lookup(color));
#ifdef WRAP
yd += yd_x;
xd += xd_x;
#endif
#ifdef INTERP
c += c_x;
#endif
} while (--count);
}
}
#ifndef DO_NOT_USE_VGALIB
/*draw to 64k vga buffer setting vga page appropriately: */
#if TRI_BPP==8
static void colhline_neg_paged8 (void)
#endif
#if TRI_BPP==16
static void colhline_neg_paged16 (void)
#endif
#if TRI_BPP==24
static void colhline_neg_paged24 (void)
#endif
#if TRI_BPP==32
static void colhline_neg_paged32 (void)
#endif
{
int count, y = py + dy0, x1 = px1 + dx0, x2 = px2 + dx0;
unsigned short offst;
int pg;
declarevp;
int vp = 0;
if (__clip) {
if (y < __clipy1 || y > __clipy2)
return;
if (x1 > __clipx2 + 1) {
#ifdef WRAP
xd -= (x1 - __clipx2 - 1) * xd_x;
yd -= (x1 - __clipx2 - 1) * yd_x;
#endif
#ifdef INTERP
c -= (x1 - __clipx2 - 1) * c_x;
#endif
x1 = __clipx2 + 1;
}
if (x2 < __clipx1) {
x2 = __clipx1;
}
}
count = x1 - x2;
assignvpoffset (x1, y, vp);
pg = vp >> 16;
vga_setpage (pg);
offst = vp;
if (count > 0) {
do {
decvpoffset;
#ifdef WRAP
yd -= yd_x;
xd -= xd_x;
#endif
#ifdef INTERP
c -= c_x;
#endif
} while (--count);
}
}
#if TRI_BPP==8
static void colhline_pos_paged8 (void)
#endif
#if TRI_BPP==16
static void colhline_pos_paged16 (void)
#endif
#if TRI_BPP==24
static void colhline_pos_paged24 (void)
#endif
#if TRI_BPP==32
static void colhline_pos_paged32 (void)
#endif
{
int count, y = py + dy0, x1 = px1 + dx0, x2 = px2 + dx0;
unsigned short offst;
int pg;
declarevp;
int vp = 0;
if (__clip) {
if (y < __clipy1 || y > __clipy2)
return;
if (x1 < __clipx1) {
#ifdef WRAP
xd += (__clipx1 - x1) * xd_x;
yd += (__clipx1 - x1) * yd_x;
#endif
#ifdef INTERP
c += (__clipx1 - x1) * c_x;
#endif
x1 = __clipx1;
}
if (x2 > __clipx2 + 1) {
x2 = __clipx2 + 1;
}
}
count = x2 - x1;
assignvpoffset (x1, y, vp);
pg = vp >> 16;
vga_setpage (pg);
offst = vp;
if (count > 0) {
do {
incvpoffset;
#ifdef WRAP
yd += yd_x;
xd += xd_x;
#endif
#ifdef INTERP
c += c_x;
#endif
} while (--count);
}
}
#else
/*draw to 64k vga buffer setting vga page appropriately: */
#if TRI_BPP==8
static void colhline_neg_paged8 (void)
#endif
#if TRI_BPP==16
static void colhline_neg_paged16 (void)
#endif
#if TRI_BPP==24
static void colhline_neg_paged24 (void)
#endif
#if TRI_BPP==32
static void colhline_neg_paged32 (void)
#endif
{
}
#if TRI_BPP==8
static void colhline_pos_paged8 (void)
#endif
#if TRI_BPP==16
static void colhline_pos_paged16 (void)
#endif
#if TRI_BPP==24
static void colhline_pos_paged24 (void)
#endif
#if TRI_BPP==32
static void colhline_pos_paged32 (void)
#endif
{
}
#endif
/*draw to a linear address space (320x200 or virtual screen): */
#if TRI_BPP==8
static void colhline_neg_direct8 (void)
#endif
#if TRI_BPP==16
static void colhline_neg_direct16 (void)
#endif
#if TRI_BPP==24
static void colhline_neg_direct24 (void)
#endif
#if TRI_BPP==32
static void colhline_neg_direct32 (void)
#endif
{
int count, y = py + dy0, x1 = px1 + dx0, x2 = px2 + dx0;
declarevp;
if (__clip) {
if (y < __clipy1 || y > __clipy2)
return;
if (x1 > __clipx2 + 1) {
#ifdef WRAP
xd -= (x1 - __clipx2 - 1) * xd_x;
yd -= (x1 - __clipx2 - 1) * yd_x;
#endif
#ifdef INTERP
c -= (x1 - __clipx2 - 1) * c_x;
#endif
x1 = __clipx2 + 1;
}
if (x2 < __clipx1)
x2 = __clipx1;
}
count = x1 - x2;
assignvp (x1, y, vp);
if (count > 0) {
do {
decvp;
#ifdef WRAP
yd -= yd_x;
xd -= xd_x;
#endif
#ifdef INTERP
c -= c_x;
#endif
} while (--count);
}
}
#if TRI_BPP==8
static void colhline_pos_direct8 (void)
#endif
#if TRI_BPP==16
static void colhline_pos_direct16 (void)
#endif
#if TRI_BPP==24
static void colhline_pos_direct24 (void)
#endif
#if TRI_BPP==32
static void colhline_pos_direct32 (void)
#endif
{
int count, y = py + dy0, x1 = px1 + dx0, x2 = px2 + dx0;
declarevp;
if (__clip) {
if (y < __clipy1 || y > __clipy2)
return;
if (x1 < __clipx1) {
#ifdef WRAP
xd += (__clipx1 - x1) * xd_x;
yd += (__clipx1 - x1) * yd_x;
#endif
#ifdef INTERP
c += (__clipx1 - x1) * c_x;
#endif
x1 = __clipx1;
}
if (x2 > __clipx2 + 1)
x2 = __clipx2 + 1;
}
count = x2 - x1;
assignvp (x1, y, vp);
if (count > 0) {
do {
incvp;
#ifdef WRAP
yd += yd_x;
xd += xd_x;
#endif
#ifdef INTERP
c += c_x;
#endif
} while (--count);
}
}
/*The following have not yet been implemented */
/* Draws to planar 256 (these could be complicated) */
/*static void colhline_neg_planar (void);
static void colhline_pos_planar (void);*/
/* Draws using accelerated */
/*static void colhline_neg_accel (void);
static void colhline_pos_accel (void);*/

630
threeDKit/wrapdemo.c Normal file
View file

@ -0,0 +1,630 @@
/*
3DKIT version 1.3
High speed 3D graphics and rendering library for Linux.
1996 Paul Sheer psheer@icon.co.za
This file is an example program demonstrating the use of the
3dkit library. It is not part of the library and is not copyright.
The author takes no responsibility, for the results
of compilation, execution or other usage of this program.
*/
/*
File: wrapdemo.c
comments or suggestions welcome.
This program wraps a portrait by Rubens of Susanna Lunden (1622 to 1625)
around an ellipsoid. Because the ellipsoid is defined by six surfaces,
each with the same bitmap the pattern is repeated over the ellipse.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h> /*for stderr */
#include <math.h>
#include <vga.h>
#include <vgagl.h>
#include "3dkit.h"
#include "3dinit.h"
#include "wrapdemo.h"
#ifdef WORLD_VIEW
#define EL_TDOPTION_ROTATE_OBJECT 0
#define EL_TDOPTION_LIGHT_SOURCE_CAM 0
#else
#define EL_TDOPTION_ROTATE_OBJECT TDOPTION_ROTATE_OBJECT
/*Lighting vector follows camera: */
#define EL_TDOPTION_LIGHT_SOURCE_CAM TDOPTION_LIGHT_SOURCE_CAM
#endif
/*closer to 1.25 on my screen: */
#define EL_SCREEN_ASPECT 1.333
/*Number of surfaces in ths ellip */
#define EL_NUMSURFACES 6
/*maximum width or length of a surface (for malloc) */
#define EL_SURF_SIZE 20
/*globals used for initialisation of surfaces */
unsigned char *susanna;
/*width and breadth of body surfaces (in grid points) */
int DENS = 2;
/*width of wing surfaces (in grid points). */
int DENS2 = 2;
/* length of wing surfaces is inherent in the following
made-up aerofoil: */
int gmode;
int EL_screen_width;
int EL_screen_height;
/*A trivial example of how to initialise a surface: */
void initplate (TD_Surface * surf, float xstart, float ystart, float zstart, float x, float y, int w, int l)
{
int i, k, j;
/*setup width and length */
surf->w = w + 1;
surf->l = l + 1;
/*initialise a 6 meter square plate with its centre at the origin */
for (k = 0; k < w + 1; k++)
for (i = 0; i < l + 1; i++) {
j = l - i;
surf->point[i * (w + 1) + k].x = (float) EL_METER *(xstart + (float) x * k / w);
surf->point[i * (w + 1) + k].y = (float) EL_METER *(ystart + (float) y * j / l);
surf->point[i * (w + 1) + k].z = (float) EL_METER *zstart;
}
}
/*exchanges the x and y values of a surface, making y negative */
/* This is a patch to get the coords aligned with flight-dynamic's
axes. */
void xchgxy (TD_Surface * surf)
{
int j;
int t;
for (j = 0; j < surf->l * surf->w; j++) {
t = surf->point[j].x;
surf->point[j].x = surf->point[j].y;
surf->point[j].y = -t;
}
}
/*returns 0 on error */
TD_Solid *EL_init_solid (void)
{
TD_Solid *ellip_demo;
int i;
int n = EL_NUMSURFACES;
if ((ellip_demo = malloc (sizeof (TD_Solid))) == NULL)
return 0;
ellip_demo->num_surfaces = n;
if ((ellip_demo->surf = malloc (n * sizeof (TD_Surface))) == NULL)
return 0;
for (i = 0; i < n; i++) {
if ((ellip_demo->surf[i].point
= malloc (EL_SURF_SIZE * EL_SURF_SIZE * sizeof (TD_Point))) == NULL)
return 0;
/* ellip_demo->surf[i].render = TD_MESH_AND_SOLID; *//*can leave out and set option ALL_SAME_RENDER */
ellip_demo->surf[i].shadow = 1;
ellip_demo->surf[i].maxcolor = 15;
ellip_demo->surf[i].mesh_color = 111;
ellip_demo->surf[i].backfacing = 1;
ellip_demo->surf[i].depth_per_color = 4; /*2^4 = 16 colors in
the grey scale */
ellip_demo->surf[i].bitmap1 = susanna + 245 + 256 * 8; /*skip
header and an unsightly border*/
ellip_demo->surf[i].bitmap2 = NULL; /*no bitmap on reverse
side of surface*/
ellip_demo->surf[i].bitmapwidth = 244; /*skip border on sides*/
ellip_demo->surf[i].bitmaplength = 352; /*and on bottom*/
}
ellip_demo->alpha = 0; /* begin all at zero (flight dynamics */
ellip_demo->beta = 0; /* says plane is level */
ellip_demo->gamma = 0;
ellip_demo->xlight = -147; /* lighting out of the screen,... */
ellip_demo->ylight = -147; /* ...to the right,... */
ellip_demo->zlight = 147; /* ...and from the top. */
ellip_demo->distance = EL_METER * 35; /* distance of the camera from the */
/* origin, EL_METER * meters. */
/*if EL_TDOPTION_ROTATE_OBJECT is set to zero then we need to
define the full camera position instead: */
ellip_demo->x_cam = EL_METER * 35;
ellip_demo->y_cam = EL_METER * 0;
ellip_demo->z_cam = EL_METER * 0;
/* These two are scale factors for the screen: */
/* xscale is now calculated so that the maximum volume (-2^15 to 2^15 or
-2^31 to 2^31) will just fit inside the screen width at this distance: */
ellip_demo->xscale = (int) ellip_demo->distance * EL_screen_width / (32768 * 2);
ellip_demo->yscale = (float) ellip_demo->xscale * EL_SCREEN_ASPECT
* EL_screen_height / EL_screen_width; /*to get display aspect square */
/*The above gives an average (not to telescopic, and not to wide angle) view */
/*use any triangle or linedrawing routine: */
ellip_demo->draw_triangle = gl_triangle;
ellip_demo->draw_striangle = gl_striangle;
ellip_demo->draw_wtriangle = gl_wtriangle;
ellip_demo->draw_swtriangle = gl_swtriangle;
ellip_demo->draw_line = gl_line;
ellip_demo->draw_point = gl_setpixel;
/* very important to set TDOPTION_INIT_ROTATION_MATRIX if you don't
calculate the rotation matrix yourself. */
ellip_demo->option_flags = TDOPTION_INIT_ROTATION_MATRIX
| TDOPTION_ALL_SAME_RENDER | TDOPTION_SORT_SURFACES
| EL_TDOPTION_ROTATE_OBJECT | EL_TDOPTION_LIGHT_SOURCE_CAM;
ellip_demo->render = TD_MESH_AND_SOLID; /*how we want to render it */
return ellip_demo;
}
void EL_init_surfaces (TD_Solid * ellip)
{
/* To see what an example of the ellipsoid initialisation: */
TD_initsellipsoid (ellip, 0, 0, 0, 0,
EL_METER * 12, EL_METER * 8, EL_METER * 8, 6, -256);
/* initplate (&ellip->surf[6], -10, -14.3, -10, 20, 28.7, 2, 3);
TD_initcolor (&ellip->surf[6], -256);*/
/*initialises the color vector (vector normal to each point) */
}
/*returns 1 on error */
int EL_init_ellip (TD_Solid ** ellip)
{
if (!(*ellip = EL_init_solid ()))
return 1;
EL_init_surfaces (*ellip);
return 0;
}
void EL_init_palette2 (void)
{
/* Here the depth_per_color is 5 (for 64 colors).
256 / 64 gives 4 colors so TD_Surface->color
can be 0, 64, 128, OR 192 */
int i;
unsigned char palette[768];
for (i = 0; i < 64; i++) {
palette[i * 3] = i;
palette[i * 3 + 1] = i;
palette[i * 3 + 2] = 16 + i / 2;
}
for (i = 0; i < 64; i++) {
palette[(i + 64) * 3 + 1] = i;
palette[(i + 64) * 3 + 2] = 0;
palette[(i + 64) * 3 + 3] = 0;
}
for (i = 0; i < 64; i++) {
palette[(i + 128) * 3 + 1] = 0;
palette[(i + 128) * 3 + 2] = i;
palette[(i + 128) * 3 + 3] = 0;
}
for (i = 0; i < 64; i++) {
palette[(i + 192) * 3 + 1] = 0;
palette[(i + 192) * 3 + 2] = 0;
palette[(i + 192) * 3 + 3] = i;
}
gl_setpalette (&palette);
}
void EL_init_palette (void)
{
/* Here the depth_per_color is 4 (for 16 colors).
256 / 16 gives 16 colors so TD_Surface->color
can be 0, 16, 32, 48,... */
unsigned char pal16susanna[16][3] =
{
{40, 47, 47},
{98, 96, 129},
{114, 60, 59},
{138, 143, 159},
{70, 73, 71},
{117, 105, 111},
{142, 160, 195},
{61, 58, 70},
{145, 129, 137},
{62, 54, 53},
{166, 173, 219},
{110, 83, 86},
{160, 152, 161},
{150, 115, 105},
{83, 87, 90},
{120, 124, 140}
};
unsigned char palette[768];
int i, j, k;
int max = 0;
for (i = 0; i < 16; i++)
for (j = 0; j < 3; j++)
if (max < pal16susanna[i][j])
max = pal16susanna[i][j];
for (i = 0; i < 16; i++) /*through all 16 susanna */
for (j = 0; j < 16; j++) /*through all shades */
for (k = 0; k < 3; k++) /*through red green and blue */
palette[((i * 16) + j) * 3 + k] =
(int) pal16susanna[i][k] * ( (16 - (15 - j)*12/15 ) * 4 - 1) / max;
gl_setpalette (&palette);
}
/*returns 1 if exit key is pressed */
int EL_handle_key (TD_Solid * ellip)
{
static float incr = 0.1047198;
int finished = 0;
int c;
/*ellip->gamma += incr;
ellip->beta = -0.5;
ellip->render = TD_SOLID;
return 0; *//*---> a screen saver*/
switch (c = getchar ()) {
case 'q':
ellip->alpha += incr;
break;
case 'a':
ellip->alpha -= incr;
break;
case 'o':
ellip->beta += incr;
break;
case 'p':
ellip->beta -= incr;
break;
case 'z':
ellip->gamma += incr;
break;
case 'x':
ellip->gamma -= incr;
break;
case 't':
ellip->z_cam += EL_METER;
break;
case 'v':
ellip->z_cam -= EL_METER;
break;
case 'g':
ellip->x_cam += EL_METER;
break;
case 'f':
ellip->x_cam -= EL_METER;
break;
case 'w':
ellip->distance += EL_METER;
ellip->y_cam += EL_METER;
break;
case 's':
ellip->distance -= EL_METER;
ellip->y_cam -= EL_METER;
break;
case 'c':
finished = 1;
break;
case 'i':
ellip->gamma = 0;
ellip->alpha = 0;
ellip->beta = 0;
break;
case ' ':
switch (ellip->render) {
case TD_MESH:
ellip->render = TD_MESH_AND_SOLID;
break;
case TD_MESH_AND_SOLID:
ellip->render = TD_SOLID;
break;
case TD_SOLID:
ellip->render = TD_EDGES_ONLY;
break;
case TD_EDGES_ONLY:
ellip->render = TD_MESH;
break;
}
break;
case 'r':
if (ellip->option_flags & TDOPTION_FLAT_TRIANGLE)
ellip->option_flags &= 0xFFFFFFFF - TDOPTION_FLAT_TRIANGLE;
else
ellip->option_flags |= TDOPTION_FLAT_TRIANGLE;
break;
case '1':
incr += .01047198;
break;
case '2':
incr -= .01047198;
break;
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
DENS = c - '2';
EL_init_surfaces (ellip);
}
if (incr < 0)
incr = 0;
return (finished);
}
/*WRITE-PAGE FLIPPING*/
GraphicsContext physcr, virscr;
int winflipping, vgawindow = 0;
int Startpage[2];
int gmode, chipset;
void EL_redraw (TD_Solid * ellip)
{
gl_clearscreen (0);
TD_draw_solid (ellip);
}
void EL_cleanup (TD_Solid * ellip)
{
/*this function should free all allocated memory*/
return;
}
void winpointto (int win)
{
if (chipset == TVGA8900 && gmode == G320x200x256) {
/*trident has 4 bpp in this mode */
vga_ext_set(VGA_EXT_PAGE_OFFSET, (Startpage[win] * 4) >> 16);
} else {
vga_ext_set(VGA_EXT_PAGE_OFFSET, Startpage[win] >> 16);
}
vga_setpage (0);
}
void winview (int win)
{
vga_waitretrace();
vga_setdisplaystart (Startpage[win] * win);
}
void winflip (void)
{
winview (vgawindow);
vgawindow = 1 - vgawindow;
winpointto (vgawindow);
}
void EL_animate (TD_Solid * ellip, void (*EL_redraw_callback) (TD_Solid *),
int (*EL_key_callback) (TD_Solid *))
{
do {
EL_redraw_callback (ellip);
if(winflipping) {
winflip ();
} else {
gl_setscreenoffset( HEIGHT * WIDTH * currentcontext.flippage );
gl_copyscreen (&physcr);
}
} while (!(int *) EL_key_callback (ellip));
}
int el_getchar (void)
{
int c = 0;
while (c == 0 || c == '\n') {
c = vga_getkey ();
}
if (c >= 'a' && c <= 'z')
c += 'A' - 'a';
return c;
}
int main (void)
{
int i;
int mode[7] = {5, 6, 7, 8, 10, 11, 12};
int Winflipping[7] = {1, 0, 0, 0, 1, 1, 1};
int Winflippages[7] = {65536, 0, 0, 0, 8 * 65536, 8 * 65536, 16 * 256};
int c, c2;
vga_modeinfo *ginfo;
TD_Solid *ellip;
FILE *in;
/* Call vga_init as early as possible to get rid of root priv when reading files. */
vga_init ();
if (!(vga_ext_set(VGA_EXT_AVAILABLE, VGA_AVAIL_SET) & (1 << VGA_EXT_PAGE_OFFSET))) {
puts("You need at least svgalib 1.2.10 to run this program!\n");
exit(1);
}
if((susanna = malloc(150000))==NULL) {
fprintf(stderr, "Error returned from malloc.\n");
exit(1);
}
/*susanna[109000] = 'A';*/
if ((in = fopen ("susannaRUBENS.bmp", "rb")) == NULL) {
fprintf (stderr, "Cannot open input file.");
exit (1);
}
i = 0;
while ((c = fgetc (in)) != EOF) {
/*to prevent picture aliasing, use random():*/
if(random() & 1) {
susanna[i++] = c & 0xf0;
susanna[i++] = (c >> 4) & 0x0f;
} else {
susanna[i++] = (c >> 4) & 0x0f;
susanna[i++] = c & 0xf0;
}
}
fclose (in);
/*printf("\n\n%d\n\n\n", susanna[109000]);*/
/* Note that in this demo, graphics are written to all modes as
virtual modes, so that the triangle routine optimisations will
operate all the time (see triangle.c). */
do {
printf ("\n256 color modes:\n\n1: 320x200\n2: 320x240\n3: 320x400\n");
printf ("4: 360x480\n5: 640x480\n6: 800x600\n7: 1024x768\n");
printf ("\nWhich? ");
c = el_getchar () - '1';
printf ("\n");
} while (c < 0 || c > 6);
printf("Want (W)rite-page flipping, normal (P)age flipping\n");
printf("using copyscreen, or (N)o page flipping (W/F/N)\n");
printf("(W is faster but may not work, N will always work\n");
printf("but sometimes looks tacky) ?\n");
c2 = el_getchar();
printf ("\n");
gmode = mode[c];
winflipping = Winflipping[c];
if (!vga_hasmode (gmode)) {
fprintf (stderr, "Mode not available.\n");
exit (-1);
}
vga_setmode (gmode);
gl_setcontextvga (gmode);
ginfo = vga_getmodeinfo (gmode);
EL_screen_width = ginfo->width;
EL_screen_height = ginfo->height;
if (EL_init_ellip (&ellip)) {
fprintf (stderr, "Unable to intialise data structures.\n");
}
ellip->posx = EL_screen_width / 2; /*Where origin will be printed */
ellip->posy = EL_screen_height / 2;
EL_init_palette ();
/* to see what the palette looks like: */
/* for(i=0;i<256;i++) gl_line(0,i,EL_screen_width,i,i); el_getchar(); */
/*to see the bitmap*/
/*
for (j = 0; j < (EL_screen_height < 416 ? EL_screen_height : 416); j++)
for (i = 0; i < 256; i++)
gl_setpixel (i, j, 15 + susanna[240 + i + (j << 8)]);
el_getchar ();
*/
/* My trident 8900CL/D will allow write flipping
on 320x200 even though ginfo doesn't report more
than 64k of memory:*/
if ((EL_screen_width * EL_screen_height * 2 > ginfo->maxpixels
&& gmode != G320x200x256) || c2 != 'W')
winflipping = 0;
if (winflipping) {
printf("Using Write-page Flipping.\n");
Startpage[0] = 0; /*define pages offsets into memory*/
Startpage[1] = Winflippages[c];
winflip ();
} else {
gl_getcontext (&physcr);
gl_setcontextvgavirtual (gmode);
gl_getcontext (&virscr);
if(c2 != 'N') {
if(gl_enablepageflipping (&physcr))
printf("Using Page Flipping.\n");
}
}
gl_enableclipping ();
EL_animate (ellip, EL_redraw, EL_handle_key);
EL_cleanup (ellip);
vga_setmode (TEXT);
return;
}

30
threeDKit/wrapdemo.h Normal file
View file

@ -0,0 +1,30 @@
/*
3DKIT version 1.2
High speed 3D graphics and rendering library for Linux.
1996 Paul Sheer psheer@icon.co.za
This file is an example program demonstrating the use of the
3dkit library. It is not part of the library and is not copyright.
*/
/*
File: wrapdemo.h
*/
/*choose method (see 3dkit.h for explanation)*/
/*comment out for real world-like view:*/
/*#define WORLD_VIEW 0*/
#ifdef WORLD_VIEW
#define EL_METER 1200
/* In this demo EL_METER is about a meter
so the wing is 20 * 1200 = 24000 units across,
within the 65536 limit. */
#else
#define EL_METER 2400
#endif

315
threeDKit/wrapsurf.c Normal file
View file

@ -0,0 +1,315 @@
/*
3DKIT version 1.2
High speed 3D graphics and rendering library for Linux.
Copyright (C) 1996, 1997 Paul Sheer psheer@icon.co.za
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
MA 02111-1307, USA
*/
/*
File: wrapsurf.c
Comments or suggestions welcome.
This function wraps a bitmap over a surface.
See wrapdemo.c for a demonstration of its use.
Use bitmap data of only 256x512 (see wtriangle.c)
although any region within the bitmap can be drawn
to the full extents of the surface, so multiple
small bitmaps can reside in one image and be
used for different surfaces.
*/
#define TD_MULCONSTANT 4096
#include <config.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef DO_NOT_USE_VGALIB
#include <vga.h>
#endif
#include <vgagl.h>
#include "3dkit.h"
/*global for holding a surface temporarily:*/
extern TD_Short_Point *temp;
static void xchg (int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
void TD_drawwrapsurface (TD_Solid * s, int which)
{
TD_Surface *surf = &s->surf[which];
int w = surf->w;
int l = surf->l;
int bitmapwidth = surf->bitmapwidth;
int bitmaplength = surf->bitmaplength;
int i = 0, j = 0, k = 0, c = surf->mesh_color;
void (*dl) (int, int, int, int, int) = s->draw_line;
void (*dsw) (int, int, int, int,
int, int, int, int,
int, int, int, int, int,
TD_tridata *) = s->draw_swtriangle;
void (*dw) (int, int, int, int, int,
int, int, int, int, int,
int, int, int, int, int,
TD_tridata *) = s->draw_wtriangle;
int mesh;
int d1, d2, d3, d4, d;
int x1, y1, c1;
int x2, y2, c2;
int x3, y3, c3;
int x4, y4, c4;
int u1, v1;
int u2, v2;
int u3, v3;
int u4, v4;
int furthest, clockwise = 0;
TD_tridata tri;
tri.bitmap1 = surf->bitmap1;
tri.bitmap2 = surf->bitmap2;
if (s->option_flags & TDOPTION_ALL_SAME_RENDER)
mesh = (s->render == TD_MESH_AND_SOLID);
else
mesh = (surf->render == TD_MESH_AND_SOLID);
/*distance of four corners (numbered clockwise): */
d1 = TD_finddistance (s, &surf->point[0]);
d2 = TD_finddistance (s, &surf->point[w - 1]);
d3 = TD_finddistance (s, &surf->point[w * l - 1]);
d4 = TD_finddistance (s, &surf->point[w * (l - 1)]);
/*find furthest point */
furthest = 1;
d = d1;
if (d2 > d) {
furthest = 2;
d = d2;
}
if (d3 > d) {
furthest = 3;
d = d3;
}
if (d4 > d)
furthest = 4;
/*draw scanning from the furthest point to the second furthest point */
/*there are eight possibilities: */
switch (furthest) {
case 1:
if (d2 > d4) {
clockwise = 0;
for (j = 0; j < l; j++)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].u = i * bitmapwidth / (w - 1);
temp[k].v = j * bitmaplength / (l - 1);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 1;
for (j = 0; j < l; j++)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].u = j * bitmapwidth / (l - 1);
temp[k].v = i * bitmaplength / (w - 1);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
case 2:
if (d1 > d3) {
clockwise = 1;
for (j = 0; j < l; j++)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].u = i * bitmapwidth / (w - 1);
temp[k].v = j * bitmaplength / (l - 1);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 0;
for (j = l - 1; j >= 0; j--)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].u = j * bitmapwidth / (l - 1);
temp[k].v = i * bitmaplength / (w - 1);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
case 3:
if (d4 > d2) {
clockwise = 0;
for (j = l - 1; j >= 0; j--)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].u = i * bitmapwidth / (w - 1);
temp[k].v = j * bitmaplength / (l - 1);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 1;
for (j = l - 1; j >= 0; j--)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].u = j * bitmapwidth / (l - 1);
temp[k].v = i * bitmaplength / (w - 1);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
case 4:
if (d3 > d1) {
clockwise = 1;
for (j = l - 1; j >= 0; j--)
for (i = 0; i < w; i++) {
TD_translate (s, &surf->point[i + j * w], &temp[k]);
temp[k].u = i * bitmapwidth / (w - 1);
temp[k].v = j * bitmaplength / (l - 1);
temp[k].c = TD_findcolor (s, &surf->point[i + j * w], which);
k++;
}
} else {
xchg (&l, &w);
clockwise = 0;
for (j = 0; j < l; j++)
for (i = w - 1; i >= 0; i--) {
TD_translate (s, &surf->point[i * l + j], &temp[k]);
temp[k].u = j * bitmapwidth / (l - 1);
temp[k].v = i * bitmaplength / (w - 1);
temp[k].c = TD_findcolor (s, &surf->point[i * l + j], which);
k++;
}
}
break;
}
if (!surf->backfacing)
clockwise += 2; /*Otherwise a different bitmap on either side*/
tri.bf = clockwise;
for (k = 0, j = 0; j < l - 1; j++, k++) {
for (i = 0; i < w - 1; i++, k++) {
/*define the grid square we are currently drawing: */
x1 = temp[k].x;
y1 = temp[k].y;
u1 = temp[k].u;
v1 = temp[k].v;
c1 = temp[k].c;
x2 = temp[k + 1].x;
y2 = temp[k + 1].y;
u2 = temp[k + 1].u;
v2 = temp[k + 1].v;
c2 = temp[k + 1].c;
x3 = temp[k + w + 1].x;
y3 = temp[k + w + 1].y;
u3 = temp[k + w + 1].u;
v3 = temp[k + w + 1].v;
c3 = temp[k + w + 1].c;
x4 = temp[k + w].x;
y4 = temp[k + w].y;
u4 = temp[k + w].u;
v4 = temp[k + w].v;
c4 = temp[k + w].c;
/*draw with two triangles */
if (furthest & 1) { /*draw with hypotenuse from point 1 to point 3*/
if (s->option_flags & TDOPTION_FLAT_TRIANGLE) {
c1 = (c1 + c2 + c3 + c4) >> 2;
(*dsw) (x1, y1, u1, v1, x2, y2, u2, v2, x3, y3, u3, v3, c1, &tri);
(*dsw) (x1, y1, u1, v1, x3, y3, u3, v3, x4, y4, u4, v4, c1, &tri);
} else {
(*dw) (x1, y1, u1, v1, c1, x2, y2, u2, v2, c2, x3, y3, u3, v3, c3, &tri);
(*dw) (x1, y1, u1, v1, c1, x3, y3, u3, v3, c3, x4, y4, u4, v4, c4, &tri);
}
} else { /*draw with hypotenuse from point 2 to point 4*/
if (s->option_flags & TDOPTION_FLAT_TRIANGLE) {
c1 = (c1 + c2 + c3 + c4) >> 2;
(*dsw) (x1, y1, u1, v1, x2, y2, u2, v2, x4, y4, u4, v4, c1, &tri);
(*dsw) (x2, y2, u2, v2, x3, y3, u3, v3, x4, y4, u4, v4, c1, &tri);
} else {
(*dw) (x1, y1, u1, v1, c1, x2, y2, u2, v2, c2, x4, y4, u4, v4, c4, &tri);
(*dw) (x2, y2, u2, v2, c2, x3, y3, u3, v3, c3, x4, y4, u4, v4, c4, &tri);
}
}
if (mesh) {
(*dl) (x1, y1, x2, y2, c);
(*dl) (x1, y1, x4, y4, c);
}
}
if (mesh)
(*dl) (temp[k + w].x, temp[k + w].y, temp[k].x, temp[k].y, c);
}
if (mesh) {
for (i = 0; i < w - 1; i++, k++)
(*dl) (temp[k + 1].x, temp[k + 1].y, temp[k].x, temp[k].y, c);
}
}

3
threeDKit/wtriangle.c Normal file
View file

@ -0,0 +1,3 @@
#define WRAP
#define INTERP
#include "triangl.c"