Advertisement
Guest User

opengl

a guest
May 16th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.53 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <SFML/Window.hpp>
  3. #include <math.h>
  4. #include <fstream>
  5.  
  6.  
  7. GLuint createShader(std::string vertShader, std::string fragShader)
  8. {
  9.     std::streampos size;
  10.  
  11.     std::ifstream ifs1(vertShader);
  12.     std::string vertSource((std::istreambuf_iterator<char>(ifs1)),
  13.         (std::istreambuf_iterator<char>()));
  14.  
  15.     std::ifstream ifs2(fragShader);
  16.     std::string fragSource((std::istreambuf_iterator<char>(ifs2)),
  17.         (std::istreambuf_iterator<char>()));
  18.  
  19.     const GLchar* vertexSource = vertSource.data();
  20.     const GLchar* fragmentSource = fragSource.data();
  21.  
  22.     GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  23.  
  24.     glShaderSource(vertexShader, 1, &vertexSource, NULL);
  25.  
  26.     glCompileShader(vertexShader);
  27.  
  28.     GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  29.     glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  30.  
  31.     glCompileShader(fragmentShader);
  32.  
  33.     GLuint shaderProgram = glCreateProgram();
  34.  
  35.     glAttachShader(shaderProgram, vertexShader);
  36.     glAttachShader(shaderProgram, fragmentShader);
  37.     glLinkProgram(shaderProgram);
  38.     return shaderProgram;
  39. }
  40.  
  41. GLuint createCube()
  42. {
  43.     const float vertices[] = {
  44.         0.25f, 0.25f, -1.25f, 1.0f,
  45.         0.25f, -0.25f, -1.25f, 1.0f,
  46.         -0.25f, 0.25f, -1.25f, 1.0f,
  47.         -0.25f, -0.25f, -1.25f, 1.0f,
  48.         0.25f, 0.25f, -2.75f, 1.0f,
  49.         -0.25f, 0.25f, -2.75f, 1.0f,
  50.         0.25f, -0.25f, -2.75f, 1.0f,
  51.         -0.25f, -0.25f, -2.75f, 1.0f,
  52.  
  53.  
  54.         0.0f, 0.0f, 1.0f, 1.0f,
  55.         0.8f, 0.8f, 0.8f, 1.0f,
  56.         1.0f, 0.0f, 0.0f, 1.0f,
  57.         0.0f, 1.0f, 0.0f, 1.0f,
  58.         0.5f, 0.5f, 0.0f, 1.0f,
  59.         0.0f, 1.0f, 1.0f, 1.0f,
  60.         0.0f, 1.0f, 0.0f, 1.0f,
  61.         0.5f, 0.5f, 0.0f, 1.0f,
  62.     };
  63.  
  64.     const GLshort indexData[]
  65.     {
  66.             0, 1, 2,
  67.             1, 3, 2,
  68.             4, 5, 6,
  69.             6, 5, 7,
  70.             2, 3, 7,
  71.             2, 7, 5,
  72.             0, 6, 1,
  73.             0, 4, 6,
  74.             4, 0, 2,
  75.             4, 2, 5,
  76.             6, 3, 1,
  77.             6, 7, 3,
  78.     };
  79.  
  80.     GLuint vao;
  81.     glGenVertexArrays(1, &vao);
  82.    
  83.     GLuint vbo;
  84.     glGenBuffers(1, &vbo);
  85.  
  86.     GLuint indexObjectBuffer;
  87.     glGenBuffers(1, &indexObjectBuffer);
  88.  
  89.     glBindVertexArray(vao);
  90.  
  91.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  92.     glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  93.  
  94.    
  95.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexObjectBuffer);
  96.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36*4, indexData, GL_STATIC_DRAW);
  97.  
  98.     glEnableVertexAttribArray(0);
  99.     glEnableVertexAttribArray(1);
  100.  
  101.     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
  102.     glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)128);
  103.  
  104.     glBindVertexArray(0);
  105.  
  106.     return vao;
  107. }
  108.  
  109.  
  110.  
  111. int main()
  112. {
  113.  
  114.  
  115.     sf::Window window(sf::VideoMode(800, 600), "OpenGL");
  116.  
  117.     glewExperimental =GL_TRUE;
  118.     glewInit();
  119.  
  120.  
  121.     glDisableClientState(GL_COLOR_ARRAY);
  122.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  123.  
  124.     glEnable(GL_CULL_FACE);
  125.     glCullFace(GL_BACK);
  126.     glFrontFace(GL_CW);
  127.  
  128.     GLuint vao = createCube();
  129.  
  130.     GLuint program = createShader("vertex.vert", "fragment.frag");
  131.  
  132.     float fFrustumScale = 1.0f; float fzNear =0.2f; float fzFar = 300.0f;
  133.  
  134.     float theMatrix[16];
  135.     memset(theMatrix, 0, sizeof(float)* 16);
  136.  
  137.     theMatrix[0] = fFrustumScale;
  138.     theMatrix[5] = fFrustumScale;
  139.     theMatrix[10] = (fzFar + fzNear) / (fzNear - fzFar);
  140.     theMatrix[14] = (2 * fzFar * fzNear) / (fzNear - fzFar);
  141.     theMatrix[11] = -1.0f;
  142.  
  143.     glUseProgram(program);
  144.     GLuint offsetUniform = glGetUniformLocation(program, "offset");
  145.     GLuint perspectiveMatrixUnif = glGetUniformLocation(program, "perspectiveMatrix");
  146.     glUniformMatrix4fv(perspectiveMatrixUnif, 1, GL_FALSE, theMatrix);
  147.     glUseProgram(0);
  148.  
  149.     glUseProgram(program);
  150.     glBindVertexArray(vao);
  151.    
  152.     float offsetX = 0.0f;
  153.     float offsetY = 0.0f;
  154.     float offsetZ = 0.0f;
  155.  
  156.     while (window.isOpen())
  157.     {
  158.         sf::Event event;
  159.         while (window.pollEvent(event))
  160.         {
  161.             if (event.type == sf::Event::Closed)
  162.             {
  163.                 window.close();
  164.             }
  165.         }
  166.  
  167.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
  168.         {
  169.             offsetX += 0.001;
  170.         }
  171.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
  172.         {
  173.             offsetX -= 0.001;
  174.         }
  175.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
  176.         {
  177.             offsetY += 0.001;
  178.         }
  179.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
  180.         {
  181.             offsetY -= 0.001;
  182.         }
  183.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::PageDown))
  184.         {
  185.             offsetZ += 0.001;
  186.         }
  187.         if (sf::Keyboard::isKeyPressed(sf::Keyboard::PageUp))
  188.         {
  189.             offsetZ -= 0.001;
  190.         }
  191.  
  192.         glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  193.         glClear(GL_COLOR_BUFFER_BIT);
  194.  
  195.  
  196.         glUniform3f(offsetUniform, offsetX, offsetY, offsetZ);
  197.  
  198.         glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
  199.  
  200.         window.display();
  201.     }
  202.  
  203.  
  204.     glDeleteProgram(program);
  205.     glDeleteVertexArrays(1, &vao);
  206.  
  207.     return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement