Mail Archives: djgpp/2001/04/19/16:18:14
From: | "Santing, Peter" <peter DOT nospam DOT santing AT phibian DOT net>
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | trouble with classes (for me a unexplainable crash)
|
Date: | Thu, 19 Apr 2001 22:01:48 +0200
|
Message-ID: | <987710007.24446.0.pluto.d4ee1e86@news.demon.nl>
|
NNTP-Posting-Host: | santi.demon.nl
|
X-NNTP-Posting-Host: | santi.demon.nl:212.238.30.134
|
X-Trace: | news.demon.nl 987710007 pluto:24446 NO-IDENT santi.demon.nl:212.238.30.134
|
X-Complaints-To: | abuse AT nl DOT demon DOT net
|
X-Priority: | 3
|
X-MSMail-Priority: | Normal
|
X-Newsreader: | Microsoft Outlook Express 5.00.2314.1300
|
X-MimeOLE: | Produced By Microsoft MimeOLE V5.00.2314.1300
|
Lines: | 283
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
Hello, I have a small problem that causes big problems (hehe). I am using
DJGPP 2.95.3 with Allegro 3.12
I wrote something, well it progressed nicely, but suddenly (after a machine
restart) the code will not
work and CAN NOT work because of a "for me" unknown reason.
I found out where it crashed.. it is in the "void main()", when declaring
the object "mypoly" as classtype "object3d"
I see NO reason why that should crash.. for me (as far as I know it). it
just crashes when it initailizes (even when I comment out
that code, it crashes).. I have even commented out all the other code
referring to the object "mypoly", but left in the
declaration intact. it crashes.. when I remove the declaration.. it doesn't
crash. (but then I do not have a object to start with either)..
I am really getting desperate here (and not to mention.. irritated):
Regards,
Peter Santing
this is the code: (the WHOLE thing)::
#include <allegro.h>
#include <math.h>
const int midz = 300; // Z-middle at 300!
const long maxpolygons = 5000; // objects and world object may
consist up to 5000 polygons
RGB_MAP rgb_table;
COLOR_MAP light_table;
// structures
struct polygon {
int avgz; // needed for Z sorting (4)
BITMAP *texmap; // texture map for this polygon! (4) (8)
fixed x[3]; // 3 'x' points (12) (20)
fixed y[3]; // 3 'y' points (12) (32)
fixed z[3]; // 3 'z' points (12) (44)
fixed u[3]; // uv-mapping#1 3 points (12) (56)
fixed v[3]; // "" #2 "" (12) (68)
}; // structure size is 68 bytes..
const long polysize = 68;
// some 3D specific computations
int sort3d (const void *a, const void *b) {
int c = *(int *)a;
int d = *(int *)b;
if (c == d) return 0;
if (c < d) return 1;
return -1;
}
void c3d (float x, float y, float z, int w, int h, int &x2, int &y2) {
x2 = w/2 + int((midz/(midz+z))*x);
y2 = h/2 + int((midz/(midz+z))*y);
}
double dCos (int deg) { return cos (((6.28)/360.)*deg); }
double dSin (int deg) { return sin (((6.28)/360.)*deg); }
void rotate3d (float x, float y, float z, int rx, int ry, int rz, int &nx,
int &ny, int &nz) {
int ox = int (x), oy = int (y), oz = int (z);
// phase I (x rot)
nx = ox;
ny = int(dCos (rx) * oy - dSin (rx) * oz);
nz = int(dSin (rx) * oy + dCos (rx) * oz);
// phase II (y rot)
ox = nx; oy = ny; oz = nz;
ny = oy;
nx = int(dCos (ry) * ox - dSin (ry) * oz);
nz = int(dSin (ry) * ox + dCos (ry) * oz);
// phase III (z rot)
ox = nx; oy = ny; oz = nz;
nz = oz;
ny = int(dCos (rz) * oy - dSin (rz) * ox);
nx = int(dSin (rz) * oy + dCos (rz) * ox);
}
class object3d {
public:
struct polygon polys[maxpolygons]; // declare maximum polygon
size! (raw polygon code)
struct polygon xpolys[maxpolygons]; // translated polygons (x,
y, z, u, v, avgz xlated)
int numpolys; // actual used number of polygons!
int _x, _y, _z; // object position!
int rx, ry, rz; // object rotation!
object3d :: object3d () {
numpolys = 0;
_x = 0;
_y = 0;
_z = 0;
rx = 0;
ry = 0;
rz = 0;
}
void createpolygon (int x1, int y1, int z1, int x2, int y2, int
z2, int x3, int y3, int z3, BITMAP *texmap, int xr, int yr, int type) {
// this creates a polygon
polys [numpolys].x[0] = x1;
polys [numpolys].x[1] = x2;
polys [numpolys].x[2] = x3;
polys [numpolys].y[0] = y1;
polys [numpolys].y[1] = y2;
polys [numpolys].y[2] = y3;
polys [numpolys].z[0] = z1;
polys [numpolys].z[1] = z2;
polys [numpolys].z[2] = z3;
// compute UV mappings!
if (type == 0) {
polys [numpolys].u[0] = 0;
polys [numpolys].v[0] = 0;
polys [numpolys].u[1] = (texmap->w * xr) << 16;
polys [numpolys].v[1] = 0;
polys [numpolys].u[2] = 0;
polys [numpolys].v[2] = (texmap->h * yr) << 16;
polys [numpolys].texmap = texmap;
}
if (type == 1) {
polys [numpolys].u[0] = 0;
polys [numpolys].v[0] = (texmap->h * yr) << 16;
polys [numpolys].u[1] = (texmap->w * xr) << 16;
polys [numpolys].v[1] = 0;
polys [numpolys].u[2] = (texmap->w * xr) << 16;
polys [numpolys].v[2] = (texmap->h * yr) << 16;
polys [numpolys].texmap = texmap;
}
numpolys++;
}
void setposition (int nx, int ny, int nz) {
_x = nx;
_y = ny;
_z = nz;
}
void setrotation (int nx, int ny, int nz) {
rx = nx;
ry = ny;
rz = nz;
}
void render (BITMAP *target) {
V3D triangle [3];
int x1, y1, x2, y2, x3, y3;
int _x1, _y1, _z1, _x2, _y2, _z2, _x3, _y3, _z3;
int t;
int az;
set_trans_blender (0, 0, 0, 128);
for (t = 0; t < numpolys; t++) {
rotate3d (polys [t].x[0], polys [t].y[0], polys
[t].z[0],
rx, ry, rz, _x1, _y1, _z1);
rotate3d (polys [t].x[1], polys [t].y[1], polys
[t].z[1],
rx, ry, rz, _x2, _y2, _z2);
rotate3d (polys [t].x[2], polys [t].y[2], polys
[t].z[2],
rx, ry, rz, _x3, _y3, _z3);
// reconstruct polygons (raw->compiled)
xpolys [t].x[0] = _x1; xpolys [t].y[0] = _y1; xpolys
[t].z[0] = _z1;
xpolys [t].x[1] = _x2; xpolys [t].y[1] = _y2; xpolys
[t].z[1] = _z2;
xpolys [t].x[2] = _x3; xpolys [t].y[2] = _y3; xpolys
[t].z[2] = _z3;
xpolys [t].u[0] = polys [t].u[0];
xpolys [t].v[0] = polys [t].v[0];
xpolys [t].u[1] = polys [t].u[1];
xpolys [t].v[1] = polys [t].v[1];
xpolys [t].u[2] = polys [t].u[2];
xpolys [t].v[2] = polys [t].v[2];
xpolys [t].texmap = polys [t].texmap;
// compute current average
az = (_z1*10 + _z2*10 + _z3*10) / 3;
polys [t].avgz = az;
}
// sort the "compiled" polygons
qsort ((void *)xpolys, numpolys, polysize, sort3d);
// the pre-rendering part is done.. now do the actual
rendering!
for (t = 0; t < numpolys; t++) {
c3d (xpolys [t].x[0]+_x, xpolys [t].y[0]+_y, xpolys
[t].z[0]+_z,
target->cr, target->cb, x1, y1);
c3d (xpolys [t].x[1]+_x, xpolys [t].y[1]+_y, xpolys
[t].z[1]+_z,
target->cr, target->cb, x2, y2);
c3d (xpolys [t].x[2]+_x, xpolys [t].y[2]+_y, xpolys
[t].z[2]+_z,
target->cr, target->cb, x3, y3);
// point #0
triangle [0].x = x1 << 16;
triangle [0].y = y1 << 16;
triangle [0].z = midz + (xpolys[t].z[0] + _z);
triangle [0].u = xpolys [t].u[0];
triangle [0].v = xpolys [t].v[0];
triangle [0].c = 227 - (xpolys [t].z[0]+_z) / 5;
// point #1
triangle [1].x = x2 << 16;
triangle [1].y = y2 << 16;
triangle [1].z = midz + (xpolys[t].z[1] + _z);
triangle [1].u = xpolys [t].u[1];
triangle [1].v = xpolys [t].v[1];
triangle [1].c = 227 - (xpolys [t].z[1]+_z) / 5;
// point #2
triangle [2].x = x3 << 16;
triangle [2].y = y3 << 16;
triangle [2].z = midz + (xpolys[t].z[2] + _z);
triangle [2].u = xpolys [t].u[2];
triangle [2].v = xpolys [t].v[2];
triangle [2].c = 227 - (xpolys [t].z[2]+_z) / 5;
if (triangle[2].c<0) triangle [2].c=0;
if (triangle[1].c<0) triangle [1].c=0;
if (triangle[0].c<0) triangle [0].c=0;
if (triangle[2].c>255) triangle [2].c=255;
if (triangle[1].c>255) triangle [1].c=255;
if (triangle[0].c>255) triangle [0].c=255;
// computation done.. render!
triangle3d (target, POLYTYPE_PTEX_LIT, xpolys
[t].texmap, &triangle[0], &triangle[1], &triangle[2]);
}
}
};
// testing purposes only!
void main () {
allegro_init();
install_keyboard();
set_color_depth (32);
set_gfx_mode (GFX_VESA2L, 640, 480, 0, 0);
BITMAP *mytex;
BITMAP *back;
mytex = load_bitmap ("tex.bmp", NULL);
back = create_bitmap (640, 480);
object3d mypoly;
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ THIS CAUSES PROBLEMS.. WHY?? I
DUNNO
mypoly.createpolygon (-130, -30, 0, 130, -30, 0, -130, 30, 0,
mytex, 3, 1, 0);
mypoly.createpolygon (-130, 30, 0, 130, -30, 0, 130, 30, 0,
mytex, 3, 1, 1);
int rot = 0;
int d = 1400;
do {
clear (back);
mypoly.setposition (0, 0, d);
mypoly.render (back);
draw_sprite (screen, back, 0 ,0);
mypoly.setrotation (0, rot*2, rot);
rot+=1; if (rot == 360) rot = 0;
d-=5; if (d == 0) d = 5;
} while (!keypressed());
readkey();
allegro_exit();
}
- Raw text -