Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <SFML/Graphics.hpp>
- #include <iostream>
- #include <ctime>
- #include <cmath>
- #include <string>
- using namespace std;
- #include "ShaderUtil.h"
- /**
- * In this sample we switch to using the basics of the programmable pipeline of opengl without using VBO's yet.
- * We'll show those in the next sample.
- */
- const GLfloat vertices[] = {
- 0.75f , -0.75f, 0, 1, // bottom-right
- 0.75f , 0.75f , 0, 1, // top-right
- -0.75f, -0.75f, 0, 1, // bottom-left
- -0.75f, 0.75f, 0, 1 // top-left
- };
- const GLfloat colors[] = {
- 1,0,0,1,
- 0,1,0,1,
- 0,0,1,1,
- 1,1,0,0
- };
- const GLubyte indices[] = {
- 0,1,2,
- 1,2,3
- };
- int main () {
- // create a window
- sf::Window window(sf::VideoMode (800, 600), "Wawsom!");
- // make it ready for opengl.
- window.setVerticalSyncEnabled( true );
- cout << "Initializing GlEW..." << endl;
- // bind the created opengl context that was created by sfml.
- bool glewInitResult = (glewInit() == GLEW_OK);
- cout << "GlEW Initialized:" << glewInitResult << endl;
- GLuint programID = ShaderUtil::createProgam("vertexshader.vs", "fragmentshader.fs");
- glUseProgram (programID);
- // make a buffer
- GLuint vertexBufferId;
- glGenBuffers (1, &vertexBufferId);
- // bind it so we can interact with it.
- glBindBuffer (GL_ARRAY_BUFFER, vertexBufferId);
- // fill in the data.
- glBufferData (GL_ARRAY_BUFFER, sizeof (vertices), vertices, GL_STATIC_DRAW);
- // unbind.
- glBindBuffer (GL_ARRAY_BUFFER, 0);
- // make a buffer
- GLuint colorBufferId;
- glGenBuffers (1, &colorBufferId);
- // bind it so we can interact with it.
- glBindBuffer (GL_ARRAY_BUFFER, colorBufferId);
- // fill in the data.
- glBufferData (GL_ARRAY_BUFFER, sizeof (colors), colors, GL_STATIC_DRAW);
- // unbind.
- glBindBuffer (GL_ARRAY_BUFFER, 0);
- // black is clear.
- glClearColor(0,0,0,1);
- // get pointers for uniforms.
- GLint vertexIndex = glGetAttribLocation(programID, "vertex");
- GLint colorIndex = glGetAttribLocation(programID, "color");
- GLint offsetIndex = glGetUniformLocation(programID, "offset");
- GLint mouseIndex = glGetUniformLocation(programID, "mouse");
- // set the default mouse position to left bottom.
- GLfloat mouse[] = {0, 0};
- glUniform2fv (mouseIndex, 1, mouse);
- while (window.isOpen()) {
- // clear the screen.
- glClear( GL_COLOR_BUFFER_BIT );
- // set offset on a circle with radius of 0.25.
- GLfloat offset[] = {0.25f * cos (clock()/500.0f), 0.25f * sin (clock()/500.0f)};
- // send it to shader.
- glUniform2fv (offsetIndex, 1, offset);
- // update the mouse position.
- sf::Vector2i pos = sf::Mouse::getPosition(window);
- mouse[0] = pos.x;
- mouse[1] = window.getSize().y - pos.y;
- // set it only if we are within window bounds.
- if (mouse[0] > -1 && mouse[0] < window.getSize().x && mouse[1] > -1 && mouse[1] < window.getSize().y) {
- cout << "{" + to_string(mouse[0]) + ", " + to_string(mouse[1]) + "}" << endl;
- glUniform2fv(mouseIndex, 1, mouse);
- }
- // enable vertex array as they are disabled by default.
- glEnableVertexAttribArray(vertexIndex);
- // bind it so we can interact with it.
- glBindBuffer (GL_ARRAY_BUFFER, vertexBufferId);
- // tell opengl how to read it.
- // (
- // memory index,
- // number of values in each read,
- // fixed point values so no normalization needed,
- // data offset,
- // null
- // )
- glVertexAttribPointer(vertexIndex, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
- glEnableVertexAttribArray(colorIndex);
- glBindBuffer (GL_ARRAY_BUFFER, colorBufferId);
- glVertexAttribPointer(colorIndex, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
- // feed indices
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
- // disable arrays as we are done.
- glDisableVertexAttribArray(vertexIndex);
- glDisableVertexAttribArray(colorIndex);
- // draw.
- window.display();
- // check for events to exit from loop or set the size of viewport.
- sf::Event event;
- while (window.pollEvent(event)) {
- if (event.type == sf::Event::Closed) window.close();
- if (event.type == sf::Event::Resized) glViewport(0, 0, event.size.width, event.size.height);
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement