Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // gcc -o test.exe -mwindows -mconsole test.c glad.c -lopengl32 -lglfw3
- #include <stdio.h>
- #include <glad\glad.h>
- #include <GLFW\glfw3.h>
- float step = 0.1f;
- float scale = 1.04f;
- #define PAN_X_DEFAULT -0.1f
- #define PAN_Y_DEFAULT -0.35f
- #define ZOOM_K_DEFAULT 180.0f
- GLFWmonitor* test_monitor = NULL;
- GLFWwindow* test_window = NULL;
- int test_width = 1024;
- int test_height = 780;
- BOOL test_fullscreen = FALSE;
- GLfloat pan_x = PAN_X_DEFAULT;
- GLfloat pan_y = PAN_Y_DEFAULT;
- GLfloat zoom_k = ZOOM_K_DEFAULT;
- int test_verbose = 2;
- GLvoid reset(GLvoid)
- {
- pan_x = PAN_X_DEFAULT;
- pan_y = PAN_Y_DEFAULT;
- zoom_k = ZOOM_K_DEFAULT;
- }
- GLvoid zoom(double factor)
- {
- zoom_k *= factor;
- if (zoom_k < 0.01f) zoom_k = 0.01f;
- if (zoom_k > 1000000.0f) zoom_k = 1000000.0f;
- }
- GLvoid pan(double dx, double dy)
- {
- pan_x += dx / zoom_k;
- pan_y += dy / zoom_k;
- }
- GLvoid zoom_at(double x, double y, double factor)
- {
- pan(-x, -y);
- zoom(factor);
- pan(x, y);
- }
- void error_callback(int error, const char* description)
- {
- fprintf(stderr, "Error: %s\n", description);
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (action != GLFW_PRESS)
- return;
- switch (key)
- {
- case GLFW_KEY_ESCAPE:
- glfwSetWindowShouldClose(window, GLFW_TRUE);
- break;
- case GLFW_KEY_HOME:
- if (test_verbose >= 2) printf("reset to original view\n");
- reset();
- break;
- case GLFW_KEY_UP:
- if (test_verbose >= 2) printf("move up\n");
- pan(0, -step);
- break;
- case GLFW_KEY_DOWN:
- if (test_verbose >= 2) printf("move down\n");
- pan(0, step);
- break;
- case GLFW_KEY_RIGHT:
- if (test_verbose >= 2) printf("move right\n");
- pan(-step, 0);
- break;
- case GLFW_KEY_LEFT:
- if (test_verbose >= 2) printf("move left\n");
- pan( step, 0);
- break;
- case GLFW_KEY_PAGE_UP:
- if (test_verbose >= 2) printf("zoom in\n");
- zoom_at(test_width/2, test_height/2, scale);
- break;
- case GLFW_KEY_PAGE_DOWN:
- if (test_verbose >= 2) printf("zoom out\n");
- zoom_at(test_width/2, test_height/2, 1.0/scale);
- break;
- }
- }
- GLvoid draw_map(GLvoid)
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glScalef(zoom_k, zoom_k, 1);
- glTranslatef(pan_x, pan_y, 0);
- glLineWidth(2.0f);
- glColor3f(1.0, 1.0, 1.0); // white
- glBegin(GL_LINE_STRIP);
- glVertex2f(0.1, 0.1);
- glVertex2f(0.2, 0.1);
- glVertex2f(0.1, 0.2);
- glVertex2f(0.2, 0.2);
- glVertex2f(100.3, 10.1);
- glVertex2f(100.4, 10.1);
- glVertex2f(100.3, 10.2);
- glVertex2f(100.4, 10.2);
- glVertex2f(1100.3, 110.1);
- glVertex2f(1100.4, 110.1);
- glVertex2f(1100.3, 110.2);
- glVertex2f(1100.4, 110.2);
- glEnd();
- }
- GLvoid DrawGLScene(GLvoid)
- {
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- draw_map();
- }
- int main ( int argc, char* argv[])
- {
- glfwSetErrorCallback(error_callback);
- if (!glfwInit())
- exit(EXIT_FAILURE);
- glfwWindowHint(GLFW_DECORATED, GLFW_TRUE);
- test_monitor = glfwGetPrimaryMonitor();
- test_window = glfwCreateWindow(test_width, test_height, "Test Program", NULL, NULL);
- if (!test_window)
- {
- fprintf( stderr, "Failed to create GLFW window\n" );
- glfwTerminate();
- exit( EXIT_FAILURE );
- }
- glfwSetWindowSizeLimits(test_window, 640, 480, GLFW_DONT_CARE, GLFW_DONT_CARE);
- glfwSetKeyCallback(test_window, key_callback);
- glfwMakeContextCurrent(test_window);
- gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
- glfwSwapInterval(1);
- glfwGetFramebufferSize(test_window, &test_width, &test_height);
- glViewport(0, 0, test_width, test_height);
- while ( !glfwWindowShouldClose(test_window))
- {
- DrawGLScene();
- glfwSwapBuffers(test_window);
- glfwPollEvents();
- }
- glfwTerminate();
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement