Главная страница

Курсовой проект 106 страниц, 42 рисунка, 2 таблицы, 18 фомул, 15 матриц, 4 приложения


Скачать 5.65 Mb.
НазваниеКурсовой проект 106 страниц, 42 рисунка, 2 таблицы, 18 фомул, 15 матриц, 4 приложения
АнкорKursach.doc
Дата17.08.2018
Размер5.65 Mb.
Формат файлаdoc
Имя файлаKursach.doc
ТипКурсовой проект
#23097
страница5 из 10
1   2   3   4   5   6   7   8   9   10

HINSTANCE hPrevInstance,

LPSTR lpCmdLine,

int nCmdShow)

{

InitMainWindow(hInstance, "КП по ГГМ [Робот-краб]", 500);

InitCommonControls();

InitPanel(hInstance);
PreSetup();

MSG m;

while(1)

{

if(PeekMessage( &m, 0, 0, 0, PM_NOREMOVE))

{

if(GetMessage(&m, 0, 0, 0))

{

TranslateMessage(&m);

DispatchMessage(&m);

}else return 0;
}
DrawScene();

}

return 0;

}
_____________camera______________
#define CAMERA_

#include
void MultMatrix(float *vect, float *matr)

{

float res[4]={0};

for(int i=0; i<4; i++)

for(int j=0; j<4; j++)

res[i]+=

vect[j]*

matr[4*j+i];

memcpy(vect, res,4*sizeof(float));

}

class CAMERA

{

public:

struct

{

float posx, posy, posz;

float dirx, diry, dirz;

float xrot, zrot;

}settings;

void GetPosition(float *x, float *y, float *z)

{

*x=settings.posx;

*y=settings.posy;

*z=settings.posz;

}

CAMERA(float PosX, float PosY, float PosZ)

{settings.posx=PosX, settings.posy=PosY, settings.posz=PosZ;

settings.dirx=0; settings.diry=0; settings.dirz=0;

settings.xrot=0;

settings.zrot=0;

}

void GetDirection(float *x, float *y, float *z)

{

*x=settings.dirx;

*y=settings.diry;

*z=settings.dirz;

}

void Dispose(float PosX, float PosY, float PosZ)

{settings.posx=PosX; settings.posy=PosY; settings.posz=PosZ;

}

void Direct(float DirX, float DirY, float DirZ)

{settings.dirx=DirX; settings.diry=DirY; settings.dirz=DirZ;

}

void SetZRot(float rot)

{

settings.zrot=rot;

return;

float pos[]={settings.posx, settings.posy, settings.posz, 1};

settings.posx=pos[0];

settings.posy=pos[1];
____________drawscense_______________
#ifndef DRAWSCENE

#define DRAWSCENE

#include "primitives.h"

void DrawAll()

{

glPushMatrix();
glScalef((ObjHeight)/15, (ObjHeight)/15, (ObjHeight)/15);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);

hsp.Draw();

glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);

sp.Draw();

cube.Draw();

prism3.Draw();

el.Draw();

cyl.Draw();

pir3.Draw();

kospr[0].Draw();

kospr[1].Draw();

glPopMatrix();

if(DrawAxes)

{

if(AxesAlwaysOnTop)

glDisable(GL_DEPTH_TEST);

glPushMatrix();

glLoadIdentity();

glDisable(GL_LIGHTING);

glBegin(GL_LINES);

glColor3f(1, 0, 0);

glVertex3f(0, 0, 0);

glVertex3f(100, 0, 0);

glColor3f(0, 1, 0);

glVertex3f(0, 0, 0);

glVertex3f(0, 100, 0);

glColor3f(0, 0, 1);

glVertex3f(0, 0, 0);

glVertex3f(0, 0, 100);

glEnd();

glEnable(GL_POINT_SMOOTH);

glPointSize(4);

glColor3f(1, 1, 1);

glBegin(GL_POINTS);

for(int i=0; i<100; i++)

{

glVertex3f(i, 0, 0);

glVertex3f(0, i, 0);

glVertex3f(0, 0, i);

}

glEnd();
glEnable(GL_LIGHTING);

glPopMatrix();

}

if(AxesAlwaysOnTop)

glEnable(GL_DEPTH_TEST);

}

int Frames=0;

int Seconds=0;

int ms=0;

void DrawScene()

{

glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glDisable(GL_LIGHTING);

SetViewport(VP_LEFTTOP, (FullScreenVP==VP_LEFTTOP)?true:false );

if(FullScreenVP==VP_LEFTTOP || FullScreenVP==-1) {DrawAll();

glColor3d(0,0,0);

OutText(0, 17.5, 19, "OYZ");

}

glDisable(GL_LIGHTING);

SetViewport(VP_RIGHTTOP, (FullScreenVP==VP_RIGHTTOP)?true:false);

if(FullScreenVP==VP_RIGHTTOP || FullScreenVP==-1) {DrawAll();

glColor3d(0,0,0);

OutText(17.5, 0, -19, "OXZ");

}

glDisable(GL_LIGHTING );

SetViewport(VP_LEFTBOTTOM, (FullScreenVP==VP_LEFTBOTTOM)?true:false);

if(FullScreenVP==VP_LEFTBOTTOM || FullScreenVP==-1) {DrawAll();

glColor3d(0,0,0);

OutText(-19, 17.5, 0, "OXY");

}

int tc=GetTickCount();

glEnable(GL_LIGHTING);

float d[3]={0};

float p[4]={20, 0, 0, 0};

SetViewport(VP_RIGHTBOTTOM, (FullScreenVP==VP_RIGHTBOTTOM)?true:false );

if(FullScreenVP==VP_RIGHTBOTTOM || FullScreenVP==-1) {DrawAll();

for(int i=0; i<4; i++)

lights[i].Draw();

}

SwapBuffers(wglGetCurrentDC());

char s[20];

Frames++;

Seconds+=GetTickCount()-tc;

if(IsWindow(hSceneParams) && Seconds>=100)

{

sprintf(s, "FPS:%.0f", (float)Frames/((float)Seconds/1000));

SetDlgItemText(hSceneParams, IDC_FPS, s);

Seconds=0;

Frames=0;

}

ms+=GetTickCount()-tc;

if(IsWindow(hSceneParams) && ms>=500)

{

sprintf(s, "Время построения (мс):%i", (GetTickCount()-tc));

SetDlgItemText(hSceneParams, IDC_RENDER_TIME, s);

ms=0;

}

}

#endif
__________globals____________
#ifndef GLOBALS_

#define GLOBALS_
#include

#include "light.h"

#include "camera.h"

#include "primitives.h"

#include "resource.h"
#define HLP_DIR "help"

#define HLP_PRIM 0

#define HLP_AFF_AJ 8
LIGHT lights[4]={LIGHT(GL_LIGHT0), LIGHT(GL_LIGHT1), LIGHT(GL_LIGHT2),

LIGHT(GL_LIGHT3)};

float ObjHeight=15;

CAMERA camera(0, 0, 5);

void MultMatrix(float *vect, float matr[4][4]);

char helpText[1500];
char *HelpFiles[]={"sp.txt", "hsp.txt", "cube.txt", "prism5.txt", "pir5.txt", "cone.txt",

"pir3.txt", "prism3.txt",
"translate.txt", "scale.txt", "shift.txt", "rotate.txt", "refl.txt"};
char *HelpCaptions[]={"Параметрическая модель сферы", "Параметрическая полусферы",

"Параметрическая модель эллипсоида", "Параметрическая модель призмы с треугольным основанием",

"Параметрическая модель косоугольной призмы с 3уг. основанием",

"Параметрическая модель косоугольного цилиндра", "Параметрическая модель усеч. пир. с 3уг основанием",

"Параметрическая модель призмы с 3уг. основанием",
"Матрица переноса:", "Матрица масштабировния:", "Матрица сдвига:", "Матрицы поворота",

"Матрица отражения"};
HALFSPHERE hsp(0, 0 , 0, 1, 1, 0, 0);

SPHERE sp(0, 0, 0, 1 , 0, 1, 0);

IKO cube( 0, 0, 0, 1, 0 );

PRISM3 prism3 ( 0, -6, 0, 1, 0 );

ELIPS el(0, 0, 0);

CYL cyl( 1, 1, 1, 1, 0, .6 , .5);

PIRAMID3 pir3(0, 0, 0, 2, 0, 1);

KOSPR kospr[2]={KOSPR(1, 1, 1, 0, 0, .5), KOSPR(1, 1, 1, 0, 0, .5) };
OBJECT3D *Objects[9]={&sp, &hsp, &prism3,

&el, &cyl, &pir3, &kospr[0], &kospr[1]};
OBJECT3D *ObjectsNotAll[8]={&sp, &hsp, &prism3,

&el, &cyl, &pir3, &kospr[0]};
bool ajusted=false;

bool AxesAlwaysOnTop=false;

HWND hSceneParams=0;

HWND hLightParams=0;

HWND hPrimParams=0;

HWND hAffAjWnd=0;

HWND hCamDlg;

HWND hWndMain;

HINSTANCE hInst;
int FullScreenVP=-1;

HWND hHorSeparator, hVertSeparator;
void ShowSeparators(int cmd)

{

ShowWindow(hHorSeparator, cmd);

ShowWindow(hVertSeparator, cmd);

}

int ObjMat=-1;

int ObjModel=-1;
GLdouble mprojection[16];

GLdouble mmodelview[16];

int mviewport[4];
double ltx, lty, ltz;

bool DrawAxes=false;

void OutText(float x, float y, float z, char *s)

{

glMatrixMode(GL_MODELVIEW);

glPushMatrix();

glLoadIdentity();

glRasterPos3f(x, y, z);

glColor3f(1, 1, 1);

int l=strlen(s);

for(int i=0; i
{

glutBitmapCharacter(GLUT_BITMAP_9_BY_15, s[i]);

}

glPopMatrix();

}
char fn[MAX_PATH];

void SaveScene()

{

OPENFILENAME ofn;

memset(&ofn, 0, sizeof(ofn));

ofn.hwndOwner=hWndMain;

ofn.lpstrTitle="Сохранение состояния сцены";

ofn.lStructSize=sizeof(ofn);

ofn.lpstrFile=fn;

ofn.nMaxFile=MAX_PATH;

ofn.lpstrFilter="*.scn\0*.scn\0\0";

ofn.lpstrDefExt="scn";

ofn.Flags=OFN_FILEMUSTEXIST|OFN_EXTENSIONDIFFERENT|OFN_OVERWRITEPROMPT;

if(!GetSaveFileName(&ofn)) return;


FILE *f=fopen(fn, "wb");
for(int i=0; i<12; i++)

fwrite(&Objects[i]->settings, sizeof(Objects[i]->settings), 1, f);
for(i=0; i<4; i++)

fwrite(&lights[i].settings, sizeof(lights[i].settings), 1, f);
fwrite(&camera.settings, sizeof(camera.settings), 1, f);

fwrite(&ObjModel, sizeof(ObjModel), 1, f);

fwrite(&ObjMat, sizeof(ObjMat), 1, f);

fwrite(&FullScreenVP, sizeof(FullScreenVP), 1, f);
float mvm[16];

glGetFloatv(GL_MODELVIEW_MATRIX, (float*)mvm);

fwrite(mvm, sizeof(mvm), 1, f);
fwrite(&ajusted, sizeof(ajusted), 1, f);

fwrite(&DrawAxes, sizeof(DrawAxes), 1, f);

fwrite(&AxesAlwaysOnTop, sizeof(AxesAlwaysOnTop), 1, f);

fwrite(&ObjHeight, sizeof(ObjHeight), 1, f);

fclose(f);

}
bool JustLoaded=false;

void LoadScene()

{

OPENFILENAME ofn;

memset(&ofn, 0, sizeof(ofn));

ofn.hwndOwner=hWndMain;

ofn.lpstrTitle="Выберите файл с параметрами сцены";

ofn.lStructSize=sizeof(ofn);

ofn.lpstrFile=fn;

ofn.nMaxFile=MAX_PATH;

ofn.lpstrFilter="*.scn\0*.scn\0\0";

ofn.Flags=OFN_FILEMUSTEXIST|OFN_EXTENSIONDIFFERENT;

if(!GetOpenFileName(&ofn)) return;

FILE *f=fopen(fn, "rb");
for(int i=0; i<12; i++)

fread(&Objects[i]->settings, sizeof(Objects[i]->settings), 1, f);
for(i=0; i<4; i++)

{

fread(&lights[i].settings, sizeof(lights[i].settings), 1, f);

lights[i].Dispose(lights[i].settings.pos[0], lights[i].settings.pos[1], lights[i].settings.pos[2], 0);
if(lights[i].settings.shining) lights[i].TurnOn();

else lights[i].TurnOff();
lights[i].SetColor(lights[i].settings.color[0], lights[i].settings.color[1],lights[i].settings.color[2]);

lights[i].SetLightModelType(lights[i].settings.lightmodeltype);

HMENU hMenu=GetMenu(hWndMain);

CheckMenuItem(hMenu, IDC_LIGHT0+i, MF_BYCOMMAND|(lights[i].Shining())?

MF_CHECKED:MF_UNCHECKED);

}

fread(&camera.settings, sizeof(camera.settings), 1, f);
fread(&ObjModel, sizeof(ObjModel), 1, f);

fread(&ObjMat, sizeof(ObjMat), 1, f);

fread(&FullScreenVP, sizeof(FullScreenVP), 1, f);
if(FullScreenVP!=-1) ShowSeparators(SW_HIDE);

else ShowSeparators(SW_SHOW);
float mvm[16];

fread(mvm, sizeof(mvm), 1, f);

glMatrixMode(GL_MODELVIEW);

glLoadMatrixf(mvm);

fread(&ajusted, sizeof(ajusted), 1, f);

fread(&DrawAxes, sizeof(DrawAxes), 1, f);

fread(&AxesAlwaysOnTop, sizeof(DrawAxes), 1, f);
fread(&ObjHeight, sizeof(ObjHeight), 1, f);

JustLoaded=true;
if(IsWindow(hSceneParams))

SendMessage(hSceneParams, WM_INITDIALOG, 0, 0);
if(IsWindow(hPrimParams))

SendMessage(hPrimParams, WM_INITDIALOG, 0, 0);
if(IsWindow(hAffAjWnd))

SendMessage(hAffAjWnd, WM_INITDIALOG, 0, 0);
if(IsWindow(hLightParams))

SendMessage(hLightParams, WM_INITDIALOG, 0, 0);
if(IsWindow(hCamDlg))

SendMessage(hCamDlg, WM_INITDIALOG, 0, 0);
fclose(f);

}
void SetPrimitiveColor(int obj, HWND hR, HWND hG, HWND hB);

void SetModelRadio(int obj, HWND hSolid, HWND hWire);
void LoadHelpText(char *fn, char *ret, ULONG count)

{

OFSTRUCT ofs;

char hfn[MAX_PATH];

sprintf(hfn, "%s\\%s",HLP_DIR, fn);

HANDLE hFile=(HANDLE)OpenFile(hfn, &ofs, OF_READ);

DWORD fsize=GetFileSize(hFile, 0);
if(fsize>count)

{

strcpy(ret, "! Ошибка при загрузке файла справки");

CloseHandle(hFile);

return;

}
ReadFile(hFile, ret, count, &count, NULL);

ret[count]=0;

CloseHandle(hFile);

}
#endif
____________glsetupe__________________
#ifndef GLSETUP

#define GLSETUP

#include

#include

#include "camera.h"

#include "globals.h"
#define VP_RIGHTBOTTOM 0

#define VP_LEFTBOTTOM 1

#define VP_RIGHTTOP 2

#define VP_LEFTTOP 3
void SetOrtho();

void SetPerspective();

void SetViewport(int vp, bool fullscreen=false)

{

RECT rct;

GetClientRect(hWndMain, &rct);

int h=rct.bottom;

int w=rct.right-200;
if(fullscreen)

{

h*=2;

w*=2;

}
switch(vp)

{

case VP_RIGHTBOTTOM:

glViewport(!fullscreen?w/2:0, 0, w/2, h/2);

SetPerspective();

camera.Apply();

break;
case VP_LEFTBOTTOM:

glViewport(0, 0, w/2, h/2);

SetOrtho();

gluLookAt(0, 0, 4, 0, 0, 0, 0, 1, 0);

break;

case VP_LEFTTOP:

glViewport(0, !fullscreen?h/2:0, w/2, h/2);

SetOrtho();

gluLookAt(4, 0, 0, 0, 0, 0, 1 , 1, 0);

break;
case VP_RIGHTTOP:

glViewport(!fullscreen?w/2:0, !fullscreen?h/2:0, w/2, h/2);

SetOrtho();

gluLookAt(0, 4, 0, 0, 0, 0, 1 , 0, 0);

break;

}
glMatrixMode(GL_MODELVIEW);

}
void SetOrtho()

{

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-20, 20, -20, 20, -20, 20);

}

