fork download
  1. /* Copyright (C) 2011 by Eddy Luten
  2.  
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9.  
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12.  
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21.  
  22. #include "Utils.h"
  23.  
  24. float Cotangent(float angle)
  25. {
  26. return (float)(1.0 / tan(angle));
  27. }
  28.  
  29. float DegreesToRadians(float degrees)
  30. {
  31. return degrees * (float)(PI / 180);
  32. }
  33.  
  34. float RadiansToDegrees(float radians)
  35. {
  36. return radians * (float)(180 / PI);
  37. }
  38.  
  39. Matrix MultiplyMatrices(const Matrix* m1, const Matrix* m2)
  40. {
  41. Matrix out = IDENTITY_MATRIX;
  42. unsigned int row, column, row_offset;
  43.  
  44. for (row = 0, row_offset = row * 4; row < 4; ++row, row_offset = row * 4)
  45. for (column = 0; column < 4; ++column)
  46. out.m[row_offset + column] =
  47. (m1->m[row_offset + 0] * m2->m[column + 0]) +
  48. (m1->m[row_offset + 1] * m2->m[column + 4]) +
  49. (m1->m[row_offset + 2] * m2->m[column + 8]) +
  50. (m1->m[row_offset + 3] * m2->m[column + 12]);
  51.  
  52. return out;
  53. }
  54.  
  55. void ScaleMatrix(Matrix* m, float x, float y, float z)
  56. {
  57. Matrix scale = IDENTITY_MATRIX;
  58.  
  59. scale.m[0] = x;
  60. scale.m[5] = y;
  61. scale.m[10] = z;
  62.  
  63. memcpy(m->m, MultiplyMatrices(m, &scale).m, sizeof(m->m));
  64. }
  65.  
  66. void TranslateMatrix(Matrix* m, float x, float y, float z)
  67. {
  68. Matrix translation = IDENTITY_MATRIX;
  69.  
  70. translation.m[12] = x;
  71. translation.m[13] = y;
  72. translation.m[14] = z;
  73.  
  74. memcpy(m->m, MultiplyMatrices(m, &translation).m, sizeof(m->m));
  75. }
  76.  
  77. void RotateAboutX(Matrix* m, float angle)
  78. {
  79. Matrix rotation = IDENTITY_MATRIX;
  80. float sine = (float)sin(angle);
  81. float cosine = (float)cos(angle);
  82.  
  83. rotation.m[5] = cosine;
  84. rotation.m[6] = -sine;
  85. rotation.m[9] = sine;
  86. rotation.m[10] = cosine;
  87.  
  88. memcpy(m->m, MultiplyMatrices(m, &rotation).m, sizeof(m->m));
  89. }
  90.  
  91. void RotateAboutY(Matrix* m, float angle)
  92. {
  93. Matrix rotation = IDENTITY_MATRIX;
  94. float sine = (float)sin(angle);
  95. float cosine = (float)cos(angle);
  96.  
  97. rotation.m[0] = cosine;
  98. rotation.m[8] = sine;
  99. rotation.m[2] = -sine;
  100. rotation.m[10] = cosine;
  101.  
  102. memcpy(m->m, MultiplyMatrices(m, &rotation).m, sizeof(m->m));
  103. }
  104.  
  105. void RotateAboutZ(Matrix* m, float angle)
  106. {
  107. Matrix rotation = IDENTITY_MATRIX;
  108. float sine = (float)sin(angle);
  109. float cosine = (float)cos(angle);
  110.  
  111. rotation.m[0] = cosine;
  112. rotation.m[1] = -sine;
  113. rotation.m[4] = sine;
  114. rotation.m[5] = cosine;
  115.  
  116. memcpy(m->m, MultiplyMatrices(m, &rotation).m, sizeof(m->m));
  117. }
  118.  
  119. Matrix CreateProjectionMatrix(
  120. float fovy,
  121. float aspect_ratio,
  122. float near_plane,
  123. float far_plane
  124. )
  125. {
  126. Matrix out = { { 0 } };
  127.  
  128. const float
  129. y_scale = Cotangent(DegreesToRadians(fovy / 2)),
  130. x_scale = y_scale / aspect_ratio,
  131. frustum_length = far_plane - near_plane;
  132.  
  133. out.m[0] = x_scale;
  134. out.m[5] = y_scale;
  135. out.m[10] = -((far_plane + near_plane) / frustum_length);
  136. out.m[11] = -1;
  137. out.m[14] = -((2 * near_plane * far_plane) / frustum_length);
  138.  
  139. return out;
  140. }
  141.  
  142. void ExitOnGLError(const char* error_message)
  143. {
  144. const GLenum ErrorValue = glGetError();
  145.  
  146. if (ErrorValue != GL_NO_ERROR)
  147. {
  148. const char* APPEND_DETAIL_STRING = ": %s\n";
  149. const size_t APPEND_LENGTH = strlen(APPEND_DETAIL_STRING) + 1;
  150. const size_t message_length = strlen(error_message);
  151. char* display_message = (char*)malloc(message_length + APPEND_LENGTH);
  152.  
  153. memcpy(display_message, error_message, message_length);
  154. memcpy(&display_message[message_length], APPEND_DETAIL_STRING, APPEND_LENGTH);
  155.  
  156. fprintf(stderr, display_message, gluErrorString(ErrorValue));
  157.  
  158. free(display_message);
  159. exit(EXIT_FAILURE);
  160. }
  161. }
  162.  
  163. GLuint LoadShader(const char* filename, GLenum shader_type)
  164. {
  165. GLuint shader_id = 0;
  166. FILE* file;
  167. long file_size = -1;
  168. char* glsl_source;
  169.  
  170. if (NULL != (file = fopen(filename, "rb")) &&
  171. 0 == fseek(file, 0, SEEK_END) &&
  172. -1 != (file_size = ftell(file)))
  173. {
  174. rewind(file);
  175.  
  176. if (NULL != (glsl_source = (char*)malloc(file_size + 1)))
  177. {
  178. if (file_size == (long)fread(glsl_source, sizeof(char), file_size, file))
  179. {
  180. glsl_source[file_size] = '\0';
  181.  
  182. if (0 != (shader_id = glCreateShader(shader_type)))
  183. {
  184. glShaderSource(shader_id, 1, &glsl_source, NULL);
  185. glCompileShader(shader_id);
  186. ExitOnGLError("Could not compile a shader");
  187. }
  188. else
  189. fprintf(stderr, "ERROR: Could not create a shader.\n");
  190. }
  191. else
  192. fprintf(stderr, "ERROR: Could not read file %s\n", filename);
  193.  
  194. free(glsl_source);
  195. }
  196. else
  197. fprintf(stderr, "ERROR: Could not allocate %i bytes.\n", file_size);
  198.  
  199. fclose(file);
  200. }
  201. else
  202. fprintf(stderr, "ERROR: Could not open file %s\n", filename);
  203.  
  204. return shader_id;
  205. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:22:19: fatal error: Utils.h: No such file or directory
 #include "Utils.h"
                   ^
compilation terminated.
stdout
Standard output is empty