_vim_

util.c

Dec 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <math.h>
  4.  
  5. #include "util.h"
  6.  
  7. int file_size(FILE *fp)
  8. {
  9.     int prev = ftell(fp);
  10.     fseek(fp, 0L, SEEK_END);
  11.     int size = ftell(fp);
  12.     fseek(fp, prev, SEEK_SET); /* go back to where we were */
  13.  
  14.     return size;
  15. }
  16.  
  17. char *file_contents(const char *name)
  18. {
  19.     int c;
  20.     FILE *fp = fopen(name, "r");
  21.  
  22.     if (!fp)
  23.     {
  24.         fputs("Error opening file for reading!\n", stderr);
  25.         return NULL;
  26.     }
  27.  
  28.     char *ret = malloc(file_size(fp) + 1);
  29.  
  30.     if (!ret)
  31.     {
  32.         fputs("Error allocating memory for ret!\n", stderr);
  33.         return NULL;
  34.     }
  35.  
  36.     size_t i = 0;
  37.     while ((c = fgetc(fp)) != EOF)
  38.     {
  39.         ret[i] = c;
  40.         ++i;
  41.     }
  42.  
  43.     ret[i] = '\0';
  44.  
  45.     fclose(fp);
  46.  
  47.     return ret;
  48. }
  49.  
  50. void perspective(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar)
  51. {
  52.     const GLdouble pi = 3.1415926535897932384626433832795;
  53.     GLdouble fW, fH;
  54.  
  55.     //fH = tan( (fovY / 2) / 180 * pi ) * zNear;
  56.     fH = tan(fovY / 360 * pi) * zNear;
  57.     fW = fH * aspect;
  58.  
  59.     glFrustum(-fW, fW, -fH, fH, zNear, zFar);
  60. }
Add Comment
Please, Sign In to add comment