void SetPerspective()

{

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

float ang=(acos(1.f/sqrt(3))/3.14)*180;
glOrtho(-30, 30, -30, 30, -30, 30);

glRotatef(ang, 1, 0, 0);

glRotatef(-ang, 0, 1, 0);
}

bool PtInViewport(int x, int y, int vp)

{

POINT pt={x, y};

RECT wndrct;

GetClientRect(hWndMain, &wndrct);

int h=wndrct.bottom;

int w=wndrct.right-200;
RECT VPRect;
if(vp==VP_LEFTTOP)

{

VPRect.left=0;

VPRect.top=0;

VPRect.right=w/2;

VPRect.bottom=h/2;

return PtInRect(&VPRect, pt);

}
if(vp==VP_LEFTBOTTOM)

{

VPRect.left=0;

VPRect.top=h/2;

VPRect.right=w/2;

VPRect.bottom=h;
return PtInRect(&VPRect, pt);

}
if(vp==VP_RIGHTTOP)

{

VPRect.left=w/2;

VPRect.top=0;

VPRect.right=w;

VPRect.bottom=h/2;

return PtInRect(&VPRect, pt);

}

if(vp==VP_RIGHTBOTTOM)

{

VPRect.left=w/2;

VPRect.top=h/2;

VPRect.right=w;

VPRect.bottom=h;

return PtInRect(&VPRect, pt);

}

}
void PreSetup()

{

glEnable(GL_LIGHTING);

glEnable(GL_NORMALIZE);

glEnable(GL_AUTO_NORMAL);

glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);
lights[0].SetColor(1, .2, .95);

lights[0].Dispose(-20, -15, 0, 0);

lights[0].SetLightModelType(LMT_CYL);

lights[1].SetColor(1, 0, 0);

lights[1].Dispose(15, 0, -20, -1);

lights[1].SetLightModelType(LMT_POINT);
lights[2].SetColor(0, 1, 0);

lights[2].Dispose(15, -20, 10, 1);

lights[2].SetLightModelType(LMT_REMOTE);
glGetDoublev(GL_MODELVIEW_MATRIX, mmodelview);

glGetDoublev(GL_PROJECTION_MATRIX, mprojection);
glClearColor(.5, .5 ,.5, 0);

}

void SetPF(HDC hdc)

{

PIXELFORMATDESCRIPTOR pfd = {sizeof(PIXELFORMATDESCRIPTOR),

1,

PFD_DRAW_TO_WINDOW |

PFD_SUPPORT_OPENGL |

PFD_DOUBLEBUFFER,

PFD_TYPE_RGBA,

24,

0, 0, 0, 0, 0, 0,

0,

0,

0,

0, 0, 0, 0,

32,

0,

0,

PFD_MAIN_PLANE,

0,

0, 0, 0

};

int index=ChoosePixelFormat(hdc, &pfd);

SetPixelFormat(hdc, index, &pfd);

}
#endif

_________light___________________
#ifndef LIGHT_

#define LIGHT_
#define LMT_POINT 0

#define LMT_CYL 1

#define LMT_REMOTE 2
class LIGHT

{

public:

struct

{

float pos[4];

float dir[3];

float color[4];

int lightmodeltype;

GLboolean shining;

} settings;
int index;

void Draw()

{

if(!Shining()) return;

glDisable(GL_LIGHTING);

glPushMatrix();
1   2   3   4   5   6   7   8   9   10


написать администратору сайта