Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <GLFW/glfw3.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- typedef struct {
- float x, y, z;
- float yaw, pitch;
- } camera_t;
- camera_t camera;
- camera_t camera_new(void) {
- camera_t camera;
- memset(&camera, 0, sizeof(camera_t));
- return camera;
- }
- void camera_update(camera_t *camera) {
- glTranslatef(-camera->x, -camera->y, -camera->z);
- glRotatef(-camera->pitch, 1, 0, 0);
- glRotatef(-camera->yaw, 0, 1, 0);
- }
- void init() {
- glClearColor(1, 0, 0, 1);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- const int eye_angle = 45;
- const float cam_len = 1.0f;
- const float dim_ratio = (float)640/(float)480;
- gluPerspective(eye_angle, dim_ratio, cam_len, 500.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- camera = camera_new();
- camera.pitch = 10;
- }
- void draw() {
- camera_update(&camera);
- glBegin(GL_TRIANGLES);
- glVertex3f(0.0,2.0,-5.0);
- glVertex3f(-2.0,-2.0,-5.0);
- glVertex3f(2.0,-2.0,-5.0);
- glEnd();
- }
- void cb_keyboard(GLFWwindow *window, int key, int scancode, int action, int mods) {
- if (key == GLFW_KEY_W && action == GLFW_PRESS)
- camera.z += 1;
- else if (key == GLFW_KEY_S && action == GLFW_PRESS)
- camera.z -= 1;
- camera_update(&camera);
- }
- int main() {
- GLFWwindow *window;
- if (!glfwInit())
- return 1;
- window = glfwCreateWindow(640, 480, "Ethan's 3D Engine", NULL, NULL);
- if (!window) {
- glfwTerminate();
- return -1;
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, &cb_keyboard);
- init();
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT);
- glPushMatrix();
- draw();
- glfwSwapBuffers(window);
- glPopMatrix();
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment