[C++] The IN55 project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
6.8KB

  1. /*
  2. * Skia < lordbanana25 AT mailoo DOT org >
  3. *
  4. * Beerware licensed software - 2015
  5. */
  6. #include <iostream>
  7. #include <fstream>
  8. #include <string>
  9. #include <list>
  10. #include <scene.h>
  11. #include <camera.h>
  12. #include <utils.h>
  13. SceneHandler *scene;
  14. Camera c;
  15. std::list<GLuint> shaderList;
  16. GLuint shaderProg;
  17. GLuint shaderObj;
  18. float d_angle_x =0.0f;
  19. float d_angle_y =0.0f;
  20. int xorigin = 0;
  21. int yorigin = 0;
  22. bool is_l_clicked = false;
  23. bool is_r_clicked = false;
  24. bool AddShader(GLenum ShaderType, const char* pFilename)
  25. {
  26. std::string s;
  27. std::ifstream f(pFilename);
  28. if (f.is_open()) {
  29. std::string line;
  30. while (getline(f, line)) {
  31. s.append(line);
  32. s.append("\n");
  33. }
  34. f.close();
  35. } else {
  36. std::cerr << "Error reading file " << pFilename << "\n";
  37. return false;
  38. }
  39. shaderObj = glCreateShader(ShaderType);
  40. if (shaderObj == 0) {
  41. std::cerr << "Error creating shader type " << ShaderType << "\n";
  42. return false;
  43. }
  44. // Save the shader object - will be deleted in the destructor
  45. shaderList.push_back(shaderObj);
  46. const GLchar* p[1];
  47. p[0] = s.c_str();
  48. GLint Lengths[1] = { (GLint)s.size() };
  49. glShaderSource(shaderObj, 1, p, Lengths);
  50. glCompileShader(shaderObj);
  51. GLint success;
  52. glGetShaderiv(shaderObj, GL_COMPILE_STATUS, &success);
  53. if (!success) {
  54. GLchar InfoLog[1024];
  55. glGetShaderInfoLog(shaderObj, 1024, NULL, InfoLog);
  56. std::cerr << "Error compiling '" << pFilename << "': '" << InfoLog << "'\n";
  57. return false;
  58. }
  59. glAttachShader(shaderProg, shaderObj);
  60. return true;
  61. }
  62. /* Initialize OpenGL */
  63. void initGL() {
  64. glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
  65. glClearDepth(1.0f); // Set background depth to farthest
  66. glEnable(GL_LIGHTING);
  67. glEnable(GL_LIGHT0); // Uses default lighting parameters
  68. glEnable(GL_CULL_FACE);
  69. glEnable(GL_DEPTH_TEST); // Enable depth testing
  70. glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  71. glEnable(GL_NORMALIZE);
  72. glDepthFunc(GL_LEQUAL); // Set the type of depth-test
  73. glShadeModel(GL_SMOOTH); // Enable smooth shading
  74. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
  75. }
  76. void display()
  77. {
  78. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  79. glMatrixMode(GL_MODELVIEW);
  80. glClearColor(0.0, 0.0, 0.0, 0.0);
  81. glLoadIdentity();
  82. c.updateCamera();
  83. scene->render();
  84. glutSwapBuffers();
  85. }
  86. /* Timer to refresh every 15ms */
  87. void timer(int value) {
  88. glutPostRedisplay();
  89. glutTimerFunc(50, timer, 0);
  90. }
  91. /* Resize the window */
  92. void reshape(GLsizei width, GLsizei height) {
  93. if (height == 0) height = 1;
  94. GLfloat aspect = (GLfloat)width / (GLfloat)height;
  95. glViewport(0, 0, width, height);
  96. glMatrixMode(GL_PROJECTION);
  97. glLoadIdentity();
  98. gluPerspective(45.0f, aspect, 0.1f, 100.0f);
  99. }
  100. /* Manage ASCII key input */
  101. void keyInput(unsigned char key, int x, int y){
  102. glUseProgram(shaderProg);
  103. switch(key){
  104. case'1': scene->setNumAnimation(1);
  105. scene->activateAnimation(true);
  106. scene->resetNumFrame();
  107. break;
  108. case'2': scene->setNumAnimation(2);
  109. scene->activateAnimation(true);
  110. scene->resetNumFrame();
  111. break;
  112. case'3': scene->setNumAnimation(3);
  113. scene->activateAnimation(true);
  114. scene->resetNumFrame();
  115. break;
  116. default : break;
  117. }
  118. }
  119. /* Manage non-ASCII key input */
  120. void specialInput(int key, int x, int y){
  121. Vec3 newPos;
  122. switch(key){
  123. case GLUT_KEY_UP :
  124. newPos = c.getPos();
  125. newPos.y+=0.1;
  126. c.setPos(newPos);break;
  127. case GLUT_KEY_DOWN :
  128. newPos = c.getPos();
  129. newPos.y-=0.1;
  130. c.setPos(newPos);break;
  131. case GLUT_KEY_RIGHT :
  132. newPos = c.getPos();
  133. newPos.x+=0.1;
  134. c.setPos(newPos);break;
  135. case GLUT_KEY_LEFT :
  136. newPos = c.getPos();
  137. newPos.x-=0.1;
  138. c.setPos(newPos);break;
  139. default : break;
  140. }
  141. }
  142. /* Manage mouse input */
  143. void mouseInput(int button, int state, int x, int y){
  144. Vec3 newPos;
  145. switch(button){
  146. case GLUT_LEFT_BUTTON :
  147. if(state==GLUT_UP){
  148. is_l_clicked=false;
  149. }else if(state == GLUT_DOWN){
  150. is_l_clicked=true;
  151. xorigin=x;
  152. }
  153. break;
  154. case GLUT_RIGHT_BUTTON :
  155. if(state==GLUT_UP){
  156. is_r_clicked=false;
  157. }else if(state == GLUT_DOWN){
  158. is_r_clicked=true;
  159. yorigin=y;
  160. }
  161. break;
  162. case 3 :
  163. newPos = c.getPos();
  164. newPos.z-=0.1;
  165. c.setPos(newPos);break;
  166. case 4 :
  167. newPos = c.getPos();
  168. newPos.z+=0.1;
  169. c.setPos(newPos);break;
  170. default : break;
  171. }
  172. }
  173. /* Manage mouse move */
  174. void mouseMoveInput(int x, int y){
  175. Vec3 newOri;
  176. if(is_l_clicked){
  177. //d_angle_y=(y-yorigin)*0.001f;
  178. d_angle_x=(x-xorigin)*0.001f;
  179. newOri=c.getOrient();
  180. newOri.x+=d_angle_x;
  181. //newOri.y+=d_angle_y;
  182. c.setOrient(newOri);
  183. }
  184. if(is_r_clicked){
  185. d_angle_y=(y-yorigin)*0.001f;
  186. //d_angle_x=(x-xorigin)*0.001f;
  187. newOri=c.getOrient();
  188. //newOri.x+=d_angle_x;
  189. newOri.y+=d_angle_y;
  190. c.setOrient(newOri);
  191. }
  192. }
  193. int main(int argc, char** argv)
  194. {
  195. c = Camera();
  196. glutInit(&argc, argv);
  197. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  198. glutInitWindowSize(640, 480);
  199. glutInitWindowPosition(50, 50);
  200. glutCreateWindow("IN55 - Animation rendering");
  201. glewInit();
  202. glutKeyboardFunc(keyInput);
  203. glutSpecialFunc(specialInput);
  204. glutMouseFunc(mouseInput);
  205. glutMotionFunc(mouseMoveInput);
  206. glutDisplayFunc(display);
  207. glutReshapeFunc(reshape);
  208. initGL();
  209. glutTimerFunc(0, timer, 0);
  210. shaderProg = glCreateProgram();
  211. if (shaderProg == 0) {
  212. std::cerr << "Error creating shader program\n";
  213. return 1;
  214. }
  215. if (AddShader(GL_VERTEX_SHADER, "shader.vs")) {
  216. std::cout << "loading 3.3 shader\n";
  217. } else if (AddShader(GL_VERTEX_SHADER, "shader_legacy150.vs")) {
  218. std::cout << "loading 1.5 shader\n";
  219. } else if (AddShader(GL_VERTEX_SHADER, "shader_legacy120.vs")) {
  220. std::cout << "loading 1.2 shader\n";
  221. }
  222. glLinkProgram(shaderProg);
  223. scene = new SceneHandler(argv[1], shaderProg);
  224. glutMainLoop();
  225. delete scene;
  226. return 0;
  227. }