Guest User

SFML&OpenGL

a guest
Jun 4th, 2015
290
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <GL/glew.h>
  2. #include <glm/glm.hpp>
  3. #include <glm/gtc/matrix_transform.hpp>
  4. #include <SFML\OpenGL.hpp>
  5. #include <SFML\Graphics.hpp>
  6. #include <fstream>
  7. #include <string>
  8. #include <vector>
  9.  
  10. GLuint CompileShader (const char *pszVertexFile, const char *pszFragmentFile)
  11.     {
  12.         bool bOk = true;
  13.         GLint iResult = GL_FALSE;
  14.         int iInfoLogLength;
  15.         GLuint uiProgramID = -1;
  16.  
  17.         // Output some info
  18.         const unsigned char* version = (const unsigned char*)glGetString(GL_SHADING_LANGUAGE_VERSION);
  19.         printf("SHADER: Running GLSL Version %s\n",version);
  20.  
  21.         // Create the shaders
  22.         GLuint uiVertexShaderID = glCreateShader(GL_VERTEX_SHADER);
  23.         GLuint uiFragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
  24.  
  25.         bOk = uiVertexShaderID != 0 && uiFragmentShaderID != 0;
  26.        
  27.         // Read the Vertex Shader code from the file
  28.         std::string sVertexShaderCode;
  29.         if (bOk)
  30.         {
  31.             std::ifstream ifsVertexShaderStream(pszVertexFile, std::ios::in);
  32.             if(ifsVertexShaderStream.is_open())
  33.             {
  34.                 std::string sLine = "";
  35.                 while(getline(ifsVertexShaderStream, sLine))
  36.                     sVertexShaderCode += "\n" + sLine;
  37.                 ifsVertexShaderStream.close();
  38.             }
  39.             else
  40.             {
  41.                 bOk = false;
  42.             }
  43.         }
  44.  
  45.         // Read the Fragment Shader code from the file
  46.         std::string FragmentShaderCode;
  47.         if (bOk)
  48.         {
  49.             std::ifstream FragmentShaderStream(pszFragmentFile, std::ios::in);
  50.             if(FragmentShaderStream.is_open())
  51.             {
  52.                 std::string sLine = "";
  53.                 while(getline(FragmentShaderStream, sLine))
  54.                     FragmentShaderCode += "\n" + sLine;
  55.                 FragmentShaderStream.close();
  56.             }
  57.             else
  58.             {
  59.                 bOk = false;
  60.             }
  61.         }
  62.  
  63.         if (bOk)
  64.         {
  65.             // Compile Vertex Shader
  66.             printf("SHADER: Compiling vertex shader '%s'\n", pszVertexFile);
  67.             char const * VertexSourcePointer = sVertexShaderCode.c_str();
  68.             glShaderSource(uiVertexShaderID, 1, &VertexSourcePointer , NULL);
  69.             glCompileShader(uiVertexShaderID);
  70.  
  71.             // Check Vertex Shader
  72.             glGetShaderiv(uiVertexShaderID, GL_COMPILE_STATUS, &iResult);
  73.             glGetShaderiv(uiVertexShaderID, GL_INFO_LOG_LENGTH, &iInfoLogLength);
  74.             std::vector<char> VertexShaderErrorMessage(iInfoLogLength);
  75.             glGetShaderInfoLog(uiVertexShaderID, iInfoLogLength, NULL, &VertexShaderErrorMessage[0]);
  76.  
  77.             bOk = strlen(&VertexShaderErrorMessage[0]) == 0;
  78.         }
  79.  
  80.         if (bOk)
  81.         {
  82.             // Compile Fragment Shader
  83.             printf("SHADER: Compiling fragment shader '%s'\n", pszFragmentFile);
  84.             char const * FragmentSourcePointer = FragmentShaderCode.c_str();
  85.             glShaderSource(uiFragmentShaderID, 1, &FragmentSourcePointer , NULL);
  86.             glCompileShader(uiFragmentShaderID);
  87.  
  88.             // Check Fragment Shader
  89.             glGetShaderiv(uiFragmentShaderID, GL_COMPILE_STATUS, &iResult);
  90.             glGetShaderiv(uiFragmentShaderID, GL_INFO_LOG_LENGTH, &iInfoLogLength);
  91.             std::vector<char> FragmentShaderErrorMessage(iInfoLogLength);
  92.             glGetShaderInfoLog(uiFragmentShaderID, iInfoLogLength, NULL, &FragmentShaderErrorMessage[0]);
  93.  
  94.             bOk = strlen(&FragmentShaderErrorMessage[0]) == 0;
  95.         }
  96.  
  97.         if (bOk)
  98.         {
  99.             // Link the program
  100.             printf("SHADER: Linking shader to program\n");
  101.             uiProgramID = glCreateProgram();
  102.             glAttachShader(uiProgramID, uiVertexShaderID);
  103.             glAttachShader(uiProgramID, uiFragmentShaderID);
  104.             glLinkProgram(uiProgramID);
  105.  
  106.             // Check the program
  107.             glGetProgramiv(uiProgramID, GL_LINK_STATUS, &iResult);
  108.             glGetProgramiv(uiProgramID, GL_INFO_LOG_LENGTH, &iInfoLogLength);
  109.             std::vector<char> ProgramErrorMessage(std::max(iInfoLogLength, int(1)) );
  110.             glGetProgramInfoLog(uiProgramID, iInfoLogLength, NULL, &ProgramErrorMessage[0]);
  111.  
  112.             bOk = strlen(&ProgramErrorMessage[0]) == 0;
  113.         }
  114.  
  115.         if (bOk)
  116.         {
  117.             // Delete temp shaders
  118.             glDeleteShader(uiVertexShaderID);
  119.             glDeleteShader(uiFragmentShaderID);
  120.  
  121.             // Flush
  122.             glFlush();
  123.         }
  124.  
  125.         return (bOk ? uiProgramID : -1);
  126.     }
  127.  
  128.  
  129. void renderingThread(sf::RenderWindow* window)
  130. {
  131.     // activate the window's context on this thread
  132.     window->setActive(true);
  133.  
  134.     // Create and compile shader
  135.     GLuint programID = CompileShader( "Basic.vertexshader", "Basic.fragmentshader" );
  136.  
  137.     // Get a handle for our "MVP" uniform
  138.     GLuint MatrixID = glGetUniformLocation(programID, "MVP");
  139.  
  140.     // Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
  141.     glm::mat4 Projection = glm::perspective(45.0f, 4.0f / 3.0f, 0.1f, 100.0f);
  142.  
  143.     // Camera matrix
  144.     glm::mat4 View       = glm::lookAt(
  145.         glm::vec3(4,3,3), // Camera is at (4,3,3), in World Space
  146.         glm::vec3(0,0,0), // and looks at the origin
  147.         glm::vec3(0,1,0)  // Head is up
  148.     );
  149.  
  150.     // Model matrix : an identity matrix (model will be at the origin)
  151.     glm::mat4 Model      = glm::mat4(1.0f);  // Changes for each model !
  152.  
  153.     // Our ModelViewProjection : multiplication of our 3 matrices
  154.     glm::mat4 MVP        = Projection * View * Model; // Remember, matrix multiplication is the other way around
  155.  
  156.     // Create triangle
  157.     static const GLfloat g_vertex_buffer_data[] = {
  158.         -1.0f, -1.0f, 0.0f,
  159.          1.0f, -1.0f, 0.0f,
  160.          0.0f,  1.0f, 0.0f,
  161.     };
  162.     GLuint vertexbuffer;
  163.     glGenBuffers(1, &vertexbuffer);
  164.     glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  165.     glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
  166.  
  167.  
  168.     // the rendering loop
  169.     float lastTime = 0.f;
  170.     sf::Clock clock;
  171.     while (window->isOpen())
  172.     {
  173.         // Clear buffers
  174.         glClearColor(1, 0, 0, 1);
  175.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  176.  
  177.         // Use our shader
  178.         glUseProgram(programID);
  179.  
  180.         // Send our transformation to the currently bound shader, in the "MVP" uniform
  181.         glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
  182.  
  183.         // 1rst attribute buffer : vertices
  184.         glEnableVertexAttribArray(0);
  185.         glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
  186.         glVertexAttribPointer(
  187.             0,                  // attribute 0. No particular reason for 0, but must match the layout in the shader.
  188.             3,                  // size
  189.             GL_FLOAT,           // type
  190.             GL_FALSE,           // normalized?
  191.             0,                  // stride
  192.             (void*)0            // array buffer offset
  193.         );
  194.  
  195.         // Draw the triangle
  196.         glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
  197.  
  198.         // Disable attribute
  199.         glDisableVertexAttribArray(0);
  200.  
  201.         glUseProgram(NULL);
  202.  
  203.  
  204.             // draw 2D (NO WORKING)
  205.         window->pushGLStates(); //save the current OpenGL state
  206.         window->resetGLStates();
  207.  
  208.         sf::CircleShape shape(50);
  209.         shape.setFillColor(sf::Color(0, 255, 0));
  210.         window->draw(shape);
  211.  
  212.         window->popGLStates();
  213.  
  214.         // end the current frame
  215.         window->display();
  216.     }
  217.  
  218.     // Cleanup VBO
  219.     glDeleteBuffers(1, &vertexbuffer);
  220.     glDeleteProgram(programID);
  221. }
  222.  
  223. int main()
  224. {
  225.     // Create OpenGL 3.3 context and window
  226.     sf::ContextSettings settings;
  227.     settings.depthBits = 24;
  228.     settings.stencilBits = 8;
  229.     settings.antialiasingLevel = 4;
  230.     settings.majorVersion = 3;
  231.     settings.minorVersion = 3;
  232.  
  233.         // create the window (remember: it's safer to create it in the main thread due to OS limitations)
  234.         sf::RenderWindow window(sf::VideoMode(800, 600, 32), "OpenGL + SFML", sf::Style::Default, settings);
  235.  
  236.     // Window default options
  237.     window.setVerticalSyncEnabled(true);
  238.     window.setMouseCursorVisible(true);
  239.     window.setFramerateLimit(60);
  240.     window.setVisible(true);
  241.  
  242.         // deactivate its OpenGL context
  243.         window.setActive(false);
  244.  
  245.     // Init GLEW (OpenGL Extension Wrangler Library)
  246.     glewExperimental = GL_TRUE;     // ensures that all extensions with valid entry points will be exposed
  247.     bool bOk = (glewInit() == GLEW_OK);
  248.     printf("NFO: Using GLEW %s\n", glewGetString(GLEW_VERSION));
  249.  
  250.     // Enable depth test (to avoid back triangles to be drawn in first plane)
  251.     glEnable(GL_DEPTH_TEST);
  252.  
  253.     // Accept fragment if it closer to the camera than the former one
  254.     glDepthFunc(GL_LESS);
  255.  
  256.     // Create a VAO (Vertex Array Object)
  257.     GLuint m_uiVertexArrayID;
  258.     glGenVertexArrays(1, &m_uiVertexArrayID);
  259.     glBindVertexArray(m_uiVertexArrayID);
  260.  
  261.     glFlush();
  262.  
  263.         // launch the rendering thread
  264.         sf::Thread thread(&renderingThread, &window);
  265.         thread.launch();
  266.  
  267.         // the event/logic/whatever loop
  268.         while (window.isOpen())
  269.         {
  270.         sf::Event event;
  271.             while (window.pollEvent(event))
  272.             {
  273.                     // "close requested" event: we close the window
  274.                 if (event.type == sf::Event::Closed)
  275.                         window.close();
  276.             }
  277.         }
  278.  
  279.     thread.wait();
  280.  
  281.     glDeleteVertexArrays(1, &m_uiVertexArrayID);
  282.         return 0;
  283. }
  284.  
  285.  
  286. /*
  287.  
  288. BASIC.VERTEXSHADER
  289. -----------------------------------------------------------------------------
  290.  
  291. #version 330 core
  292.  
  293. uniform mat4 MVP;
  294. layout(location = 0) in vec3 modelVertices;
  295.  
  296. void main()
  297. {
  298.     gl_Position =  MVP * vec4(modelVertices, 1);
  299. }
  300.  
  301.  
  302. BASIC.FRAGMENTSHADER
  303. -----------------------------------------------------------------------------
  304. #version 330 core
  305.  
  306. out vec4 color;
  307.  
  308. void main()
  309. {
  310.     color = vec4 (1.0f, 1.0f, 1.0f, 1.0f);
  311. }
  312. */
RAW Paste Data