Guest User

emd's engine

a guest
Feb 11th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <GLFW/glfw3.h>
  4. #include <GL/gl.h>
  5. #include <GL/glu.h>
  6.  
  7. typedef struct {
  8. float x, y, z;
  9. float yaw, pitch;
  10. } camera_t;
  11.  
  12. camera_t camera;
  13.  
  14. camera_t camera_new(void) {
  15. camera_t camera;
  16. memset(&camera, 0, sizeof(camera_t));
  17. return camera;
  18. }
  19.  
  20. void camera_update(camera_t *camera) {
  21. glTranslatef(-camera->x, -camera->y, -camera->z);
  22. glRotatef(-camera->pitch, 1, 0, 0);
  23. glRotatef(-camera->yaw, 0, 1, 0);
  24. }
  25.  
  26. void init() {
  27. glClearColor(1, 0, 0, 1);
  28. glMatrixMode(GL_PROJECTION);
  29. glLoadIdentity();
  30. const int eye_angle = 45;
  31. const float cam_len = 1.0f;
  32. const float dim_ratio = (float)640/(float)480;
  33. gluPerspective(eye_angle, dim_ratio, cam_len, 500.0);
  34. glMatrixMode(GL_MODELVIEW);
  35. glLoadIdentity();
  36. camera = camera_new();
  37. camera.pitch = 10;
  38.  
  39. }
  40.  
  41. void draw() {
  42. camera_update(&camera);
  43. glBegin(GL_TRIANGLES);
  44. glVertex3f(0.0,2.0,-5.0);
  45. glVertex3f(-2.0,-2.0,-5.0);
  46. glVertex3f(2.0,-2.0,-5.0);
  47. glEnd();
  48.  
  49. }
  50.  
  51. void cb_keyboard(GLFWwindow *window, int key, int scancode, int action, int mods) {
  52. if (key == GLFW_KEY_W && action == GLFW_PRESS)
  53. camera.z += 1;
  54. else if (key == GLFW_KEY_S && action == GLFW_PRESS)
  55. camera.z -= 1;
  56. camera_update(&camera);
  57. }
  58.  
  59. int main() {
  60. GLFWwindow *window;
  61. if (!glfwInit())
  62. return 1;
  63. window = glfwCreateWindow(640, 480, "Ethan's 3D Engine", NULL, NULL);
  64. if (!window) {
  65. glfwTerminate();
  66. return -1;
  67. }
  68. glfwMakeContextCurrent(window);
  69. glfwSetKeyCallback(window, &cb_keyboard);
  70.  
  71. init();
  72.  
  73. while (!glfwWindowShouldClose(window)) {
  74. glClear(GL_COLOR_BUFFER_BIT);
  75. glPushMatrix();
  76. draw();
  77. glfwSwapBuffers(window);
  78. glPopMatrix();
  79. glfwPollEvents();
  80. }
  81. glfwTerminate();
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment