Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLFW/glfw3.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <GL/gl.h>
- #include <time.h>
- #include <SOIL/SOIL.h>
- int n=1;
- int myargc;
- char **myargv;
- int X = 2000;
- int Y = 2000;
- int C = 3;
- int x,y;
- void *picture;
- GLFWwindow *window;
- GLchar *fragmentsource, *vertexsource;
- GLuint vertexshader, fragmentshader;
- GLuint shaderprogram;
- GLuint image;
- GLuint sampler;
- GLuint texture;
- GLuint vao, vbo[1], UV[1];
- struct vec2 {
- GLfloat point[2];
- };
- struct vec3 {
- GLdouble point[3];
- };
- const struct vec3 flat[4] = {
- {{ -1.0, 1.0, 0.0 }},
- {{ 1.0, 1.0, 0.0 }},
- {{ 1.0, -1.0, 0.0 }},
- {{ -1.0, -1.0, 0.0 }}
- };
- const struct vec2 UVmap[4] = {
- {{ 0.0, 0.0 }},
- {{ 1.0, 0.0 }},
- {{ 1.0, 1.0 }},
- {{ 0.0, 1.0 }}
- };
- /* A simple function that will read a file into an allocated char pointer buffer */
- char* filetobuf(char *file) {
- FILE *fptr;
- long length;
- char *buf;
- fptr = fopen(file, "r"); /* Open file for reading */
- if (!fptr) /* Return NULL on failure */
- return NULL;
- fseek(fptr, 0, SEEK_END); /* Seek to the end of the file */
- length = ftell(fptr); /* Find out how many bytes into the file we are */
- buf = (char*)malloc(length + 1); /* Allocate a buffer for the entire length of the file plus a null terminator */
- fseek(fptr, 0, SEEK_SET); /* Go back to the beginning of the file */
- fread(buf, length, 1, fptr); /* Read the contents of the file in to the buffer */
- fclose(fptr); /* Close the file */
- buf[length] = 0; /* Null terminator */
- return buf; /* Return the buffer */
- }
- static void error_callback(int error, const char *description) {
- fputs(description, stderr);
- }
- static void key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- else if (action == GLFW_PRESS) {
- if (key == GLFW_KEY_RIGHT) {
- if (n==myargc-1)
- n=0;
- n++;
- }
- else if (key == GLFW_KEY_LEFT) {
- if (n==1)
- n=myargc;
- n--;
- }
- else goto nothing;
- picture = SOIL_load_image(myargv[n], &X, &Y, &C, SOIL_LOAD_RGB);
- glfwSetWindowSize(window, X,Y);
- glfwGetFramebufferSize(window, &x, &y);
- glViewport(0, 0, x, y);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, X, Y, 0, GL_RGB, GL_UNSIGNED_BYTE, picture);
- SOIL_free_image_data(picture);
- nothing:;
- }
- }
- void framebuffer_size_callback(window, x, y) {
- glViewport(0, 0, x, y);
- }
- void init_glfw() {
- // povezati error callback provo za svaki slucaj
- glfwSetErrorCallback(error_callback);
- if (!glfwInit())
- exit(EXIT_FAILURE);
- }
- void create_window() {
- picture = SOIL_load_image(myargv[1], &X, &Y, &C, SOIL_LOAD_RGB);
- if (!picture) {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- window = glfwCreateWindow(X, Y, "Simple example", NULL, NULL);
- if (!window) {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- glfwGetWindowSize (window, &x, &y);
- glViewport(0, 0, x, y);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LESS);
- glEnable(GL_TEXTURE_2D);
- }
- void opengl() {
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glGenBuffers(1, vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
- glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(struct vec3), flat, GL_STATIC_DRAW);
- glVertexAttribPointer((GLuint) 0, 3, GL_DOUBLE, GL_FALSE, sizeof(struct vec3), 0);
- glEnableVertexAttribArray(0);
- glGenBuffers(1, UV);
- glBindBuffer(GL_ARRAY_BUFFER, UV);
- glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(struct vec2), UVmap, GL_STATIC_DRAW);
- glVertexAttribPointer((GLuint) 1, 2, GL_FLOAT, GL_FALSE, sizeof(struct vec2), 0);
- glEnableVertexAttribArray(1);
- vertexsource = filetobuf("vertexshader.vert");
- fragmentsource = filetobuf("fragmentshader.frag");
- vertexshader = glCreateShader(GL_VERTEX_SHADER);
- fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(vertexshader, 1, (const GLchar **)&vertexsource, 0);
- glShaderSource(fragmentshader, 1, (const GLchar **)&fragmentsource, 0);
- glCompileShader(vertexshader);
- glCompileShader(fragmentshader);
- shaderprogram = glCreateProgram();
- glAttachShader(shaderprogram, vertexshader);
- glAttachShader(shaderprogram, fragmentshader);
- glLinkProgram(shaderprogram);
- glUseProgram(shaderprogram);
- glGenSamplers(1, &sampler);
- glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
- glSamplerParameterf(sampler, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16.0f);
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, X, Y, 0, GL_RGB, GL_UNSIGNED_BYTE, picture);
- SOIL_free_image_data(picture);
- }
- int main(int argc, char** argv) {
- myargc = argc;
- myargv = argv;
- init_glfw();
- create_window();
- opengl();
- int i=1;
- while (!glfwWindowShouldClose(window)) {
- struct timespec tps, tpe;
- glClearColor(1.0, 0.0, 0.0, 1.0); // pozadinu u crveno, za debug
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glDrawArrays(GL_QUADS, 0, 4); // u ovom slucaju QUAD umjesto trokuta
- glfwSwapBuffers(window);
- usleep(10000);
- if (i==0) {
- if (clock_gettime(CLOCK_MONOTONIC, &tpe) != 0) {
- perror("clock_gettime");
- return -1;
- }
- printf("%lu s, %lu ns %lu fps \n", tpe.tv_sec-tps.tv_sec, tpe.tv_nsec-tps.tv_nsec, 1000000000/(tpe.tv_nsec-tps.tv_nsec));
- i=1000;
- }
- i--;
- clock_gettime(CLOCK_MONOTONIC, &tps);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement