Changeset 9cbdc9c in network-game


Ignore:
Timestamp:
Feb 7, 2014, 3:28:10 PM (11 years ago)
Author:
Dmitry Portnoy <dportnoy@…>
Branches:
master
Children:
2e5aa0c
Parents:
8df0c49
Message:

Graphics libary can load textures and handle mouse and keyboard controls (using tutorial code)

Location:
graphics_library
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • graphics_library/graphics_library.vcxproj

    r8df0c49 r9cbdc9c  
    6767  </ItemDefinitionGroup>
    6868  <ItemGroup>
     69    <ClCompile Include="common\controls.cpp" />
     70    <ClCompile Include="common\shader.cpp" />
     71    <ClCompile Include="common\texture.cpp" />
    6972    <ClCompile Include="main.cpp" />
    7073  </ItemGroup>
     
    7477    <None Include="SimpleVertexShader.vertexshader" />
    7578    <None Include="SingleColor.fragmentshader" />
     79    <None Include="TextureFragmentShader.fragmentshader" />
     80    <None Include="TransformVertexShader.vertexshader" />
     81  </ItemGroup>
     82  <ItemGroup>
     83    <ClInclude Include="common\controls.hpp" />
     84    <ClInclude Include="common\shader.hpp" />
     85    <ClInclude Include="common\texture.hpp" />
    7686  </ItemGroup>
    7787  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
  • graphics_library/graphics_library.vcxproj.filters

    r8df0c49 r9cbdc9c  
    1414      <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
    1515    </Filter>
     16    <Filter Include="Header Files\common">
     17      <UniqueIdentifier>{d934654a-a839-48d6-a4a6-a620fcaee36e}</UniqueIdentifier>
     18    </Filter>
     19    <Filter Include="Source Files\common">
     20      <UniqueIdentifier>{a04b1a63-118b-4076-b6ab-fafdf8011916}</UniqueIdentifier>
     21    </Filter>
    1622  </ItemGroup>
    1723  <ItemGroup>
    1824    <ClCompile Include="main.cpp">
     25      <Filter>Source Files</Filter>
     26    </ClCompile>
     27    <ClCompile Include="common\controls.cpp">
     28      <Filter>Source Files</Filter>
     29    </ClCompile>
     30    <ClCompile Include="common\shader.cpp">
     31      <Filter>Source Files</Filter>
     32    </ClCompile>
     33    <ClCompile Include="common\texture.cpp">
    1934      <Filter>Source Files</Filter>
    2035    </ClCompile>
     
    3348      <Filter>Resource Files</Filter>
    3449    </None>
     50    <None Include="TextureFragmentShader.fragmentshader">
     51      <Filter>Resource Files</Filter>
     52    </None>
     53    <None Include="TransformVertexShader.vertexshader">
     54      <Filter>Resource Files</Filter>
     55    </None>
     56  </ItemGroup>
     57  <ItemGroup>
     58    <ClInclude Include="common\controls.hpp">
     59      <Filter>Header Files\common</Filter>
     60    </ClInclude>
     61    <ClInclude Include="common\shader.hpp">
     62      <Filter>Header Files\common</Filter>
     63    </ClInclude>
     64    <ClInclude Include="common\texture.hpp">
     65      <Filter>Header Files\common</Filter>
     66    </ClInclude>
    3567  </ItemGroup>
    3668</Project>
  • graphics_library/main.cpp

    r8df0c49 r9cbdc9c  
    1 #include <cstdio>
    2 #include <cstdlib>
    3 #include <string>
    4 #include <fstream>
    5 #include <vector>
    6 #include <algorithm>
    7 
     1// Include standard headers
     2#include <stdio.h>
     3#include <stdlib.h>
     4
     5// Include GLEW
    86#include <GL/glew.h>
     7
     8// Include GLFW
    99#include <GL/glfw.h>
    1010
     11// Include GLM
    1112#include <glm/glm.hpp>
    1213#include <glm/gtc/matrix_transform.hpp>
    1314using namespace glm;
    1415
    15 using namespace std;
    16 
    17 /*
    18 
    19 Rendering Pipeline
    20 
    21 All models are defined
    22 All vertices in each model are in model space
    23 
    24 Apply the model transformation to each model (based on model position, rotation, and scaling)
    25 This results in all vertices being in world space
    26 
    27 Apply the view transformation (based on camera position and rotation)
    28 Vertices are in camera coordinates
    29 
    30 Apply the projection matrix (camera lens angle, clipping planes) to turn coordinates into screen coordinates
    31 
    32 */
    33 
    34 GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path);
    35 
    36 int main() {
    37    // Initialise GLFW
    38    if( !glfwInit() )
    39    {
    40       fprintf( stderr, "Failed to initialize GLFW\n" );
    41       return -1;
    42    }
    43 
    44    glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
    45    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
    46    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
    47    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    48 
    49    // Open a window and create its OpenGL context
    50    if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
    51    {
    52       fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
    53       glfwTerminate();
    54       return -1;
    55    }
    56 
    57    // Initialize GLEW
    58    glewExperimental = true; // Needed for core profile
    59    if (glewInit() != GLEW_OK) {
    60       fprintf(stderr, "Failed to initialize GLEW\n");
    61       return -1;
    62    }
    63 
    64    glfwSetWindowTitle( "Tutorial 02" );
    65 
    66    // Ensure we can capture the escape key being pressed below
    67    glfwEnable( GLFW_STICKY_KEYS );
    68 
    69    // Dark blue background
    70    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
    71 
    72    // Enable depth test
    73    glEnable(GL_DEPTH_TEST);
    74    // Accept fragment if it closer to the camera than the former one
    75    glDepthFunc(GL_LESS);
    76 
    77    GLuint VertexArrayID;
    78    glGenVertexArrays(1, &VertexArrayID);
    79    glBindVertexArray(VertexArrayID);
    80 
    81    // Create and compile our GLSL program from the shaders
    82    GLuint programID = LoadShaders( "SimpleTransform.vertexshader", "SingleColor.fragmentshader" );
    83 
    84    // Get a handle for our "MVP" uniform
    85    GLuint MatrixID = glGetUniformLocation(programID, "MVP");
    86 
    87    // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
    88    glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
    89    // Or, for an ortho camera :
    90    //glm::mat4 Projection = glm::ortho(-10.0f,10.0f,-10.0f,10.0f,0.0f,100.0f); // In world coordinates
     16#include "common/shader.hpp"
     17#include "common/texture.hpp"
     18#include "common/controls.hpp"
     19
     20int main( void )
     21{
     22        // Initialise GLFW
     23        if( !glfwInit() )
     24        {
     25                fprintf( stderr, "Failed to initialize GLFW\n" );
     26                return -1;
     27        }
     28
     29        glfwOpenWindowHint(GLFW_FSAA_SAMPLES, 4);
     30        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
     31        glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
     32        glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
     33
     34        // Open a window and create its OpenGL context
     35        if( !glfwOpenWindow( 1024, 768, 0,0,0,0, 32,0, GLFW_WINDOW ) )
     36        {
     37                fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
     38                glfwTerminate();
     39                return -1;
     40        }
     41
     42        // Initialize GLEW
     43        glewExperimental = true; // Needed for core profile
     44        if (glewInit() != GLEW_OK) {
     45                fprintf(stderr, "Failed to initialize GLEW\n");
     46                return -1;
     47        }
     48
     49        glfwSetWindowTitle( "Tutorial 06" );
     50
     51        // Ensure we can capture the escape key being pressed below
     52        glfwEnable( GLFW_STICKY_KEYS );
     53        glfwSetMousePos(1024/2, 768/2);
     54
     55        // Dark blue background
     56        glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
     57
     58        // Enable depth test
     59        glEnable(GL_DEPTH_TEST);
     60        // Accept fragment if it closer to the camera than the former one
     61        glDepthFunc(GL_LESS);
     62
     63        // Cull triangles which normal is not towards the camera
     64        glEnable(GL_CULL_FACE);
     65
     66        GLuint VertexArrayID;
     67        glGenVertexArrays(1, &VertexArrayID);
     68        glBindVertexArray(VertexArrayID);
     69
     70        // Create and compile our GLSL program from the shaders
     71        GLuint programID = LoadShaders( "TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader" );
     72
     73        // Get a handle for our "MVP" uniform
     74        GLuint MatrixID = glGetUniformLocation(programID, "MVP");
     75
     76        // Load the texture
     77        GLuint Texture = loadDDS("uvtemplate.DDS");
    9178       
    92    // Camera matrix
    93    glm::mat4 View = glm::lookAt(
    94       glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
    95       glm::vec3(0,0,0), // and looks at the origin
    96       glm::vec3(0,1,0)  // Head is up (set to 0,-1,0 to look upside-down)
    97    );
    98 
    99    // Model matrix : an identity matrix (model will be at the origin)
    100    glm::mat4 Model = glm::mat4(1.0f);
    101    // Our ModelViewProjection : multiplication of our 3 matrices
    102    glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around
    103 
    104    // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
    105    // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
    106    static const GLfloat g_vertex_buffer_data[] = {
    107       -1.0f,-1.0f,-1.0f, // triangle 1 : begin
    108       -1.0f,-1.0f, 1.0f,
    109       -1.0f, 1.0f, 1.0f, // triangle 1 : end
    110       1.0f, 1.0f,-1.0f, // triangle 2 : begin
    111       -1.0f,-1.0f,-1.0f,
    112       -1.0f, 1.0f,-1.0f, // triangle 2 : end
    113       1.0f,-1.0f, 1.0f,
    114       -1.0f,-1.0f,-1.0f,
    115       1.0f,-1.0f,-1.0f,
    116       1.0f, 1.0f,-1.0f,
    117       1.0f,-1.0f,-1.0f,
    118       -1.0f,-1.0f,-1.0f,
    119       -1.0f,-1.0f,-1.0f,
    120       -1.0f, 1.0f, 1.0f,
    121       -1.0f, 1.0f,-1.0f,
    122       1.0f,-1.0f, 1.0f,
    123       -1.0f,-1.0f, 1.0f,
    124       -1.0f,-1.0f,-1.0f,
    125       -1.0f, 1.0f, 1.0f,
    126       -1.0f,-1.0f, 1.0f,
    127       1.0f,-1.0f, 1.0f,
    128       1.0f, 1.0f, 1.0f,
    129       1.0f,-1.0f,-1.0f,
    130       1.0f, 1.0f,-1.0f,
    131       1.0f,-1.0f,-1.0f,
    132       1.0f, 1.0f, 1.0f,
    133       1.0f,-1.0f, 1.0f,
    134       1.0f, 1.0f, 1.0f,
    135       1.0f, 1.0f,-1.0f,
    136       -1.0f, 1.0f,-1.0f,
    137       1.0f, 1.0f, 1.0f,
    138       -1.0f, 1.0f,-1.0f,
    139       -1.0f, 1.0f, 1.0f,
    140       1.0f, 1.0f, 1.0f,
    141       -1.0f, 1.0f, 1.0f,
    142       1.0f,-1.0f, 1.0f
    143    };
    144 
    145    GLuint vertexbuffer;
    146    glGenBuffers(1, &vertexbuffer);
    147    glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    148    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
    149 
    150    // One color for each vertex. They were generated randomly.
    151    static const GLfloat g_color_buffer_data[] = {
    152       0.583f,  0.771f,  0.014f,
    153       0.609f,  0.115f,  0.436f,
    154       0.327f,  0.483f,  0.844f,
    155       0.822f,  0.569f,  0.201f,
    156       0.435f,  0.602f,  0.223f,
    157       0.310f,  0.747f,  0.185f,
    158       0.597f,  0.770f,  0.761f,
    159       0.559f,  0.436f,  0.730f,
    160       0.359f,  0.583f,  0.152f,
    161       0.483f,  0.596f,  0.789f,
    162       0.559f,  0.861f,  0.639f,
    163       0.195f,  0.548f,  0.859f,
    164       0.014f,  0.184f,  0.576f,
    165       0.771f,  0.328f,  0.970f,
    166       0.406f,  0.615f,  0.116f,
    167       0.676f,  0.977f,  0.133f,
    168       0.971f,  0.572f,  0.833f,
    169       0.140f,  0.616f,  0.489f,
    170       0.997f,  0.513f,  0.064f,
    171       0.945f,  0.719f,  0.592f,
    172       0.543f,  0.021f,  0.978f,
    173       0.279f,  0.317f,  0.505f,
    174       0.167f,  0.620f,  0.077f,
    175       0.347f,  0.857f,  0.137f,
    176       0.055f,  0.953f,  0.042f,
    177       0.714f,  0.505f,  0.345f,
    178       0.783f,  0.290f,  0.734f,
    179       0.722f,  0.645f,  0.174f,
    180       0.302f,  0.455f,  0.848f,
    181       0.225f,  0.587f,  0.040f,
    182       0.517f,  0.713f,  0.338f,
    183       0.053f,  0.959f,  0.120f,
    184       0.393f,  0.621f,  0.362f,
    185       0.673f,  0.211f,  0.457f,
    186       0.820f,  0.883f,  0.371f,
    187       0.982f,  0.099f,  0.879f
    188    };
    189 
    190    GLuint colorbuffer;
    191    glGenBuffers(1, &colorbuffer);
    192    glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
    193    glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);
    194 
    195    do{
    196 
    197       // Clear the screen
    198       glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    199 
    200       // Use our shader
    201       glUseProgram(programID);
    202 
    203       // Send our transformation to the currently bound shader,
    204       // in the "MVP" uniform
    205       glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
    206 
    207       // 1rst attribute buffer : vertices
    208       glEnableVertexAttribArray(0);
    209       glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
    210       glVertexAttribPointer(
    211          0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
    212          3,                  // size
    213          GL_FLOAT,           // type
    214          GL_FALSE,           // normalized?
    215          0,                  // stride
    216          (void*)0            // array buffer offset
    217       );
    218 
    219       // 2nd attribute buffer : colors
    220       glEnableVertexAttribArray(1);
    221       glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
    222       glVertexAttribPointer(
    223          1,                   // attribute. No particular reason for 1, but must match the layout in the shader.
    224          3,                   // size
    225          GL_FLOAT,            // type
    226          GL_FALSE,            // normalized?
    227          0,                   // stride
    228          (void*)0             // array buffer offset
    229       );
    230 
    231       // Draw the triangle !
    232       glDrawArrays(GL_TRIANGLES, 0, 12*3);
    233 
    234       glDisableVertexAttribArray(0);
    235 
    236       // Swap buffers
    237       glfwSwapBuffers();
    238 
    239    } // Check if the ESC key was pressed or the window was closed
    240    while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
    241       glfwGetWindowParam( GLFW_OPENED ) );
    242 
    243    // Cleanup VBO
    244    glDeleteBuffers(1, &vertexbuffer);
    245    glDeleteProgram(programID);
    246    glDeleteVertexArrays(1, &VertexArrayID);
    247 
    248    // Close OpenGL window and terminate GLFW
    249    glfwTerminate();
    250 
    251    return 0;
     79        // Get a handle for our "myTextureSampler" uniform
     80        GLuint TextureID  = glGetUniformLocation(programID, "myTextureSampler");
     81
     82        // Our vertices. Tree consecutive floats give a 3D vertex; Three consecutive vertices give a triangle.
     83        // A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
     84        static const GLfloat g_vertex_buffer_data[] = {
     85                -1.0f,-1.0f,-1.0f,
     86                -1.0f,-1.0f, 1.0f,
     87                -1.0f, 1.0f, 1.0f,
     88                 1.0f, 1.0f,-1.0f,
     89                -1.0f,-1.0f,-1.0f,
     90                -1.0f, 1.0f,-1.0f,
     91                 1.0f,-1.0f, 1.0f,
     92                -1.0f,-1.0f,-1.0f,
     93                 1.0f,-1.0f,-1.0f,
     94                 1.0f, 1.0f,-1.0f,
     95                 1.0f,-1.0f,-1.0f,
     96                -1.0f,-1.0f,-1.0f,
     97                -1.0f,-1.0f,-1.0f,
     98                -1.0f, 1.0f, 1.0f,
     99                -1.0f, 1.0f,-1.0f,
     100                 1.0f,-1.0f, 1.0f,
     101                -1.0f,-1.0f, 1.0f,
     102                -1.0f,-1.0f,-1.0f,
     103                -1.0f, 1.0f, 1.0f,
     104                -1.0f,-1.0f, 1.0f,
     105                 1.0f,-1.0f, 1.0f,
     106                 1.0f, 1.0f, 1.0f,
     107                 1.0f,-1.0f,-1.0f,
     108                 1.0f, 1.0f,-1.0f,
     109                 1.0f,-1.0f,-1.0f,
     110                 1.0f, 1.0f, 1.0f,
     111                 1.0f,-1.0f, 1.0f,
     112                 1.0f, 1.0f, 1.0f,
     113                 1.0f, 1.0f,-1.0f,
     114                -1.0f, 1.0f,-1.0f,
     115                 1.0f, 1.0f, 1.0f,
     116                -1.0f, 1.0f,-1.0f,
     117                -1.0f, 1.0f, 1.0f,
     118                 1.0f, 1.0f, 1.0f,
     119                -1.0f, 1.0f, 1.0f,
     120                 1.0f,-1.0f, 1.0f
     121        };
     122
     123        // Two UV coordinatesfor each vertex. They were created withe Blender.
     124        static const GLfloat g_uv_buffer_data[] = {
     125                0.000059f, 0.000004f,
     126                0.000103f, 0.336048f,
     127                0.335973f, 0.335903f,
     128                1.000023f, 0.000013f,
     129                0.667979f, 0.335851f,
     130                0.999958f, 0.336064f,
     131                0.667979f, 0.335851f,
     132                0.336024f, 0.671877f,
     133                0.667969f, 0.671889f,
     134                1.000023f, 0.000013f,
     135                0.668104f, 0.000013f,
     136                0.667979f, 0.335851f,
     137                0.000059f, 0.000004f,
     138                0.335973f, 0.335903f,
     139                0.336098f, 0.000071f,
     140                0.667979f, 0.335851f,
     141                0.335973f, 0.335903f,
     142                0.336024f, 0.671877f,
     143                1.000004f, 0.671847f,
     144                0.999958f, 0.336064f,
     145                0.667979f, 0.335851f,
     146                0.668104f, 0.000013f,
     147                0.335973f, 0.335903f,
     148                0.667979f, 0.335851f,
     149                0.335973f, 0.335903f,
     150                0.668104f, 0.000013f,
     151                0.336098f, 0.000071f,
     152                0.000103f, 0.336048f,
     153                0.000004f, 0.671870f,
     154                0.336024f, 0.671877f,
     155                0.000103f, 0.336048f,
     156                0.336024f, 0.671877f,
     157                0.335973f, 0.335903f,
     158                0.667969f, 0.671889f,
     159                1.000004f, 0.671847f,
     160                0.667979f, 0.335851f
     161        };
     162
     163        GLuint vertexbuffer;
     164        glGenBuffers(1, &vertexbuffer);
     165        glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
     166        glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
     167
     168        GLuint uvbuffer;
     169        glGenBuffers(1, &uvbuffer);
     170        glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
     171        glBufferData(GL_ARRAY_BUFFER, sizeof(g_uv_buffer_data), g_uv_buffer_data, GL_STATIC_DRAW);
     172
     173        do{
     174
     175                // Clear the screen
     176                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     177
     178                // Use our shader
     179                glUseProgram(programID);
     180
     181                // Compute the MVP matrix from keyboard and mouse input
     182                computeMatricesFromInputs();
     183                glm::mat4 ProjectionMatrix = getProjectionMatrix();
     184                glm::mat4 ViewMatrix = getViewMatrix();
     185                glm::mat4 ModelMatrix = glm::mat4(1.0);
     186                glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
     187
     188                // Send our transformation to the currently bound shader,
     189                // in the "MVP" uniform
     190                glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
     191
     192                // Bind our texture in Texture Unit 0
     193                glActiveTexture(GL_TEXTURE0);
     194                glBindTexture(GL_TEXTURE_2D, Texture);
     195                // Set our "myTextureSampler" sampler to user Texture Unit 0
     196                glUniform1i(TextureID, 0);
     197
     198                // 1rst attribute buffer : vertices
     199                glEnableVertexAttribArray(0);
     200                glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
     201                glVertexAttribPointer(
     202                        0,                  // attribute. No particular reason for 0, but must match the layout in the shader.
     203                        3,                  // size
     204                        GL_FLOAT,           // type
     205                        GL_FALSE,           // normalized?
     206                        0,                  // stride
     207                        (void*)0            // array buffer offset
     208                );
     209
     210                // 2nd attribute buffer : UVs
     211                glEnableVertexAttribArray(1);
     212                glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
     213                glVertexAttribPointer(
     214                        1,                                // attribute. No particular reason for 1, but must match the layout in the shader.
     215                        2,                                // size : U+V => 2
     216                        GL_FLOAT,                         // type
     217                        GL_FALSE,                         // normalized?
     218                        0,                                // stride
     219                        (void*)0                          // array buffer offset
     220                );
     221
     222                // Draw the triangle !
     223                glDrawArrays(GL_TRIANGLES, 0, 12*3); // 12*3 indices starting at 0 -> 12 triangles
     224
     225                glDisableVertexAttribArray(0);
     226                glDisableVertexAttribArray(1);
     227
     228                // Swap buffers
     229                glfwSwapBuffers();
     230
     231        } // Check if the ESC key was pressed or the window was closed
     232        while( glfwGetKey( GLFW_KEY_ESC ) != GLFW_PRESS &&
     233                   glfwGetWindowParam( GLFW_OPENED ) );
     234
     235        // Cleanup VBO and shader
     236        glDeleteBuffers(1, &vertexbuffer);
     237        glDeleteBuffers(1, &uvbuffer);
     238        glDeleteProgram(programID);
     239        glDeleteTextures(1, &TextureID);
     240        glDeleteVertexArrays(1, &VertexArrayID);
     241
     242        // Close OpenGL window and terminate GLFW
     243        glfwTerminate();
     244
     245        return 0;
    252246}
    253 
    254 GLuint LoadShaders(const char * vertex_file_path,const char * fragment_file_path) {
    255  
    256    // Create the shaders
    257    GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
    258    GLuint FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    259 
    260    // Read the Vertex Shader code from the file
    261    string VertexShaderCode;
    262    ifstream VertexShaderStream(vertex_file_path, ios::in);
    263    if(VertexShaderStream.is_open()){
    264       string Line = "";
    265       while(getline(VertexShaderStream, Line))
    266             VertexShaderCode += "\n" + Line;
    267       VertexShaderStream.close();
    268    }else {
    269       printf("Impossible to open %s. Are you in the right directory ? Don't forget to read the FAQ !\n", vertex_file_path);
    270       getchar();
    271       return 0;
    272    }
    273 
    274    // Read the Fragment Shader code from the file
    275    string FragmentShaderCode;
    276    ifstream FragmentShaderStream(fragment_file_path, ios::in);
    277    if(FragmentShaderStream.is_open()) {
    278       string Line = "";
    279       while(getline(FragmentShaderStream, Line))
    280          FragmentShaderCode += "\n" + Line;
    281       FragmentShaderStream.close();
    282    }
    283 
    284 
    285 
    286    GLint Result = GL_FALSE;
    287    int InfoLogLength;
    288 
    289 
    290 
    291    // Compile Vertex Shader
    292    printf("Compiling shader : %s\n", vertex_file_path);
    293    char const * VertexSourcePointer = VertexShaderCode.c_str();
    294    glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL);
    295    glCompileShader(VertexShaderID);
    296 
    297    // Check Vertex Shader
    298    glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result);
    299    glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    300    if ( InfoLogLength > 0 ){
    301       vector<char> VertexShaderErrorMessage(InfoLogLength+1);
    302       glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]);
    303       printf("%s\n", &VertexShaderErrorMessage[0]);
    304    }
    305 
    306 
    307 
    308    // Compile Fragment Shader
    309    printf("Compiling shader : %s\n", fragment_file_path);
    310    char const * FragmentSourcePointer = FragmentShaderCode.c_str();
    311    glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL);
    312    glCompileShader(FragmentShaderID);
    313 
    314    // Check Fragment Shader
    315    glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result);
    316    glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    317    if ( InfoLogLength > 0 ){
    318       vector<char> FragmentShaderErrorMessage(InfoLogLength+1);
    319       glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
    320       printf("%s\n", &FragmentShaderErrorMessage[0]);
    321    }
    322 
    323 
    324 
    325    // Link the program
    326    printf("Linking program\n");
    327    GLuint ProgramID = glCreateProgram();
    328    glAttachShader(ProgramID, VertexShaderID);
    329    glAttachShader(ProgramID, FragmentShaderID);
    330    glLinkProgram(ProgramID);
    331 
    332    // Check the program
    333    glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result);
    334    glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength);
    335    if ( InfoLogLength > 0 ){
    336       vector<char> ProgramErrorMessage(InfoLogLength+1);
    337       glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]);
    338       printf("%s\n", &ProgramErrorMessage[0]);
    339    }
    340 
    341    glDeleteShader(VertexShaderID);
    342    glDeleteShader(FragmentShaderID);
    343 
    344    return ProgramID;
    345 }
Note: See TracChangeset for help on using the changeset viewer.