blua tigro
Gebruiker
- Lid geworden
- 21 apr 2009
- Berichten
- 48
ik probeer n raytrace dll te schrijven in code::blocks
hij moet gaan samen werken met libertybasic
ik kwam zo ver :
dit is mijn eerste dll
dit is vast met error's
help aub
hij moet gaan samen werken met libertybasic
ik kwam zo ver :
Code:
#ifndef __MAIN_H__
#define __MAIN_H__
#include <windows.h>
#include <vector>
#include <math.h>
/* To use this exported function of dll, include this header
* in your project.
*/
#ifdef BUILD_DLL
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
#ifdef __cplusplus
extern "C"
{
#endif
class t3d
{
public :
double x , y , z ;
t3d( double x , double y , double z ) ;
} ;
t3d operator + ( t3d , t3d ) ;
t3d operator - ( t3d , t3d ) ;
t3d operator / ( t3d , double ) ;
t3d operator * ( t3d , double ) ;
double dot( t3d , t3d ) ;
double len( t3d ) ;
t3d cross( t3d , t3d ) ;
double getangle( t3d , t3d ) ;
t3d normalize( t3d ) ;
long toColor( t3d ) ;
const double verry_big = 1e9 ;
struct Material
{
t3d diffuse ;
double reflect ;
} ;
class Sphere
{
public :
t3d center ;
double radius , radius2 ;
Material mat ;
Sphere( double x , double y , double z , double d , t3d kl ) ;
double hit( t3d o , t3d d ) ;
} ;
vector<Sphere> vsph ;
void DLL_EXPORT addSphere( double x , double y
, double z , double d , long kl ) ;
long DLL_EXPORT render( double ox , double oy
, double oz , double dx , double dy
, double dz , int dept )
#ifdef __cplusplus
}
#endif
#endif // __MAIN_H__
Code:
#include "main.h"
t3d operator + ( t3d a , t3d b )
{
return t3d( a.x + b.x
, a.y + b.y
, a.z + b.z ) ;
}
t3d operator - ( t3d a , t3d b )
{
return t3d( a.x - b.x
, a.y - b.y
, a.z - b.z ) ;
}
t3d operator / ( t3d a , double b )
{
return t3d( a.x / b
, a.y / b
, a.z / b ) ;
}
t3d operator * ( t3d a , double b )
{
return t3d( a.x * b
, a.y * b
, a.z * b ) ;
}
double dot( t3d a , t3d b )
{
return a.x * b.x + a.y * b.y + a.z * b.z ;
}
double len( t3d a )
{
return sqrt( dot( a , a ) ) ;
}
t3d cross( t3d a , t3d b )
{
return t3d( a.y * b.z - a.z * b.y
, a.z * b.x - a.x * b.z
, a.x * b.y - a.y * b.x ) ;
}
double getanlge( t3d a , t3d b )
{
double la , lb , d ;
la = len( a ) ;
lb = len( b ) ;
d = dot( a , b ) ;
return acs( d / ( la * lb ) ) ;
}
t3d normalize( t3d a )
{
return a / len( a ) ;
}
long toColor( t3d a )
{
int r , g , b ;
r = ( (int)( a.x * 255 ) ) & 255 ;
g = ( (int)( a.y * 255 ) ) & 255 ;
b = ( (int)( a.z * 255 ) ) & 255 ;
return r + g * 256 + b * 256 * 256 ;
}
Sphere::Sphere(double x , double y , double z
double d , t3d kl )
{
center = t3d( x , y , z ) ;
radius = d ;
radius2 = d * d ;
mat.diffuse = kl ;
mat.reflect = 0.0 ;
}
double Sphere::hit( t3d o , t3d d )
{
t3d p = o - center ;
double a = dot( d , d ) ;
double b = 2 * dot( p , d ) ;
double c = dot( p , p ) - radius2 ;
double disc = b * b - 4 * a * c ;
if ( disc < 0.0 ) return verry_big ;
double e = sqrt( disc ) ;
double demon = 2 * a ;
double t = ( b - e ) / demon ;
if ( t > 1e-9 ) return t ;
t = ( b + e ) / demon ;
if ( t > 1e-9 ) return t ;
return verry_big ;
}
// a sample exported function
void DLL_EXPORT addSphere( double x , double y
, double z , double d , long kl )
{
double r , g , b ;
Material mat ;
r = ( (double)( kl & 255 ) / 256 ) ;
g = ( (double)( ( kl >> 8 ) & 255 ) / 256 ) ;
b = ( (double)( ( kl >> 16 ) & 255 ) / 256 / 256 ) ;
vsph.push_back( Sphere( x , y , z , d
, t3d( r , g , b ) ) ) ;
}
long DLL_EXPORT render( double ox , double oy
, double oz , double dx , double dy
, double dz , int dept )
{
t3d o = t3d( ox , oy , oz ) ;
t3d d = t3d( dx , dy , dz ) ;
int isph = -1 , i ;
double dist , low = verry_big ;
for ( i = 0 ; i < vsph.size() , i++ )
{
dist = vsph.at( i ).hit( o , d ) ;
if ( dist < low )
{
low = dist ;
isph = i ;
}
}
if ( isph == -1 ) return 0 ;
t3d kl = vsph.at( isph ).mat.diffuse ;
// calc hit point in space
t3d p = o + d * low ;
// calc normal of sphere
t3d n = p - vsph.at( isph ).center ;
// calc angle normal - light
double angle = getangle( n
, t3d( -1 , 1 , -1 ) ) ;
kl = kl * ( cos( angle ) / 2 + .5 ) ;
return toColor( kl ) ;
}
extern "C" DLL_EXPORT BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
// attach to process
// return FALSE to fail DLL load
break;
case DLL_PROCESS_DETACH:
// detach from process
break;
case DLL_THREAD_ATTACH:
// attach to thread
break;
case DLL_THREAD_DETACH:
// detach from thread
break;
}
return TRUE; // succesful
}
dit is vast met error's
help aub