diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..51757d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +cmake-build-debug/ +.idea diff --git a/CGProject/.idea/.name b/CGProject/.idea/.name deleted file mode 100644 index dad5286..0000000 --- a/CGProject/.idea/.name +++ /dev/null @@ -1 +0,0 @@ -CGProject \ No newline at end of file diff --git a/CGProject/.idea/CGProject.iml b/CGProject/.idea/CGProject.iml deleted file mode 100644 index 5f83d42..0000000 --- a/CGProject/.idea/CGProject.iml +++ /dev/null @@ -1,364 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/CGProject/.idea/encodings.xml b/CGProject/.idea/encodings.xml deleted file mode 100644 index 97626ba..0000000 --- a/CGProject/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/CGProject/.idea/misc.xml b/CGProject/.idea/misc.xml deleted file mode 100644 index 3eb495b..0000000 --- a/CGProject/.idea/misc.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - - - - - - - - - - \ No newline at end of file diff --git a/CGProject/.idea/modules.xml b/CGProject/.idea/modules.xml deleted file mode 100644 index 90474bb..0000000 --- a/CGProject/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/CGProject/.idea/workspace.xml b/CGProject/.idea/workspace.xml deleted file mode 100644 index f83d5e6..0000000 --- a/CGProject/.idea/workspace.xml +++ /dev/null @@ -1,194 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 1452297051194 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/CGProject/CMakeLists.txt b/CGProject/CMakeLists.txt index 80151ec..0d4d85e 100644 --- a/CGProject/CMakeLists.txt +++ b/CGProject/CMakeLists.txt @@ -1,13 +1,26 @@ cmake_minimum_required(VERSION 3.3) project(CGProject) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") +## OpenGL +FIND_PACKAGE(OpenGL REQUIRED) +FIND_PACKAGE(GLUT REQUIRED) +INCLUDE_DIRECTORIES(${OPENGL_INCLUDE_DIR}) +INCLUDE_DIRECTORIES(${GLUT_INCLUDE_DIR}) + +## default +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") set(SOURCE_FILES - main.cpp - mtl.cpp - mtl.hpp - obj.cpp - obj.hpp) + main.cpp + glm.cpp + Mtl.cpp + Mtl.h + Obj.cpp + Obj.h) + +add_executable(CGProject ${SOURCE_FILES}) -add_executable(CGProject ${SOURCE_FILES}) \ No newline at end of file +## add linked libraries +TARGET_LINK_LIBRARIES(CGProject ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES}) diff --git a/CGProject/Mtl.cpp b/CGProject/Mtl.cpp index 488a3d3..9f3668a 100644 --- a/CGProject/Mtl.cpp +++ b/CGProject/Mtl.cpp @@ -16,8 +16,13 @@ // C Headers #include // OpenGL and GLM Headers +#ifndef __linux #include #include +#else +#include +#include +#endif //#include "glm.hpp" using namespace std; diff --git a/CGProject/Mtl.h b/CGProject/Mtl.h index 0dee846..f367a74 100644 --- a/CGProject/Mtl.h +++ b/CGProject/Mtl.h @@ -17,8 +17,13 @@ // C Headers #include // OpenGL and GLM Headers +#ifndef __linux #include #include +#else +#include +#include +#endif //#include "glm.hpp" using namespace std; diff --git a/CGProject/Obj.cpp b/CGProject/Obj.cpp index f71dc39..abd6b70 100644 --- a/CGProject/Obj.cpp +++ b/CGProject/Obj.cpp @@ -18,8 +18,13 @@ // C Headers #include // OpenGL and GLM Headers +#ifndef __linux #include #include +#else +#include +#include +#endif //#include "glm.hpp" using namespace std; diff --git a/CGProject/Obj.h b/CGProject/Obj.h index 1535590..b179e73 100644 --- a/CGProject/Obj.h +++ b/CGProject/Obj.h @@ -17,8 +17,13 @@ // C Headers #include // OpenGL and GLM Headers +#ifndef __linux #include #include +#else +#include +#include +#endif //#include "glm.hpp" using namespace std; diff --git a/CGProject/glm.h b/CGProject/glm.h index 3329be0..aecd17a 100644 --- a/CGProject/glm.h +++ b/CGProject/glm.h @@ -8,8 +8,11 @@ /* includes */ +#ifndef __linux #include - +#else +#include +#endif #ifndef M_PI #define M_PI 3.14159265 diff --git a/CGProject/main.cpp b/CGProject/main.cpp index 0530ff1..3176aea 100644 --- a/CGProject/main.cpp +++ b/CGProject/main.cpp @@ -1,28 +1,36 @@ //project #include "main.h" -int notForbidden(double x, double z) { - for (int i = 0; i < 5; i++) { - if (x <= forbiddenlist[i].x1 && x >= forbiddenlist[i].x2 && z <= forbiddenlist[i].z1 && z >= forbiddenlist[i].z2) { +int notForbidden(double x, double z) +{ + for (int i = 0; i < 5; i++) + { + if (x <= forbiddenlist[i].x1 && x >= forbiddenlist[i].x2 && z <= forbiddenlist[i].z1 && + z >= forbiddenlist[i].z2) + { cout << "----" << i << " " << endl; cout << forbiddenlist[i].x1 << " " << forbiddenlist[i].x2 << " " << x << endl; cout << forbiddenlist[i].z1 << " " << forbiddenlist[i].z2 << " " << z << endl; return 0; } } - if(x < 8.5 && x > -8.5) + if (x < 8.5 && x > -8.5) { - if (x < -17 || x > 17 || z < -18.5 || z > 19) { - if (x < -17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) { + if (x < -17 || x > 17 || z < -18.5 || z > 19) + { + if (x < -17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) + { inroom = false; return 1; } - if (x > 17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) { + if (x > 17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) + { inroom = false; return 1; } - if (z < -18.5 && (x < 8.5 && x > -8.5)) { - if( !ifdooropen && inroom ) return 0; + if (z < -18.5 && (x < 8.5 && x > -8.5)) + { + if (!ifdooropen && inroom) return 0; else { inroom = false; @@ -34,20 +42,23 @@ int notForbidden(double x, double z) { else return 0; } - } - else + } else { - if (x < -17 || x > 17 || z < -17 || z > 19) { - if (x < -17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) { + if (x < -17 || x > 17 || z < -17 || z > 19) + { + if (x < -17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) + { inroom = false; return 1; } - if (x > 17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) { + if (x > 17 && ((z < -5 && z > -9) || (z < 11 && z > 8))) + { inroom = false; return 1; } - if (z < -17 && (x < 8.5 && x > -8.5)) { - if( !ifdooropen && inroom ) return 0; + if (z < -17 && (x < 8.5 && x > -8.5)) + { + if (!ifdooropen && inroom) return 0; else { inroom = false; @@ -60,8 +71,10 @@ int notForbidden(double x, double z) { return 0; } } - if (!inroom) { - if ((z < -5 && z > -9) || (z < 11 && z > 8) || (x < 8.5 && x > -8.5)) { + if (!inroom) + { + if ((z < -5 && z > -9) || (z < 11 && z > 8) || (x < 8.5 && x > -8.5)) + { inroom = true; } else return 0; @@ -69,23 +82,32 @@ int notForbidden(double x, double z) { return 1; } -void readBMPfileHeader(BMPfileHeader &t){ - myfin.read((char*)&t,sizeof(struct BMPfileHeader)); +void readBMPfileHeader(BMPfileHeader &t) +{ + myfin.read((char *) &t, sizeof(struct BMPfileHeader)); } -void readBMPinfoHeader(BMPinfoHeader &t){ - myfin.read((char*)&t,sizeof(struct BMPinfoHeader)); - if (t.biHeight<0){ - cerr<<"height < 0 !!"<biSizeImage + 2]; - if (!bitmapImage) { + if (!bitmapImage) + { fprintf(stderr, "Error in loadBitmapFile: memory error\n"); return NULL; } - + fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr); - if (bitmapImage == NULL) { + if (bitmapImage == NULL) + { fprintf(stderr, "Error in loadBitmapFile: memory error\n"); return NULL; } - + for (imageIdx = 0; - imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3) { + imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3) + { tempRGB = bitmapImage[imageIdx]; bitmapImage[imageIdx] = bitmapImage[imageIdx + 2]; bitmapImage[imageIdx + 2] = tempRGB; @@ -233,23 +269,27 @@ unsigned char *loadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader } -void loadTexture(int i, char* filename, bool type) { +void loadTexture(int i, char *filename, bool type) +{ BITMAPINFOHEADER bitmapInfoHeader; - unsigned char* bitmapData; + unsigned char *bitmapData; bitmapData = loadBitmapFile(filename, &bitmapInfoHeader); // bind the texture glBindTexture(GL_TEXTURE_2D, type ? textureVideo[i] : textureObjects[i]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth, bitmapInfoHeader.biHeight, 0, GL_RGB, + GL_UNSIGNED_BYTE, bitmapData); } -void initTextureVideo() { +void initTextureVideo() +{ glGenTextures(30, textureVideo); - + char videoLoc[14] = "video/000.bmp"; - for (int i = 0; i <= 29; i++) { + for (int i = 0; i <= 29; i++) + { videoLoc[7] = i / 10 + '0'; videoLoc[8] = i % 10 + '0'; // cout << videoLoc << endl; @@ -258,65 +298,95 @@ void initTextureVideo() { loadTexture(30, "black.bmp", true); } -void renderVideoFrame(GLfloat x) { +void renderVideoFrame(GLfloat x) +{ glPushMatrix(); glScalef(x / 2, x / 2, x / 2); - + // glBindTexture(GL_TEXTURE_2D, textureVideo[displayFPS]); glBegin(GL_QUADS); - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0f, 1.0f, 1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); - - + glTexCoord2f(0.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + + glEnd(); - + glBindTexture(GL_TEXTURE_2D, textureVideo[30]); //glColor3f(0, 0, 0); glBegin(GL_QUADS); - - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); - + + glTexCoord2f(0.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + // »æÖƺóÃæ - glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); - + glTexCoord2f(1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + // ÉÏÃæ - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); - + glTexCoord2f(0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + //µ×Ãæ - glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); - + glTexCoord2f(1.0f, 1.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + // ÓÒÃæ - glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); - glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); - + glTexCoord2f(1.0f, 0.0f); + glVertex3f(1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(1.0f, 1.0f, -1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 0.0f); + glVertex3f(1.0f, -1.0f, 1.0f); + // ×óÃæ - glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); - glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); - glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); - glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); - + glTexCoord2f(0.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, -1.0f); + glTexCoord2f(1.0f, 0.0f); + glVertex3f(-1.0f, -1.0f, 1.0f); + glTexCoord2f(1.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, 1.0f); + glTexCoord2f(0.0f, 1.0f); + glVertex3f(-1.0f, 1.0f, -1.0f); + glEnd(); - + glPopMatrix(); } -void renderVideo() { +void renderVideo() +{ glEnable(GL_TEXTURE_2D); glPushMatrix(); glTranslatef(0, 0, 0.0); @@ -330,146 +400,185 @@ void renderVideo() { void loadObjectTextures() { - glGenTextures(textureObjectCnt, textureObjects); // µÚÒ»²ÎÊýÊÇÐèÒªÉú³É±êʾ·ûµÄ¸öÊý, µÚ¶þ²ÎÊýÊÇ·µ»Ø±êʾ·ûµÄÊý×é - - for (int i = 0; i1.0f) h=1.0f; //视点y坐标作一些限制,不会使视点太奇怪 - else if(h<-1.0f) h=-1.0f; + eye[0] = center[0] + r * cos(c * du); + eye[2] = center[2] + r * sin(c * du); + h += 0.03f * (y - oldmy); //鼠标在窗口y轴方向上的改变加到视点的y坐标上,就上下转了 + if (h > 1.0f) h = 1.0f; //视点y坐标作一些限制,不会使视点太奇怪 + else if (h < -1.0f) h = -1.0f; eye[1] = h; dir[0] = center[0] - eye[0]; dir[1] = center[1] - eye[1]; dir[2] = center[2] - eye[2]; - + FLlightDir[0] = dir[0]; FLlightDir[1] = dir[1]; FLlightDir[2] = dir[2]; FLposition[0] = eye[0]; FLposition[1] = eye[1]; FLposition[2] = eye[2]; - + oldmx = x, oldmy = y; //把此时的鼠标坐标作为旧值,为下一次计算增量做准备 } -void key(unsigned char k, int x, int y) { +void key(unsigned char k, int x, int y) +{ static char titleBuffer[256]; static char snapScreenFileName[256]; static int snapScreenCnt; bool titleStateChanged = false; float eyeDeltaX = 0.5 * (eye[0] - center[0]) / 1.5; float eyeDeltaZ = 0.5 * (eye[2] - center[2]) / 1.5; - switch (k) { - case ' ':{ ifmainlight = !ifmainlight; break; } // Main light Switch - case '0':{ operation = 0; break; } // Main light Pos - case '1':{ operation = 1; break; } // Main light Color - - case '2':{ ViewMode = 2; break; } // Bird(kotori) view - case '3':{ ViewMode = 3; break; } // First persion view - - case '4':{ ifflashlight = !ifflashlight; break; } // Flashlight Switch - case '5':{ ifspotlight = !ifspotlight; break; } // Spotlight Switch - - case '6':{ ifmodelshow = !ifmodelshow; break; } - case '7':{ ifmodelscale = !ifmodelscale; break; } - case '8':{ ifmodeltranslate = !ifmodeltranslate; break; } - case '9':{ ifmodelrotate = !ifmodelrotate; break; } - - case ',':{ ifdooropen = !ifdooropen; break; } + switch (k) + { + case ' ': + { + ifmainlight = !ifmainlight; + break; + } // Main light Switch + case '0': + { + operation = 0; + break; + } // Main light Pos + case '1': + { + operation = 1; + break; + } // Main light Color + + case '2': + { + ViewMode = 2; + break; + } // Bird(kotori) view + case '3': + { + ViewMode = 3; + break; + } // First persion view + + case '4': + { + ifflashlight = !ifflashlight; + break; + } // Flashlight Switch + case '5': + { + ifspotlight = !ifspotlight; + break; + } // Spotlight Switch + + case '6': + { + ifmodelshow = !ifmodelshow; + break; + } + case '7': + { + ifmodelscale = !ifmodelscale; + break; + } + case '8': + { + ifmodeltranslate = !ifmodeltranslate; + break; + } + case '9': + { + ifmodelrotate = !ifmodelrotate; + break; + } + + case ',': + { + ifdooropen = !ifdooropen; + break; + } case 27: - case 'q': {exit(0); break; } - case 'l': { - if (operation == 0){ + case 'q': + { + exit(0); + break; + } + case 'l': + { + if (operation == 0) + { MainLightPos[0] += MainLightPosVar; - } - else if (operation == 1){ + } else if (operation == 1) + { if (MainLightColor[0] + MainLightColorVar <= 1.0) MainLightColor[0] += MainLightColorVar; } break; } - case 'd': { - if (ViewMode == 2){ + case 'd': + { + if (ViewMode == 2) + { FP_eye[0] += FP_eyeVar; - } - else if (ViewMode == 3) + } else if (ViewMode == 3) { //if (angle > 0 ){ - dir[0] = center[0] - eye[0]; - dir[2] = center[2] - eye[2]; - dir[0] = dir[0] * cos(c * 5) - dir[2] * sin(c * 5); - dir[2] = dir[0] * sin(c * 5) + dir[2] * cos(c * 5); - center[0] = eye[0] + dir[0]; - center[2] = eye[2] + dir[2]; - + dir[0] = center[0] - eye[0]; + dir[2] = center[2] - eye[2]; + dir[0] = dir[0] * cos(c * 5) - dir[2] * sin(c * 5); + dir[2] = dir[0] * sin(c * 5) + dir[2] * cos(c * 5); + center[0] = eye[0] + dir[0]; + center[2] = eye[2] + dir[2]; + FLlightDir[0] = dir[0]; FLlightDir[1] = dir[1]; FLlightDir[2] = dir[2]; @@ -645,33 +816,36 @@ void key(unsigned char k, int x, int y) { cout << "Dir:" << dir[0] << "," << dir[1] << "," << dir[2] << endl; //} } - + break; } - case 'j': { - if (operation == 0){ + case 'j': + { + if (operation == 0) + { MainLightPos[0] -= MainLightPosVar; - } - else if (operation == 1){ + } else if (operation == 1) + { if (MainLightColor[0] - MainLightColorVar >= 0.0) MainLightColor[0] -= MainLightColorVar; } break; } - case 'a': { - if (ViewMode == 2){ + case 'a': + { + if (ViewMode == 2) + { FP_eye[0] -= FP_eyeVar; - } - else if (ViewMode == 3) + } else if (ViewMode == 3) { //if (angle < 180 ){ - dir[0] = center[0] - eye[0]; - dir[2] = center[2] - eye[2]; - dir[0] = dir[0] * cos(c*(-5)) - dir[2] * sin(c*(-5)); - dir[2] = dir[0] * sin(c*(-5)) + dir[2] * cos(c*(-5)); - center[0] = eye[0] + dir[0]; - center[2] = eye[2] + dir[2]; - + dir[0] = center[0] - eye[0]; + dir[2] = center[2] - eye[2]; + dir[0] = dir[0] * cos(c * (-5)) - dir[2] * sin(c * (-5)); + dir[2] = dir[0] * sin(c * (-5)) + dir[2] * cos(c * (-5)); + center[0] = eye[0] + dir[0]; + center[2] = eye[2] + dir[2]; + FLlightDir[0] = dir[0]; FLlightDir[1] = dir[1]; FLlightDir[2] = dir[2]; @@ -683,22 +857,25 @@ void key(unsigned char k, int x, int y) { cout << "Dir:" << dir[0] << "," << dir[1] << "," << dir[2] << endl; //} } - + break; } case 'k': { - if (operation == 0){ + if (operation == 0) + { MainLightPos[1] -= MainLightPosVar; - } - else if (operation == 1){ + } else if (operation == 1) + { if (MainLightColor[1] - MainLightColorVar >= 0.0) MainLightColor[1] -= MainLightColorVar; } break; } - case 's': { - if (ViewMode == 2){ + case 's': + { + if (ViewMode == 2) + { FP_eye[1] -= FP_eyeVar; } if (ViewMode == 3) @@ -711,30 +888,34 @@ void key(unsigned char k, int x, int y) { eye[2] += eyeDeltaZ; center[0] = eye[0] - dir[0]; center[2] = eye[2] - dir[2]; - + FLposition[0] = eye[0]; FLposition[1] = eye[1]; FLposition[2] = eye[2]; } - cout << "Center:" << center[0] << "," << center[1] << "," << center[2] << endl; + cout << "Center:" << center[0] << "," << center[1] << "," << center[2] << endl; cout << "Eye:" << eye[0] << "," << eye[1] << "," << eye[2] << endl; cout << "Dir:" << dir[0] << "," << dir[1] << "," << dir[2] << endl; } - + break; } - case 'i':{ - if (operation == 0){ + case 'i': + { + if (operation == 0) + { MainLightPos[1] += MainLightPosVar; - } - else if (operation == 1){ + } else if (operation == 1) + { if (MainLightColor[1] + MainLightColorVar <= 1.0) MainLightColor[1] += MainLightColorVar; } break; } - case 'w': { - if (ViewMode == 2){ + case 'w': + { + if (ViewMode == 2) + { FP_eye[1] += FP_eyeVar; } if (ViewMode == 3) @@ -747,7 +928,7 @@ void key(unsigned char k, int x, int y) { eye[2] -= eyeDeltaZ; center[0] = eye[0] - dir[0]; center[2] = eye[2] - dir[2]; - + FLposition[0] = eye[0]; FLposition[1] = eye[1]; FLposition[2] = eye[2]; @@ -756,27 +937,32 @@ void key(unsigned char k, int x, int y) { cout << "Eye:" << eye[0] << "," << eye[1] << "," << eye[2] << endl; cout << "Dir:" << dir[0] << "," << dir[1] << "," << dir[2] << endl; } - + break; } - case 'z': { - if (ViewMode== 2){ + case 'z': + { + if (ViewMode == 2) + { FP_eye[2] -= FP_eyeVar; } break; } - case 'x': { - if (ViewMode == 2){ + case 'x': + { + if (ViewMode == 2) + { FP_eye[2] += FP_eyeVar; } break; } case 'c': { - if (operation == 0){ + if (operation == 0) + { MainLightPos[2] -= MainLightPosVar; - } - else if (operation == 1){ + } else if (operation == 1) + { if (MainLightColor[2] - MainLightColorVar >= 0.0) MainLightColor[2] -= MainLightColorVar; } @@ -784,62 +970,72 @@ void key(unsigned char k, int x, int y) { } case 'v': { - if (operation == 0){ + if (operation == 0) + { MainLightPos[2] += MainLightPosVar; - } - else if (operation == 1){ + } else if (operation == 1) + { if (MainLightColor[2] + MainLightColorVar <= 1.0) MainLightColor[2] += MainLightColorVar; } break; } - case 'p':{ + case 'p': + { sprintf(snapScreenFileName, "ScreenShot%03d.bmp", ++snapScreenCnt); snapScreen(screenWidth, screenHeight, snapScreenFileName); } - case 't': { + case 't': + { SPlightDir[2] -= 0.05; break; } - case 'f': { + case 'f': + { SPlightDir[0] -= 0.05; break; } - case 'g': { + case 'g': + { SPlightDir[2] += 0.05; break; } - case 'h': { + case 'h': + { SPlightDir[0] += 0.05; break; } - case 'n': { + case 'n': + { SPspotangle -= 1.0f; - if( SPspotangle < 0.0f ) SPspotangle = 30.0f; + if (SPspotangle < 0.0f) SPspotangle = 30.0f; break; } - case 'm': { + case 'm': + { SPspotangle += 1.0f; - if( SPspotangle > 60.0 ) SPspotangle = 30.0f; + if (SPspotangle > 60.0) SPspotangle = 30.0f; break; } - case 'b': { + case 'b': + { light_color_index++; - if(light_color_index > 9) light_color_index = 0; + if (light_color_index > 9) light_color_index = 0; break; } } - + // if (!titleStateChanged) // sprintf(titleBuffer, "CG Project - それは僕たちの奇跡"), // glutSetWindowTitle(titleBuffer); } -void reshape(int w, int h) { +void reshape(int w, int h) +{ glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); - gluPerspective(75.0f, (float)w / h, 1.0f, 1000.0f); + gluPerspective(75.0f, (float) w / h, 1.0f, 1000.0f); glMatrixMode(GL_MODELVIEW); } @@ -849,9 +1045,10 @@ void idle() } -void initGlobalScene() { +void initGlobalScene() +{ glClearColor(0.0, 0.0, 0.3, 1.0); - + initTextureVideo(); } @@ -859,29 +1056,31 @@ void getFPS() { static int frame = 0, time, timebase = 0; static char buffer[256] = {0}; - + char mode[64] = {0}; - - + + frame++; time = glutGet(GLUT_ELAPSED_TIME); - if (time - timebase > 1000) { + if (time - timebase > 1000) + { sprintf(buffer, "FPS:%4.2f %s", - frame*1000.0 / (time - timebase), mode); + frame * 1000.0 / (time - timebase), mode); timebase = time; frame = 0; } - + glutSetWindowTitle(buffer); } GLuint stage, kotori; -GLuint glm_drawOBJ(char * filename){ +GLuint glm_drawOBJ(char *filename) +{ GLMmodel *glm_model; GLuint list; glm_model = glmReadOBJ(filename); - stage2M=glm_model; + stage2M = glm_model; glmUnitize(glm_model); glmScale(glm_model, 20); glmFacetNormals(glm_model); @@ -891,18 +1090,20 @@ GLuint glm_drawOBJ(char * filename){ return list; } -void loadOBJ(){ +void loadOBJ() +{ stage = glm_drawOBJ("model/stage_big.obj"); // kotori = glm_drawOBJ("model/kotori_sbk_test.obj"); - + } -void display() { - +void display() +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); - + //背景 glDisable(GL_DEPTH_TEST); glDisable(GL_LIGHTING); @@ -927,36 +1128,35 @@ void display() { glMatrixMode(GL_MODELVIEW); } glEnable(GL_DEPTH_TEST); - glShadeModel(GL_SMOOTH); + glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); - + glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, 1, 0.1f, 1000.0f); glMatrixMode(GL_MODELVIEW); - + //First person View - if (ViewMode==3) + if (ViewMode == 3) { gluLookAt(eye[0], eye[1], eye[2], center[0], center[1], center[2], 0, 1, 0); //从视点看远点,y轴方向(0,1,0)是上方向 } - //Bird View + //Bird View else //if( operation == 2) { gluLookAt(FP_eye[0], FP_eye[1], FP_eye[2], FP_center[0], FP_center[1], FP_center[2], FP_up[0], FP_up[1], FP_up[2]); } - + //大灯 glEnable(GL_LIGHTING); glLightfv(GL_LIGHT0, GL_POSITION, MainLightPos); glLightfv(GL_LIGHT0, GL_AMBIENT, MainLightColor); - if( ifmainlight ) + if (ifmainlight) { glEnable(GL_LIGHT0); - } - else + } else { glDisable(GL_LIGHT0); } @@ -966,35 +1166,33 @@ void display() { glLightfv(GL_LIGHT1, GL_DIFFUSE, white); //设置漫射光成分 glLightfv(GL_LIGHT1, GL_SPECULAR, white); //设置镜面光成分 glLightfv(GL_LIGHT1, GL_POSITION, FLposition); //设置光源位置 - glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, FLspotangle); //裁减角度 + glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, FLspotangle); //裁减角度 glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, FLlightDir); //光源方向 glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 30); - if( ifflashlight ) + if (ifflashlight) { glEnable(GL_LIGHT1); - } - else + } else { glDisable(GL_LIGHT1); } - + //聚光灯 glLightfv(GL_LIGHT2, GL_AMBIENT, light_color[light_color_index]); //设置环境光成分 glLightfv(GL_LIGHT2, GL_DIFFUSE, white); //设置漫射光成分 glLightfv(GL_LIGHT2, GL_SPECULAR, white); //设置镜面光成分 glLightfv(GL_LIGHT2, GL_POSITION, SPposition); //设置光源位置 - glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, SPspotangle); //裁减角度 + glLightf(GL_LIGHT2, GL_SPOT_CUTOFF, SPspotangle); //裁减角度 glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, SPlightDir); //光源方向 glLightf(GL_LIGHT2, GL_SPOT_EXPONENT, 30); - if( ifspotlight ) + if (ifspotlight) { glEnable(GL_LIGHT2); - } - else + } else { glDisable(GL_LIGHT2); } - + // glMatrixMode(GL_MODELVIEW); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glEnable(GL_TEXTURE_2D); @@ -1002,44 +1200,45 @@ void display() { glScaled(-1, 1, 1); glTranslatef(0, 10, 3); //textureObjectCnt=6; - for (int ii = 0; ii < textureObjectCnt; ii++) { - glBindTexture(GL_TEXTURE_2D, textureObjects[textureObjectCnt-1-ii]); - GLMmodel *stage2=stage2M; + for (int ii = 0; ii < textureObjectCnt; ii++) + { + glBindTexture(GL_TEXTURE_2D, textureObjects[textureObjectCnt - 1 - ii]); + GLMmodel *stage2 = stage2M; //GLMgroup *group=stage2->groups; //glPushMatrix(); //group-> //glmDraw(stage2->groups,GLM_SMOOTH | GLM_MATERIAL | GLM_TEXTURE); // stage2->groups=stage2->groups->next; //cout<, <#float h#>, <#float d#>) - - if( ifmodelshow ) + + if (ifmodelshow) { // glEnable(GL_TEXTURE_2D); // glBindTexture(GL_TEXTURE_2D, texture[1]); glPushMatrix(); - glMaterialfv(GL_FRONT,GL_AMBIENT,no_mat); - glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse); - glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular); - glMaterialfv(GL_FRONT,GL_SHININESS,low_shininess); - glMaterialfv(GL_FRONT,GL_EMISSION,no_mat); - + glMaterialfv(GL_FRONT, GL_AMBIENT, no_mat); + glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); + glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); + glMaterialfv(GL_FRONT, GL_SHININESS, low_shininess); + glMaterialfv(GL_FRONT, GL_EMISSION, no_mat); + // glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse); //散射颜色 // glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); //镜面反射颜色 // glMateriali(GL_FRONT_AND_BACK,GL_SHININESS,50); glRotatef(180, 0, 1, 0); glScaled(0.4, 0.22, 0.3); glTranslatef(0, 2.7, 20); - - if( ifmodelscale ) + + if (ifmodelscale) { glScaled(modelscale, modelscale, modelscale); modelscale -= 0.02; - if( modelscale <= 0.2 ) + if (modelscale <= 0.2) modelscale = 2.0; } - if( ifmodeltranslate ) + if (ifmodeltranslate) { glTranslatef(modeltranslate, 0, 0); modeltranslate += 0.2; - if( modeltranslate >= 5.0 ) + if (modeltranslate >= 5.0) modeltranslate = -5.0; } - if( ifmodelrotate ) + if (ifmodelrotate) { glRotatef(modelrotate, 0, 1, 0); modelrotate += 2; - if( modelrotate == 360 ) + if (modelrotate == 360) modelrotate = 0; } - + gluBeginSurface(pNurb); gluNurbsSurface( - pNurb,//指针指向NURBS渲染器 - 8, - Knots,//u定义域内的结点个数,以及结点序列 - 8, - Knots,//v定义域内的结点个数,以及结点序列 - 4*3 ,//u方向上控制点的间隔 - 3,//v方向上控制点的间隔 - &ctrlPoints[0][0][0],//控制点数组 - 4, - 4,//u, v 的次数 - GL_MAP2_VERTEX_3);//表面的类型 + pNurb,//指针指向NURBS渲染器 + 8, + Knots,//u定义域内的结点个数,以及结点序列 + 8, + Knots,//v定义域内的结点个数,以及结点序列 + 4 * 3,//u方向上控制点的间隔 + 3,//v方向上控制点的间隔 + &ctrlPoints[0][0][0],//控制点数组 + 4, + 4,//u, v 的次数 + GL_MAP2_VERTEX_3);//表面的类型 //完成定义 gluEndSurface(pNurb); glPopMatrix(); // glDisable(GL_TEXTURE_2D); } - + fRotate += 5; if (fRotate == 365) fRotate = 0; - + //getFPS(); glutSwapBuffers(); } -void getVideoFrame(int value){ +void getVideoFrame(int value) +{ displayFPS++; displayFPS = displayFPS % 30; display(); @@ -1139,12 +1339,12 @@ void texload(int i, char *filename) readBMP(filename, RGBpic); // if (i == 0) // cout << RGBpic.data[0] << RGBpic.data[1] << RGBpic.data[2]< #include #include "glm.h" +#ifndef __linux #include #include +#else +#include +#include +#endif #define GL_BGR 0x80E0 //#define M_PI 3.141592653589793238 #define GL_LIGHT_MODEL_COLOR_CONTROL_EXT 0x81F8