BluaRay.dll poging

Status
Niet open voor verdere reacties.

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 :
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 mijn eerste dll
dit is vast met error's
help aub
 
blua ray dll update

update :
poging tot 3dengine
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 ) ;

t3d SK( 64 ) ;

struct M44
{
  double m[4][4] ;
  M44()
} ;
t3d operator * ( M44 , t3d ) ;
M44 operator * ( M44 , M44 ) ;

M44 V( 200 )  ;

const double verry_big = 1e9 ;
const double pi = atn( 1.0 ) * 4.0 ;

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 ;
int m44no ;
void DLL_EXPORT identity_matrix() ;
void DLL_EXPORT push_matrix() ;
void DLL_EXPORT pop_matrix() ;
double rad( double deg ) ;
void DLL_EXPORT link( double x , double y
, double z , double xz , double yz
, double xy , int ax ) ;
void DLL_EXPORT child( double x , double y
, double z , int lim , int ax ) ;
void DLL_EXPORT skelet( int lim , double x
, double y , double z ) ;
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 math.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 ;
}
M44::M44()
{
  int i , j ;
  for ( i = 0 ; i < 4 ; i++ )
  {
    for ( j = 0 ; i < 4 ; j++ )
      m[ i ][ j ] = 0.0 ;
    m[ i ][ i ] = 1.0 ;
  }
}
t3d operator * ( M44 m , t3d v )
{
  return t3d( m.m[0][0]*v.x
             + m.m[1][0]*v.y
             + m.m[2][0]*v.z
             + m.m[3][0]
             , m.m[0][1]*v.x
             + m.m[1][1]*v.y
             + m.m[2][1]*v.z
             + m.m[3][1]
             , m.m[0][2]*v.x
             + m.m[1][2]*v.y
             + m.m[2][2]*v.z
             + m.m[3][2] ) ;
}
M44 operator * ( M44 a , M44 b )
{
  int i , j , k ;
  M44 uit ;
  for ( i = 0 ; i < 4 ; i++ )
  {
    for ( j = 0 ; j < 4 ; j++ )
    {
      uit.m[i][j] = 0.0 ;
      for ( k = 0 ; k < 4 ; k++ )
        uit.m[i][j]+=a.m[i][k]*b.m[k][j] ;
    }
  }
  return uit ;
}
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 ;
}
double rad( double deg )
{
  return deg * pi / 180.0 ;
}
void DLL_EXPORT identity_matrix()
{
  M44no = 0 ;
}
void DLL_EXPORT push_matrix()
{
  if ( M44no < 199 )
    M44no++ ;
}

void DLL_EXPORT pop_matrix()
{
  if ( M44no > 0 )
    M44no-- ;
}
void DLL_EXPORT link( double x , double y
, double z , double xz , double yz
, double xy , int ax )
{
  M44 rotx , roty , rotz , trans ;

  rotx[1][1] = cos( rad( yz ) ) ;
  rotx[2][1] = -sin( rad( yz ) ) ;
  rotx[1][2] = sin( rad( yz ) ) ;
  rotx[2][2] = cos( rad( yz ) ) ;

  roty[0][0] = cos( rad( xz ) ) ;
  roty[2][0] = -sin( rad( xz ) ) ;
  roty[0][2] = sin( rad( xz ) ) ;
  roty[2][2] = cos( rad( xz ) ) ;

  rotz[1][1] = cos( rad( xy ) ) ;
  rotz[0][1] = -sin( rad( xy ) ) ;
  rotz[1][0] = sin( rad( xy ) ) ;
  rotz[0][0] = cos( rad( xy ) ) ;

  trans[3][0] = x ;
  trans[3][1] = y ;
  trans[3][2] = z ;

  switch ( ax )
  {
    case 0 :
      V( M44no ) = rotx * roty * rotz
      * trans * V( M44no - 1 ) ;
      break ;
    case 1 :
      V( M44no ) = rotx * rotz * roty
      * trans * V( M44no - 1 ) ;
      break ;
    case 2 :
      V( M44no ) = roty * rotx * rotz
      * trans * V( M44no - 1 ) ;
      break ;
    case 3 :
      V( M44no ) = roty * rotz * rotx
      * trans * V( M44no - 1 ) ;
      break ;
    case 4 :
      V( M44no ) = rotz * rotx * roty
      * trans * V( M44no - 1 ) ;
      break ;
    case 5 :
      V( M44no ) = rotz * roty * rotx
      * trans * V( M44no - 1 ) ;
      break ;
    default :
      V( M44no ) = trans * V( M44no - 1 ) ;
  }
}
void DLL_EXPORT child( double x , double y
, double z , int lim , int ax ) ;
{
  link( x , y , z
  , SK( lim ).y , SK( lim ).x , SK(lim).z
  , ax ) ;
}
void DLL_EXPORT skelet( int lim , double x
, double y , double z )
{
  if ( lim < 0 || lim > 63 ) return ;
  SK( lim ) = t3d( x , y , z ) ;
}
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 )
// a sample exported function
void DLL_EXPORT addSphere( double x , double y
, double z , double d , long kl )
{
  double r , g , b ;
  t3d v3d = t3d( x , y , z ) ;
  v3d = V( M44no ) * v3d
  Material mat ;
  r = ( (double)( kl & 255 ) / 256 ) ;
  g = ( (double)( ( kl >> 8 ) & 255 ) / 256 ) ;
  b = ( (double)( ( kl >> 16 ) & 255 ) / 256 / 256 ) ;
  vsph.push_back( Sphere( v3d.x , v3d.y , v3d.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
}
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan