Untitled


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 9:28 p.m.

FORMAT: Text only

SIZE: 22.4 kB

HITS: 6466

  1. MAIN.CPP
  2. //Some Windows Headers (For Time, IO, etc.)
  3. #include <windows.h>
  4. #include <mmsystem.h>
  5. #include <GL/glew.h>
  6. #include <GL/freeglut.h>
  7. #include <iostream>
  8. #include "maths_funcs.h"
  9. #include "mesh.h"
  10. // Assimp includes
  11. #include <assimp/cimport.h> // C importer
  12. #include <assimp/scene.h> // collects data
  13. #include <assimp/postprocess.h> // various extra operations
  14. #include <stdio.h>
  15. #include <math.h>
  16. #include <vector> // STL dynamic memory.
  17. /*----------------------------------------------------------------------------
  18. MESH TO LOAD
  19. ----------------------------------------------------------------------------*/
  20. // this mesh is a dae file format but you should be able to use any other format too, obj is typically what is used
  21. // put the mesh in your project directory, or provide a filepath for it here
  22. #define MESHNAME1 "monkeyhead.dae"
  23. #define MESHNAME2 "chair.obj"
  24. /*----------------------------------------------------------------------------
  25. ----------------------------------------------------------------------------*/
  26. std::vector<float> g_vp, g_vn, g_vt;
  27. int g_point_count = 0;
  28. // Macro for indexing vertex buffer
  29. #define BUFFER_OFFSET(i) ((char *)NULL + (i))
  30. using namespace std;
  31. GLuint shaderProgramID;
  32. unsigned int mesh_vao = 0;
  33. int width = 800;
  34. int height = 600;
  35. GLfloat xpos = 0.0f;
  36. GLfloat ypos = 0.0f;
  37. GLfloat zpos = -5.0f;
  38. GLuint loc1, loc2, loc3;
  39. GLfloat rotate_y = 0.0f;
  40. mat4 model = identity_mat4 ();
  41. mesh chair, monkey;
  42. /*
  43. #pragma region MESH LOADING
  44. /*----------------------------------------------------------------------------
  45. MESH LOADING FUNCTION
  46. ----------------------------------------------------------------------------
  47. bool load_mesh (const char* file_name) {
  48. const aiScene* scene = aiImportFile (file_name, aiProcess_Triangulate); // TRIANGLES!
  49. if (!scene) {
  50. fprintf (stderr, "ERROR: reading mesh %s\n", file_name);
  51. return false;
  52. }
  53. printf (" %i animations\n", scene->mNumAnimations);
  54. printf (" %i cameras\n", scene->mNumCameras);
  55. printf (" %i lights\n", scene->mNumLights);
  56. printf (" %i materials\n", scene->mNumMaterials);
  57. printf (" %i meshes\n", scene->mNumMeshes);
  58. printf (" %i textures\n", scene->mNumTextures);
  59. for (unsigned int m_i = 0; m_i < scene->mNumMeshes; m_i++) {
  60. const aiMesh* mesh = scene->mMeshes[m_i];
  61. printf (" %i vertices in mesh\n", mesh->mNumVertices);
  62. g_point_count += mesh->mNumVertices;
  63. for (unsigned int v_i = 0; v_i < mesh->mNumVertices; v_i++) {
  64. if (mesh->HasPositions ()) {
  65. const aiVector3D* vp = &(mesh->mVertices[v_i]);
  66. //printf (" vp %i (%f,%f,%f)\n", v_i, vp->x, vp->y, vp->z);
  67. g_vp.push_back (vp->x);
  68. g_vp.push_back (vp->y);
  69. g_vp.push_back (vp->z);
  70. }
  71. if (mesh->HasNormals ()) {
  72. const aiVector3D* vn = &(mesh->mNormals[v_i]);
  73. //printf (" vn %i (%f,%f,%f)\n", v_i, vn->x, vn->y, vn->z);
  74. g_vn.push_back (vn->x);
  75. g_vn.push_back (vn->y);
  76. g_vn.push_back (vn->z);
  77. }
  78. if (mesh->HasTextureCoords (0)) {
  79. const aiVector3D* vt = &(mesh->mTextureCoords[0][v_i]);
  80. //printf (" vt %i (%f,%f)\n", v_i, vt->x, vt->y);
  81. g_vt.push_back (vt->x);
  82. g_vt.push_back (vt->y);
  83. }
  84. if (mesh->HasTangentsAndBitangents ()) {
  85. // NB: could store/print tangents here
  86. }
  87. }
  88. }
  89. aiReleaseImport (scene);
  90. return true;
  91. }
  92. #pragma endregion MESH LOADING
  93. */
  94. // Shader Functions- click on + to expand
  95. #pragma region SHADER_FUNCTIONS
  96. // Create a NULL-terminated string by reading the provided file
  97. char* readShaderSource(const char* shaderFile) {
  98. FILE* fp = fopen(shaderFile, "rb"); //!->Why does binary flag "RB" work and not "R"... wierd msvc thing?
  99. if ( fp == NULL ) { return NULL; }
  100. fseek(fp, 0L, SEEK_END);
  101. long size = ftell(fp);
  102. fseek(fp, 0L, SEEK_SET);
  103. char* buf = new char[size + 1];
  104. fread(buf, 1, size, fp);
  105. buf[size] = '\0';
  106. fclose(fp);
  107. return buf;
  108. }
  109. static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
  110. {
  111. // create a shader object
  112. GLuint ShaderObj = glCreateShader(ShaderType);
  113. if (ShaderObj == 0) {
  114. fprintf(stderr, "Error creating shader type %d\n", ShaderType);
  115. exit(0);
  116. }
  117. const char* pShaderSource = readShaderSource( pShaderText);
  118. // Bind the source code to the shader, this happens before compilation
  119. glShaderSource(ShaderObj, 1, (const GLchar**)&pShaderSource, NULL);
  120. // compile the shader and check for errors
  121. glCompileShader(ShaderObj);
  122. GLint success;
  123. // check for shader related errors using glGetShaderiv
  124. glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
  125. if (!success) {
  126. GLchar InfoLog[1024];
  127. glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
  128. fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
  129. exit(1);
  130. }
  131. // Attach the compiled shader object to the program object
  132. glAttachShader(ShaderProgram, ShaderObj);
  133. }
  134. GLuint CompileShaders()
  135. {
  136. //Start the process of setting up our shaders by creating a program ID
  137. //Note: we will link all the shaders together into this ID
  138. shaderProgramID = glCreateProgram();
  139. if (shaderProgramID == 0) {
  140. fprintf(stderr, "Error creating shader program\n");
  141. exit(1);
  142. }
  143. // Create two shader objects, one for the vertex, and one for the fragment shader
  144. AddShader(shaderProgramID, "../Shaders/simpleVertexShader.txt", GL_VERTEX_SHADER);
  145. AddShader(shaderProgramID, "../Shaders/simpleFragmentShader.txt", GL_FRAGMENT_SHADER);
  146. GLint Success = 0;
  147. GLchar ErrorLog[1024] = { 0 };
  148. // After compiling all shader objects and attaching them to the program, we can finally link it
  149. glLinkProgram(shaderProgramID);
  150. // check for program related errors using glGetProgramiv
  151. glGetProgramiv(shaderProgramID, GL_LINK_STATUS, &Success);
  152. if (Success == 0) {
  153. glGetProgramInfoLog(shaderProgramID, sizeof(ErrorLog), NULL, ErrorLog);
  154. fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
  155. exit(1);
  156. }
  157. // program has been successfully linked but needs to be validated to check whether the program can execute given the current pipeline state
  158. glValidateProgram(shaderProgramID);
  159. // check for program related errors using glGetProgramiv
  160. glGetProgramiv(shaderProgramID, GL_VALIDATE_STATUS, &Success);
  161. if (!Success) {
  162. glGetProgramInfoLog(shaderProgramID, sizeof(ErrorLog), NULL, ErrorLog);
  163. fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
  164. exit(1);
  165. }
  166. // Finally, use the linked shader program
  167. // Note: this program will stay in effect for all draw calls until you replace it with another or explicitly disable its use
  168. glUseProgram(shaderProgramID);
  169. return shaderProgramID;
  170. }
  171. #pragma endregion SHADER_FUNCTIONS
  172. // VBO Functions - click on + to expand
  173. #pragma region VBO_FUNCTIONS
  174. /*
  175. void generateObjectBufferMesh() {
  176. /*----------------------------------------------------------------------------
  177. LOAD MESH HERE AND COPY INTO BUFFERS
  178. ----------------------------------------------------------------------------
  179. //Note: you may get an error "vector subscript out of range" if you are using this code for a mesh that doesnt have positions and normals
  180. //Might be an idea to do a check for that before generating and binding the buffer.
  181. //load_mesh (MESH_NAME1);
  182. //int variable = g_point_count;
  183. load_mesh (MESH_NAME2);
  184. unsigned int vp_vbo = 0;
  185. loc1 = glGetAttribLocation(shaderProgramID, "vertex_position");
  186. loc2 = glGetAttribLocation(shaderProgramID, "vertex_normal");
  187. loc3 = glGetAttribLocation(shaderProgramID, "vertex_texture");
  188. glGenBuffers (1, &vp_vbo);
  189. glBindBuffer (GL_ARRAY_BUFFER, vp_vbo);
  190. glBufferData (GL_ARRAY_BUFFER, g_point_count * 3 * sizeof (float), &g_vp[0], GL_STATIC_DRAW);
  191. unsigned int vn_vbo = 0;
  192. glGenBuffers (1, &vn_vbo);
  193. glBindBuffer (GL_ARRAY_BUFFER, vn_vbo);
  194. glBufferData (GL_ARRAY_BUFFER, g_point_count * 3 * sizeof (float), &g_vn[0], GL_STATIC_DRAW);
  195. // This is for texture coordinates which you don't currently need, so I have commented it out
  196. // unsigned int vt_vbo = 0;
  197. // glGenBuffers (1, &vt_vbo);
  198. // glBindBuffer (GL_ARRAY_BUFFER, vt_vbo);
  199. // glBufferData (GL_ARRAY_BUFFER, g_point_count * 2 * sizeof (float), &g_vt[0], GL_STATIC_DRAW);
  200. unsigned int vao = 0;
  201. glBindVertexArray (vao);
  202. glEnableVertexAttribArray (loc1);
  203. glBindBuffer (GL_ARRAY_BUFFER, vp_vbo);
  204. glVertexAttribPointer (loc1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  205. glEnableVertexAttribArray (loc2);
  206. glBindBuffer (GL_ARRAY_BUFFER, vn_vbo);
  207. glVertexAttribPointer (loc2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  208. // This is for texture coordinates which you don't currently need, so I have commented it out
  209. // glEnableVertexAttribArray (loc3);
  210. // glBindBuffer (GL_ARRAY_BUFFER, vt_vbo);
  211. // glVertexAttribPointer (loc3, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  212. }
  213. #pragma endregion VBO_FUNCTIONS
  214. */
  215. void display(){
  216. // tell GL to only draw onto a pixel if the shape is closer to the viewer
  217. glEnable (GL_DEPTH_TEST); // enable depth-testing
  218. glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
  219. glClearColor (0.5f, 0.5f, 0.5f, 1.0f);
  220. glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  221. glUseProgram (shaderProgramID);
  222. //Declare your uniform variables that will be used in your shader
  223. int matrix_location = glGetUniformLocation (shaderProgramID, "model");
  224. int view_mat_location = glGetUniformLocation (shaderProgramID, "view");
  225. int proj_mat_location = glGetUniformLocation (shaderProgramID, "proj");
  226. monkey.view = identity_mat4();
  227. monkey.persp_proj = perspective(45.0, (float)width/(float)height, 0.1, 100.0);
  228. monkey.view = translate (monkey.view, vec3 (xpos, ypos, zpos));
  229. /*
  230. // Root of the Hierarchy
  231. mat4 view = identity_mat4 ();
  232. mat4 persp_proj = perspective(45.0, (float)width/(float)height, 0.1, 100.0);
  233. model = rotate_y_deg (model, rotate_y);
  234. view = translate (view, vec3 (xpos, ypos, zpos));
  235. // update uniforms & draw
  236. glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, persp_proj.m);
  237. glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view.m);
  238. glUniformMatrix4fv (matrix_location, 1, GL_FALSE, model.m);
  239. */
  240. glutSwapBuffers();
  241. }
  242. void updateScene() {
  243. // Placeholder code, if you want to work with framerate
  244. // Wait until at least 16ms passed since start of last frame (Effectively caps framerate at ~60fps)
  245. static DWORD last_time = 0;
  246. DWORD curr_time = timeGetTime();
  247. float delta = (curr_time - last_time) * 0.001f;
  248. if (delta > 0.03f)
  249. delta = 0.03f;
  250. last_time = curr_time;
  251. // rotate the model slowly around the y axis
  252. //rotate_y+=0.0002f;
  253. // Draw the next frame
  254. glutPostRedisplay();
  255. }
  256. void init()
  257. {
  258. // Set up the shaders
  259. GLuint shaderProgramID = CompileShaders();
  260. // load mesh into a vertex buffer array
  261. monkey.generateObjectBufferMesh(shaderProgramID, MESHNAME1);
  262. chair.generateObjectBufferMesh(shaderProgramID, MESHNAME2);
  263. }
  264. void scale1()
  265. {
  266. mat4 scalar = identity_mat4();
  267. scalar = scale(scalar, vec3(0.8, 0.8, 0.8));
  268. model = model * scalar;
  269. }
  270. void scale2()
  271. {
  272. mat4 scalar = identity_mat4();
  273. scalar = scale(scalar, vec3(1.2, 1.2, 1.2));
  274. model = model * scalar;
  275. }
  276. // Placeholder code for the keypress
  277. void keypress(unsigned char key, int x, int y) {
  278. switch (key)
  279. {
  280. case 'w':
  281. ypos += 0.1;
  282. break;
  283. case 'a':
  284. xpos -= 0.1;
  285. break;
  286. case 's':
  287. ypos -= 0.1;
  288. break;
  289. case 'd':
  290. xpos += 0.1;
  291. break;
  292. case '1':
  293. scale1();
  294. break;
  295. case '2':
  296. scale2();
  297. break;
  298. }
  299. }
  300. int main(int argc, char** argv){
  301. // Set up the window
  302. glutInit(&argc, argv);
  303. glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  304. glutInitWindowSize(width, height);
  305. glutCreateWindow("Hello Triangle");
  306. // Tell glut where the display function is
  307. glutDisplayFunc(display);
  308. glutIdleFunc(updateScene);
  309. glutKeyboardFunc(keypress);
  310. // A call to glewInit() must be done after glut is initialized!
  311. GLenum res = glewInit();
  312. // Check for any errors
  313. if (res != GLEW_OK) {
  314. fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
  315. return 1;
  316. }
  317. // Set up your objects and shaders
  318. init();
  319. // Begin infinite event loop
  320. glutMainLoop();
  321. return 0;
  322. }
  323. MESH.CPP
  324. //Some Windows Headers (For Time, IO, etc.)
  325. #include <windows.h>
  326. #include <mmsystem.h>
  327. #include <GL/glew.h>
  328. #include <GL/freeglut.h>
  329. #include <iostream>
  330. #include "maths_funcs.h"
  331. #include "mesh.h"
  332. // Assimp includes
  333. #include <assimp/cimport.h> // C importer
  334. #include <assimp/scene.h> // collects data
  335. #include <assimp/postprocess.h> // various extra operations
  336. #include <stdio.h>
  337. #include <math.h>
  338. #include <vector> // STL dynamic memory.
  339. unsigned int vp_vbo = 0;
  340. unsigned int vn_vbo = 0;
  341. //loadmesh, genertateobjectbufferMesh, draw
  342. bool mesh::load_mesh (const char* file_name) {
  343. const aiScene* scene = aiImportFile (file_name, aiProcess_Triangulate); // TRIANGLES!
  344. if (!scene) {
  345. fprintf (stderr, "ERROR: reading mesh %s\n", file_name);
  346. return false;
  347. }
  348. printf (" %i animations\n", scene->mNumAnimations);
  349. printf (" %i cameras\n", scene->mNumCameras);
  350. printf (" %i lights\n", scene->mNumLights);
  351. printf (" %i materials\n", scene->mNumMaterials);
  352. printf (" %i meshes\n", scene->mNumMeshes);
  353. printf (" %i textures\n", scene->mNumTextures);
  354. g_point_count = 0;
  355. for (unsigned int m_i = 0; m_i < scene->mNumMeshes; m_i++) {
  356. const aiMesh* mesh = scene->mMeshes[m_i];
  357. printf (" %i vertices in mesh\n", mesh->mNumVertices);
  358. g_point_count += mesh->mNumVertices;
  359. for (unsigned int v_i = 0; v_i < mesh->mNumVertices; v_i++) {
  360. if (mesh->HasPositions ()) {
  361. const aiVector3D* vp = &(mesh->mVertices[v_i]);
  362. //printf (" vp %i (%f,%f,%f)\n", v_i, vp->x, vp->y, vp->z);
  363. g_vp.push_back (vp->x);
  364. g_vp.push_back (vp->y);
  365. g_vp.push_back (vp->z);
  366. }
  367. if (mesh->HasNormals ()) {
  368. const aiVector3D* vn = &(mesh->mNormals[v_i]);
  369. //printf (" vn %i (%f,%f,%f)\n", v_i, vn->x, vn->y, vn->z);
  370. g_vn.push_back (vn->x);
  371. g_vn.push_back (vn->y);
  372. g_vn.push_back (vn->z);
  373. }
  374. if (mesh->HasTextureCoords (0)) {
  375. const aiVector3D* vt = &(mesh->mTextureCoords[0][v_i]);
  376. //printf (" vt %i (%f,%f)\n", v_i, vt->x, vt->y);
  377. g_vt.push_back (vt->x);
  378. g_vt.push_back (vt->y);
  379. }
  380. if (mesh->HasTangentsAndBitangents ()) {
  381. // NB: could store/print tangents here
  382. }
  383. }
  384. }
  385. aiReleaseImport (scene);
  386. return true;
  387. }
  388. void mesh::generateObjectBufferMesh(GLuint shaderProgramID, const char* file_name) {
  389. /*----------------------------------------------------------------------------
  390. LOAD MESH HERE AND COPY INTO BUFFERS
  391. ----------------------------------------------------------------------------*/
  392. //Note: you may get an error "vector subscript out of range" if you are using this code for a mesh that doesnt have positions and normals
  393. //Might be an idea to do a check for that before generating and binding the buffer.
  394. //load_mesh (MESH_NAME1);
  395. //int variable = g_point_count;
  396. load_mesh (file_name);
  397. loc1 = glGetAttribLocation(shaderProgramID, "vertex_position");
  398. loc2 = glGetAttribLocation(shaderProgramID, "vertex_normal");
  399. loc3 = glGetAttribLocation(shaderProgramID, "vertex_texture");
  400. glGenBuffers (1, &vp_vbo);
  401. glBindBuffer (GL_ARRAY_BUFFER, vp_vbo);
  402. glBufferData (GL_ARRAY_BUFFER, g_point_count * 3 * sizeof (float), &g_vp[0], GL_STATIC_DRAW);
  403. glGenBuffers (1, &vn_vbo);
  404. glBindBuffer (GL_ARRAY_BUFFER, vn_vbo);
  405. glBufferData (GL_ARRAY_BUFFER, g_point_count * 3 * sizeof (float), &g_vn[0], GL_STATIC_DRAW);
  406. // This is for texture coordinates which you don't currently need, so I have commented it out
  407. // unsigned int vt_vbo = 0;
  408. // glGenBuffers (1, &vt_vbo);
  409. // glBindBuffer (GL_ARRAY_BUFFER, vt_vbo);
  410. // glBufferData (GL_ARRAY_BUFFER, g_point_count * 2 * sizeof (float), &g_vt[0], GL_STATIC_DRAW);
  411. unsigned int vao = 0;
  412. glBindVertexArray (vao);
  413. // This is for texture coordinates which you don't currently need, so I have commented it out
  414. // glEnableVertexAttribArray (loc3);
  415. // glBindBuffer (GL_ARRAY_BUFFER, vt_vbo);
  416. // glVertexAttribPointer (loc3, 2, GL_FLOAT, GL_FALSE, 0, NULL);
  417. }
  418. void mesh::draw(int width, int heigth, int matrix_location, int view_mat_location, int proj_mat_location)
  419. {
  420. glEnableVertexAttribArray (loc1);
  421. glBindBuffer (GL_ARRAY_BUFFER, vp_vbo);
  422. glVertexAttribPointer (loc1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  423. glEnableVertexAttribArray (loc2);
  424. glBindBuffer (GL_ARRAY_BUFFER, vn_vbo);
  425. glVertexAttribPointer (loc2, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  426. glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, persp_proj.m);
  427. glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view.m);
  428. glUniformMatrix4fv (matrix_location, 1, GL_FALSE, model.m);
  429. glDrawArrays (GL_TRIANGLES, 0, g_point_count);
  430. }

comments powered by Disqus