rendered paste body//trojkat sierpinskiego - Krzysztof Bielec
/**************************
* Includes
*
**************************/
#include <windows.h>
#include <gl/gl.h>
#include <math.h>
float xo=0, yo=0, zo=0;
/**************************/
void obrot (float x[], float y[],int k) //obrot wokol osi
{
float a,b;
for (int i=0;i<3;i++)
{
a = xo + (x[i]-xo)*cos(k*0.006) + (y[i]-yo)*sin(k*0.006);
b = yo - (x[i]-xo)*sin(k*0.006) + (y[i]-yo)*cos(k*0.006);
x[i]=a;
y[i]=b;
}
}
/**************************/
int rys(float x1, float y1, float x2, float y2, float x3, float y3, int t)
{
if ( t == 0 )
{
glBegin(GL_POLYGON);
glColor3f( 0,1,0);
glVertex3f( x1, y1 , 0.0f);
glVertex3f( x2, y2 , 0.0f);
glVertex3f( x3, y3 , 0.0f);
glEnd();
}
else
{
rys(x1,y1, (x1+x2)/2, (y1+y2)/2, (x3+x1)/2, (y3+y1)/2, t-1);
rys(x2,y2, (x1+x2)/2, (y1+y2)/2, (x3+x2)/2, (y3+y2)/2, t-1);
rys(x3,y3, (x3+x2)/2, (y3+y2)/2, (x3+x1)/2, (y3+y1)/2, t-1);
}
}
/**************************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
int rys(float x1, float y1, float x2, float y2, float x3, float y3, int t);
void obrot(float a[], float b[],int s);
int i = 0;
bool keys[256];
/**************************
* WinMain
*
**************************/
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);
/* create main window */
hWnd = CreateWindow (
"GLSample", "OpenGL Sample",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 750, 750,
NULL, NULL, hInstance, NULL);
/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);
/* program main loop */
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
// 1 2 3
float x[3]={-0.6, 0.6, 0.0 };
float y[3]={-0.5, -0.5, 0.55 };
float z[3]={0.0, 0.0, 0.0 };
int k=1;
while (!bQuit)
{
/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
// if(keys['U']) // czy wciśnięty jest ESC
{
obrot(z,y,1);
}
if(keys['J']) // czy wciśnięty jest ESC ?
{
obrot(z,y,-1);
}
//if(keys['I']) // czy wciśnięty jest ESC ?
{
obrot(x,z,1);
}
if(keys['K']) // czy wciśnięty jest ESC ?
{
obrot(x,z,-1);
}
//if(keys['O']) // czy wciśnięty jest ESC ?
{
obrot(x,y,1);
}
if(keys['L']) // czy wciśnięty jest ESC ?
{
obrot(x,y,-1);
}
if(keys['A'])
{
k++;
Sleep(200);
}
else if(keys['Z'])
{
if (k>0)
{
k--;
Sleep(200);
}
}
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
rys(x[0],y[0], x[1],y[1], x[2],y[2], k );
SwapBuffers (hDC);
}
}
//*********************************************************************************************
/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);
/* destroy the window explicitly */
DestroyWindow (hWnd);
return msg.wParam;
}
/********************
Standardowe proceduty windowsa
********************/
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_DESTROY:
return 0;
case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
default:
keys[wParam]=true; // Odpowiednie pole zostaje ustawione
return 0; // skocz dalej
}
case WM_KEYUP: // Czy klawisz został wciśnięty
{
keys[wParam]=false; // Odpowiednie pole zostaje ustawione na false
return 0; // skocz dalej
}
return 0;
default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
/*******************
* Enable OpenGL
*
*******************/
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
/* get the device context (DC) */
*hDC = GetDC (hWnd);
/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
glClearDepth(1.0f); // ustawienie bufora głebi
glEnable(GL_DEPTH_TEST); // włączenie testowania głębi
glDepthFunc(GL_LEQUAL); // ustawienie typu testowania
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
/******************
* Disable OpenGL
*
******************/
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}