home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Houseplan Collection
/
HRCD2005.ISO
/
data1.cab
/
Zusatz
/
3DS
/
DATA2.Z
/
CTorusDlg.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1998-05-28
|
14KB
|
491 lines
// CTorusDlg.cpp : implementation file
//
#include "stdafx.h"
#include "CTorus.h"
#include "CTorusDlg.h"
#include "ArConEventSink.h"
#include <AfxCtl.h>
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CTorusDlg dialog
CTorusDlg::CTorusDlg(CWnd* pParent /*=NULL*/)
: CDialog(CTorusDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CTorusDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_events = NULL;
m_pArCon = NULL;
}
CTorusDlg::~CTorusDlg()
{
if (m_events) {
AfxConnectionUnadvise(m_pArCon, DIID__ArConEvents,
m_events->GetIDispatch(FALSE), FALSE, m_eventCookie);
m_events->ExternalRelease();
m_events = NULL;
}
if (m_pArCon)
m_pArCon->Release();
}
void CTorusDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CTorusDlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CTorusDlg, CDialog)
//{{AFX_MSG_MAP(CTorusDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_MIT, ErzeugeTorusMitLoechern)
ON_BN_CLICKED(IDC_OHNE, ErzeugeEinfachenTorus)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CTorusDlg message handlers
BOOL CTorusDlg::OnInitDialog()
{
CDialog::OnInitDialog();
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// Erzeuge ein ArCon Server Objekt
HRESULT res = CoCreateInstance(CLSID_ArCon, NULL, CLSCTX_LOCAL_SERVER, IID_IArCon, (void**)&m_pArCon);
if (FAILED(res)) {
TRACE("CoCreateInstance ist fehlgeschlagen: 0x%x\n", res);
PostQuitMessage(0);
return TRUE;
}
// Starte die ArCon Verbindung
CString helpFileName = "";
VARIANT_BOOL ok;
m_pArCon->StartMe((long)m_hWnd, helpFileName.AllocSysString(), &ok);
// Erzeuge die Senke fⁿr die ArCon-Ereignisse
m_events = new ArConEventSink();
if (!AfxConnectionAdvise(m_pArCon, DIID__ArConEvents,
m_events->GetIDispatch(FALSE), FALSE, &m_eventCookie))
{
TRACE("Konnte die Event-Verbindung nicht herstellen!\n");
}
// ArCon Modus ermitteln, evtl. neues Projekt erzeugen und in den Design-Modus schalten
long mode;
m_pArCon->get_Mode(&mode);
if (mode == AC_NoMode)
m_pArCon->CreateProject(NULL);
if (mode != AC_ModeDesign)
m_pArCon->put_Mode(AC_ModeDesign);
return TRUE; // return TRUE unless you set the focus to a control
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CTorusDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR CTorusDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
#define PI 3.1415926f
#define TWOPI (PI * 2.0f)
#define FACX 30
#define FACY 16
#define RRAT 0.8f
#define FAK 0.2f
#define FAK2 0.3f
#define TEXTURE_NAME ">wand\\tapete\\kinder\\kaefer1.bmp"
struct Point {
float x, y, z, u, v;
};
static Point ps[FACY][FACX];
void CTorusDlg::GeneratePoints()
{
float cth, cah, dct, dst, st, ct, ct1, st1, dca, dsa, ca, sa;
int a, t;
CString msg("Punktkoordinaten berechnen");
m_pArCon->SetProgressbarSubTitle(msg.AllocSysString());
dct = (float)cos(TWOPI / FACX);
dst = (float)sqrt(1.0f - (dct * dct));
dca = (float)cos(TWOPI / FACY);
dsa = (float)sqrt(1.0f - (dca * dca));
ca = RRAT;
sa = 0.0f;
for (a = 0; a < FACY; a++) {
m_pArCon->SetProgressbarValue(a * 10 / FACY);
ct = 1.0f + sa;
st = 0.0f;
ct1 = 1.0f;
st1 = 0.0f;
for (t = 0; t < FACX; t++) {
ps[a][t].u = t * 3.0f / FACX;
ps[a][t].v = a * 3.0f / FACY;
ps[a][t].x = ct;
ps[a][t].y = st;
ps[a][t].z = ca;
cth = ct;
ct = ct * dct - st * dst;
st = st * dct + cth * dst;
cth = ct1;
ct1 = ct1 * dct - st1 * dst;
st1 = st1 * dct + cth * dst;
}
cah = ca;
ca = ca * dca - sa * dsa;
sa = sa * dca + cah * dsa;
}
}
typedef Point ConturType[4];
static void PointsToVariant(ConturType &contur, VARIANT &v)
{
// Das C-Array in ein OLE SafeArray konvertieren
SAFEARRAY * array;
SAFEARRAYBOUND bounds[2];
bounds[0].lLbound = 0;
bounds[0].cElements = 5;
bounds[1].lLbound = 0;
bounds[1].cElements = 4;
array = SafeArrayCreate(VT_R4, 2, bounds);
void * mem = NULL;
SafeArrayAccessData(array, &mem);
memcpy(mem, contur, sizeof contur);
SafeArrayUnaccessData(array);
VariantInit(&v);
v.vt = VT_ARRAY|VT_R4;
v.parray = array;
}
void CTorusDlg::GenPoly(IObjectConstructor* constr, int a, int t, int ap, int tp)
{
// ▄ber gebe eine Polygoncontur als Array. Dazu die vier Punkt in ein C-Array kopieren
Point contur[4];
contur[0] = ps[a][t];
contur[1] = ps[ap][t];
contur[2] = ps[ap][tp];
contur[3] = ps[a][tp];
VARIANT v;
PointsToVariant(contur, v);
constr->SetContur(4, v);
VariantClear(&v);
}
void CTorusDlg::InstanceIntoWorld(IObjectConstructor* constr)
{
// Eine Einheitsmatrix als "model to world" Transformation
float m2w[4][4];
memset(m2w, 0, sizeof m2w);
int i;
for (i = 0; i < 4; i++)
m2w[i][i] = 1.0f;
IObject3D * inst = NULL;
constr->Create(NULL, 0, &inst);
if (!inst) {
AfxMessageBox("Konstruktion des Torus ist fehlgeschlagen!");
return;
}
inst->put_Flags(AC_3DFL_DBLCLICK | AC_3DFL_CONSTMODE | AC_3DFL_DESIGNMODE);
// Matrix in ein SafeArray verpacken
VARIANT v;
VariantInit(&v);
v.vt = VT_ARRAY|VT_R4;
SAFEARRAYBOUND bounds[2];
bounds[0].lLbound = 0;
bounds[0].cElements = 4;
bounds[1].lLbound = 0;
bounds[1].cElements = 4;
v.parray = SafeArrayCreate(VT_R4, 2, bounds);
void *mem = NULL;
SafeArrayAccessData(v.parray, &mem);
memcpy(mem, m2w, sizeof m2w);
SafeArrayUnaccessData(v.parray);
// Transformation setzen
VARIANT_BOOL ok;
inst->SetModelToWorldTransformation(v, &ok);
// SafeArray freigeben
SafeArrayDestroy(v.parray);
// und in die Welt einfⁿgen
inst->InsertIntoWorld(0, &ok);
}
void CTorusDlg::ErzeugeTorusMitLoechern()
{
CString textureName(TEXTURE_NAME);
DWORD start, end;
start = GetTickCount();
IObjectConstructor * constr = NULL;
m_pArCon->NewObjectConstructor(0, TWOPI / 360 * 105, &constr);
IMaterial * mat = NULL;
m_pArCon->NewMaterial(&mat);
mat->put_AmbientCoefficient(0.2f);
mat->put_DiffuseCoefficient(0.4f);
mat->put_SpecularCoefficient(0.4f);
mat->put_DiffuseColor(RGB(255, 255, 255));
mat->put_SpecularColor(RGB(255, 255, 255));
mat->put_Transparent(0);
mat->put_Flags(ACMATFL_ISTEXTURED | ACMATFL_TWOSIDED);
CString msg("Erzeuge einen Torus mit Lochpolygonen");
m_pArCon->StartProgressbar(msg.AllocSysString(), 0);
GeneratePoints();
int a, t, ap, tp;
msg = "FlΣchen erzeugen";
m_pArCon->SetProgressbarSubTitle(msg.AllocSysString());
for (a = 0; a < FACY; a++) {
m_pArCon->SetProgressbarValue(10 + a * 90 / FACY);
ap = a + 1;
if (ap >= FACY) ap = 0;
for (t = 0; t < FACX; t++) {
tp = t + 1;
if (tp >= FACX) tp = 0;
GenHolePoly(constr, a, t, ap, tp);
constr->AddPolygonWithHoles(0, mat, textureName.AllocSysString());
}
}
m_pArCon->SetProgressbarValue(100);
msg = "Nachbarschaften & Normalen berechnen";
m_pArCon->SetProgressbarSubTitle(msg.AllocSysString());
msg = "Der Torus";
VARIANT_BOOL ok;
constr->Finish(msg.AllocSysString(), 0, ACO_DURATION_CACHEABLE, &ok);
m_pArCon->StopProgressbar();
// Konstruktion ist abgeschlossen, in die Welt damit
InstanceIntoWorld(constr);
// OLE Objekte wieder freigeben
mat->Release();
constr->Release();
end = GetTickCount();
msg.Format("Zeit: %lu ms", end - start);
m_pArCon->SetStatusText(msg.AllocSysString());
}
void CTorusDlg::ErzeugeEinfachenTorus()
{
DWORD start, end;
start = GetTickCount();
CString textureName(TEXTURE_NAME);
IObjectConstructor * constr = NULL;
m_pArCon->NewObjectConstructor(0, TWOPI / 360 * 105, &constr);
IMaterial * mat = NULL;
m_pArCon->NewMaterial(&mat);
mat->put_AmbientCoefficient(0.2f);
mat->put_DiffuseCoefficient(0.4f);
mat->put_SpecularCoefficient(0.4f);
mat->put_DiffuseColor(RGB(255, 255, 255));
mat->put_SpecularColor(RGB(255, 255, 255));
mat->put_Transparent(0);
mat->put_Flags(ACMATFL_ISTEXTURED);
CString msg("Erzeuge einen einfachen Torus");
m_pArCon->StartProgressbar(msg.AllocSysString(), 0);
GeneratePoints();
int a, t, ap, tp;
msg = "FlΣchen erzeugen";
m_pArCon->SetProgressbarSubTitle(msg.AllocSysString());
for (a = 0; a < FACY; a++) {
m_pArCon->SetProgressbarValue(10 + a * 90 / FACY);
ap = a + 1;
if (ap >= FACY) ap = 0;
for (t = 0; t < FACX; t++) {
tp = t + 1;
if (tp >= FACX) tp = 0;
GenPoly(constr, a, t, ap, tp);
constr->AddPolygonWithHoles(0, mat, textureName.AllocSysString());
}
}
m_pArCon->SetProgressbarValue(100);
msg = "Nachbarschaften & Normalen berechnen";
m_pArCon->SetProgressbarSubTitle(msg.AllocSysString());
msg = "Der Torus";
VARIANT_BOOL ok;
constr->Finish(msg.AllocSysString(), 0, ACO_DURATION_CACHEABLE, &ok);
m_pArCon->StopProgressbar();
// Konstruktion ist abgeschlossen, in die Welt damit
InstanceIntoWorld(constr);
// OLE Objekte wieder freigeben
mat->Release();
constr->Release();
end = GetTickCount();
msg.Format("Zeit: %lu ms", end - start);
m_pArCon->SetStatusText(msg.AllocSysString());
}
void CTorusDlg::GenHolePoly(IObjectConstructor* constr, int a, int t, int ap, int tp)
{
VARIANT v;
// ▄ber gebe eine Polygoncontur als Array. Dazu die vier Punkt in ein C-Array kopieren
Point contur[4];
contur[0] = ps[a][t];
contur[1] = ps[ap][t];
contur[2] = ps[ap][tp];
contur[3] = ps[a][tp];
PointsToVariant(contur, v);
constr->SetHoleContur(0, 4, v);
VariantClear(&v);
Point loch1[4];
Point loch2[4];
float d;
Point hp, hp2;
d = (ps[ap][t].x - ps[a][t].x) + (ps[a][tp].x - ps[a][t].x);
hp.x = ps[a][t].x + d * FAK;
hp2.x = ps[a][t].x + d * FAK2;
d = (ps[ap][t].y - ps[a][t].y) + (ps[a][tp].y - ps[a][t].y);
hp.y = ps[a][t].y + d * FAK;
hp2.y = ps[a][t].y + d * FAK2;
d = (ps[ap][t].z - ps[a][t].z) + (ps[a][tp].z - ps[a][t].z);
hp.z = ps[a][t].z + d * FAK;
hp2.z = ps[a][t].z + d * FAK2;
d = (ps[ap][t].u - ps[a][t].u) + (ps[a][tp].u - ps[a][t].u);
hp.u = ps[a][t].u + d * FAK;
hp2.u = ps[a][t].u + d * FAK2;
d = (ps[ap][t].v - ps[a][t].v) + (ps[a][tp].v - ps[a][t].v);
hp.v = ps[a][t].v + d * FAK;
hp2.v = ps[a][t].v + d * FAK2;
loch1[0] = hp;
loch1[1] = hp2;
d = (ps[ap][tp].x - ps[ap][t].x) + (ps[a][t].x - ps[ap][t].x);
hp.x = ps[ap][t].x + d * FAK;
hp2.x = ps[ap][t].x + d * FAK2;
d = (ps[ap][tp].y - ps[ap][t].y) + (ps[a][t].y - ps[ap][t].y);
hp.y = ps[ap][t].y + d * FAK;
hp2.y = ps[ap][t].y + d * FAK2;
d = (ps[ap][tp].z - ps[ap][t].z) + (ps[a][t].z - ps[ap][t].z);
hp.z = ps[ap][t].z + d * FAK;
hp2.z = ps[ap][t].z + d * FAK2;
d = (ps[ap][tp].u - ps[ap][t].u) + (ps[a][t].u - ps[ap][t].u);
hp.u = ps[ap][t].u + d * FAK;
hp2.u = ps[ap][t].u + d * FAK2;
d = (ps[ap][tp].v - ps[ap][t].v) + (ps[a][t].v - ps[ap][t].v);
hp.v = ps[ap][t].v + d * FAK;
hp2.v = ps[ap][t].v + d * FAK2;
loch2[0] = hp;
loch2[3] = hp2;
d = (ps[a][tp].x - ps[ap][tp].x) + (ps[ap][t].x - ps[ap][tp].x);
hp.x = ps[ap][tp].x + d * FAK;
hp2.x = ps[ap][tp].x + d * FAK2;
d = (ps[a][tp].y - ps[ap][tp].y) + (ps[ap][t].y - ps[ap][tp].y);
hp.y = ps[ap][tp].y + d * FAK;
hp2.y = ps[ap][tp].y + d * FAK2;
d = (ps[a][tp].z - ps[ap][tp].z) + (ps[ap][t].z - ps[ap][tp].z);
hp.z = ps[ap][tp].z + d * FAK;
hp2.z = ps[ap][tp].z + d * FAK2;
d = (ps[a][tp].u - ps[ap][tp].u) + (ps[ap][t].u - ps[ap][tp].u);
hp.u = ps[ap][tp].u + d * FAK;
hp2.u = ps[ap][tp].u + d * FAK2;
d = (ps[a][tp].v - ps[ap][tp].v) + (ps[ap][t].v - ps[ap][tp].v);
hp.v = ps[ap][tp].v + d * FAK;
hp2.v = ps[ap][tp].v + d * FAK2;
loch2[1] = hp;
loch2[2] = hp2;
d = (ps[a][t].x - ps[a][tp].x) + (ps[ap][tp].x - ps[a][tp].x);
hp.x = ps[a][tp].x + d * FAK;
hp2.x = ps[a][tp].x + d * FAK2;
d = (ps[a][t].y - ps[a][tp].y) + (ps[ap][tp].y - ps[a][tp].y);
hp.y = ps[a][tp].y + d * FAK;
hp2.y = ps[a][tp].y + d * FAK2;
d = (ps[a][t].z - ps[a][tp].z) + (ps[ap][tp].z - ps[a][tp].z);
hp.z = ps[a][tp].z + d * FAK;
hp2.z = ps[a][tp].z + d * FAK2;
d = (ps[a][t].u - ps[a][tp].u) + (ps[ap][tp].u - ps[a][tp].u);
hp.u = ps[a][tp].u + d * FAK;
hp2.u = ps[a][tp].u + d * FAK2;
d = (ps[a][t].v - ps[a][tp].v) + (ps[ap][tp].v - ps[a][tp].v);
hp.v = ps[a][tp].v + d * FAK;
hp2.v = ps[a][tp].v + d * FAK2;
loch1[3] = hp;
loch1[2] = hp2;
PointsToVariant(loch1, v);
constr->SetHoleContur(1, 4, v);
VariantClear(&v);
PointsToVariant(loch2, v);
constr->SetHoleContur(2, 4, v);
VariantClear(&v);
}