blua tigro
Gebruiker
- Lid geworden
- 21 apr 2009
- Berichten
- 48
dit is een codering oefening
ik probeer een simple VR systeempje te coderen
je zou n roterende kleuren kubus moeten zien
ik wil echter dubbel buffeering
zodat ik geen 'flitsen' meer krijg
hoe doe ik dat ?
ik probeer een simple VR systeempje te coderen
je zou n roterende kleuren kubus moeten zien
ik wil echter dubbel buffeering
zodat ik geen 'flitsen' meer krijg
hoe doe ik dat ?
Code:
// bluatigro 31 okt 2017
// win triangle
#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 ) ;
} // end if mid.z > -900
}
} ;
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 ;
}
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");
int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nCmdShow)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);
/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("Rotating cube 1.0"), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
800, /* The programs width */
600, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
/* Make the window visible on the screen */
ShowWindow (hwnd, nCmdShow);
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int t ;
PAINTSTRUCT paint ;
HDC hdc ;
RECT rect ;
GetClientRect( hwnd , &rect ) ;
switch (message) /* handle the messages */
{
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 ) ; /* send a WM_QUIT to the message queue */
break ;
default : /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}
return 0 ;
}