Author Topic: Decal code  (Read 4918 times)

0 Members and 1 Guest are viewing this topic.

Offline Fry_Day

  • 28
Erm, does anyone happen to have an example weapon for the decal code? I just wanna check things out there, see if I can optimize it some, and finding the graphics for anything isn't my strong suite :)

 

Offline Petrarch of the VBB

  • Koala-monkey
  • 211
AFAIK the decal code was removed as it proved unsatisfactory.

 

Offline Fry_Day

  • 28
As I'm looking at the code right now, it wasn't removed, just ifdef'd

 

Offline Petrarch of the VBB

  • Koala-monkey
  • 211
Meh. What I meant was it was decided not to implement it until it had been improved, or something.

 

Offline karajorma

  • King Louie - Jungle VIP
  • Administrator
  • 214
    • Karajorma's Freespace FAQ
Quote
Originally posted by Petrarch of the VBB
Meh. What I meant was it was decided not to implement it until it had been improved, or something.


Which if you're paying attention is what Fry_day wants to do! :rolleyes:
Karajorma's Freespace FAQ. It's almost like asking me yourself.

[ Diaspora ] - [ Seeds Of Rebellion ] - [ Mind Games ]

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
add

$decal:
+texture: dmgdecal (a texture that you have might be good)
+radius: 1.5

to some weapon

last time I tryed playing with it, it seemed very messed up though, but I was useing TBP for testing and it has been found that PCS had a critical error in it's bsp generation
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Fry_Day

  • 28
Well, I haven't gotten the decals showing up correctly - there are some polygons created, but they don't seem to be of the right shape.
On the other hand, the sqaure root in the drawing loop is totally pointless, and so is one in the decal creation function, meaning that there is some speed to be gained.

Update: Still drawing wrong, but faster than before. Still horrible speed in debug (I feel the low framerates), but release seems smooth on my system. I have no idea why it's so significantly slower in debug (slow enough to get ~17-19FPS on my system, which is an AthlonXP 1700+ w/512MB DDR-SDRAM and a GeForce 4 Ti4200, and that's when I'm just close to one ship that has a decal potentially viewable), but I noticed no framerate dips when using a release build. Of course, all the decals are wrong, so that doesn't matter anyway.
« Last Edit: May 21, 2003, 11:38:55 am by 791 »

 

Offline Fry_Day

  • 28
Okay, I have a zillion questions about the code. Having nowhere better to ask them, I'll ask here. In the structure decal, you have an array of 1000 vectors. What's it there for? I saw it referenced several times, but never enough to require 4 kilobytes of memory per decal, meaning no less than 100kb of RAM per ship.

Update: Changed it to render a decal invalid if it has a polygon with a point referencing to a vertice lower than 0 or higher than 1000, thus saving some extra checking, and some other minor optimizations (removed some unnecessary square roots in the decal creation code - as Michael Abrash said - "The fastest code is the code not written" :) ). There's still plenty of headroom, and will be more once I actually understand what the code does :)
« Last Edit: May 21, 2003, 12:21:53 pm by 791 »

 

Offline Fry_Day

  • 28
Changed it to render a decal invalid if it has a polygon with a point referencing to a vertex lower than 0 or higher than 1000 (theoretically, if a decal references to point 1000 right now, it'll generate an access violation, since the array has 1000 members, from 0 to 999. should be changed to >=, and already is in my version - it's those kinds of errors that are hard to track down later), thus saving some extra checking, and some other minor optimizations (removed some unnecessary square roots in the decal creation code - you can get away with comparing squared distances more often than not - e.g:
Code: [Select]

for (i=0;i {
dist = vm_vec_dist(decal_point_list[decal_poly[i]], &pcenter);
if(dist>pradius){
pradius = dist;
}
}

is equivalent to:
Code: [Select]

for (i=0;i {
//check for largest squared distance, and only then sqrtf
dist = vm_vec_dist_squared(decal_point_list[decal_poly[i]], &pcenter);
if(dist>pradius){
pradius = dist;
}
}
pradius = fl_sqrt(pradius);

There's still plenty of headroom, and will be more once I actually understand what the code actually does :)
« Last Edit: May 21, 2003, 12:28:32 pm by 791 »

 

Offline phreak

  • Gun Phreak
  • 211
  • -1
as for the slow debug, try looking at the debug spew and commenting out a bunch of unneeded messages
Offically approved by Ebola Virus Man :wtf:
phreakscp - gtalk
phreak317#7583 - discord

 

Offline Fry_Day

  • 28
Well, I have no idea what the decal code does, so I can't fix it (For the record, even a clean copy doesn't generate correct decals, so it wasn't my messing around with the code :) ). On the other hand, Bobbau, if you're reading this, add the two following lines to the function d3d_set_initial_render_state():
Code: [Select]
d3d_SetTextureStageState(1, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
d3d_SetTextureStageState(1, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );

Or else glowmaps will be drawn with point-sampling.

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
that's a good idea

and the decal code got screwed up during it's hibernation, I don't know how or why.

there are a lot of quick and ugly things like the 1000 vector array that I just put in there so I could get the generation code working, eventualy I will have to put in some sort of dynamic memory alocation and some sort of optomisation that kulls redundent def points

when it gets working again these improvements will be a good addition to the code but it is broken for now (though we both know it was working at one point)

with the new dx8 merge I will be able to stuff all the pollys into a vertex buffer and that should make the rendering faster than anything else in the game.

also this thread explains the development of the decal code, the code at the top before dave explained the way it should be done it totaly wrong and unrelated to the way it is done now, the thing you will want to read is the stuff twards the bottom of the first page
« Last Edit: May 22, 2003, 10:55:19 am by 57 »
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
in fact I think I will try breaking it down a bit realy quick before I have to go to work
this is all from the decal_create_tmappoly function, it is the thing that does all the hard work

what is first is simple initalisation code, this sets all the verts and stuff that the rest of the code will use to what is in the current polly
Code: [Select]
// mprintf(("entering decal_create_tmappoly\n"));

if(poly_num >= MAX_DECAL_POLY){
mprintf(("bugging out becase there are too many polys in the decal\n"));
return;
}


int i;
int nv;
// uv_pair uvlist[TMAP_MAX_VERTS];
// vector points[TMAP_MAX_VERTS];
model_tmap_vert *verts;

nv = dw(p+36);
if ( nv < 0 ) return;

// int tmap_num = dw(p+40);
/*
if ( (!(Mc->flags & MC_CHECK_INVISIBLE_FACES)) && (Mc_pm->textures[tmap_num] < 0) ) {
// Don't check invisible polygons.
return;
}
*/
verts = (model_tmap_vert *)(p+44);
vector temppoly[820];

for (i=0;i {
decal_poly[i] = (int)verts[i].vertnum;
temppoly[i].xyz = decal_point_list[decal_poly[i]]->xyz;
}

vector pnorm;
vm_vec_perp(&pnorm, decal_point_list[decal_poly[0]], decal_point_list[decal_poly[1]], decal_point_list[decal_poly[2]]);

float back = vm_vec_dot(&decal_cube_plane[4][1], &pnorm);

if( (back_faceing == 0) && (back >= 0) )return;


next is some polly culling
I'm not sure how well this worked and I probly didn't think it through as much as I should have
Code: [Select]
//poly culling
vector pcenter;
vm_vec_avg_n(&pcenter, nv, temppoly);
float pradius = 0;
float dist = 0;
for (i=0;i {
dist = vm_vec_dist(decal_point_list[decal_poly[i]], &pcenter);
if(dist>pradius){
pradius = dist;
}
}

//if it's too far to posably get near the cube get out
if(vm_vec_dist_quick(&pcenter, &decal_hit_point) >= (pradius + decal_hit_radius)){
// mprintf(("leaveing becase poly is too far away\n"));
return;
}

//this is for faster testing, I'm not sure if it's realy makeing it faster or not
int k;
for(k = 0; k<6; k++){
int first_good = -1;
for(i = 0; ((i< nv) && (first_good == -1)); i++){
if(fvi_point_dist_plane(&decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[i]]) > 0){
first_good = i;
}
}
if(first_good == -1){
// mprintf(("all points are behind a plane, polygon is not in the cube\n"));
return;
}
}

//poly culling


now for the fun part
first a few last second initalisation
Code: [Select]

int temp_poly[820];

int skip = 0, numv = 0, valid = 0;


now for each plane of the cube
Code: [Select]
for(k = 0; k<6; k++){


find the first point of the decal poly in front of the plane, if any
Code: [Select]
//find the first point in front of the plain
int first_good = -1;
for(i = 0; ((i< nv) && (first_good == -1)); i++){
// mprintf(("testing point %d with plane %d\n", i, k));
// mprintf(("point's position is x %0.2f y %0.2f z %0.2f\n", decal_point_list[decal_poly[i]]->xyz.x, decal_point_list[decal_poly[i]]->xyz.y, decal_point_list[decal_poly[i]]->xyz.z));
// mprintf(("plane's position is x %0.2f y %0.2f z %0.2f\n", decal_cube_plane[k][0].xyz.x, decal_cube_plane[k][0].xyz.y, decal_cube_plane[k][0].xyz.z));
// mprintf(("plane's normal is x %0.2f y %0.2f z %0.2f\n", decal_cube_plane[k][1].xyz.x, decal_cube_plane[k][1].xyz.y, decal_cube_plane[k][1].xyz.z));
if(fvi_point_dist_plane(&decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[i]]) >= 0){
first_good = i;
// mprintf(("%d is the first good point\n", i));
}
}
if(first_good == -1){
// mprintf(("all points are behind a plane, polygon is not in the cube\n"));
return;
}else{
valid = 1;
}


now for each vert in th decal polly starting with the first one in front of the plane
Code: [Select]
numv = 0;
//go through each point and see if there is an intersection between it the next point and the current plane
//if there is then make the intersection point the next point in the poly
//ignor all points untill there is another intersection
//other wise make the next point the next point in the poly
// if(k==1)Int3();
int is_infront = 1, is_infront2 = 1;
for(i = first_good; i < (nv + first_good); i++){


we go through each vert of the polly and check to see if the line between it and the next one intersects the plane of the cube we are currently testing.

Code: [Select]
// mprintf(("looking for intersections between point %d and point %d, with plane %d\n", i%nv, (i + 1)%nv, k));
// mprintf((" point %d, is at, x %0.2f y %0.2f z %0.2f and point %d, is at, x %0.2f y %0.2f z %0.2f\n", i%nv, decal_point_list[decal_poly[i]]->xyz.x, decal_point_list[decal_poly[i]]->xyz.y, decal_point_list[decal_poly[i]]->xyz.z,  (i + 1)%nv, decal_point_list[decal_poly[(i + 1)%nv]]->xyz.x, decal_point_list[decal_poly[(i + 1)%nv]]->xyz.y, decal_point_list[decal_poly[(i + 1)%nv]]->xyz.z));
is_infront2 = is_infront;
float is_in = fvi_point_dist_plane(&decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[(i + 1)%nv]]);
if(is_in >= 0){
is_infront = 1;
}else{
is_infront= 0;
}
if(is_infront != is_infront2){
if(fvi_segment_plane(&new_decal->vert[nverts], &decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[i%nv]], decal_point_list[decal_poly[(i + 1)%nv]], 0)){
// mprintf(("segment %d %d has an an itersection\n", i%nv, (i + 1)%nv));
decal_point_list[nverts] = &new_decal->vert[nverts];
// mprintf(("new decal defpoint has been added as number %d at x %0.2f y %0.2f z %0.2f\n",nverts, new_decal->vert[nverts].xyz.x, new_decal->vert[nverts].xyz.y, new_decal->vert[nverts].xyz.z));
temp_poly[numv] = nverts++;
// mprintf(("temp_poly point %d, should have been set to defpoint %d, it was set to %d\n",numv, nverts-1, temp_poly[numv]));
skip = (skip+1)%2;
// mprintf(("skip has been set to %d\n",skip));
numv++;
if((skip == 0)){
// mprintf(("adding point, becase it is after an intersection point\n", (i + 1)%nv));
temp_poly[numv] = decal_poly[(i + 1)%nv];
numv++;
}
}else{
// mprintf(("there sould have been an intersection but there wasnt"));
}

}else{
if((skip == 0)){//don't copy the first point
// mprintf(("segment %d %d has no itersection, copying\n", i%nv, (i + 1)%nv));
temp_poly[numv] = decal_poly[(i + 1)%nv];
numv++;
}else{
// mprintf(("skipping segment %d %d becase it isn't in front of the plane\n", i%nv, (i + 1)%nv));
}
}


now we copy the temp poly to the decal poly and move to the next plane
Code: [Select]
// mprintf(("temp poly has %d points\n", numv));
for(int h = 0; h < numv; h++){
decal_poly[h] = temp_poly[h];
mprintf(("seting decal_poly point %d to %d\n",h,temp_poly[h]));
}
nv = numv;
numv=0;
// mprintf(("decal_poly now has has %d points\n", nv));
if(nv < 3)return; //if at any time we get fewer than three points this poly is dead


then we copy the poly to the decal

Code: [Select]
if((valid == 1) && (nv > 2)){
/* for (i=1;i<(nv-1);i++) {
if(poly_num >= MAX_DECAL_POLY){
mprintf(("bugging out becase there are too many polys in the decal\n"));
return;
}
new_decal->poly[poly_num].point[0] = decal_poly[0];
new_decal->poly[poly_num].point[1] = decal_poly[i];
new_decal->poly[poly_num].point[2] = decal_poly[i+1];
new_decal->poly[poly_num].norm = pnorm;
if(back > 0){
new_decal->poly[poly_num].backfaced = 0;
}else{
new_decal->poly[poly_num].backfaced = 1;
}
for (k=0;k<3;k++) {
//calculate the UV coords
new_decal->poly[poly_num].uv[k].u = (fvi_point_dist_plane(&decal_cube_plane[0][0], &decal_cube_plane[0][1], &new_decal->vert[new_decal->poly[poly_num].point[k]]) / (min_rad*2));
new_decal->poly[poly_num].uv[k].v = (fvi_point_dist_plane(&decal_cube_plane[2][0], &decal_cube_plane[2][1], &new_decal->vert[new_decal->poly[poly_num].point[k]]) / (min_rad*2));
}
// mprintf(("decal poly %d, vert 0 being set to %d, vert 1 %d, and vert 2 %d\n", poly_num, new_decal->poly[poly_num].point[0], new_decal->poly[poly_num].point[1], new_decal->poly[poly_num].point[2]));
poly_num++;
}*/
for (i=0;i if(poly_num >= MAX_DECAL_POLY){
mprintf(("bugging out becase there are too many polys in the decal\n"));
return;
}
if(i >= MAX_DECAL_POLY_POINT){
mprintf(("bugging out becase there are too many points in the poly\n"));
return;
}
new_decal->poly[poly_num].point[i] = decal_poly[i];
new_decal->poly[poly_num].norm = pnorm;
if(back > 0){
new_decal->poly[poly_num].backfaced = 0;
}else{
new_decal->poly[poly_num].backfaced = 1;
}
//calculate the UV coords
new_decal->poly[poly_num].uv[i].u = (fvi_point_dist_plane(&decal_cube_plane[0][0], &decal_cube_plane[0][1], &new_decal->vert[new_decal->poly[poly_num].point[i]]) / (min_rad*2));
new_decal->poly[poly_num].uv[i].v = (fvi_point_dist_plane(&decal_cube_plane[2][0], &decal_cube_plane[2][1], &new_decal->vert[new_decal->poly[poly_num].point[i]]) / (min_rad*2));
// mprintf(("decal poly %d, vert 0 being set to %d, vert 1 %d, and vert 2 %d\n", poly_num, new_decal->poly[poly_num].point[0], new_decal->poly[poly_num].point[1], new_decal->poly[poly_num].point[2]));
}
poly_num++;
}else{
// mprintf(("no poly, all points are out side of planes"));
}
mprintf(("there are now %d polys in the current decal\n", poly_num));


I will go over this tonight (saw a few things that seemed odd) but now I have to hurry to work
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
I'm working through the code right now I think I might have it fixed (was almost fixed a second ago, I think I fixed the last bug)
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
ok I fixed it :)
Code: [Select]

// Textured Poly
// +0      int         id
// +4      int         size
// +8      vector      normal
// +20     vector      normal_point
// +32     int         tmp = 0
// +36     int         nverts
// +40     int         tmap_num
// +44     nverts*(model_tmap_vert) vertlist (n,u,v)
void decal_create_tmappoly(ubyte * p)
{
// mprintf(("entering decal_create_tmappoly\n"));

if(poly_num >= MAX_DECAL_POLY){
mprintf(("bugging out becase there are too many polys in the decal\n"));
return;
}


int i;
int nv;
// uv_pair uvlist[TMAP_MAX_VERTS];
// vector points[TMAP_MAX_VERTS];
model_tmap_vert *verts;

nv = dw(p+36);
if ( nv < 0 ) return;

// int tmap_num = dw(p+40);
/*
if ( (!(Mc->flags & MC_CHECK_INVISIBLE_FACES)) && (Mc_pm->textures[tmap_num] < 0) ) {
// Don't check invisible polygons.
return;
}
*/
verts = (model_tmap_vert *)(p+44);
vector temppoly[820];

for (i=0;i {
decal_poly[i] = (int)verts[i].vertnum;
temppoly[i].xyz = decal_point_list[decal_poly[i]]->xyz;
}

vector pnorm;
vm_vec_perp(&pnorm, decal_point_list[decal_poly[0]], decal_point_list[decal_poly[1]], decal_point_list[decal_poly[2]]);

float back = vm_vec_dot(&decal_cube_plane[4][1], &pnorm);

if( (back_faceing == 0) && (back >= 0) )return;
/*
//poly culling
vector pcenter;
vm_vec_avg_n(&pcenter, nv, temppoly);
float pradius = 0;
float dist = 0;
for (i=0;i {
dist = vm_vec_dist(decal_point_list[decal_poly[i]], &pcenter);
if(dist>pradius){
pradius = dist;
}
}

//if it's too far to posably get near the cube get out
if(vm_vec_dist_quick(&pcenter, &decal_hit_point) >= (pradius + decal_hit_radius)){
// mprintf(("leaveing becase poly is too far away\n"));
return;
}

//this is for faster testing, I'm not sure if it's realy makeing it faster or not
int k;
for(k = 0; k<6; k++){
int first_good = -1;
for(i = 0; ((i< nv) && (first_good == -1)); i++){
if(fvi_point_dist_plane(&decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[i]]) > 0){
first_good = i;
}
}
if(first_good == -1){
// mprintf(("all points are behind a plane, polygon is not in the cube\n"));
return;
}
}

//poly culling
*/
int temp_poly[820];

int skip = 0, numv = 0, valid = 0;

// mprintf(("starting to test the poly, with %d verts in it\n",nv));
for(int k = 0; k<6; k++){
//find the first point in front of the plain
int first_good = -1;
for(i = 0; ((i< nv) && (first_good == -1)); i++){
// mprintf(("testing point %d with plane %d\n", i, k));
// mprintf(("point's position is x %0.2f y %0.2f z %0.2f\n", decal_point_list[decal_poly[i]]->xyz.x, decal_point_list[decal_poly[i]]->xyz.y, decal_point_list[decal_poly[i]]->xyz.z));
// mprintf(("plane's position is x %0.2f y %0.2f z %0.2f\n", decal_cube_plane[k][0].xyz.x, decal_cube_plane[k][0].xyz.y, decal_cube_plane[k][0].xyz.z));
// mprintf(("plane's normal is x %0.2f y %0.2f z %0.2f\n", decal_cube_plane[k][1].xyz.x, decal_cube_plane[k][1].xyz.y, decal_cube_plane[k][1].xyz.z));
if(fvi_point_dist_plane(&decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[i]]) >= 0){
first_good = i;
// mprintf(("%d is the first good point\n", i));
}
}
if(first_good == -1){
// mprintf(("all points are behind a plane, polygon is not in the cube\n"));
return;
}else{
valid = 1;
}

//first point in front of the plane is good of corse
// temp_poly[0] =  decal_poly[(first_good + nv)%nv];
numv = 0;
//go through each point and see if there is an intersection between it the next point and the current plane
//if there is then make the intersection point the next point in the poly
//ignor all points untill there is another intersection
//other wise make the next point the next point in the poly
// if(k==1)Int3();
int is_infront = 1, is_infront2 = 1;
for(i = first_good; i < (nv + first_good); i++){
// mprintf(("looking for intersections between point %d and point %d, with plane %d\n", i%nv, (i + 1)%nv, k));
// mprintf((" point %d, is at, x %0.2f y %0.2f z %0.2f and point %d, is at, x %0.2f y %0.2f z %0.2f\n", i%nv, decal_point_list[decal_poly[i]]->xyz.x, decal_point_list[decal_poly[i]]->xyz.y, decal_point_list[decal_poly[i]]->xyz.z,  (i + 1)%nv, decal_point_list[decal_poly[(i + 1)%nv]]->xyz.x, decal_point_list[decal_poly[(i + 1)%nv]]->xyz.y, decal_point_list[decal_poly[(i + 1)%nv]]->xyz.z));
is_infront2 = is_infront;
float is_in = fvi_point_dist_plane(&decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[(i + 1)%nv]]);
if(is_in >= 0){
is_infront = 1;
}else{
is_infront= 0;
}
if(is_infront != is_infront2){
if(fvi_segment_plane(&new_decal->vert[nverts], &decal_cube_plane[k][0], &decal_cube_plane[k][1], decal_point_list[decal_poly[i%nv]], decal_point_list[decal_poly[(i + 1)%nv]], 0)){
// mprintf(("segment %d %d has an an itersection\n", i%nv, (i + 1)%nv));
decal_point_list[nverts] = &new_decal->vert[nverts];
// mprintf(("new decal defpoint has been added as number %d at x %0.2f y %0.2f z %0.2f\n",nverts, new_decal->vert[nverts].xyz.x, new_decal->vert[nverts].xyz.y, new_decal->vert[nverts].xyz.z));
temp_poly[numv] = nverts++;
// mprintf(("temp_poly point %d, should have been set to defpoint %d, it was set to %d\n",numv, nverts-1, temp_poly[numv]));
skip = (skip+1)%2;
// mprintf(("skip has been set to %d\n",skip));
numv++;
if((skip == 0)){
// mprintf(("adding point, becase it is after an intersection point\n", (i + 1)%nv));
temp_poly[numv] = decal_poly[(i + 1)%nv];
numv++;
}
}else{
// mprintf(("there sould have been an intersection but there wasnt"));
}

}else{
if((skip == 0)){//don't copy the first point
// mprintf(("segment %d %d has no itersection, copying\n", i%nv, (i + 1)%nv));
temp_poly[numv] = decal_poly[(i + 1)%nv];
numv++;
}else{
// mprintf(("skipping segment %d %d becase it isn't in front of the plane\n", i%nv, (i + 1)%nv));
}
}
}

// mprintf(("temp poly has %d points\n", numv));
for(int h = 0; h < numv; h++){
decal_poly[h] = temp_poly[h];
mprintf(("seting decal_poly point %d to %d\n",h,temp_poly[h]));
}
nv = numv;
numv=0;
// mprintf(("decal_poly now has has %d points\n", nv));
if(nv < 3)return; //if at any time we get fewer than three points this poly is dead
}


// mprintf(("copying the poly to the decal\n"));
//copy a triangulated version of the poly to the new decal
if((valid == 1) && (nv > 2)){
//older version of the code that copied triangulated pollies, could be usefull for makeing vertex buffers later
/* for (i=1;i<(nv-1);i++) {
if(poly_num >= MAX_DECAL_POLY){
mprintf(("bugging out becase there are too many polys in the decal\n"));
return;
}
new_decal->poly[poly_num].point[0] = decal_poly[0];
new_decal->poly[poly_num].point[1] = decal_poly[i];
new_decal->poly[poly_num].point[2] = decal_poly[i+1];
new_decal->poly[poly_num].norm = pnorm;
if(back > 0){
new_decal->poly[poly_num].backfaced = 0;
}else{
new_decal->poly[poly_num].backfaced = 1;
}
for (k=0;k<3;k++) {
//calculate the UV coords
new_decal->poly[poly_num].uv[k].u = (fvi_point_dist_plane(&decal_cube_plane[0][0], &decal_cube_plane[0][1], &new_decal->vert[new_decal->poly[poly_num].point[k]]) / (min_rad*2));
new_decal->poly[poly_num].uv[k].v = (fvi_point_dist_plane(&decal_cube_plane[2][0], &decal_cube_plane[2][1], &new_decal->vert[new_decal->poly[poly_num].point[k]]) / (min_rad*2));
}
// mprintf(("decal poly %d, vert 0 being set to %d, vert 1 %d, and vert 2 %d\n", poly_num, new_decal->poly[poly_num].point[0], new_decal->poly[poly_num].point[1], new_decal->poly[poly_num].point[2]));
poly_num++;
}
new_decal->poly[poly_num].n_point = 3;
*/

//newer version that copys the pollies untriangulated
for (i=0;i if(poly_num >= MAX_DECAL_POLY){
mprintf(("bugging out becase there are too many polys in the decal\n"));
return;
}
if(i >= MAX_DECAL_POLY_POINT){
mprintf(("bugging out becase there are too many points in the poly\n"));
return;
}
new_decal->poly[poly_num].point[i] = decal_poly[i];
new_decal->poly[poly_num].norm = pnorm;
if(back > 0){
new_decal->poly[poly_num].backfaced = 0;
}else{
new_decal->poly[poly_num].backfaced = 1;
}
//calculate the UV coords
new_decal->poly[poly_num].uv[i].u = (fvi_point_dist_plane(&decal_cube_plane[0][0], &decal_cube_plane[0][1], &new_decal->vert[new_decal->poly[poly_num].point[i]]) / (min_rad*2));
new_decal->poly[poly_num].uv[i].v = (fvi_point_dist_plane(&decal_cube_plane[2][0], &decal_cube_plane[2][1], &new_decal->vert[new_decal->poly[poly_num].point[i]]) / (min_rad*2));
// mprintf(("decal poly %d, vert 0 being set to %d, vert 1 %d, and vert 2 %d\n", poly_num, new_decal->poly[poly_num].point[0], new_decal->poly[poly_num].point[1], new_decal->poly[poly_num].point[2]));
}
new_decal->poly[poly_num].n_point = nv;
poly_num++;
}else{
// mprintf(("no poly, all points are out side of planes"));
}
mprintf(("there are now %d polys in the current decal\n", poly_num));


}


Code: [Select]

void decal_render_all(object * obj){
#ifdef DECALS_ENABLED
if(obj->type != OBJ_SHIP){
return;
}

vertex vecs[MAX_DECAL_POLY_POINT];
vertex *vlist[MAX_DECAL_POLY_POINT];
for(int k = 0; k
// mprintf(("about to render all decals\n"));

ship *shipp = &Ships[obj->instance];
polymodel *pm = model_get( shipp->modelnum );

for(int h = 0; h < MAX_SHIP_DECALS; h++){
decal *dec = &shipp->decals[h];
if(dec->is_valid){
mprintf(("decal %d is valid, and has %d polys\n",h,dec->n_poly));
// vector g_pos;
// vm_vec_add(&g_pos, &dec->position, &obj->pos);
float dist = vm_vec_dist(&dec->position, &View_position);
if(dist < (dec->radius*10)){
mprintf(("decal %d too far away to be drawn %0.2f\n", h, dist));
// HUD_printf("decal %d too far away to be drawn %0.2f\n", h, dist);
continue;
}
for(int i = 0; in_poly; i++){

// if((pm->submodel[dec->submodel_parent].blown_off != 0) && (dec->submodel_parent > 0))continue;

for( int j = 0; j < dec->poly[i].n_point; j++){
// vecs[j] = dec->vert[i][j];
// vector pos;
// vm_vert2vec(&vecs[j], &pos);
// vm_vec_add2(&pos, &obj->pos);
if(dec->poly[i].point[j]<0 || dec->poly[i].point[j]>1000)break;
vector pnt = dec->vert[dec->poly[i].point[j]];
if(dec->submodel_parent != pm->detail[0]){
matrix m;
angles angs = pm->submodel[dec->submodel_parent].angs;
angs.b = PI2 - angs.b;
angs.p = PI2 - angs.p;
angs.h = PI2 - angs.h;
mprintf(("decal is child of a submodel, who's angles are b %0.2f p %0.2f h %0.2f, it should be rotateing with it\n", angs.b, angs.p, angs.h));

vector offset = pm->submodel[dec->submodel_parent].offset;
vm_angles_2_matrix(&m, &angs);

vector p = pnt;
vm_vec_rotate(&pnt, &p, &m);

vm_vec_add2(&pnt, &offset);
}

g3_rotate_vertex(&vecs[j], &pnt);
g3_project_vertex(&vecs[j]);
vecs[j].u = dec->poly[i].uv[j].u;
vecs[j].v = dec->poly[i].uv[j].v;
mprintf(("vert %d at x %0.2f y %0.2f z %0.2f u %0.2f v %0.2f\n", j, vecs[j].x, vecs[j].y, vecs[j].z, vecs[j].u, vecs[j].v));
/*
if ( D3D_enabled ) {
light_apply_rgb( &vlist[j]->r, &vlist[j]->g, &vlist[j]->b, &pnt, &dec->poly[i].norm, 0 );
} else {
vlist[j]->b = light_apply( &pnt, &dec->poly[i].norm, 0 );
}
*/
}

if(dec->poly[i].point[j]<0 || dec->poly[i].point[j]>1000)break;

gr_set_bitmap(dec->texture, 0, GR_BITBLT_MODE_NORMAL, 1.0f );
if((dec->poly[i].backfaced == 1) && (dec->backfaced_texture > -1)){
gr_set_bitmap(dec->backfaced_texture, 0, GR_BITBLT_MODE_NORMAL, 1.0f );
mprintf(("drawing decal poly %d, with bitmap %d\n", i, dec->backfaced_texture));
}else{
mprintf(("drawing decal poly %d, with bitmap %d\n", i, dec->texture));
}

// gr_set_cull(0);
g3_draw_poly(dec->poly[i].n_point, vlist, TMAP_FLAG_TEXTURED | TMAP_FLAG_CORRECT | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD );
// gr_set_cull(1);
}
}
}
// mprintf(("decals rendered\n"));
#endif
}


now by 'fix' I mean I got it to generate corect decals under the vast majority of situations, none of you're improvements were added.

I'm going to try and make this more of an object (so I can have constructors and destructors mostly) and try to get the defpoints to be dynamicly alocated, but I will have to make a guess on how many new defpoints will be created during the proces, I am going to make a wild guess and say no more than 100 note that does not mean a decal will ever have 100 points but that while it is being created each poly has two new defpoints every time  a polly intersects one of the 6 planes
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

  

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
after makeing some changes to the structure and several functions (implemented dynamic memory alocation on creation of a decal) I am about to test some improvements, includeing this def point culler
now I wonder if it will work
I haven't had nearly as much experience with dynamic memory alocation as I would have liked
Code: [Select]

void cull_def_points( decal *dec){
//first compare every point in every polly with every point in every other polly
//and if they are at the same place make them the same point
for(int i =0; in_poly; i++){
for(int k = i+1; kn_poly; k++){
for(int g = 0; gpoly[i].n_point; g++){
for(int h = 0; hpoly[k].n_point; g++){
if(
(dec->vert[dec->poly[i].point[g]].xyz.x == dec->vert[dec->poly[k].point[h]].xyz.x) &&
(dec->vert[dec->poly[i].point[g]].xyz.y == dec->vert[dec->poly[k].point[h]].xyz.y) &&
(dec->vert[dec->poly[i].point[g]].xyz.z == dec->vert[dec->poly[k].point[h]].xyz.z) )
dec->poly[i].point[g] = dec->poly[k].point[h];
}
}
}
}
//now go through and find every point that isn't used
int end_points = 0;
bool p[MAX_DECAL_POLY_POINT] = {false};
for(i =0; in_poly; i++){
for(int g = 0; gpoly[i].n_point; g++){
if(!p[dec->poly[i].point[g]])end_points++;
p[dec->poly[i].point[g]] = true;
}
}
vector *new_def = new vector[end_points];
//copy all the used def points and update those pollies that refrence them
int k = 0;
for(i = 0; in_def; i++){
if(p[i]){
new_def[k] = dec->vert[i];
for(int g = 0; gn_poly; g++){
for(int h = 0; h< dec->poly[g].n_point; h++){
if(dec->poly[g].point[h] == i)dec->poly[g].point[h] = k;
}
}
}
k++;
}
delete dec->vert;
dec->vert = new_def;
dec->n_def = end_points;
}



now why is this culling so important?
well we can get all of the transformations done on the smaller number of def points one time and then render it, rather than two or three times (each point is likely going to be used by more than one polly), so it is that much faster
« Last Edit: May 23, 2003, 01:29:05 am by 57 »
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 
wow, and you go on about people with double posts:D

 

Offline Fry_Day

  • 28
Hmm, Bobbau, it seems you changed the structure of the decal too, since I get references to variables that don't exist, namely decal->poly[].n_point, and decal->n_def.

Also, when you delete an array, do "delete [] array;" and not "delete array"

Of course, vector vert[1000] in the decal structure should be changed to vector *vert, or else the culling function will be illegal. besides of which, you need to change the references for every pointer that used to point to the array you deleted into the new array. right now it'll cause a crash, since older decals didn't have their pointer updated, and they point into unallocated memory.

On the other hand, without culling, decals indeed work like a charm :)
« Last Edit: May 23, 2003, 10:52:30 am by 791 »

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
decal->poly[].n_point is an int, telling how many points are in this polly,
decal->n_def is the number of defpoints (ie how big the vert array is)

I don't think anyone uses vert other than localy so that shouldn't be an issue (though I will check)
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together

 

Offline Bobboau

  • Just a MODern kinda guy
    Just MODerately cool
    And MODest too
  • 213
I've made a lot of progress tonight, I restructured the rendering system, it is probly an order of magnatude faster, It is at least two orders of magnatude more memory efecent over all, and parts of it actualy look pleaseing to the eye for once I've fixed a few small bugs in the culling code, I can't beleve how close to working I got it when I was barly awake,
as I type I am loading a mission that I hope will have working (relitively) bug free decals that render at a reasonable framerate, I probly won't get this, but it's my hope :).

[edit]
argg why does it always stop working just when I think it's almost done :mad:
it just toaly stoped generating decals seemingly on a very low level :mad2:
WHY!!! :wtf:

[edit 2]
no wait it's working again, I DIDN'T EVEN DO ANYTHING!!!
maybe I'm just running out of memory

[edit 3]
no, no it isn't working :doubt:
« Last Edit: May 24, 2003, 02:20:07 am by 57 »
Bobboau, bringing you products that work... in theory
learn to use PCS
creator of the ProXimus Procedural Texture and Effect Generator
My latest build of PCS2, get it while it's hot!
PCS 2.0.3


DEUTERONOMY 22:11
Thou shalt not wear a garment of diverse sorts, [as] of woollen and linen together