Advertisement
Guest User

Untitled

a guest
Sep 26th, 2012
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "SFML/Window.hpp"
  2. //#include <SFML/OpenGL.hpp>
  3. #include "GL/glew.h"
  4. #include <iostream>
  5.  
  6. //create a handle for the glslprogram (vert+frag)
  7. GLuint glslprogram;
  8. //this will hold our vertex coords that we pass on to the glsl program
  9. GLuint attribute_coord2d;
  10.  
  11. int initresources()
  12. {
  13.     //this will hold the status of the compiler check
  14.     // set it to false initially
  15.     GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
  16.  
  17.     //get a handle for our vertex shader
  18.     GLuint myvertexshader = glCreateShader(GL_VERTEX_SHADER);
  19.     //get a handle for our fragment shader
  20.     GLuint myfragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
  21.  
  22.  
  23.     //define the source of the vertex shader
  24.     const char* vs_source =
  25.     "#version 150"
  26.     "attribute vec2 coord2d;"
  27.     "void main(void) {"
  28.     "gl_Position = vec4(coord2d, 0.0, 1.0);"
  29.     "}";
  30.  
  31.     //define the source of the fragment shader
  32.     const char* fs_source =
  33.      "#version 150           \n"
  34.     "void main(void) {        "
  35.     "  gl_FragColor[0] = 0.0; " //r
  36.     "  gl_FragColor[1] = 0.0; " //g
  37.     "  gl_FragColor[2] = 1.0; " //b
  38.     "}";
  39.  
  40.     //set the sourcecode of myvertexshader to vs_source
  41.     glShaderSource(myvertexshader, 1, &vs_source, NULL);
  42.     //set the sourcecode of myfragmentshader to fs_source
  43.     glShaderSource(myfragmentshader, 1, &fs_source, NULL);
  44.  
  45.     //compile the vertex shader
  46.     glCompileShader(myvertexshader);
  47.     //compile the fragment shader
  48.     glCompileShader(myfragmentshader);
  49.  
  50.     //check if compile was OK, the result will be written to compile_ok
  51.     glGetShaderiv(myvertexshader, GL_COMPILE_STATUS, &compile_ok);
  52.     if (compile_ok == 0)
  53.         std::cout << "Error compiling vertex shader";
  54.     //check if compile of fragment shader was ok
  55.     glGetShaderiv(myfragmentshader, GL_COMPILE_STATUS, &compile_ok);
  56.     if (compile_ok == 0)
  57.         std::cout << "Error compiling fragment shader";
  58.  
  59.     //now that we have compiled the shaders, lets make a program
  60.  
  61.     glslprogram = glCreateProgram();
  62.     //attach the vertex shader to our program
  63.     glAttachShader(glslprogram, myvertexshader);
  64.     //attach the fragment shader to our progrma
  65.     glAttachShader(glslprogram, myfragmentshader);
  66.     //let's link the program
  67.     glLinkProgram(glslprogram);
  68.     //check if the linking was successful
  69.     glGetProgramiv(glslprogram, GL_LINK_STATUS, &link_ok);
  70.     if (!link_ok)
  71.         std::cout << "Error linking our glsl program";
  72.  
  73.  
  74.  
  75.     //we called our variable in the glslprogram "coord2d"
  76.     //lets make sure that opengl knows where to get it
  77.     //ie lets bind attribute_coord2d (our code) to coord2d (shader code)
  78.     const char* attribute_name = "coord2d";
  79.     attribute_coord2d = glGetAttribLocation(glslprogram, attribute_name);
  80.     if (attribute_coord2d == -1)
  81.         std::cout << "Could not bind attribute";
  82.  
  83.     return 1;
  84. }
  85.  
  86.  
  87. void onDisplay()
  88. {
  89.     //now that we have made our shaders and combinded them into a program
  90.     //and now that the program knows where to get its input
  91.     //lets make a display function that feeds our vertexes to the program
  92.  
  93.     glClearColor(1.0, 1.0, 1.0, 1.0);
  94.     glClear(GL_COLOR_BUFFER_BIT);
  95.  
  96.     //tell GL to use our program
  97.     glUseProgram(glslprogram);
  98.     glEnableVertexAttribArray(attribute_coord2d);
  99.  
  100.     //lets define our vertexes
  101.     GLfloat triangle_vertices[] = {
  102.      0.0,  0.8,
  103.     -0.8, -0.8,
  104.      0.8, -0.8
  105.     };
  106.  
  107.     //describe the array to opengl
  108.     glVertexAttribPointer(
  109.         attribute_coord2d,  //our attribute
  110.         2,                  //number of elements per vertex (x, y)
  111.         GL_FLOAT,           // these are floats
  112.         GL_FALSE,           // take our value as-is
  113.         0,                  // no extra data between each position
  114.         triangle_vertices); // the name of our array
  115.  
  116.     //
  117.     glDrawArrays(GL_TRIANGLES, 0, 3);
  118.     glDisableVertexAttribArray(attribute_coord2d);
  119.  
  120.  
  121. }
  122.  
  123. int main(int argc, char* argv[])
  124. {
  125.  
  126.  
  127.  
  128.     sf::Window App(sf::VideoMode(800, 600, 32),
  129.                     "OpenGL 3 window",
  130.                     sf::Style::Close,
  131.                     sf::ContextSettings(24, 8, 0, 3, 3));
  132.  
  133.     sf::ContextSettings settings = App.getSettings();
  134.  
  135.     std::cout << "depth bits:" << settings.depthBits << std::endl;
  136.     std::cout << "stencil bits:" << settings.stencilBits << std::endl;
  137.     std::cout << "antialiasing level:" << settings.antialiasingLevel << std::endl;
  138.     std::cout << "version:" << settings.majorVersion << "." << settings.minorVersion << std::endl;
  139.  
  140.  
  141.     App.display();
  142.  
  143.     glewInit();
  144.  
  145.  
  146.  
  147.     if ( !initresources() )
  148.         return 1;
  149.    
  150.  
  151.     while (1)
  152.     {
  153.         onDisplay();    //do the opengl rendering
  154.         App.display();  //draw it on the screen...
  155.    
  156.     }
  157.  
  158.    
  159.  
  160.     return 0;
  161. }
Advertisement
RAW Paste Data Copied
Advertisement