Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.44 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <SFML/Graphics.hpp>
  3. #include <iostream>
  4. #include <ctime>
  5. #include <cmath>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. #include "ShaderUtil.h"
  11.  
  12. /**
  13.  * In this sample we switch to using the basics of the programmable pipeline of opengl without using VBO's yet.
  14.  * We'll show those in the next sample.
  15.  */
  16.  
  17. const GLfloat vertices[] = {
  18.     0.75f ,  -0.75f, 0, 1, // bottom-right
  19.     0.75f ,  0.75f , 0, 1, // top-right
  20.     -0.75f,  -0.75f, 0, 1, // bottom-left
  21.     -0.75f,   0.75f, 0, 1  // top-left
  22. };
  23.  
  24. const GLfloat colors[] = {
  25.     1,0,0,1,
  26.     0,1,0,1,
  27.     0,0,1,1,
  28.     1,1,0,0
  29. };
  30.  
  31. const GLubyte indices[] = {
  32.     0,1,2,
  33.     1,2,3
  34. };
  35.  
  36.  
  37. int main () {
  38.     // create a window
  39.     sf::Window window(sf::VideoMode (800, 600), "Wawsom!");
  40.     // make it ready for opengl.
  41.     window.setVerticalSyncEnabled( true );
  42.  
  43.     cout << "Initializing GlEW..." << endl;
  44.     // bind the created opengl context that was created by sfml.
  45.     bool glewInitResult = (glewInit() == GLEW_OK);
  46.     cout << "GlEW Initialized:" << glewInitResult  << endl;
  47.  
  48.     GLuint programID = ShaderUtil::createProgam("vertexshader.vs", "fragmentshader.fs");
  49.     glUseProgram (programID);
  50.  
  51.     // make a buffer
  52.     GLuint vertexBufferId;
  53.     glGenBuffers (1, &vertexBufferId);
  54.     // bind it so we can interact with it.
  55.     glBindBuffer (GL_ARRAY_BUFFER, vertexBufferId);
  56.     // fill in the data.
  57.     glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
  58.     // unbind.
  59.     glBindBuffer (GL_ARRAY_BUFFER, 0);
  60.  
  61.     // make a buffer
  62.     GLuint colorBufferId;
  63.     glGenBuffers (1, &colorBufferId);
  64.     // bind it so we can interact with it.
  65.     glBindBuffer (GL_ARRAY_BUFFER, colorBufferId);
  66.     // fill in the data.
  67.     glBufferData (GL_ARRAY_BUFFER, sizeof (colors), colors, GL_STATIC_DRAW);
  68.     // unbind.
  69.     glBindBuffer (GL_ARRAY_BUFFER, 0);
  70.  
  71.     // black is clear.
  72.     glClearColor(0,0,0,1);
  73.  
  74.     // get pointers for uniforms.
  75.     GLint vertexIndex = glGetAttribLocation(programID, "vertex");
  76.     GLint colorIndex = glGetAttribLocation(programID, "color");
  77.     GLint offsetIndex = glGetUniformLocation(programID, "offset");
  78.     GLint mouseIndex = glGetUniformLocation(programID, "mouse");
  79.  
  80.     // set the default mouse position to left bottom.
  81.     GLfloat mouse[] = {0, 0};
  82.     glUniform2fv (mouseIndex, 1, mouse);
  83.  
  84.     while (window.isOpen()) {
  85.         // clear the screen.
  86.         glClear( GL_COLOR_BUFFER_BIT );
  87.  
  88.         // set offset on a circle with radius of 0.25.
  89.         GLfloat offset[] = {0.25f * cos (clock()/500.0f), 0.25f * sin (clock()/500.0f)};
  90.         // send it to shader.
  91.         glUniform2fv (offsetIndex, 1, offset);
  92.  
  93.         // update the mouse position.
  94.         sf::Vector2i pos = sf::Mouse::getPosition(window);
  95.         mouse[0] = pos.x;
  96.         mouse[1] = window.getSize().y - pos.y;
  97.         // set it only if we are within window bounds.
  98.         if (mouse[0] > -1 && mouse[0] < window.getSize().x && mouse[1] > -1 && mouse[1] < window.getSize().y) {
  99.             cout << "{" + to_string(mouse[0]) + ", " + to_string(mouse[1]) + "}" << endl;
  100.             glUniform2fv(mouseIndex, 1, mouse);
  101.         }
  102.  
  103.         // enable vertex array as they are disabled by default.
  104.         glEnableVertexAttribArray(vertexIndex);
  105.         // bind it so we can interact with it.
  106.         glBindBuffer (GL_ARRAY_BUFFER, vertexBufferId);
  107.         // tell opengl how to read it.
  108.         // (
  109.         //  memory index,
  110.         //  number of values in each read,
  111.         //  fixed point values so no normalization needed,
  112.         //  data offset,
  113.         //  null
  114.         //  )
  115.         glVertexAttribPointer(vertexIndex, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
  116.  
  117.         glEnableVertexAttribArray(colorIndex);
  118.         glBindBuffer (GL_ARRAY_BUFFER, colorBufferId);
  119.         glVertexAttribPointer(colorIndex, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
  120.  
  121.         // feed indices
  122.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
  123.  
  124.         // disable arrays as we are done.
  125.         glDisableVertexAttribArray(vertexIndex);
  126.         glDisableVertexAttribArray(colorIndex);
  127.  
  128.         // draw.
  129.         window.display();
  130.  
  131.         // check for events to exit from loop or set the size of viewport.
  132.         sf::Event event;
  133.         while (window.pollEvent(event)) {
  134.             if (event.type == sf::Event::Closed) window.close();
  135.             if (event.type == sf::Event::Resized) glViewport(0, 0, event.size.width, event.size.height);
  136.         }
  137.     }
  138.  
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement