i m trying as coding exersise
a verry simple VR system
error :
the picture isn't Always right
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <vector>
#include <math.h>
int winx , winy ;
class d3d
{
public :
double x , y , z ;
d3d()
{
x = 0.0 ;
y = 0.0 ;
z = 0.0 ;
}
d3d( double a , double b , double c )
{
x = a ;
y = b ;
z = c ;
}
} ;
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 pnt[256] ;
void setpoint( int no ,
double a ,
double b ,
double c )
{
pnt[ no ] = d3d(a,b,c) ;
}
class triangle
{
public :
d3d punt[ 3 ] , mid ;
COLORREF color ;
triangle()
{
;
}
triangle( d3d p1 , d3d p2 , d3d p3 ,
COLORREF kl )
{
punt[ 0 ] = p1 ;
punt[ 1 ] = p2 ;
punt[ 2 ] = p3 ;
mid = ( p1 + p2 + p3 ) / 3 ;
color = kl ;
}
void show( HDC hdc )
{
POINT p[ 3 ] ;
int i ;
if ( mid.z > -900)
{
for ( i = 0 ; i < 3 ; i++ )
{
p[i].x=(LONG)(winx/2+punt[i].x/
(punt[i].z+1000.0)*1000) ;
p[i].y=(LONG)(winy/2-punt[i].y/
(punt[i].z+1000.0)*1000) ;
}
HBRUSH brush = CreateSolidBrush( color ) ;
HPEN pen = CreatePen( PS_SOLID , 0 , color ) ;
SelectObject( hdc , pen ) ;
SelectObject( hdc , brush ) ;
Polygon( hdc , p , 3 ) ;
SelectObject( hdc , pen ) ;
DeleteObject( pen ) ;
SelectObject( hdc , brush ) ;
DeleteObject( brush ) ;
}
}
} ;
const COLORREF red = RGB( 255 , 0 , 0 ) ;
const COLORREF green = RGB( 0 , 255 , 0 ) ;
const COLORREF yellow = RGB( 255 , 255 , 0 ) ;
const COLORREF blue = RGB( 0 , 0 , 255 ) ;
const COLORREF magenta = RGB( 255 , 0 , 255 ) ;
const COLORREF cyan = RGB( 0 , 255 , 255 ) ;
bool key[ 256 ] ;
triangle triangles[100] ;
int tritel ;
#define FRAME_TIMER 1
void show_al( HDC hdc )
{
int i , j ;
triangle h ;
for ( i = 1 ; i < tritel ; i++ )
{
for ( j = 0 ; j < i - 1 ; j++ )
{
if ( triangles[i].mid.z
> triangles[j].mid.z )
{
h = triangles[i] ;
triangles[i] = triangles[j] ;
triangles[j] = h ;
}
}
}
for ( i = 0 ; i < tritel ; i++ )
triangles[i].show( hdc ) ;
}
void tri( int p1 , int p2 , int p3 , COLORREF kl )
{
if ( tritel >= 100 ) return ;
triangles[ tritel ] = triangle(pnt[p1],
pnt[p2],
pnt[p3],
kl) ;
tritel++ ;
}
void quad( int p1 , int p2 , int p3 , int p4
, COLORREF kl )
{
d3d p = ( pnt[p1]+pnt[p2]+pnt[p3]+pnt[p4])/4;
setpoint( 255 , p.x,p.y,p.z ) ;
tri( 255 , p1 , p2 , kl ) ;
tri( 255 , p2 , p3 , kl ) ;
tri( 255 , p3 , p4 , kl ) ;
tri( 255 , p4 , p1 , kl ) ;
}
d3d rotatey( d3d a , double b )
{
double s = sin( b ) , c = cos( b ) , x , z ;
x = c * a.x - s * a.z ;
z = s * a.x + c * a.z ;
return d3d( x , a.y , z ) ;
}
triangle roty( triangle t , double b )
{
t.mid = rotatey( t.mid , b ) ;
t.punt[2] = rotatey( t.punt[2] , b ) ;
t.punt[1] = rotatey( t.punt[1] , b ) ;
t.punt[0] = rotatey( t.punt[0] , b ) ;
return t ;
}
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd;
MSG messages;
WNDCLASSEX wincl;
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure;
wincl.style = CS_DBLCLKS;
wincl.cbSize = sizeof (WNDCLASSEX);
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL;
wincl.cbClsExtra = 0;
wincl.cbWndExtra = 0;
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
if (!RegisterClassEx (&wincl))
return 0;
hwnd = CreateWindowEx (
0,
szClassName,
_T("Rotating cube 1.0"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
HWND_DESKTOP,
NULL,
hThisInstance,
NULL
);
ShowWindow (hwnd, nCmdShow);
while (GetMessage (&messages, NULL, 0, 0))
{
TranslateMessage(&messages);
DispatchMessage(&messages);
}
return messages.wParam;
}
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int t ;
PAINTSTRUCT paint ;
HDC hdc ;
RECT rect ;
GetClientRect( hwnd , &rect ) ;
switch (message)
{
case WM_CREATE :
tritel = 0 ;
setpoint( 0 , 100 , 100 , 100 ) ;
setpoint( 1 , 100 , 100 , -100 ) ;
setpoint( 2 , 100 , -100 , 100 ) ;
setpoint( 3 , 100 , -100 , -100 ) ;
setpoint( 4 , -100 , 100 , 100 ) ;
setpoint( 5 , -100 , 100 , -100 ) ;
setpoint( 6 , -100 , -100 , 100 ) ;
setpoint( 7 , -100 , -100 , -100 ) ;
quad( 0 , 1 , 3 , 2 , red ) ;
quad( 7 , 6 , 4 , 5 , cyan ) ;
quad( 0 , 1 , 5 , 4 , green ) ;
quad( 7 , 6 , 2 , 3 , magenta ) ;
quad( 0 , 2 , 6 , 4 , blue ) ;
quad( 7 , 5 , 1 , 3 , yellow ) ;
SetTimer(hwnd,FRAME_TIMER,40,NULL) ;
break ;
case WM_TIMER :
if ( wParam == FRAME_TIMER )
{
for ( t = 0 ; t < tritel ; t++ )
{
triangles[t] = roty(triangles[t],.1);
}
InvalidateRect( hwnd , NULL , true ) ;
}
break ;
case WM_KEYDOWN :
if ( wParam == VK_ESCAPE )
PostQuitMessage( 0 ) ;
break ;
case WM_PAINT :
GetClientRect( hwnd , &rect ) ;
hdc = BeginPaint( hwnd , &paint ) ;
winx = rect.right ;
winy = rect.bottom ;
show_al( hdc ) ;
EndPaint( hwnd , &paint ) ;
break ;
case WM_DESTROY :
PostQuitMessage( 0 ) ;
break ;
default :
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0 ;
}
|