1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
//*
composition.cpp
simple animation using a composition transformation matrix
*/
#include <stdio.h>
#include <windows.h>
#include "resource.h"
// your path for this include may vary
#include "GraphicsFramework.h"
#include "gmath.h"
// Global variable to store the graphics framwork object
GraphicsFramework* PGraphics;
HWND HOutput = 0; // handle to the output control
HWND HDialog = 0;
// function to draw a line between two points
void DrawLine(int x1, int y1, int x2, int y2, unsigned int color) {
int dx, dy; // dy / dx is the slope
int x, y; // loop and point variables
// calculate changes in y and x between the points
dy = y2 - y1;
dx = x2 - x1;
if (Abs(dy) > Abs(dx)) {
// since there is a greater change in y than x we must
// loop in y, calculate x and draw
for (y=y1; y != y2; y += Sign(dy)) {
x = x1 + (y - y1) * dx / dy;
PGraphics->AddPoint(x, y, color);
}
}
else {
// since there is a greater (or equal) change in x than y we must
// loop in x, calculate y and draw
for (x=x1; x != x2; x += Sign(dx)) {
y = y1 + (x - x1) * dy / dx;
PGraphics->AddPoint(x, y, color);
}
}
// draw the last pixel
PGraphics->AddPoint(x2, y2, color);
}
void DrawStuff() {
COLORREF green = RGB(0, 255, 0); // green color to draw with
COLORREF red = RGB(0, 0, 255); // red color to draw with
char str[32]; // string to store user input
Vector3 pts[3]; // original data points
Vector3 newPts[3]; // transformed points
double angle; // rotation angle
Matrix4 rz,tIn, tOut; // current rotation matrix about z axis
// this composite matrix need to remember its value between calls to this draw function
// so it must be declared static or made a global variable
static Matrix4 c; // composition matrix
// set up the original points for the triangle
pts[0].set( 50, 50, 0);
pts[1].set(100, 100, 0);
pts[2].set(100, 0, 0);
// clear the scene and add an axis
PGraphics->ClearScene(RGB(0, 0, 0));
PGraphics->AddAxis(RGB(150, 150, 150), 10);
// get the user input from the edit boxes and
// convert string input to double
GetDlgItemText(HDialog, IDC_EDIT_ANGLE, str, 32);
angle = atof(str);
// make the current rotation matrix
rz.makeRotationMatrixZ(angle);
tIn.makeTranslationMatrix(50, 50, 0);
tOut.makeTranslationMatrix(-50, -50, 0);
// update the composite matrix - remember we must pre-multiply by m: c = m x c
c = Multiply(rz, c);
// transform the original triangle points into the new points for drawing
for (int i=0; i < 3; i++) {
newPts[i] = Multiply(c, pts[i]);
}
// draw the triangle lines 0-1, 1-2, 2-0
DrawLine(newPts[0].x, newPts[0].y, newPts[1].x, newPts[1].y, green);
DrawLine(newPts[1].x, newPts[1].y, newPts[2].x, newPts[2].y, green);
DrawLine(newPts[2].x, newPts[2].y, newPts[0].x, newPts[0].y, green);
// draw the points
PGraphics->Draw();
}
/*
DialogProc
this is the window event handler for the main dialog
*/
BOOL CALLBACK DialogProc (HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
// dialog is initializing - store the picture box handle in a global variable for later
HOutput = GetDlgItem(hwnd, IDC_PICTURE_OUTPUT);
// instantiate and initialize our graphics framework object
PGraphics = new GraphicsFramework(HOutput);
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDC_BTN_DRAW:
// draw button was pressed
DrawStuff();
break;
case IDC_BTN_CLEAR:
// clear button was pressed so clear the scene and draw the empty scene
PGraphics->ClearScene(RGB(0, 0, 0));
PGraphics->Draw();
break;
case IDCANCEL:
// user is quitting so release the GraphicsFramework object and quit
delete PGraphics;
PostQuitMessage(0);
break;
}
}
return FALSE;
}
// this is the main function that starts the application
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, char * cmdParam, int cmdShow)
{
// create the main window
// store its handle in a global if needed
HDialog = CreateDialog (GetModuleHandle(NULL),
MAKEINTRESOURCE(IDD_DIALOG1),
0,
DialogProc);
// make the dialog visible
ShowWindow(HDialog, SW_SHOW);
// standard windows message loop
MSG msg;
int status;
while ((status = GetMessage (&msg, 0, 0, 0)) != 0)
{
if (status == -1)
return -1;
// avoid processing messages for the dialog
if (!IsDialogMessage (HDialog, & msg))
{
TranslateMessage ( & msg );
DispatchMessage ( & msg );
}
}
return (int)(msg.wParam);
}
|