📄 gui3d3.cpp
字号:
#include <fstream.h>
#include "gui16.h"
//****************************************************************************
//CPointList implement!!!
//****************************************************************************
CPointList::CPointList(UINT s)
{ topPos = 0;
if(s <= 300000)
{ size = s;
points = new POINT3D[size];
shouldConvert = new CHAR[size];
}
else
{ points = NULL;
shouldConvert = NULL;
}
}
CPointList::~CPointList()
{ if(points != NULL)
delete[] points;
if(shouldConvert != NULL)
delete[] shouldConvert;
}
BOOL CPointList::operator!()
{ if(points != NULL && shouldConvert != NULL)
return TRUE;
else
return FALSE;
}
BOOL CPointList::copy(CPointList* l)
{ if(size != l->size)
return FALSE;
topPos = l->topPos;
memcpy( points, l->points, topPos*sizeof(POINT3D) );
memcpy( shouldConvert, l->shouldConvert, topPos*sizeof(CHAR) );
return TRUE;
}
int CPointList::addPoint(POINT3D* p)
{ if(topPos < size)
{ points[topPos] = *p;
topPos++;
return topPos - 1;
}
else
return -1;
}
int CPointList::addPointAt(UINT pos,POINT3D* p)
{ if(pos < size)
{ points[pos] = *p;
if(topPos <= pos)
topPos = pos + 1;
return pos;
}
else
return -1;
}
void CPointList::clearConvert()
{ memset( shouldConvert, 0, sizeof(CHAR)*topPos );
}
void CPointList::convertPoints(CONVERT_MATRIX3D* m,CPLCFLAG f)
{ UINT i;
if(f == CONVERTALL)
{ POINT3D* p=points;
for(i=0;i<topPos;i++)
dotConvert(p++,m);
}
else
{ CHAR* cp=shouldConvert;
for(i=0;i<topPos;i++)
if(*cp++)
dotConvert(points+i,m);
}
}
void CPointList::scalePoints(CONVERT_MATRIX3D* m,CPLCFLAG f)
{ UINT i;
if(f == CONVERTALL)
{ POINT3D* p=points;
for(i=0;i<topPos;i++)
dotScale(p++,m);
}
else
{ for(i=0;i<topPos;i++)
if(shouldConvert[i])
dotScale(points+i,m);
}
}
void CPointList::rotatePoints(CONVERT_MATRIX3D* m,CPLCFLAG f)
{ UINT i;
if(f == CONVERTALL)
{ POINT3D* p=points;
for(i=0;i<topPos;i++)
dotRotate(p++,m);
}
else
{ for(i=0;i<topPos;i++)
if(shouldConvert[i])
dotRotate(points+i,m);
}
}
void CPointList::movePoints(CONVERT_MATRIX3D* m,CPLCFLAG f)
{ UINT i;
if(f == CONVERTALL)
{ POINT3D* p=points;
for(i=0;i<topPos;i++)
dotMove(p++,m);
}
else
{ for(i=0;i<topPos;i++)
if(shouldConvert[i])
dotMove(points+i,m);
}
}
//******************************************************************************
//CFaceList implement!!!
//******************************************************************************
CFaceList::CFaceList(UINT s)
{ topPos = 0;
if(s <= 800000)
{ size = s;
faces = new TRIANGLE3D[size];
shouldConvert = new CHAR[size];
}
else
{ faces = NULL;
shouldConvert = NULL;
}
}
CFaceList::~CFaceList()
{ if(faces != NULL)
delete[] faces;
if(shouldConvert != NULL)
delete[] shouldConvert;
}
BOOL CFaceList::operator!()
{ if(faces != NULL && shouldConvert != NULL)
return TRUE;
else
return FALSE;
}
void CFaceList::clearConvert()
{ memset( shouldConvert, 0, sizeof(CHAR)*topPos );
}
int CFaceList::addTriangle(TRIANGLE3D* p)
{ if(topPos < size)
{ faces[topPos] = *p;
topPos++;
return topPos - 1;
}
else
return -1;
}
int CFaceList::addAsPolygon(POLYGON3D* p)
{ UINT pos = topPos;
if(p->num < 3 || (pos > size - p->num))
return -1;
for(UINT i=0;i<=p->num - 3;i++)
{ faces[topPos].p1 = p->points[i];
faces[topPos].p2 = p->points[i+1];
faces[topPos].p3 = p->points[i+2];
topPos++;
}
return pos;
}
int CFaceList::addTriangleAt(UINT pos,TRIANGLE3D* p)
{ if(pos < size)
{ faces[pos] = *p;
if(topPos <= pos)
topPos = pos + 1;
return pos;
}
else
return -1;
}
void CFaceList::updataParams(CPointList* ppl,CPLCFLAG f)
{ UINT i;
if(f == CONVERTALL)
{ TRIANGLE3D* p=faces;
for(i=0;i<topPos;i++)
fillTriangleParam(ppl,p++);
}
else
{ CHAR* cp=shouldConvert;
for(i=0;i<topPos;i++)
if(*cp++)
fillTriangleParam(ppl,faces+i);
}
}
void CFaceList::judgeSide(CPointList* pl,POINT3D* pt)
{ UINT i;
TRIANGLE3D* p=faces;
char* pc=pl->shouldConvert;
for(i=0;i<topPos;i++)
{ if((p->a*pt->x + p->b*pt->y + p->c*pt->z + p->d) >= 0)
{ shouldConvert[i] = -1;
pc[p->p1] = -1;
pc[p->p2] = -1;
pc[p->p3] = -1;
}
p++;
}
}
void CFaceList::calLightMirror(POINT3D* pot,CPointList* pt,UINT groundLight,UINT spotLight,CPLCFLAG f)
{ VECTOR3D l,v,h,n;
float cos;
UINT i,s,lps;
float x1,y1,z1;
// float zero=0,spl=spotLight;
s = sizeof(TRIANGLE3D);
if(f == CONVERTALL)
{ TRIANGLE3D* p=faces;
for(i=0;i<topPos;i++)
{ x1 = pt->points[p->p1].x;
y1 = pt->points[p->p1].y;
z1 = pt->points[p->p1].z;
l.x = pot->x - x1;
l.y = pot->y - y1;
l.z = pot->z - z1;
vector3dToSTD(&l);
v.x = 20-x1;
v.y = 150-y1;
v.z = 750-z1;
vector3dToSTD(&v);
h.x = l.x + v.x;
h.y = l.y + v.y;
h.z = l.z + v.z;
vector3dToSTD(&h);
n.x = p->a;
n.y = p->b;
n.z = p->c;
vector3dToSTD(&n);
// cos = (n.x*h.x + n.y*h.y + n.z*h.z)
// /(float)sqrt(n.x*n.x + n.y*n.y + n.z*n.z)
// /(float)sqrt(h.x*h.x + h.y*h.y + h.z*h.z);
cos = vectorDotMul(&n,&h);
for(int i=0;i<7;i++)
cos =cos*cos;
if(cos > 0)
p->light = groundLight + UINT(spotLight*cos);
else
p->light = groundLight;
p++;
}
}
else
{ CHAR* cp=shouldConvert;
for(i=0;i<topPos;i++)
if(*cp++)
{ x1 = pt->points[faces[i].p1].x;
y1 = pt->points[faces[i].p1].y;
z1 = pt->points[faces[i].p1].z;
l.x = pot->x - x1;
l.y = pot->y - y1;
l.z = pot->z - z1;
vector3dToSTD(&l);
v.x = 20-x1;
v.y = 150-y1;
v.z = 750-z1;
vector3dToSTD(&v);
h.x = l.x + v.x;
h.y = l.y + v.y;
h.z = l.z + v.z;
// vector3dToSTD(&h);
n.x = faces[i].a;
n.y = faces[i].b;
n.z = faces[i].c;
// vector3dToSTD(&n);
// cos = vectorDotMul(&n,&h);
cos = (n.x*h.x + n.y*h.y + n.z*h.z)
/(float)sqrt((n.x*n.x + n.y*n.y + n.z*n.z)
*(h.x*h.x + h.y*h.y + h.z*h.z));
cos = pow(cos,8);
if(cos > 0)
faces[i].light = groundLight + UINT(spotLight*cos);
else
faces[i].light = groundLight;
}
}
}
void CFaceList::calLightSpot(POINT3D* pot,CPointList* pt,UINT groundLight,UINT spotLight,CPLCFLAG f)
{ VECTOR3D v;
UINT i,s,lps;
long cos1;
float cos,zero=0,spl=spotLight;
s = sizeof(TRIANGLE3D);
if(f == CONVERTALL)
{ TRIANGLE3D* p=faces;
for(i=0;i<topPos;i++)
{ v.x = pt->points[p->p1].x - pot->x;
v.y = pt->points[p->p1].y - pot->y;
v.z = pt->points[p->p1].z - pot->z;
vector3dToSTD(&v);
cos = -(v.x*p->a + v.y*p->b + v.z*p->c)/
(float)sqrt(p->a*p->a + p->b*p->b + p->c*p->c);
if(cos > 0)
p->light = groundLight + UINT(spotLight*cos);
else
p->light = groundLight;
p++;
}
}
else
{ CHAR* cp=shouldConvert;
for(i=0;i<topPos;i++)
if(*cp++)
{ v.x = pt->points[faces[i].p1].x - pot->x;
v.y = pt->points[faces[i].p1].y - pot->y;
v.z = pt->points[faces[i].p1].z - pot->z;
vector3dToSTD(&v);
cos = -(v.x*faces[i].a + v.y*faces[i].b + v.z*faces[i].c)/
(float)sqrt(faces[i].a*faces[i].a + faces[i].b*faces[i].b + faces[i].c*faces[i].c);
if(cos > 0)
faces[i].light = groundLight + UINT(spotLight*cos);
else
faces[i].light = groundLight;
}
}
/* __asm
{ mov eax,f
mov edx,this
mov ebx,[edx]this.faces
mov ecx,[edx]this.topPos
cmp eax,CONVERTALL
mov lps,ecx
jne short __else
test ecx,ecx
mov edx,v
jz __exit
__loopTop1:
fld [edx]v.x
fmul [ebx]TRIANGLE3D.a
fld [edx]v.y
fmul [ebx]TRIANGLE3D.b
fld [edx]v.z
fmul [ebx]TRIANGLE3D.c
fxch st(1)
faddp st(2),st(0)
fld [ebx]TRIANGLE3D.a
fmul st(0),st(0)
fld [ebx]TRIANGLE3D.b
fmul st(0),st(0)
fld [ebx]TRIANGLE3D.c
fmul st(0),st(0)
fxch st(1)
faddp st(2),st(0)
faddp st(1),st(0)
fxch st(1)
faddp st(2),st(0)
fsqrt
fdivp st(1),st(0)
fchs
fcom zero
fnstsw ax
test ah,65
mov eax,groundLight
jne short __s01
fmul spl
frndint
fistp cos1
add eax,cos1
mov [ebx]TRIANGLE3D.light,eax
jmp short __loop1
__s01:
fstp cos1
mov [ebx]TRIANGLE3D.light,eax
__loop1:
mov eax,s
dec ecx
lea ebx,[ebx+eax]
jnz short __loopTop1
jmp short __exit
__else:
test ecx,ecx
mov ecx,[edx]this.shouldConvert
mov edx,v
jz __exit
__loopTop2:
mov ax,WORD PTR [ecx]
inc ecx
cmp al,0
je __loop2
fld [edx]v.x
fmul [ebx]TRIANGLE3D.a
fld [edx]v.y
fmul [ebx]TRIANGLE3D.b
fld [edx]v.z
fmul [ebx]TRIANGLE3D.c
fxch st(1)
faddp st(2),st(0)
fld [ebx]TRIANGLE3D.a
fmul st(0),st(0)
fld [ebx]TRIANGLE3D.b
fmul st(0),st(0)
fld [ebx]TRIANGLE3D.c
fmul st(0),st(0)
fxch st(1)
faddp st(2),st(0)
faddp st(1),st(0)
fxch st(1)
faddp st(2),st(0)
fsqrt
fdivp st(1),st(0)
fchs
fcom zero
fnstsw ax
test ah,65
mov eax,groundLight
jne short __s02
fmul spl
frndint
fistp cos1
add eax,cos1
mov [ebx]TRIANGLE3D.light,eax
jmp short __loop2
__s02:
fstp st(0)
mov [ebx]TRIANGLE3D.light,eax
__loop2:
mov eax,s
dec lps
lea ebx,[ebx+eax]
jnz short __loopTop2
__exit:
}*/
}
void CFaceList::calLight(VECTOR3D* v,UINT groundLight,UINT spotLight,CPLCFLAG f)
{ UINT i,s,lps;
long cos1;
float cos,zero=0,spl=spotLight;
s = sizeof(TRIANGLE3D);
__asm
{ mov eax,f
mov edx,this
mov ebx,[edx]this.faces
mov ecx,[edx]this.topPos
cmp eax,CONVERTALL
mov lps,ecx
jne short __else
test ecx,ecx
mov edx,v
jz __exit
__loopTop1:
fld [edx]v.x
fmul [ebx]TRIANGLE3D.a
fld [edx]v.y
fmul [ebx]TRIANGLE3D.b
fld [edx]v.z
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -