Advertisement
Guest User

Untitled

a guest
Dec 29th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.15 KB | None | 0 0
  1. #define GLEW_STATIC
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5. #include <glew.h>
  6. #include <glfw3.h>
  7. #include <SOIL.h>
  8.  
  9. #define SCREEN_WIDTH 192
  10. #define SCREEN_HEIGHT 224
  11. #define SCREEN_HORIZONTAL_SCALE 4
  12. #define SCREEN_VERTICAL_SCALE 2
  13. #define WINDOW_WIDTH SCREEN_WIDTH * SCREEN_HORIZONTAL_SCALE
  14. #define WINDOW_HEIGHT SCREEN_HEIGHT * SCREEN_VERTICAL_SCALE
  15.  
  16. /* Common vertex shader */
  17. const GLchar* vertexSource =
  18.     "#version 150 core\n"
  19.     "uniform mat4 uniModel;"
  20.     "uniform mat4 uniView;"
  21.     "uniform mat4 uniProj;"
  22.     "in vec3 inPosition;"
  23.     "in vec3 inColor;"
  24.     "in vec2 inTexCoord;"
  25.     "out vec3 fragColor;"
  26.     "out vec2 fragTexCoord;"
  27.     "void main() {"
  28.     "   fragColor = inColor;"
  29.     "   fragTexCoord = inTexCoord;"
  30.     "   gl_Position = model * view * proj * vec4(inPosition, 1.0);"
  31.     "}";
  32.    
  33. /* Common fragment shader */
  34. const GLchar* fragmentSource =
  35.     "#version 150 core\n"
  36.     "uniform sampler2D fragSampler;"
  37.     "in vec3 fragColor;"
  38.     "in vec2 fragTexCoord;"
  39.     "out vec4 outColor;"
  40.     "void main() {"
  41.     "   outColor = vec4(fragColor, 1.0) * texture(fragSampler, fragTexCoord);"
  42.     "}";
  43.  
  44. /* Identity matrix constant */
  45. const GLfloat identityMatrix4[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  46.  
  47. GLfloat modelMatrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  48. GLfloat viewMatrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  49. GLfloat projMatrix[] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1};
  50.  
  51. /* Matrix functions */
  52. void gfxUploadMatrix4(GLint location, GLfloat* value) {
  53.     glUniformMatrix4fv(location, 1, GL_FALSE, value);
  54. }
  55.  
  56. void gfxIdentityMatrix4(GLint location) {
  57.     glUniformMatrix4fv(location, 1, GL_FALSE, identityMatrix4);
  58. }
  59.  
  60. void gfxReady2D(GLint modelLocation, GLint viewLocation, GLint projLocation, GLint width, GLint height) {
  61.     /* Calculate the ortho matrix and set variables */
  62.     projMatrix[0] = 2 / width;
  63.     projMatrix[1] = 0;
  64.     projMatrix[2] = 0;
  65.     projMatrix[3] = -1;
  66.     projMatrix[4] = 0;
  67.     projMatrix[5] = 2 / height;
  68.     projMatrix[6] = 0;
  69.     projMatrix[7] = -1;
  70.     projMatrix[8] = 0;
  71.     projMatrix[9] = 0;
  72.     projMatrix[10] = -1;
  73.     projMatrix[11] = 0;
  74.     projMatrix[12] = 0;
  75.     projMatrix[13] = 0;
  76.     projMatrix[14] = 0;
  77.     projMatrix[15] = 1;
  78.    
  79.     /* Set viewport and upload matrices */
  80.     glViewport(0, 0, width, height);
  81.    
  82.     gfxIdentityMatrix4(modelLocation);
  83.     gfxIdentityMatrix4(viewLocation);
  84.  
  85.     gfxUploadMatrix4(projLocation, projMatrix);
  86.  
  87.     /* Disable depth testing for 2D */
  88.     glDisable(GL_DEPTH_TEST);
  89. }
  90.  
  91. void gfxReady3D(GLint modelLocation, GLint viewLocation, GLint projLocation, GLint width, GLint height) {
  92.     /* Calculate the frustum matrix and set variables */
  93.     float aspect = ((float) width) / height, fH = 1.73205081, fW = fH * aspect;
  94.    
  95.     projMatrix[0] = 2 / (2 * fW);
  96.     projMatrix[1] = 0;
  97.     projMatrix[2] = 0;
  98.     projMatrix[3] = 0;
  99.     projMatrix[4] = 0;
  100.     projMatrix[5] = 2 / (2 * fH);
  101.     projMatrix[6] = 0;
  102.     projMatrix[7] = 0;
  103.     projMatrix[8] = 0;
  104.     projMatrix[9] = 0;
  105.     projMatrix[10] = -100.0f / 99;
  106.     projMatrix[11] = -200.0f / 99;
  107.     projMatrix[12] = 0;
  108.     projMatrix[13] = 0;
  109.     projMatrix[14] = -1;
  110.     projMatrix[15] = 1;
  111.    
  112.     /* Set viewport and upload matrices */
  113.     glViewport(0, 0, width, height);
  114.    
  115.     gfxIdentityMatrix4(modelLocation);
  116.     gfxIdentityMatrix4(viewLocation);
  117.    
  118.     gfxUploadMatrix4(projLocation, projMatrix);
  119.  
  120.     /* Enable depth testing for 3D */
  121.     glEnable(GL_DEPTH_TEST);
  122. }
  123.  
  124. void gfxBindBufferSet(GLint vao, GLint vbo, GLint ebo) {
  125.     /* Bind all necessary buffers */
  126.     glBindVertexArray(vao);
  127.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  128.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  129. }
  130.  
  131. GLint main(void) {
  132.     /* Declaring variables */
  133.     GLFWwindow* window;
  134.    
  135.     /* Initialize the library */
  136.     if (!glfwInit())
  137.         return -1;
  138.        
  139.     /* Setup window hints */
  140.     glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
  141.  
  142.     /* Create a windowed mode window and its OpenGL context */
  143.     window = glfwCreateWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Project", NULL, NULL);
  144.     if (!window) {
  145.         glfwTerminate();
  146.         return -1;
  147.     }
  148.    
  149.     /* Center window */
  150.     const GLFWvidmode* windowMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
  151.     GLint windowWidth, windowHeight;
  152.     glfwGetWindowSize(window, &windowWidth, &windowHeight);
  153.     glfwSetWindowPos(window, (windowMode->width - windowWidth) / 2, (windowMode->height - windowHeight) / 2);
  154.    
  155.     /* Make the window's context current */
  156.     glfwMakeContextCurrent(window);
  157.    
  158.     /* Initialize GLEW (after contexting) */
  159.     GLenum err = glewInit();
  160.     if (GLEW_OK != err)
  161.         return -1;
  162.    
  163.     /* Setup OpenGL hints and stuff */
  164.     glEnable(GL_TEXTURE_2D);
  165.     glDepthFunc(GL_LEQUAL);
  166.    
  167.     /* Setup vertex and fragment shaders */
  168.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  169.     glShaderSource(vertexShader, 1, &vertexSource, NULL);
  170.     glCompileShader(vertexShader);
  171.  
  172.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  173.     glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  174.     glCompileShader(fragmentShader);
  175.  
  176.     /* Link vertex and fragment shaders into a program */
  177.     GLuint shaderProgram = glCreateProgram();
  178.     glAttachShader(shaderProgram, vertexShader);
  179.     glAttachShader(shaderProgram, fragmentShader);
  180.     glBindFragDataLocation(shaderProgram, 0, "outColor");
  181.     glLinkProgram(shaderProgram);
  182.     glUseProgram(shaderProgram);
  183.  
  184.     /* Get matrix locations */
  185.     GLint modelUniform = glGetUniformLocation(shaderProgram, "uniModel");
  186.     GLint viewUniform = glGetUniformLocation(shaderProgram, "uniView");
  187.     GLint projUniform = glGetUniformLocation(shaderProgram, "uniProj");
  188.    
  189.     /* Specify vertex data layout */
  190.     GLint positionAttrib = glGetAttribLocation(shaderProgram, "inPosition");
  191.     glEnableVertexAttribArray(positionAttrib);
  192.     glVertexAttribPointer(positionAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
  193.  
  194.     GLint colorAttrib = glGetAttribLocation(shaderProgram, "inColor");
  195.     glEnableVertexAttribArray(colorAttrib);
  196.     glVertexAttribPointer(colorAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
  197.  
  198.     GLint texCoordAttrib = glGetAttribLocation(shaderProgram, "inTexCoord");
  199.     glEnableVertexAttribArray(texCoordAttrib);
  200.     glVertexAttribPointer(texCoordAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 * sizeof(GLfloat)));
  201.    
  202.     /* Test rendering buffer set */
  203.    
  204.     /* Create Vertex Array Object */
  205.     GLuint vao;
  206.     glGenVertexArrays(1, &vao);
  207.     glBindVertexArray(vao);
  208.  
  209.     /* Create a Vertex Buffer Object and copy the vertex data to it */
  210.     GLuint vbo;
  211.     glGenBuffers(1, &vbo);
  212.  
  213.     GLfloat vertices[] = {
  214.     /*  Position (3) Color (3) Texcoords (2) */
  215.         20, 80, 0, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, /* Top-left */
  216.         80, 80, 0, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, /* Top-right */
  217.         80, 20, 0, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, /* Bottom-right */
  218.         20, 20, 0, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f  /* Bottom-left */
  219.     };
  220.  
  221.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  222.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  223.  
  224.     /* Create an element array */
  225.     GLuint ebo;
  226.     glGenBuffers(1, &ebo);
  227.  
  228.     GLuint elements[] = {
  229.         0, 1, 2,
  230.         2, 3, 0
  231.     };
  232.  
  233.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  234.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
  235.    
  236.     /* FBO rendering buffer set */
  237.    
  238.     /* Create Vertex Array Object */
  239.     GLuint fboVao;
  240.     glGenVertexArrays(1, &fboVao);
  241.     glBindVertexArray(fboVao);
  242.  
  243.     /* Create a Vertex Buffer Object and copy the vertex data to it */
  244.     GLuint fboVbo;
  245.     glGenBuffers(1, &fboVbo);
  246.  
  247.     GLfloat fboVertices[] = {
  248.     /*  Position (3) Color (3) Texcoords (2) */
  249.         0, 0, 0,                        0, 0, 0, 0, 0,
  250.         0, WINDOW_HEIGHT, 0,            0, 0, 0, 0, 1,
  251.         WINDOW_WIDTH, WINDOW_HEIGHT, 0, 0, 0, 0, 1, 1,
  252.         WINDOW_WIDTH, 0, 0,             0, 0, 0, 1, 0
  253.     };
  254.  
  255.     glBindBuffer(GL_ARRAY_BUFFER, fboVbo);
  256.     glBufferData(GL_ARRAY_BUFFER, sizeof(fboVertices), fboVertices, GL_STATIC_DRAW);
  257.  
  258.     /* Create an element array */
  259.     GLuint fboEbo;
  260.     glGenBuffers(1, &fboEbo);
  261.  
  262.     GLuint fboElements[] = {
  263.         0, 1, 2,
  264.         2, 3, 0
  265.     };
  266.  
  267.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, fboEbo);
  268.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(fboElements), fboElements, GL_STATIC_DRAW);
  269.    
  270.     /* Prepare an FBO for unscaled rendering */
  271.     GLuint unscaledBuffer;
  272.     glGenFramebuffers(1, &unscaledBuffer);
  273.     glBindFramebuffer(GL_FRAMEBUFFER, unscaledBuffer);
  274.    
  275.     /* Setup the FBO texture */
  276.     GLuint texColorBuffer;
  277.     glGenTextures(1, &texColorBuffer);
  278.     glBindTexture(GL_TEXTURE_2D, texColorBuffer);
  279.  
  280.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_WIDTH, SCREEN_HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
  281.  
  282.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  283.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  284.    
  285.     glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texColorBuffer, 0);
  286.    
  287.     /* Setup the FBO depth buffer */
  288.     GLuint rboDepth;
  289.     glGenRenderbuffers(1, &rboDepth);
  290.     glBindRenderbuffer(GL_RENDERBUFFER, rboDepth);
  291.     glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, SCREEN_WIDTH, SCREEN_HEIGHT);
  292.    
  293.     glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rboDepth);
  294.    
  295.     /* Load textures */
  296.     GLuint textures[2];
  297.     glGenTextures(2, textures);
  298.    
  299.     int width, height;
  300.     unsigned char* image;
  301.  
  302.     glBindTexture(GL_TEXTURE_2D, textures[0]);
  303.  
  304.     image = SOIL_load_image("img/blank.png", &width, &height, 0, SOIL_LOAD_RGB);
  305.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  306.     SOIL_free_image_data(image);
  307.  
  308.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  309.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  310.  
  311.     /* Loop until the user closes the window */
  312.     while (!glfwWindowShouldClose(window)) {
  313.         /* Unbind texture */
  314.         glBindTexture(GL_TEXTURE_2D, 0);
  315.        
  316.         /* Switch to unscaled FBO */
  317.         glBindFramebuffer(GL_FRAMEBUFFER, unscaledBuffer);
  318.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  319.        
  320.         /* Start 2D rendering */
  321.         gfxReady2D(modelUniform, viewUniform, projUniform, SCREEN_WIDTH, SCREEN_HEIGHT);
  322.        
  323.         /* Random drawing stuffs */
  324.         glBindTexture(GL_TEXTURE_2D, textures[0]);
  325.         gfxBindBufferSet(vao, vbo, ebo);
  326.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  327.        
  328.         /* Start 3D rendering */
  329.         gfxReady3D(modelUniform, viewUniform, projUniform, SCREEN_WIDTH, SCREEN_HEIGHT);
  330.        
  331.         /* Random drawing stuffs */
  332.        
  333.         // NOTHING
  334.        
  335.         /* Switch to default FBO */
  336.         glBindFramebuffer(GL_FRAMEBUFFER, 0);
  337.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  338.         gfxReady2D(modelUniform, viewUniform, projUniform, WINDOW_WIDTH, WINDOW_HEIGHT);
  339.        
  340.         /* Render unscaled FBO to default FBO */
  341.         glBindTexture(GL_TEXTURE_2D, texColorBuffer);
  342.         gfxBindBufferSet(fboVao, fboVbo, fboEbo);
  343.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
  344.  
  345.         /* Swap front and back buffers */
  346.         glfwSwapBuffers(window);
  347.  
  348.         /* Poll for and process events */
  349.         glfwPollEvents();
  350.     }
  351.  
  352.     /* Terminate program and GLFW */
  353.     glfwTerminate();
  354.     return 0;
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement