|
Thank you for your corrections.
Quote: you told it there are no clear entries ?
Quote: FileHdr.bfOffBits must point to the offset of the image data Uups. Don't know why I used the struct size here.
Quote: needs to be pack 1 All structures needs to be packed including his own PIXEL structure. Should have mentioned that.
There were two other error which I corrected:
The wrong nExtra calculation and the wrong line write within the loop.
|
|
|
|
|
Those two bitmap header structures are in the GDI headers so you don't need to redefine them and probably shouldn't. Also, you have the screen resolution defined in two constants. Will it really be a fixed size? To me, those should be parameters either passed to the save function or you should pass a window and/or DC to it and it can figure that out.
|
|
|
|
|
He isn't on windows you can't tell just by the screen memory access and no WinMain
In vino veritas
modified 30-Sep-17 17:01pm.
|
|
|
|
|
update : windows 0.1 version
i included windows
so i can use GDI bmp files
and i can see the picture
i inported ALL your input on this
i got now :
#include <windows.h>
#include <string>
const int winx = 800 ;
const int winy = 600 ;
typedef struct PIXEL {
char r , g , b , a ;
} ;
void saveBMP( string filename , PIXEL[][] pixel
, int w , int h )
{
unsigned nWidthBytes = ((w * 24 + 31) & ~31) / 8;
unsigned nExtra = nWidthBytes - 3 * w ;
unsigned nPixelSize = nWidthBytes * h ;
BITMAPFILEHEADER FileHdr ;
FileHdr.bfType = 0x4D42;
FileHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD) + nPixelSize;
FileHdr.bfReserved1 = pBmHdr.bfReserved2 = 0;
FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
BITMAPINFOHEADER InfoHdr ;
InfoHdr.biSize = sizeof(BITMAPINFOHEADER);
InfoHdr.biWidth = screenx;
InfoHdr.biHeight = screeny;
InfoHdr.biPlanes = 1;
InfoHdr.biBitCount = 24;
InfoHdr.biCompression = BI_BITFIELDS;
InfoHdr.biSizeImage = nPixelSize;
InfoHdr.biXPelsPerMeter = 0;
InfoHdr.biYPelsPerMeter = 0;
InfoHdr.biClrUsed = 0;
InfoHdr.biClrImportant = 0;
DWORD clrTable[3] = { 0xff, 0xff00, 0xff0000 };
fwrite(&FileHdr, 1, sizeof(BITMATFILEHEADER), file);
fwrite(&InfoHdr, 1, sizeof(BITMATINFOHEADER), file);
fwrite(clrTable, 1, sizeof(clrTable), file);
for (int y = 0 ; y < ; h ; y++ )
{
fwrite( pixel[y] , 3 , w , file ) ;
if ( nExtra )
{
DWORD dummy = 0;
fwrite( &dummy , 1 , nExtra , file ) ;
}
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
HDC hdc ;
PAINTSTRUCKT ps ;
switch( Message )
{
case WM_PAINT :
int x , y ;
PIXEL pixel[ winx ][ winy ] ;
unsigned long color ;
hdc = BeginPaint( hwnd , & ps ) ;
for ( x = 0 ; x < winx ; x++ )
{
if ( x > 100 && x < winx - 100
&& y > 100 && y < winy - 100 )
color = 0xff007fff ;
else
color = 0xff000000 ;
pixel[x][y].r = color & 255 ;
pixel[x][y].g = ( color >> 8 ) & 255 ;
pixel[x][y].b = ( color >> 16 ) & 255 ;
pixel[x][y].a = 255 ;
SetPixel( hdc , x , y , color ) ;
}
saveBMP( "orange-rect.bmp"
, pixel , winx , winy ) ;
EndPaint( hwnd , & ps ) ;
break ;
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
some stupid mistakes removed
#include <windows.h>
#include <string>
const int winx = 800 ;
const int winy = 600 ;
typedef struct PIXEL {
char r , g , b , a ;
} ;
void saveBMP( string filename , PIXEL[][] pixel
, int w , int h )
{
unsigned nWidthBytes = ((w * 24 + 31) & ~31) / 8;
unsigned nExtra = nWidthBytes - 3 * w ;
unsigned nPixelSize = nWidthBytes * h ;
BITMAPFILEHEADER FileHdr ;
FileHdr.bfType = 0x4D42;
FileHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD) + nPixelSize;
FileHdr.bfReserved1 = pBmHdr.bfReserved2 = 0;
FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
BITMAPINFOHEADER InfoHdr ;
InfoHdr.biSize = sizeof(BITMAPINFOHEADER);
InfoHdr.biWidth = screenx;
InfoHdr.biHeight = screeny;
InfoHdr.biPlanes = 1;
InfoHdr.biBitCount = 24;
InfoHdr.biCompression = BI_BITFIELDS;
InfoHdr.biSizeImage = nPixelSize;
InfoHdr.biXPelsPerMeter = 0;
InfoHdr.biYPelsPerMeter = 0;
InfoHdr.biClrUsed = 0;
InfoHdr.biClrImportant = 0;
DWORD clrTable[3] = { 0xff, 0xff00, 0xff0000 };
fwrite(&FileHdr, 1, sizeof(BITMATFILEHEADER), file);
fwrite(&InfoHdr, 1, sizeof(BITMATINFOHEADER), file);
fwrite(clrTable, 1, sizeof(clrTable), file);
for (int y = 0 ; y < ; h ; y++ )
{
fwrite( pixel[y] , 3 , w , file ) ;
if ( nExtra )
{
DWORD dummy = 0;
fwrite( &dummy , 1 , nExtra , file ) ;
}
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
HDC hdc ;
PAINTSTRUCKT ps ;
switch( Message )
{
case WM_PAINT :
int x , y ;
PIXEL pixel[ winx ][ winy ] ;
unsigned long color ;
hdc = BeginPaint( hwnd , & ps ) ;
for ( x = 0 ; x < winx ; x++ )
{
for ( y = 0 ; y < winy ; y++ )
{
if ( x > 100 && x < winx - 100
&& y > 100 && y < winy - 100 )
color = 0xff007fff ;
else
color = 0xff000000 ;
pixel[y][x].r = color & 255 ;
pixel[y][x].g = ( color >> 8 ) & 255 ;
pixel[y][x].b = ( color >> 16 ) & 255 ;
pixel[y][x].a = 255 ;
SetPixel( hdc , x , y , color ) ;
}
}
saveBMP( "orange-rect.bmp"
, pixel , winx , winy ) ;
EndPaint( hwnd , & ps ) ;
break ;
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
|
update :
build leon sugestion into code :
error :
lots of error's
[ i can't read the error-mesages due to a eye problem ]
#include <windows.h>
#include <string>
#include <math.h>
const int winx = 800 ;
const int winy = 600 ;
typedef struct PIXEL {
char r , g , b , a ;
} ;
class d3d
{
public :
double x , y , z ;
d3d()
{
x = 0.0 ;
y = 0.0 ;
z = 0.0 ;
}
d3d( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
void fill( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
unsigned long toColor()
{
return (unsigned long)
( floor( x * 255 ) +
floor( y * 255 ) * 256 +
floor( z * 255 ) * 256 * 256 ) ;
}
} ;
d3d operator + ( d3d a , d3d b )
{
return d3d( a.x + b.x ,
a.y + b.y ,
a.z + b.z ) ;
}
d3d operator - ( d3d a , d3d b )
{
return d3d( a.x - b.x ,
a.y - b.y ,
a.z - b.z ) ;
}
d3d operator / ( d3d a , double b )
{
return d3d( a.x / b ,
a.y / b ,
a.z / b ) ;
}
d3d operator * ( d3d a , d3d b )
{
return d3d( a.x * b ,
a.y * b ,
a.z * b ) ;
}
double dot( d3d a , d3d b )
{
return a.x * b.x
+ a.y * b.y
+ a.z * b.z ;
}
double length( d3d a )
{
return sqrt( dot( a , a ) ) ;
}
d3d normalize( d3d a )
{
return a / length( a ) ;
}
double getAngle( d3d a , d3d b )
{
double d , la , lb ;
d = dot( a , b ) ;
la = length( a ) ;
lb = length( b ) ;
return acos( d / ( la * lb ) ) ;
}
d3d cross( d3d a , d3d b )
{
return d3d( a.y * b.z - a.z * b.y ,
a.z * b.x - a.x * b.z ,
a.x * b.y - a.y * b.x ) ;
}
struct Matrial
{
d3d diffuse ;
double reflection ;
} ;
Matrial matrial ;
const double INFINITY = 1e300 ;
const double SMALL = 1e-300 ;
class Sphere
{
public :
d3d center ;
double radius , radius2 ;
Matrial mat ;
void fill( d3d c , double r )
{
center = c ;
radius = r ;
radius2 = r * r ;
mat = material ;
}
double hit( d3d o , d3d d )
{
double t , a , b , c , disc ;
d3d temp = o - center ;
a = dot( d , d ) ;
b = 2 * dot( temp , d ) ;
c = dot( temp , temp ) - radius2 ;
disc = b * b - 4 * a * c ;
if ( disc < 0 )
return INFINITY ;
else
{
double e = sqrt( disc ) ;
double demon = 2 * a ;
t = ( -b - e ) / demon ;
if ( t > SMALL ) return t ;
t = ( -b + e ) / demon ;
if ( t > SMALL ) return t ;
}
return INFINITY ;
}
} ;
Sphere spheres( 100 ) ;
int spheretel ;
void sphere( double x , double y , double z
, double r , d3d color )
{
matrial.diffuse = color ;
spheres( spheretel ).fill( x , y , z , r ) ;
spheretel++ ;
}
d3d light ;
d3d render( d3d o , d3d d , int depth )
{
double dist , sphdist = INFINITY ;
int i , isph = -1 ;
for ( i = 0 ; i < spheretel ; i++ )
{
dist = spheres( i ).hit( o , d ) ;
if ( dist < sphdist )
{
sphdist = dist ;
isph = i ;
}
}
if ( isph == -1 )
return d3d()
d3d p , n ;
p = o + d * sphdist ;
n = p - spheres( isph ).center ;
double a = getAngle( n , light ) ;
d3d color = spheres( isph ).mat.diffuse ;
color = color * ( 0.5 + cos( a * 2 ) / 2 ) ;
if ( a > PI / 2 )
color = d3d() ;
for ( i = 0 ; i < spheretel ; i ++ )
{
if ( i != isph )
{
a = spheres( i ).hit( p , light ) ;
if ( a == INFINITY )
color = d3d() ;
}
}
return color ;
}
int CaptureAnImage( HWND hWnd ,
string filename )
{
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(hWnd);
hdcMemDC = CreateCompatibleDC(hdcWindow);
if(!hdcMemDC)
{
MessageBox(hWnd, L"CreateCompatibleDC has failed",L"Failed", MB_OK);
goto done;
}
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetStretchBltMode(hdcWindow,HALFTONE);
if(!StretchBlt(hdcWindow,
0,0,
rcClient.right, rcClient.bottom,
hdcScreen,
0,0,
GetSystemMetrics (SM_CXSCREEN),
GetSystemMetrics (SM_CYSCREEN),
SRCCOPY))
{
MessageBox(hWnd, L"StretchBlt has failed",L"Failed", MB_OK);
goto done;
}
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right-rcClient.left, rcClient.bottom-rcClient.top);
if(!hbmScreen)
{
MessageBox(hWnd, L"CreateCompatibleBitmap Failed",L"Failed", MB_OK);
goto done;
}
SelectObject(hdcMemDC,hbmScreen);
if(!BitBlt(hdcMemDC,
0,0,
rcClient.right-rcClient.left, rcClient.bottom-rcClient.top,
hdcWindow,
0,0,
SRCCOPY))
{
MessageBox(hWnd, L"BitBlt has failed", L"Failed", MB_OK);
goto done;
}
GetObject(hbmScreen,sizeof(BITMAP),&bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile( filename ,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL,hdcScreen);
ReleaseDC(hWnd,hdcWindow);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
HDC hdc ;
PAINTSTRUCKT ps ;
switch( Message )
{
case WM_PAINT :
int x , y ;
d3d o , d , WHITE ;
WHITE.fill( 1.0 , 1.0 , 1.0 ) ;
unsigned long color ;
hdc = BeginPaint( hwnd , & ps ) ;
spheretel = 0 ;
light.fill( -1.0 , 1.0 , -1.0 ) ;
sphere( 0.0 , 0.0 , 0.0 , 100.0 , WHITE ) ;
sphere( 0.0 , -INIFINITY - winy / 2 , 0.0
, INFINITY , WHITE ) ;
for ( x = 0 ; x < winx ; x++ )
{
for ( y = 0 ; y < winy ; y++ )
{
o.fill( 0.0 , 0.0 , -1000 ) ;
d.fill( (double)x ,
(double)y , 1000 ) ;
color = render( o , d , 7 ).toColor() ;
SetPixel( hdc , x , y , color ) ;
}
}
int dummy = CaptureAnImage( hwnd ,
"white-sphere.bmp" ) ;
EndPaint( hwnd , & ps ) ;
break ;
case WM_DESTROY : {
PostQuitMessage( 0 ) ;
break ;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
So many typing errors .. use of ( ) instead of [ ], spelling errors, incorrect types.
You had a mixture of unicode and ascii text so I added #include <tchar.h> and put all the text
in _T( ) macro so they work either way.
I also assume you realize you haven't started the D3D render system so your D3d stuff wont work you will just get black screen.
Fixed to at least compile. I assume you sent in the wrong window handle because it
does a magnificent full screen capture (that is window handle was zero) rather than
capture your actual program window so you get a nice bitmap of your entire screen
#include <windows.h>
#include <string>
#include <math.h>
#include <tchar.h>
const int winx = 800;
const int winy = 600;
typedef struct PIXEL {
char r, g, b, a;
};
class d3d
{
public:
double x, y, z;
d3d()
{
x = 0.0;
y = 0.0;
z = 0.0;
}
d3d(double X, double Y, double Z)
{
x = X;
y = Y;
z = Z;
}
void fill(double X, double Y, double Z)
{
x = X;
y = Y;
z = Z;
}
unsigned long toColor()
{
return (unsigned long)
(floor(x * 255) +
floor(y * 255) * 256 +
floor(z * 255) * 256 * 256);
}
};
d3d operator + (d3d a, d3d b)
{
return d3d(a.x + b.x,
a.y + b.y,
a.z + b.z);
}
d3d operator - (d3d a, d3d b)
{
return d3d(a.x - b.x,
a.y - b.y,
a.z - b.z);
}
d3d operator / (d3d a, double b)
{
return d3d(a.x / b,
a.y / b,
a.z / b);
}
d3d operator * (d3d a, double b)
{
return d3d(a.x * b,
a.y * b,
a.z * b);
}
double dot(d3d a, d3d b)
{
return a.x * b.x
+ a.y * b.y
+ a.z * b.z;
}
double length(d3d a)
{
return sqrt(dot(a, a));
}
d3d normalize(d3d a)
{
return a / length(a);
}
double getAngle(d3d a, d3d b)
{
double d, la, lb;
d = dot(a, b);
la = length(a);
lb = length(b);
return acos(d / (la * lb));
}
d3d cross(d3d a, d3d b)
{
return d3d(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
struct Matrial
{
d3d diffuse;
double reflection;
};
Matrial material;
#define SMALL 1e-300
#define PI 3.141592658
class Sphere
{
public:
d3d center;
double radius, radius2;
Matrial mat;
void fill(d3d c, double r)
{
center = c;
radius = r;
radius2 = r * r;
mat = material;
}
double hit(d3d o, d3d d)
{
double t, a, b, c, disc;
d3d temp = o - center;
a = dot(d, d);
b = 2 * dot(temp, d);
c = dot(temp, temp) - radius2;
disc = b * b - 4 * a * c;
if (disc < 0)
return INFINITY;
else
{
double e = sqrt(disc);
double demon = 2 * a;
t = (-b - e) / demon;
if (t > SMALL) return t;
t = (-b + e) / demon;
if (t > SMALL) return t;
}
return INFINITY;
}
};
Sphere spheres[100];
int spheretel;
void sphere(double x, double y, double z
, double r, d3d color)
{
d3d pt;
pt.x = x;
pt.y = y;
pt.z = z;
material.diffuse = color;
spheres[spheretel].fill(pt, r);
spheretel++;
}
d3d light;
d3d render(d3d o, d3d d, int depth)
{
double dist, sphdist = INFINITY;
int i, isph = -1;
for (i = 0; i < spheretel; i++)
{
dist = spheres[i].hit(o, d);
if (dist < sphdist)
{
sphdist = dist;
isph = i;
}
}
if (isph == -1)
return d3d();
d3d p, n;
p = o + d * sphdist;
n = p - spheres[isph].center;
double a = getAngle(n, light);
d3d color = spheres[isph].mat.diffuse;
color = color * (0.5 + cos(a * 2) / 2);
if (a > PI / 2)
color = d3d();
for (i = 0; i < spheretel; i++)
{
if (i != isph)
{
a = spheres[i].hit(p, light);
if (a == INFINITY)
color = d3d();
}
}
return color;
}
int CaptureAnImage(HWND hWnd,
char* filename)
{
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(hWnd);
hdcMemDC = CreateCompatibleDC(hdcWindow);
if (!hdcMemDC)
{
MessageBox(hWnd, _T("CreateCompatibleDC has failed"), _T("Failed"), MB_OK);
goto done;
}
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetStretchBltMode(hdcWindow, HALFTONE);
if (!StretchBlt(hdcWindow,
0, 0,
rcClient.right, rcClient.bottom,
hdcScreen,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
SRCCOPY))
{
MessageBox(hWnd, _T("StretchBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
if (!hbmScreen)
{
MessageBox(hWnd, _T("CreateCompatibleBitmap Failed"), _T("Failed"), MB_OK);
goto done;
}
SelectObject(hdcMemDC, hbmScreen);
if (!BitBlt(hdcMemDC,
0, 0,
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hdcWindow,
0, 0,
SRCCOPY))
{
MessageBox(hWnd, _T("BitBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL, hdcScreen);
ReleaseDC(hWnd, hdcWindow);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
switch (Message)
{
case WM_PAINT: {
int x, y;
d3d o, d, WHITE;
WHITE.fill(1.0, 1.0, 1.0);
unsigned long color;
hdc = BeginPaint(hwnd, &ps);
spheretel = 0;
light.fill(-1.0, 1.0, -1.0);
sphere(0.0, 0.0, 0.0, 100.0, WHITE);
sphere(0.0, -INFINITY - winy / 2, 0.0
, INFINITY, WHITE);
for (x = 0; x < winx; x++)
{
for (y = 0; y < winy; y++)
{
o.fill(0.0, 0.0, -1000);
d.fill((double)x,
(double)y, 1000);
color = render(o, d, 7).toColor();
SetPixel(hdc, x, y, color);
}
}
int dummy = CaptureAnImage(hwnd,
_T("white-sphere.bmp"));
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("WindowClass"), _T("Caption"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
In vino veritas
modified 1-Oct-17 22:56pm.
|
|
|
|
|
@ leon :
i m used to basic so the () inplaceof []
update :
m44 class [ matrix ] added
3d engine added
in the future i wil give a example of a animated avatar
#include <windows.h>
#include <string>
#include <math.h>
#include <tchar.h>
const int winx = 800 ;
const int winy = 600 ;
#define SMALL 1e-300
#define PI = 3.1415929203
class d3d
{
public :
double x , y , z ;
d3d()
{
x = 0.0 ;
y = 0.0 ;
z = 0.0 ;
}
d3d( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
void fill( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
unsigned long toColor()
{
return (unsigned long)
( floor( x * 255 ) +
floor( y * 255 ) * 256 +
floor( z * 255 ) * 256 * 256 ) ;
}
} ;
d3d operator + ( d3d a , d3d b )
{
return d3d( a.x + b.x ,
a.y + b.y ,
a.z + b.z ) ;
}
d3d operator - ( d3d a , d3d b )
{
return d3d( a.x - b.x ,
a.y - b.y ,
a.z - b.z ) ;
}
d3d operator / ( d3d a , double b )
{
return d3d( a.x / b ,
a.y / b ,
a.z / b ) ;
}
d3d operator * ( d3d a , double b )
{
return d3d( a.x * b ,
a.y * b ,
a.z * b ) ;
}
double dot( d3d a , d3d b )
{
return a.x * b.x
+ a.y * b.y
+ a.z * b.z ;
}
double length( d3d a )
{
return sqrt( dot( a , a ) ) ;
}
d3d normalize( d3d a )
{
return a / length( a ) ;
}
double getAngle( d3d a , d3d b )
{
double d , la , lb ;
d = dot( a , b ) ;
la = length( a ) ;
lb = length( b ) ;
return acos( d / ( la * lb ) ) ;
}
d3d cross( d3d a , d3d b )
{
return d3d( a.y * b.z - a.z * b.y ,
a.z * b.x - a.x * b.z ,
a.x * b.y - a.y * b.x ) ;
}
const d3d BLACK = d3d( 0.0 , 0.0 , 0.0 ) ;
const d3d RED = d3d( 1.0 , 0.0 , 0.0 ) ;
const d3d GREEN = d3d( 0.0 , 1.0 , 0.0 ) ;
const d3d YELLOW = d3d( 1.0 , 1.0 , 0.0 ) ;
const d3d BLUE = d3d( 0.0 , 0.0 , 1.0 ) ;
const d3d MAGENTA = d3d( 1.0 , 0.0 , 1.0 ) ;
const d3d CYAN = d3d( 0.0 , 1.0 , 1.0 ) ;
const d3d WHITE = d3d( 1.0 , 1.0 , 1.0 ) ;
const d3d GRAY = WHITE / 2.0 ;
const d3d ORANGE = ( YELLOW + RED ) / 2.0 ;
const d3d PINK = ( WHITE + RED ) / 2.0 ;
class m44
{
public :
double m[ 4 ][ 4 ] ;
m44()
{
int i , j ;
for ( i = 0 ; i < 4 ; i++ )
{
for ( j = 0 ; j < 4 ; j++ )
m[ i ][ j ] = 0.0 ;
m[ i ][ i ] = 1.0 ;
}
}
~m44()
{
}
void translate( double x , double y , double z )
{
m[ 3 ][ 0 ] = x ;
m[ 3 ][ 1 ] = y ;
m[ 3 ][ 2 ] = z ;
}
void rotate( double deg , int ax )
{
int a = 1 , b = 2 ;
if ( ax == 1 )
{
a = 0 ;
b = 2 ;
}
if ( ax == 2 )
{
a = 0 ;
b = 1 ;
}
m[ a ][ a ] = cos( deg * PI / 180 ) ;
m[ a ][ b ] = -sin( deg * PI / 180 ) ;
m[ b ][ a ] = sin( deg * PI / 180 ) ;
m[ b ][ b ] = cos( deg * PI / 180 ) ;
}
} ;
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 ;
}
d3d operator * ( m44 m , d3d v )
{
return d3d( m.m[0][0]*v.x+m.m[1][0]*v.y+m.m[2][0]+m.m[3][0]
, m.m[0][1]*v.x+m.m[1][1]*v.y+m.m[2][1]+m.m[3][1]
, m.m[0][2]*v.x+m.m[1][2]*v.y+m.m[2][2]+m.m[3][2]);
}
m44 inverse( m44 a )
{
double d ;
m44 uit ;
d = q.m[0][0]*q.m[1][1]*q.m[2][2]
- q.m[0][0]*q.m[2][1]*q.m[1][2]
+ q.m[1][0]*q.m[2][1]*q.m[0][2]
- q.m[1][0]*q.m[0][1]*q.m[2][2]
+ q.m[2][0]*q.m[0][1]*q.m[1][2]
- q.m[2][0]*q.m[1][1]*q.m[0][2] ;
uit.m[0][0]=(q.m[1][1)*q.m[2][2]-q.m[1][2]*q.m[2][1])/d;
uit.m[1][0]=(q.m[0][1)*q.m[2][2]-q.m[0][2]*q.m[2][1])/d;
uit.m[2][0]=(q.m[0][1)*q.m[1][2]-q.m[0][2]*q.m[1][1])/d;
uit.m[0][1]=(q.m[1][0)*q.m[2][2]-q.m[1][2]*q.m[2][0])/d;
uit.m[1][1]=(q.m[0][0)*q.m[2][2]-q.m[0][2]*q.m[2][0])/d;
uit.m[2][1]=(q.m[0][0)*q.m[1][2]-q.m[0][2]*q.m[1][0])/d;
uit.m[0][2]=(q.m[1][0)*q.m[2][1]-q.m[1][1]*q.m[2][0])/d;
uit.m[1][2]=(q.m[0][0)*q.m[2][1]-q.m[0][1]*q.m[2][0])/d;
uit.m[2][2]=(q.m[0][0)*q.m[1][1]-q.m[0][1]*q.m[1][2])/d;
return uit ;
}
d3d SK[ 64 ] ;
m44 V[ 20 ] ;
int number ;
const int XYZ = 0 ;
const int XZY = 1 ;
const int YXZ = 2 ;
const int YZX = 3 ;
const int ZXY = 4 ;
const int ZYX = 5 ;
void link( int no , double x , double y , double z
, double pan , double tilt , double rol , int ax , int p )
{
if ( no < 1 ) return ;
if ( no >= 20 ) return ;
if ( p < 0 ) return ;
if ( p >= 20 ) return ;
if ( p == no ) return ;
m44 trans , rotx , roty , rotz ;
trans.translate( x , y , z ) ;
rotx.rotate( tilt , 0 ) ;
roty.rotate( pan , 1 ) ;
rotz.rotate( rol , 2 ) ;
switch( ax )
{
case XYZ :
V[ no ] = rotx * roty * rotz * trans * V[ p ] ;
break ;
case XZY :
V[ no ] = rotx * rotz * roty * trans * V[ p ] ;
break ;
case YXZ :
V[ no ] = roty * rotx * rotz * trans * V[ p ] ;
break ;
case YZX :
V[ no ] = roty * rotz * rotx * trans * V[ p ] ;
break ;
case ZXY :
V[ no ] = rotz * rotx * roty * trans * V[ p ] ;
break ;
case ZYX :
V[ no ] = rotz * roty * rotx * trans * V[ p ] ;
break ;
default :
V[ no ] = rotx * roty * rotz * trans * V[ p ] ;
}
number = no ;
}
void child( int no , doube x , double y , double z
, int lim , int ax , int p )
{
if ( lim < 0 ) then return ;
if ( lim >= 64 ) then return ;
link( no , x , y , z
, SK[ lim ].y , SK[ lim ].x , SK[ lim ].z
, ax , p ) ;
}
void skelet( int lim , double x , double y , double z )
{
if ( lim < 0 ) return ;
if ( lim >= 64 ) return ;
SK[ lim ].fill( x , y , z ) ;
}
double pend( double fase , double amp )
{
return sin( fase * PI / 180 ) * amp ;
}
d3d spot( d3d q )
{
return V[ number ] * q ;
}
struct Matrial
{
d3d diffuse ;
double reflection ;
} ;
Matrial material ;
class Sphere
{
public :
d3d center ;
double radius , radius2 ;
Matrial mat ;
void fill( d3d c , double r )
{
center = spot( c ) ;
radius = r ;
radius2 = r * r ;
mat = material ;
}
double hit( d3d o , d3d d )
{
double t , a , b , c , disc ;
d3d temp = o - center ;
a = dot( d , d ) ;
b = 2 * dot( temp , d ) ;
c = dot( temp , temp ) - radius2 ;
disc = b * b - 4 * a * c ;
if ( disc < 0 )
return INFINITY;
else
{
double e = sqrt( disc ) ;
double demon = 2 * a ;
t = ( -b - e ) / demon ;
if ( t > SMALL ) return t ;
t = ( -b + e ) / demon ;
if ( t > SMALL ) return t ;
}
return INFINITY ;
}
} ;
Sphere spheres[ 100 ] ;
int spheretel ;
void sphere( double x , double y , double z
, double r , d3d color )
{
d3d pt ;
pt.fill( x , y , z ) ;
material.diffuse = color ;
spheres[ spheretel ].fill( pt , r ) ;
spheretel++ ;
}
d3d light;
d3d render( d3d o , d3d d , int depth )
{
double dist , sphdist = INFINITY ;
int i , isph = -1 ;
for ( i = 0 ; i < spheretel ; i++ )
{
dist = spheres[ i ].hit( o , d ) ;
if ( dist < sphdist )
{
sphdist = dist ;
isph = i ;
}
}
if (isph == -1) return BLACK ;
d3d p , n ;
p = o + d * sphdist ;
n = p - spheres[ isph ].center ;
double a = getAngle( n , light ) ;
d3d color = spheres[ isph ].mat.diffuse ;
color = color * ( 0.5 + cos( a * 2 ) / 2 ) ;
if ( a > PI / 2 ) color = BLACK ;
for ( i = 0 ; i < spheretel ; i++ )
{
if ( i != isph )
{
a = spheres[ i ].hit( p , light ) ;
if ( a == INFINITY ) color = BLACK ;
}
}
return color ;
}
char * frame( char * filmname , int no , int len )
{
}
int CaptureAnImage( HWND hWnd , char* filename )
{
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(hWnd);
hdcMemDC = CreateCompatibleDC(hdcWindow);
if (!hdcMemDC)
{
MessageBox(hWnd, _T("CreateCompatibleDC has failed"), _T("Failed"), MB_OK);
goto done;
}
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetStretchBltMode(hdcWindow, HALFTONE);
if (!StretchBlt(hdcWindow,
0, 0,
rcClient.right, rcClient.bottom,
hdcScreen,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
SRCCOPY))
{
MessageBox(hWnd, _T("StretchBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
if (!hbmScreen)
{
MessageBox(hWnd, _T("CreateCompatibleBitmap Failed"), _T("Failed"), MB_OK);
goto done;
}
SelectObject(hdcMemDC, hbmScreen);
if (!BitBlt(hdcMemDC,
0, 0,
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hdcWindow,
0, 0,
SRCCOPY))
{
MessageBox(hWnd, _T("BitBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL, hdcScreen);
ReleaseDC(hWnd, hdcWindow);
return 0;
}
LRESULT CALLBACK WndProc( HWND hwnd ,
UINT Message ,
WPARAM wParam ,
LPARAM lParam )
{
HDC hdc ;
PAINTSTRUCT ps ;
switch (Message)
{
case WM_PAINT :
{
int x , y , picno ;
d3d o , d ;
unsigned long color ;
hdc = BeginPaint( hwnd , &ps ) ;
spheretel = 0 ;
light.fill( -1.0 , 1.0 , -1.0 ) ;
link( 1 , 0.0 , 0.0 , 0.0
, (double)picno * 360 / 16 , 0.0 , 0.0 , XYZ , 0 ) ;
sphere( 0.0 , 100.0 , 0.0 , 100.0 , MAGENTA ) ;
sphere( -50.0 , -86.0 , 0.0 , 100.0 , CYAN ) ;
sphere( 50.0 , -86.0 , 0.0 , 100.0 , YELLOW ) ;
sphere( 0.0 , -INFINITY - winy / 2 , 0.0
, INFINITY , WHITE ) ;
for ( x = 0 ; x < winx ; x++ )
{
for ( y = 0 ; y < winy ; y++ )
{
o.fill( 0.0 , 0.0 , -1000.0 ) ;
d.fill( (double)x , (double)y , 1000.0 ) ;
color = render( o , d , 7 ).toColor() ;
SetPixel( hdc , x , y , color ) ;
}
}
int dummy = CaptureAnImage( hwnd ,
_T( "sphere-3.bmp" ) ;
EndPaint( hwnd , &ps ) ;
break ;
}
case WM_DESTROY :
{
PostQuitMessage( 0 ) ;
break ;
}
default :
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0 ;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("WindowClass"), _T("Caption"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
Still not following why you are doing software renderer you are on windows you can setup the hardware accelerated renderer.
Beside that 2 other comments ...
1.) Your value of Pi is wrong
1000 places of Pi[^]
2.) You obviously struggling to make frame filename and frame is already use .. try this
void frameName (TCHAR* buffer, int bufsize, const TCHAR* nameStart, const TCHAR* ext, int no, int len)
{
char fmtstring[32];
sprintf_s(fmtstring, _countof(fmtstring), "%s%i%s", "%s%0", len, "i.%s");
_stprintf_s(buffer, bufsize, fmtstring, nameStart, no, ext);
}
It looks slightly strange because you have to make the format string.
"%s%08i.%s" is an example format string, which basically says string + integer to 8 digits + . + string
The problem is you don't want a constant 8 you want Len so you have to 1st build the format string
Example of use of what I suspect you are trying to do. Len I assume is length of number padded with "0"'s
TCHAR buf[256];
frameName(buf, _countof(buf), _T("test"), _T("bmp"), 1, 6);
If you haven't run across TCHAR before it is a character type that changes with compile mode.
In Ansi mode it is char
In unicode mode it is wchar
I have no idea if you are compiling in ansi mode or unicode, you had both so I am trying to be careful
to support either which is what using TCHAR does. However you need to be aware TCHAR size changes.
That is the reason for using _countof() rather than sizeof() for the buffer size.
In vino veritas
modified 2-Oct-17 10:21am.
|
|
|
|
|
|
@ leon :
hardware raytracing ?
do you meen openGL ?
or directx ?
in vs2017 i cant do c++
in vb2017 i cant do opengl or directx
in that case i won't get reflection and refraction
this is also a try at better c++ understanding
i can't run c++ on my work pc
is there a c++ ide you can start [ and compiles ] from usb ?
|
|
|
|
|
hardware raytracing ?
do you meen openGL ?
or directx ?
Those or any of the other hardware renderer frameworks?
in vs2017 i cant do c++
yes you can on the VS2017 app folder run the installer again and tick the box.
It doesn't install by default you have to select it, think only C# gets setup by default
Select it as shown on this image
https://github.com/LdB-ECM/Docs_and_Images/blob/master/Images/VS2017.jpg?raw=true
in vb2017 i cant do opengl or directx
Again yes you can you just have to manual handle the API calls ... is it easy, well no
Walkthrough: Calling Windows APIs (Visual Basic) | Microsoft Docs[^]
this is also a try at better c++ understanding
That I agree with and you have obviously made progress in that area.
I can't run c++ on my work pc, is there a c++ ide you can start [ and compiles ] from usb ?
You can install VS2017 onto a USB drive you just need to make sure the drive is mapped to a known letter.
I actually do that my VS2017 is on an external harddrive that I map to G:
I just make sure on any machine I wish to use the compiler I set the USB driver letter to G:.
It's slows slightly on USB2 but on USB3 drive and port you can't notice the difference.
In vino veritas
modified 3-Oct-17 23:38pm.
|
|
|
|
|
update :
avatar added
animation added
to be sure i REMed the animation and saving part
for so far i understand it is right
if i add :
ambient , specular , shinines , phong , emission etc...
to Material
how do i calc the end color ?
bluatigro 1 okt 2017
#include <windows.h>
#include <string>
#include <math.h>
#include <tchar.h>
const int winx = 800 ;
const int winy = 600 ;
#define SMALL 1e-300
#define PI = 3.14159265358979323846
class d3d
{
public :
double x , y , z ;
d3d()
{
x = 0.0 ;
y = 0.0 ;
z = 0.0 ;
}
d3d( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
void fill( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
unsigned long toColor()
{
return (unsigned long)
( floor( x * 255 ) +
floor( y * 255 ) * 256 +
floor( z * 255 ) * 256 * 256 ) ;
}
} ;
d3d operator + ( d3d a , d3d b )
{
return d3d( a.x + b.x ,
a.y + b.y ,
a.z + b.z ) ;
}
d3d operator - ( d3d a , d3d b )
{
return d3d( a.x - b.x ,
a.y - b.y ,
a.z - b.z ) ;
}
d3d operator / ( d3d a , double b )
{
return d3d( a.x / b ,
a.y / b ,
a.z / b ) ;
}
d3d operator * ( d3d a , double b )
{
return d3d( a.x * b ,
a.y * b ,
a.z * b ) ;
}
double dot( d3d a , d3d b )
{
return a.x * b.x
+ a.y * b.y
+ a.z * b.z ;
}
double length( d3d a )
{
return sqrt( dot( a , a ) ) ;
}
d3d normalize( d3d a )
{
return a / length( a ) ;
}
double getAngle( d3d a , d3d b )
{
double d , la , lb ;
d = dot( a , b ) ;
la = length( a ) ;
lb = length( b ) ;
return acos( d / ( la * lb ) ) ;
}
d3d cross( d3d a , d3d b )
{
return d3d( a.y * b.z - a.z * b.y ,
a.z * b.x - a.x * b.z ,
a.x * b.y - a.y * b.x ) ;
}
const d3d BLACK = d3d( 0.0 , 0.0 , 0.0 ) ;
const d3d RED = d3d( 1.0 , 0.0 , 0.0 ) ;
const d3d GREEN = d3d( 0.0 , 1.0 , 0.0 ) ;
const d3d YELLOW = d3d( 1.0 , 1.0 , 0.0 ) ;
const d3d BLUE = d3d( 0.0 , 0.0 , 1.0 ) ;
const d3d MAGENTA = d3d( 1.0 , 0.0 , 1.0 ) ;
const d3d CYAN = d3d( 0.0 , 1.0 , 1.0 ) ;
const d3d WHITE = d3d( 1.0 , 1.0 , 1.0 ) ;
const d3d GRAY = WHITE / 2.0 ;
const d3d ORANGE = ( YELLOW + RED ) / 2.0 ;
const d3d PINK = ( WHITE + RED ) / 2.0 ;
class m44
{
public :
double m[ 4 ][ 4 ] ;
m44()
{
int i , j ;
for ( i = 0 ; i < 4 ; i++ )
{
for ( j = 0 ; j < 4 ; j++ )
m[ i ][ j ] = 0.0 ;
m[ i ][ i ] = 1.0 ;
}
}
~m44()
{
}
void translate( double x , double y , double z )
{
m[ 3 ][ 0 ] = x ;
m[ 3 ][ 1 ] = y ;
m[ 3 ][ 2 ] = z ;
}
void rotate( double deg , int ax )
{
int a = 1 , b = 2 ;
if ( ax == 1 )
{
a = 0 ;
b = 2 ;
}
if ( ax == 2 )
{
a = 0 ;
b = 1 ;
}
m[ a ][ a ] = cos( deg * PI / 180 ) ;
m[ a ][ b ] = -sin( deg * PI / 180 ) ;
m[ b ][ a ] = sin( deg * PI / 180 ) ;
m[ b ][ b ] = cos( deg * PI / 180 ) ;
}
} ;
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 ;
}
d3d operator * ( m44 m , d3d v )
{
return d3d( m.m[0][0]*v.x+m.m[1][0]*v.y+m.m[2][0]+m.m[3][0]
, m.m[0][1]*v.x+m.m[1][1]*v.y+m.m[2][1]+m.m[3][1]
, m.m[0][2]*v.x+m.m[1][2]*v.y+m.m[2][2]+m.m[3][2]);
}
m44 inverse( m44 a )
{
double d ;
m44 uit ;
d = q.m[0][0]*q.m[1][1]*q.m[2][2]
- q.m[0][0]*q.m[2][1]*q.m[1][2]
+ q.m[1][0]*q.m[2][1]*q.m[0][2]
- q.m[1][0]*q.m[0][1]*q.m[2][2]
+ q.m[2][0]*q.m[0][1]*q.m[1][2]
- q.m[2][0]*q.m[1][1]*q.m[0][2] ;
uit.m[0][0]=(q.m[1][1)*q.m[2][2]-q.m[1][2]*q.m[2][1])/d;
uit.m[1][0]=(q.m[0][1)*q.m[2][2]-q.m[0][2]*q.m[2][1])/d;
uit.m[2][0]=(q.m[0][1)*q.m[1][2]-q.m[0][2]*q.m[1][1])/d;
uit.m[0][1]=(q.m[1][0)*q.m[2][2]-q.m[1][2]*q.m[2][0])/d;
uit.m[1][1]=(q.m[0][0)*q.m[2][2]-q.m[0][2]*q.m[2][0])/d;
uit.m[2][1]=(q.m[0][0)*q.m[1][2]-q.m[0][2]*q.m[1][0])/d;
uit.m[0][2]=(q.m[1][0)*q.m[2][1]-q.m[1][1]*q.m[2][0])/d;
uit.m[1][2]=(q.m[0][0)*q.m[2][1]-q.m[0][1]*q.m[2][0])/d;
uit.m[2][2]=(q.m[0][0)*q.m[1][1]-q.m[0][1]*q.m[1][2])/d;
return uit ;
}
d3d SK[ 64 ] ;
m44 V[ 20 ] ;
int number ;
const int XYZ = 0 ;
const int XZY = 1 ;
const int YXZ = 2 ;
const int YZX = 3 ;
const int ZXY = 4 ;
const int ZYX = 5 ;
void link( int no , double x , double y , double z
, double pan , double tilt , double rol , int ax , int p )
{
if ( no < 1 ) return ;
if ( no >= 20 ) return ;
if ( p < 0 ) return ;
if ( p >= 20 ) return ;
if ( p == no ) return ;
m44 trans , rotx , roty , rotz ;
trans.translate( x , y , z ) ;
rotx.rotate( tilt , 0 ) ;
roty.rotate( pan , 1 ) ;
rotz.rotate( rol , 2 ) ;
switch( ax )
{
case XYZ :
V[ no ] = rotx * roty * rotz * trans * V[ p ] ;
break ;
case XZY :
V[ no ] = rotx * rotz * roty * trans * V[ p ] ;
break ;
case YXZ :
V[ no ] = roty * rotx * rotz * trans * V[ p ] ;
break ;
case YZX :
V[ no ] = roty * rotz * rotx * trans * V[ p ] ;
break ;
case ZXY :
V[ no ] = rotz * rotx * roty * trans * V[ p ] ;
break ;
case ZYX :
V[ no ] = rotz * roty * rotx * trans * V[ p ] ;
break ;
default :
V[ no ] = rotx * roty * rotz * trans * V[ p ] ;
}
number = no ;
}
void child( int no , doube x , double y , double z
, int lim , int ax , int p )
{
if ( lim < 0 ) then return ;
if ( lim >= 64 ) then return ;
link( no , x , y , z
, SK[ lim ].y , SK[ lim ].x , SK[ lim ].z
, ax , p ) ;
}
void skelet( int lim , double x , double y , double z )
{
if ( lim < 0 ) return ;
if ( lim >= 64 ) return ;
SK[ lim ].fill( x , y , z ) ;
}
double pend( double fase , double amp )
{
return sin( fase * PI / 180 ) * amp ;
}
d3d spot( d3d q )
{
return V[ number ] * q ;
}
struct Matrial
{
d3d diffuse ;
double reflection ;
} ;
Matrial material ;
class Sphere
{
public :
d3d center ;
double radius , radius2 ;
Matrial mat ;
void fill( d3d c , double r )
{
center = spot( c ) ;
radius = r ;
radius2 = r * r ;
mat = material ;
}
double hit( d3d o , d3d d )
{
double t , a , b , c , disc ;
d3d temp = o - center ;
a = dot( d , d ) ;
b = 2 * dot( temp , d ) ;
c = dot( temp , temp ) - radius2 ;
disc = b * b - 4 * a * c ;
if ( disc < 0 )
return INFINITY;
else
{
double e = sqrt( disc ) ;
double demon = 2 * a ;
t = ( -b - e ) / demon ;
if ( t > SMALL ) return t ;
t = ( -b + e ) / demon ;
if ( t > SMALL ) return t ;
}
return INFINITY ;
}
} ;
Sphere spheres[ 100 ] ;
int spheretel ;
void sphere( double x , double y , double z
, double r , d3d color )
{
d3d pt ;
pt.fill( x , y , z ) ;
material.diffuse = color ;
spheres[ spheretel ].fill( pt , r ) ;
spheretel++ ;
}
d3d light;
d3d render( d3d o , d3d d , int depth )
{
double dist , sphdist = INFINITY ;
int i , isph = -1 ;
for ( i = 0 ; i < spheretel ; i++ )
{
dist = spheres[ i ].hit( o , d ) ;
if ( dist < sphdist )
{
sphdist = dist ;
isph = i ;
}
}
if (isph == -1) return BLACK ;
d3d p , n ;
p = o + d * sphdist ;
n = p - spheres[ isph ].center ;
double a = getAngle( n , light ) ;
d3d color = spheres[ isph ].mat.diffuse ;
color = color * ( 0.5 + cos( a * 2 ) / 2 ) ;
if ( a > PI / 2 ) color = BLACK ;
for ( i = 0 ; i < spheretel ; i++ )
{
if ( i != isph )
{
a = spheres[ i ].hit( p , light ) ;
if ( a == INFINITY ) color = BLACK ;
}
}
return color ;
}
const int arm = 1 ;
const int elbow = 2 ;
const int wrist = 3 ;
const int leg = 4 ;
const int knee = 5 ;
const int enkle = 6 ;
const int neck = 7 ;
const int eye = 8 ;
const int wenk = 9 ;
const int tail = 10 ;
const int thumb = 11 ;
const int index = 14 ;
const int midle = 17 ;
const int ring = 20 ;
const int lr = 32 ;
void Kop( int qq , d3d kl )
{
link( 15, 0, 0, 0, 0, 0, 0, xyz, number ) ;
sphere( 0, 0, 0, 30, kl ) ;
if ( qq == 1 )
{
sphere( 25, 25, 0, 9, kl ) ;
sphere( -25, 25, 0, 9, kl ) ;
sphere( 0, 0, -40, 12, GRAY ) ;
}
else
{
sphere( 30, 0, 0, 9, kl ) ;
sphere( -30, 0, 0, 9, kl ) ;
sphere( 0, 0, -40, 12, kl ) ;
}
child( 16, 14, 14, -14, eye , xyz, 15 ) ;
sphere( 0, 0, 0, 13, WHITE ) ;
sphere( 0, 0, -10, 7, BLACK ) ;
child( 16, -14, 14, -14, eye + lr, xyz, 15 ) ;
sphere( 0, 0, 0, 13, WHITE ) ;
sphere( 0, 0, -10, 7, BLACK ) ;
}
void human( trui as d3d , broek as d3d )
{
link( 9 , 0,0,0 , 0,0,0 , xyz , number ) ;
sphere( 0, 50, 0, 30, trui ) ;
sphere( 0, 25, 0, 23, broek ) ;
sphere( 0, 0, 0, 15, broek ) ;
child( 11, 0, 70, 0, neck, xyz, 9 ) ;
child( 12, 0, 30, 0, neck+lr, xyz, 11 ) ;
Kop( 0, PINK ) ;
child( 11, 20, -10, 0, leg, yzx, 9 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0, 16, broek ) ;
child( 12, 0, -40, 0, knee, xyz, 11 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0, 16, broek ) ;
child( 13, 0, -40, 0, enkle, xzy, 12 ) ;
sphere( 0, 0, 0, 12, GRAY ) ;
sphere( 0, 0, -20, 12, GRAY ) ;
child( 11, -20, -10, 0, leg+lr , yzx, 9 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0, 16, broek ) ;
child( 12, 0, -40, 0, knee+lr, xyz, 11 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0,16, broek ) ;
child( 13, 0, -40, 0,enkle+lr, xzy, 12 ) ;
sphere( 0, 0, 0, 12, GRAY ) ;
sphere( 0, 0, -20, 12, GRAY ) ;
child( 11, 40, 60, 0, arm, xzy, 9 ) ;
sphere( 0, 0, 0, 16, trui ) ;
sphere( 6, -20, 0, 12, trui ) ;
child( 12, 6, -40, 0, elbow, xyz, 11 ) ;
sphere( 0, 0, 0, 12, trui ) ;
sphere( 0, -20, 0, 12, trui ) ;
sphere( 0, -42, 0, 8, PINK ) ;
child( 11, -40, 60, 0, arm+lr, xzy, 9 ) ;
sphere( 0, 0, 0, 16, trui ) ;
sphere( -6, -20, 0, 12, trui ) ;
child( 12, -6, -40, 0, elbow+lr, xyz, 11 ) ;
sphere( 0, 0, 0, 12, trui ) ;
sphere( 0, -20, 0, 12, trui ) ;
sphere( 0, -42, 0, 8, PINK ) ;
}
void human_walk( f as double , a as double )
{
skelet( arm , pend( f , a ) , 0 , 0 ) ;
skelet( elbow , -abs( a ) , 0 , 0 ) ;
skelet( arm + lr , pend( f + 180, a ) , 0 , 0 ) ;
skelet( elbow + lr , -abs( a ) , 0 , 0 ) ;
skelet( leg , pend( f + 180 , a ) , 0 , 0 ) ;
skelet( knee , pend( f + 90 , a ) + a , 0 , 0 ) ;
skelet( leg + lr , pend( f , a ) , 0 , 0 ) ;
skelet( knee + lr , pend( f - 90 , a ) + a , 0 , 0 ) ;
}
void frameName ( TCHAR* buffer ,
int bufsize ,
const TCHAR* nameStart ,
const TCHAR* ext ,
int no , int len )
{
char fmtstring[ 32 ] ;
sprintf_s( fmtstring , _countof( fmtstring ) ,
"%s%i%s" , "%s%0" , len , "i.%s" ) ;
_stprintf_s( buffer , bufsize ,
fmtstring , nameStart , no , ext ) ;
}
int CaptureAnImage( HWND hWnd , char* filename )
{
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(hWnd);
hdcMemDC = CreateCompatibleDC(hdcWindow);
if (!hdcMemDC)
{
MessageBox(hWnd, _T("CreateCompatibleDC has failed"), _T("Failed"), MB_OK);
goto done;
}
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetStretchBltMode(hdcWindow, HALFTONE);
if (!StretchBlt(hdcWindow,
0, 0,
rcClient.right, rcClient.bottom,
hdcScreen,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
SRCCOPY))
{
MessageBox(hWnd, _T("StretchBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
if (!hbmScreen)
{
MessageBox(hWnd, _T("CreateCompatibleBitmap Failed"), _T("Failed"), MB_OK);
goto done;
}
SelectObject(hdcMemDC, hbmScreen);
if (!BitBlt(hdcMemDC,
0, 0,
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hdcWindow,
0, 0,
SRCCOPY))
{
MessageBox(hWnd, _T("BitBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL, hdcScreen);
ReleaseDC(hWnd, hdcWindow);
return 0;
}
LRESULT CALLBACK WndProc( HWND hwnd ,
UINT Message ,
WPARAM wParam ,
LPARAM lParam )
{
HDC hdc ;
PAINTSTRUCT ps ;
switch (Message)
{
case WM_PAINT :
{
int x , y , picno ;
d3d o , d ;
unsigned long color ;
hdc = BeginPaint( hwnd , &ps ) ;
spheretel = 0 ;
light.fill( -1.0 , 1.0 , -1.0 ) ;
link( 1 , 0.0 , 100 - winy / 2 , 0.0 ,
30.0 , 0.0 , 0.0 , XYZ , 0 ) ;
human( YELLOW , BLUE ) ;
sphere( 0.0 , -INFINITY - winy / 2 , 0.0
, INFINITY , ( WHITE + GREEN ) / 2 ) ;
for ( x = 0 ; x < winx ; x++ )
{
for ( y = 0 ; y < winy ; y++ )
{
o.fill( 0.0 , 0.0 , -1000.0 ) ;
d.fill( (double)x , (double)y , 1000.0 ) ;
color = render( o , d , 7 ).toColor() ;
SetPixel( hdc , x , y , color ) ;
}
}
EndPaint( hwnd , &ps ) ;
break ;
}
case WM_DESTROY :
{
PostQuitMessage( 0 ) ;
break ;
}
default :
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0 ;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("WindowClass"), _T("Caption"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
cxiu diferas el tio respondas cxio samvaloras
[ Esperanto : everybody is different conclusion everybody is equal ]
|
|
|
|
|
update :
i got most of the error's removed
2 error remains :
i got a back picture on screen
i can't find 'black.bmp' on my pc
#include <windows.h>
#include <string>
#include <math.h>
#include <tchar.h>
const int winx = 800 ;
const int winy = 600 ;
#define SMALL 1e-300
#define PI 3.14159265358979323846
class d3d
{
public :
double x , y , z ;
d3d()
{
x = 0.0 ;
y = 0.0 ;
z = 0.0 ;
}
d3d( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
void fill( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
unsigned long toColor()
{
return (unsigned long)
( floor( x * 255 ) +
floor( y * 255 ) * 256 +
floor( z * 255 ) * 256 * 256 ) ;
}
} ;
d3d operator + ( d3d a , d3d b )
{
return d3d( a.x + b.x ,
a.y + b.y ,
a.z + b.z ) ;
}
d3d operator - ( d3d a , d3d b )
{
return d3d( a.x - b.x ,
a.y - b.y ,
a.z - b.z ) ;
}
d3d operator / ( d3d a , double b )
{
return d3d( a.x / b ,
a.y / b ,
a.z / b ) ;
}
d3d operator * ( d3d a , double b )
{
return d3d( a.x * b ,
a.y * b ,
a.z * b ) ;
}
double dot( d3d a , d3d b )
{
return a.x * b.x
+ a.y * b.y
+ a.z * b.z ;
}
double length( d3d a )
{
return sqrt( dot( a , a ) ) ;
}
d3d normalize( d3d a )
{
return a / length( a ) ;
}
double getAngle( d3d a , d3d b )
{
double d , la , lb ;
d = dot( a , b ) ;
la = length( a ) ;
lb = length( b ) ;
return acos( d / ( la * lb ) ) ;
}
d3d cross( d3d a , d3d b )
{
return d3d( a.y * b.z - a.z * b.y ,
a.z * b.x - a.x * b.z ,
a.x * b.y - a.y * b.x ) ;
}
const d3d BLACK = d3d( 0.0 , 0.0 , 0.0 ) ;
const d3d RED = d3d( 1.0 , 0.0 , 0.0 ) ;
const d3d GREEN = d3d( 0.0 , 1.0 , 0.0 ) ;
const d3d YELLOW = d3d( 1.0 , 1.0 , 0.0 ) ;
const d3d BLUE = d3d( 0.0 , 0.0 , 1.0 ) ;
const d3d MAGENTA = d3d( 1.0 , 0.0 , 1.0 ) ;
const d3d CYAN = d3d( 0.0 , 1.0 , 1.0 ) ;
const d3d WHITE = d3d( 1.0 , 1.0 , 1.0 ) ;
const d3d GRAY = WHITE / 2.0 ;
const d3d ORANGE = ( YELLOW + RED ) / 2.0 ;
const d3d PINK = ( WHITE + RED ) / 2.0 ;
class m44
{
public :
double m[ 4 ][ 4 ] ;
m44()
{
int i , j ;
for ( i = 0 ; i < 4 ; i++ )
{
for ( j = 0 ; j < 4 ; j++ )
m[ i ][ j ] = 0.0 ;
m[ i ][ i ] = 1.0 ;
}
}
~m44()
{
}
void translate( double x , double y , double z )
{
m[ 3 ][ 0 ] = x ;
m[ 3 ][ 1 ] = y ;
m[ 3 ][ 2 ] = z ;
}
void rotate( double deg , int ax )
{
int a = 1 , b = 2 ;
if ( ax == 1 )
{
a = 0 ;
b = 2 ;
}
if ( ax == 2 )
{
a = 0 ;
b = 1 ;
}
m[ a ][ a ] = cos( deg * PI / 180 ) ;
m[ a ][ b ] = -sin( deg * PI / 180 ) ;
m[ b ][ a ] = sin( deg * PI / 180 ) ;
m[ b ][ b ] = cos( deg * PI / 180 ) ;
}
} ;
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 ;
}
d3d operator * ( m44 m , d3d v )
{
return d3d( m.m[0][0]*v.x+m.m[1][0]*v.y+m.m[2][0]+m.m[3][0]
, m.m[0][1]*v.x+m.m[1][1]*v.y+m.m[2][1]+m.m[3][1]
, m.m[0][2]*v.x+m.m[1][2]*v.y+m.m[2][2]+m.m[3][2]);
}
m44 inverse( m44 q )
{
double d ;
m44 uit ;
d = q.m[0][0]*q.m[1][1]*q.m[2][2]
- q.m[0][0]*q.m[2][1]*q.m[1][2]
+ q.m[1][0]*q.m[2][1]*q.m[0][2]
- q.m[1][0]*q.m[0][1]*q.m[2][2]
+ q.m[2][0]*q.m[0][1]*q.m[1][2]
- q.m[2][0]*q.m[1][1]*q.m[0][2] ;
uit.m[0][0]=(q.m[1][1]*q.m[2][2]-q.m[1][2]*q.m[2][1])/d;
uit.m[1][0]=(q.m[0][1]*q.m[2][2]-q.m[0][2]*q.m[2][1])/d;
uit.m[2][0]=(q.m[0][1]*q.m[1][2]-q.m[0][2]*q.m[1][1])/d;
uit.m[0][1]=(q.m[1][0]*q.m[2][2]-q.m[1][2]*q.m[2][0])/d;
uit.m[1][1]=(q.m[0][0]*q.m[2][2]-q.m[0][2]*q.m[2][0])/d;
uit.m[2][1]=(q.m[0][0]*q.m[1][2]-q.m[0][2]*q.m[1][0])/d;
uit.m[0][2]=(q.m[1][0]*q.m[2][1]-q.m[1][1]*q.m[2][0])/d;
uit.m[1][2]=(q.m[0][0]*q.m[2][1]-q.m[0][1]*q.m[2][0])/d;
uit.m[2][2]=(q.m[0][0]*q.m[1][1]-q.m[0][1]*q.m[1][2])/d;
return uit ;
}
d3d SK[ 64 ] ;
m44 V[ 20 ] ;
int number ;
const int xyz = 0 ;
const int xzy = 1 ;
const int yxz = 2 ;
const int yzx = 3 ;
const int zxy = 4 ;
const int zyx = 5 ;
void link( int no , double x , double y , double z
, double pan , double tilt , double rol , int ax , int p )
{
if ( no < 1 ) return ;
if ( no >= 20 ) return ;
if ( p < 0 ) return ;
if ( p >= 20 ) return ;
if ( p == no ) return ;
m44 trans , rotx , roty , rotz ;
trans.translate( x , y , z ) ;
rotx.rotate( tilt , 0 ) ;
roty.rotate( pan , 1 ) ;
rotz.rotate( rol , 2 ) ;
switch( ax )
{
case xyz :
V[ no ] = rotx * roty * rotz * trans * V[ p ] ;
break ;
case xzy :
V[ no ] = rotx * rotz * roty * trans * V[ p ] ;
break ;
case yxz :
V[ no ] = roty * rotx * rotz * trans * V[ p ] ;
break ;
case yzx :
V[ no ] = roty * rotz * rotx * trans * V[ p ] ;
break ;
case zxy :
V[ no ] = rotz * rotx * roty * trans * V[ p ] ;
break ;
case zyx :
V[ no ] = rotz * roty * rotx * trans * V[ p ] ;
break ;
default :
V[ no ] = rotx * roty * rotz * trans * V[ p ] ;
}
number = no ;
}
void child( int no , double x , double y , double z
, int lim , int ax , int p )
{
if ( lim < 0 ) return ;
if ( lim >= 64 ) return ;
link( no , x , y , z
, SK[ lim ].y , SK[ lim ].x , SK[ lim ].z
, ax , p ) ;
}
void skelet( int lim , double x , double y , double z )
{
if ( lim < 0 ) return ;
if ( lim >= 64 ) return ;
SK[ lim ].fill( x , y , z ) ;
}
double pend( double fase , double amp )
{
return sin( fase * PI / 180 ) * amp ;
}
d3d spot( d3d q )
{
return V[ number ] * q ;
}
struct Matrial
{
d3d diffuse ;
double reflection ;
} ;
Matrial material ;
class Sphere
{
public :
d3d center ;
double radius , radius2 ;
Matrial mat ;
void fill( d3d c , double r )
{
center = spot( c ) ;
radius = r ;
radius2 = r * r ;
mat = material ;
}
double hit( d3d o , d3d d )
{
double t , a , b , c , disc ;
d3d temp = o - center ;
a = dot( d , d ) ;
b = 2 * dot( temp , d ) ;
c = dot( temp , temp ) - radius2 ;
disc = b * b - 4 * a * c ;
if ( disc < 0 )
return INFINITY ;
else
{
double e = sqrt( disc ) ;
double demon = 2 * a ;
t = ( -b - e ) / demon ;
if ( t > SMALL ) return t ;
t = ( -b + e ) / demon ;
if ( t > SMALL ) return t ;
}
return INFINITY ;
}
} ;
Sphere spheres[ 100 ] ;
int spheretel ;
void sphere( double x , double y , double z
, double r , d3d color )
{
d3d pt ;
pt.fill( x , y , z ) ;
material.diffuse = color ;
spheres[ spheretel ].fill( pt , r ) ;
spheretel++ ;
}
d3d light;
d3d render( d3d o , d3d d , int depth )
{
double dist , sphdist = INFINITY ;
int i , isph = -1 ;
for ( i = 0 ; i < spheretel ; i++ )
{
dist = spheres[ i ].hit( o , d ) ;
if ( dist < sphdist )
{
sphdist = dist ;
isph = i ;
}
}
if (isph == -1) return BLACK ;
d3d p , n ;
p = o + d * sphdist ;
n = p - spheres[ isph ].center ;
double a = getAngle( n , light ) ;
d3d color = spheres[ isph ].mat.diffuse ;
color = color * ( 0.5 + cos( a * 2 ) / 2 ) ;
if ( a > PI / 2 ) color = BLACK ;
for ( i = 0 ; i < spheretel ; i++ )
{
if ( i != isph )
{
a = spheres[ i ].hit( p , light ) ;
if ( a == INFINITY ) color = BLACK ;
}
}
return color ;
}
const int arm = 1 ;
const int elbow = 2 ;
const int wrist = 3 ;
const int leg = 4 ;
const int knee = 5 ;
const int enkle = 6 ;
const int neck = 7 ;
const int eye = 8 ;
const int wenk = 9 ;
const int tail = 10 ;
const int thumb = 11 ;
const int index = 14 ;
const int midle = 17 ;
const int ring = 20 ;
const int lr = 32 ;
void Kop( int qq , d3d kl )
{
link( 15, 0, 0, 0, 0, 0, 0, xyz, number ) ;
sphere( 0, 0, 0, 30, kl ) ;
if ( qq == 1 )
{
sphere( 25, 25, 0, 9, kl ) ;
sphere( -25, 25, 0, 9, kl ) ;
sphere( 0, 0, -40, 12, GRAY ) ;
}
else
{
sphere( 30, 0, 0, 9, kl ) ;
sphere( -30, 0, 0, 9, kl ) ;
sphere( 0, 0, -40, 12, kl ) ;
}
child( 16, 14, 14, -14, eye , xyz, 15 ) ;
sphere( 0, 0, 0, 13, WHITE ) ;
sphere( 0, 0, -10, 7, BLACK ) ;
child( 16, -14, 14, -14, eye + lr, xyz, 15 ) ;
sphere( 0, 0, 0, 13, WHITE ) ;
sphere( 0, 0, -10, 7, BLACK ) ;
}
void human( d3d trui , d3d broek )
{
link( 9 , 0,0,0 , 0,0,0 , xyz , number ) ;
sphere( 0, 50, 0, 30, trui ) ;
sphere( 0, 25, 0, 23, broek ) ;
sphere( 0, 0, 0, 15, broek ) ;
child( 11, 0, 70, 0, neck, xyz, 9 ) ;
child( 12, 0, 30, 0, neck+lr, xyz, 11 ) ;
Kop( 0, PINK ) ;
child( 11, 20, -10, 0, leg, yzx, 9 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0, 16, broek ) ;
child( 12, 0, -40, 0, knee, xyz, 11 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0, 16, broek ) ;
child( 13, 0, -40, 0, enkle, xzy, 12 ) ;
sphere( 0, 0, 0, 12, GRAY ) ;
sphere( 0, 0, -20, 12, GRAY ) ;
child( 11, -20, -10, 0, leg+lr , yzx, 9 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0, 16, broek ) ;
child( 12, 0, -40, 0, knee+lr, xyz, 11 ) ;
sphere( 0, 0, 0, 16, broek ) ;
sphere( 0, -20, 0,16, broek ) ;
child( 13, 0, -40, 0,enkle+lr, xzy, 12 ) ;
sphere( 0, 0, 0, 12, GRAY ) ;
sphere( 0, 0, -20, 12, GRAY ) ;
child( 11, 40, 60, 0, arm, xzy, 9 ) ;
sphere( 0, 0, 0, 16, trui ) ;
sphere( 6, -20, 0, 12, trui ) ;
child( 12, 6, -40, 0, elbow, xyz, 11 ) ;
sphere( 0, 0, 0, 12, trui ) ;
sphere( 0, -20, 0, 12, trui ) ;
sphere( 0, -42, 0, 8, PINK ) ;
child( 11, -40, 60, 0, arm+lr, xzy, 9 ) ;
sphere( 0, 0, 0, 16, trui ) ;
sphere( -6, -20, 0, 12, trui ) ;
child( 12, -6, -40, 0, elbow+lr, xyz, 11 ) ;
sphere( 0, 0, 0, 12, trui ) ;
sphere( 0, -20, 0, 12, trui ) ;
sphere( 0, -42, 0, 8, PINK ) ;
}
void human_walk( double f , double a )
{
skelet( arm , pend( f , a ) , 0 , 0 ) ;
skelet( elbow , -abs( a ) , 0 , 0 ) ;
skelet( arm + lr , pend( f + 180, a ) , 0 , 0 ) ;
skelet( elbow + lr , -abs( a ) , 0 , 0 ) ;
skelet( leg , pend( f + 180 , a ) , 0 , 0 ) ;
skelet( knee , pend( f + 90 , a ) + a , 0 , 0 ) ;
skelet( leg + lr , pend( f , a ) , 0 , 0 ) ;
skelet( knee + lr , pend( f - 90 , a ) + a , 0 , 0 ) ;
}
void frameName ( TCHAR* buffer ,
int bufsize ,
const TCHAR* nameStart ,
const TCHAR* ext ,
int no , int len )
{
}
int CaptureAnImage( HWND hWnd , char* filename )
{
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(hWnd);
hdcMemDC = CreateCompatibleDC(hdcWindow);
if (!hdcMemDC)
{
MessageBox(hWnd, _T("CreateCompatibleDC has failed"),
_T("Failed"), MB_OK) ;
}
else
{
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetStretchBltMode(hdcWindow, HALFTONE);
if (!StretchBlt(hdcWindow,
0, 0,
rcClient.right, rcClient.bottom,
hdcScreen,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
SRCCOPY))
{
MessageBox(hWnd, _T("StretchBlt has failed"),
_T("Failed"), MB_OK) ;
}
else
{
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
if (!hbmScreen)
{
MessageBox(hWnd, _T("CreateCompatibleBitmap Failed"),
_T("Failed"), MB_OK) ;
}
else
{
SelectObject(hdcMemDC, hbmScreen);
if (!BitBlt(hdcMemDC,
0, 0,
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hdcWindow,
0, 0,
SRCCOPY))
{
MessageBox(hWnd,
_T("BitBlt has failed"),
_T("Failed"), MB_OK) ;
}
else
{
GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
}
}
}
}
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL, hdcScreen);
ReleaseDC(hWnd, hdcWindow);
return 0;
}
LRESULT CALLBACK WndProc( HWND hwnd ,
UINT Message ,
WPARAM wParam ,
LPARAM lParam )
{
HDC hdc ;
PAINTSTRUCT ps ;
switch (Message)
{
case WM_PAINT :
{
int x , y , picno ;
d3d o , d ;
unsigned long color ;
hdc = BeginPaint( hwnd , &ps ) ;
spheretel = 0 ;
light.fill( -1.0 , 1.0 , -1.0 ) ;
link( 1 , 0.0 , 100 - winy / 2 , 0.0 ,
30.0 , 0.0 , 0.0 , xyz , 0 ) ;
human( YELLOW , BLUE ) ;
sphere( 0.0 , -INFINITY - winy / 2 , 0.0
, INFINITY , ( WHITE + GREEN ) / 2 ) ;
for ( x = 0 ; x < winx ; x++ )
{
for ( y = 0 ; y < winy ; y++ )
{
o.fill( 0.0 , 0.0 , -1000.0 ) ;
d.fill( (double)x , (double)y , 1000.0 ) ;
color = render( o , d , 7 ).toColor() ;
SetPixel( hdc , x , y , color ) ;
}
}
int dummy = CaptureAnImage( hwnd , _T( "black.bmp" ) ;
EndPaint( hwnd , &ps ) ;
break ;
}
case WM_DESTROY :
{
PostQuitMessage( 0 ) ;
break ;
}
default :
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0 ;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("WindowClass"), _T("Caption"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
Hi,
I have a Class, which contains as member another class. This latter contained class has no default constructor, but requires a Parameter in it's constructor. How do I call this latter constructor in the constructor of the first class. This is a General CPP Question, but it is a bit abstract, so I give a Concrete example:
For instance:
I have a class derived from CDialog. It contains four instances of a custom class CDragBar. The CDragBar class in it's constructor determines whether it is horizontal or vertical. (one cannot have an indeterminate one) In this case I know ways around this conundrum,(e.g. Provide a default constructor for the CDragBar class, and provide methods like SetVertical() and SetHorizontal(), to be called in the 'CDialog derived classes constructor') but wonder is there a CPP syntax that could be used instead. The example I gave is in a way quite trivial. The Colon separated Constructor List only works for base class constructors of the main classes constructor in Microsoft Compilers, and not for contained member classes.
Anyone any ideas? Even if the idea is that no such syntax exists.
Kind Regards,
Bram van Kampen
modified 28-Sep-17 17:24pm.
|
|
|
|
|
Do you mean something like
#include <iostream>
using namespace std;
class A
{
int _i;
public:
A(int i):_i(i){}
int get() const {return _i;}
};
class B
{
A _a;
public:
B():_a(10){}
B(int i):_a(i){}
const A & get(){ return _a;}
};
int main()
{
B b0;
B b1(1);
cout << b0.get().get() << ", " << b1.get().get() << endl;
}
|
|
|
|
|
Well,
The issue is very similar to that example.
I note the curly braces and the absence of a terminating semi colon.
What is the reasoning behind the syntax.
Does that syntax intentionally leave out the terminating semi colon, or, is that an oversight?
Going to try that!
Thanks and Regards
Bram van Kampen
|
|
|
|
|
.
modified 24-Nov-17 13:55pm.
|
|
|
|
|
Can you solve it on a piece of paper? While many could, no one is going to write the code for you. Give it a shot, and if you have specific questions, people here are usually more than willing to help.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment
"Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst
"I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
|
|
|
|
|
Have you considered FC from a command prompt?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
If you are looking for a solution and not just to code it then you can google for an open source software called winmerge. It allows for a command line option to produce a diff and provides for UI view as well as various configuration options.
There are other tools as well.
|
|
|
|
|
.
-- modified 24-Nov-17 13:56pm.
|
|
|
|
|
Member 13433022 wrote: regardless of the order in which those paragraphs are placed in the two different files
With that requirement I don't know any existing tool because those usually try to find the next identical sequence.
But writing such a tool for your requirement is not very difficult:
- Read file 2 completely into allocated memory (one more byte to append a NULL char)
- Count the number of lines (number of LF chars)
- Allocate a buffer for
char* pointers to hold the pointers to the beginning of each line
- Fill the line pointer buffer (search for next LF and store address plus one)
- Read file 1 line by line and check if that line matches any line from the file 2 buffer
- If there is no match, write the line to file 3
|
|
|
|
|
Message Closed
modified 24-Nov-17 14:01pm.
|
|
|
|
|