?? transformation.c
字號:
/* transformation.c Nate Robins, 1997 Tool for teaching about OpenGL transformations. */#include <math.h>#include <stdio.h>#include <stdlib.h>#include <stdarg.h>#include <string.h>#include <GL/glut.h>#include "glm.h"
#pragma comment( linker, "/entry:\"mainCRTStartup\"" ) // set the entry point to be main()
typedef struct _cell { int id; int x, y; float min, max; float value; float step; char* info; char* format;} cell;cell translation[3] = { { 1, 120, 40, -5.0, 5.0, 0.0, 0.01, "Specifies X coordinate of translation vector.", "%.2f" }, { 2, 180, 40, -5.0, 5.0, 0.0, 0.01, "Specifies Y coordinate of translation vector.", "%.2f" }, { 3, 240, 40, -5.0, 5.0, 0.0, 0.01, "Specifies Z coordinate of translation vector.", "%.2f" },};cell rotation[4] = { { 4, 120, 80, -360.0, 360.0, 0.0, 1.0, "Specifies angle of rotation, in degrees.", "%.1f" }, { 5, 180, 80, -1.0, 1.0, 0.0, 0.01, "Specifies X coordinate of vector to rotate about.", "%.2f" }, { 6, 240, 80, -1.0, 1.0, 1.0, 0.01, "Specifies Y coordinate of vector to rotate about.", "%.2f" }, { 7, 300, 80, -1.0, 1.0, 0.0, 0.01, "Specifies Z coordinate of vector to rotate about.", "%.2f" },};cell scale[3] = { { 8, 120, 120, -5.0, 5.0, 1.0, 0.01, "Specifies scale factor along X axis.", "%.2f" }, { 9, 180, 120, -5.0, 5.0, 1.0, 0.01, "Specifies scale factor along Y axis.", "%.2f" }, { 10, 240, 120, -5.0, 5.0, 1.0, 0.01, "Specifies scale factor along Z axis.", "%.2f" },};GLfloat eye[3] = { 0.0, 0.0, 2.0 };GLfloat at[3] = { 0.0, 0.0, 0.0 };GLfloat up[3] = { 0.0, 1.0, 0.0 };GLboolean swapped = GL_FALSE;GLboolean world_draw = GL_TRUE;GLMmodel* pmodel = NULL;GLint selection = 0;void redisplay_all(void);GLdouble projection[16], modelview[16], inverse[16];GLuint window, world, screen, command;GLuint sub_width = 256, sub_height = 256;GLvoid *font_style = GLUT_BITMAP_TIMES_ROMAN_10;voidsetfont(char* name, int size){ font_style = GLUT_BITMAP_HELVETICA_10; if (strcmp(name, "helvetica") == 0) { if (size == 12) font_style = GLUT_BITMAP_HELVETICA_12; else if (size == 18) font_style = GLUT_BITMAP_HELVETICA_18; } else if (strcmp(name, "times roman") == 0) { font_style = GLUT_BITMAP_TIMES_ROMAN_10; if (size == 24) font_style = GLUT_BITMAP_TIMES_ROMAN_24; } else if (strcmp(name, "8x13") == 0) { font_style = GLUT_BITMAP_8_BY_13; } else if (strcmp(name, "9x15") == 0) { font_style = GLUT_BITMAP_9_BY_15; }}void drawstr(GLuint x, GLuint y, char* format, ...){ va_list args; char buffer[255], *s; va_start(args, format); vsprintf(buffer, format, args); va_end(args); glRasterPos2i(x, y); for (s = buffer; *s; s++) glutBitmapCharacter(font_style, *s);}voidcell_draw(cell* cell){ glColor3ub(0, 255, 128); if (selection == cell->id) { glColor3ub(255, 255, 0); drawstr(10, 240, cell->info); glColor3ub(255, 0, 0); } drawstr(cell->x, cell->y, cell->format, cell->value);}intcell_hit(cell* cell, int x, int y){ if (x > cell->x && x < cell->x + 60 && y > cell->y-30 && y < cell->y+10) return cell->id; return 0;}voidcell_update(cell* cell, int update){ if (selection != cell->id) return; cell->value += update * cell->step; if (cell->value < cell->min) cell->value = cell->min; else if (cell->value > cell->max) cell->value = cell->max; }voidcell_vector(float* dst, cell* cell, int num){ while (--num >= 0) dst[num] = cell[num].value;}voiddrawmodel(void){ if (!pmodel) { pmodel = glmReadOBJ("data/porsche.obj"); if (!pmodel) exit(0); glmUnitize(pmodel); glmFacetNormals(pmodel); glmVertexNormals(pmodel, 90.0); } glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);}voiddrawaxes(void){ glColor3ub(255, 0, 0); glBegin(GL_LINE_STRIP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(0.75, 0.25, 0.0); glVertex3f(0.75, -0.25, 0.0); glVertex3f(1.0, 0.0, 0.0); glVertex3f(0.75, 0.0, 0.25); glVertex3f(0.75, 0.0, -0.25); glVertex3f(1.0, 0.0, 0.0); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.0, 0.75, 0.25); glVertex3f(0.0, 0.75, -0.25); glVertex3f(0.0, 1.0, 0.0); glVertex3f(0.25, 0.75, 0.0); glVertex3f(-0.25, 0.75, 0.0); glVertex3f(0.0, 1.0, 0.0); glEnd(); glBegin(GL_LINE_STRIP); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glVertex3f(0.25, 0.0, 0.75); glVertex3f(-0.25, 0.0, 0.75); glVertex3f(0.0, 0.0, 1.0); glVertex3f(0.0, 0.25, 0.75); glVertex3f(0.0, -0.25, 0.75); glVertex3f(0.0, 0.0, 1.0); glEnd(); glColor3ub(255, 255, 0); glRasterPos3f(1.1, 0.0, 0.0); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'x'); glRasterPos3f(0.0, 1.1, 0.0); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'y'); glRasterPos3f(0.0, 0.0, 1.1); glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, 'z');}voididentity(GLdouble m[16]){ m[0+4*0] = 1; m[0+4*1] = 0; m[0+4*2] = 0; m[0+4*3] = 0; m[1+4*0] = 0; m[1+4*1] = 1; m[1+4*2] = 0; m[1+4*3] = 0; m[2+4*0] = 0; m[2+4*1] = 0; m[2+4*2] = 1; m[2+4*3] = 0; m[3+4*0] = 0; m[3+4*1] = 0; m[3+4*2] = 0; m[3+4*3] = 1;}GLbooleaninvert(GLdouble src[16], GLdouble inverse[16]){ double t; int i, j, k, swap; GLdouble tmp[4][4]; identity(inverse); for (i = 0; i < 4; i++) { for (j = 0; j < 4; j++) { tmp[i][j] = src[i*4+j]; } } for (i = 0; i < 4; i++) { /* look for largest element in column. */ swap = i; for (j = i + 1; j < 4; j++) { if (fabs(tmp[j][i]) > fabs(tmp[i][i])) { swap = j; } } if (swap != i) { /* swap rows. */ for (k = 0; k < 4; k++) { t = tmp[i][k]; tmp[i][k] = tmp[swap][k]; tmp[swap][k] = t; t = inverse[i*4+k]; inverse[i*4+k] = inverse[swap*4+k]; inverse[swap*4+k] = t; } } if (tmp[i][i] == 0) { /* no non-zero pivot. the matrix is singular, which shouldn't happen. This means the user gave us a bad matrix. */ return GL_FALSE; } t = tmp[i][i]; for (k = 0; k < 4; k++) { tmp[i][k] /= t; inverse[i*4+k] /= t; } for (j = 0; j < 4; j++) { if (j != i) { t = tmp[j][i]; for (k = 0; k < 4; k++) { tmp[j][k] -= tmp[i][k]*t; inverse[j*4+k] -= inverse[i*4+k]*t; } } } } return GL_TRUE;}floatnormalize(float* v){ float length; length = sqrt(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]); v[0] /= length; v[1] /= length; v[2] /= length; return length;}voidmain_reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, width, height, 0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); #define GAP 25 /* gap between subwindows */ sub_width = (width-GAP*3)/2.0; sub_height = (height-GAP*3)/2.0; glutSetWindow(world); glutPositionWindow(GAP, GAP); glutReshapeWindow(sub_width, sub_height); glutSetWindow(screen); glutPositionWindow(GAP+sub_width+GAP, GAP); glutReshapeWindow(sub_width, sub_height); glutSetWindow(command); glutPositionWindow(GAP, GAP+sub_height+GAP); glutReshapeWindow(sub_width+GAP+sub_width, sub_height);}voidmain_display(void){ glClearColor(0.8, 0.8, 0.8, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3ub(0, 0, 0); setfont("helvetica", 12); drawstr(GAP, GAP-5, "World-space view"); drawstr(GAP+sub_width+GAP, GAP-5, "Screen-space view"); drawstr(GAP, GAP+sub_height+GAP-5, "Command manipulation window"); glutSwapBuffers();}voidmain_keyboard(unsigned char key, int x, int y){ switch (key) { case 's': swapped = !swapped; break; case 'r': translation[0].value = 0.0; translation[1].value = 0.0; translation[2].value = 0.0; rotation[0].value = 0.0; rotation[1].value = 0.0; rotation[2].value = 1.0; rotation[3].value = 0.0; scale[0].value = 1.0; scale[1].value = 1.0; scale[2].value = 1.0; break; case 27: exit(0); } redisplay_all();}voidworld_reshape(int width, int height){ glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0, (GLfloat)width/height, 0.01, 256.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -5.0); glRotatef(-45.0, 0.0, 1.0, 0.0); glClearColor(0.0, 0.0, 0.0, 0.0); glEnable(GL_DEPTH_TEST); glEnable(GL_LIGHT0);}void
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -