Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <SFML/Window.hpp>
- #include <math.h>
- #include <fstream>
- GLuint createShader(std::string vertShader, std::string fragShader)
- {
- std::streampos size;
- std::ifstream ifs1(vertShader);
- std::string vertSource((std::istreambuf_iterator<char>(ifs1)),
- (std::istreambuf_iterator<char>()));
- std::ifstream ifs2(fragShader);
- std::string fragSource((std::istreambuf_iterator<char>(ifs2)),
- (std::istreambuf_iterator<char>()));
- const GLchar* vertexSource = vertSource.data();
- const GLchar* fragmentSource = fragSource.data();
- GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vertexShader, 1, &vertexSource, NULL);
- glCompileShader(vertexShader);
- GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
- glCompileShader(fragmentShader);
- GLuint shaderProgram = glCreateProgram();
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- glLinkProgram(shaderProgram);
- return shaderProgram;
- }
- GLuint createCube()
- {
- const float vertices[] = {
- 0.25f, 0.25f, -1.25f, 1.0f,
- 0.25f, -0.25f, -1.25f, 1.0f,
- -0.25f, 0.25f, -1.25f, 1.0f,
- -0.25f, -0.25f, -1.25f, 1.0f,
- 0.25f, 0.25f, -2.75f, 1.0f,
- -0.25f, 0.25f, -2.75f, 1.0f,
- 0.25f, -0.25f, -2.75f, 1.0f,
- -0.25f, -0.25f, -2.75f, 1.0f,
- 0.0f, 0.0f, 1.0f, 1.0f,
- 0.8f, 0.8f, 0.8f, 1.0f,
- 1.0f, 0.0f, 0.0f, 1.0f,
- 0.0f, 1.0f, 0.0f, 1.0f,
- 0.5f, 0.5f, 0.0f, 1.0f,
- 0.0f, 1.0f, 1.0f, 1.0f,
- 0.0f, 1.0f, 0.0f, 1.0f,
- 0.5f, 0.5f, 0.0f, 1.0f,
- };
- const GLshort indexData[]
- {
- 0, 1, 2,
- 1, 3, 2,
- 4, 5, 6,
- 6, 5, 7,
- 2, 3, 7,
- 2, 7, 5,
- 0, 6, 1,
- 0, 4, 6,
- 4, 0, 2,
- 4, 2, 5,
- 6, 3, 1,
- 6, 7, 3,
- };
- GLuint vao;
- glGenVertexArrays(1, &vao);
- GLuint vbo;
- glGenBuffers(1, &vbo);
- GLuint indexObjectBuffer;
- glGenBuffers(1, &indexObjectBuffer);
- glBindVertexArray(vao);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexObjectBuffer);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, 36*4, indexData, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
- glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)128);
- glBindVertexArray(0);
- return vao;
- }
- int main()
- {
- sf::Window window(sf::VideoMode(800, 600), "OpenGL");
- glewExperimental =GL_TRUE;
- glewInit();
- glDisableClientState(GL_COLOR_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glFrontFace(GL_CW);
- GLuint vao = createCube();
- GLuint program = createShader("vertex.vert", "fragment.frag");
- float fFrustumScale = 1.0f; float fzNear =0.2f; float fzFar = 300.0f;
- float theMatrix[16];
- memset(theMatrix, 0, sizeof(float)* 16);
- theMatrix[0] = fFrustumScale;
- theMatrix[5] = fFrustumScale;
- theMatrix[10] = (fzFar + fzNear) / (fzNear - fzFar);
- theMatrix[14] = (2 * fzFar * fzNear) / (fzNear - fzFar);
- theMatrix[11] = -1.0f;
- glUseProgram(program);
- GLuint offsetUniform = glGetUniformLocation(program, "offset");
- GLuint perspectiveMatrixUnif = glGetUniformLocation(program, "perspectiveMatrix");
- glUniformMatrix4fv(perspectiveMatrixUnif, 1, GL_FALSE, theMatrix);
- glUseProgram(0);
- glUseProgram(program);
- glBindVertexArray(vao);
- float offsetX = 0.0f;
- float offsetY = 0.0f;
- float offsetZ = 0.0f;
- while (window.isOpen())
- {
- sf::Event event;
- while (window.pollEvent(event))
- {
- if (event.type == sf::Event::Closed)
- {
- window.close();
- }
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
- {
- offsetX += 0.001;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
- {
- offsetX -= 0.001;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
- {
- offsetY += 0.001;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
- {
- offsetY -= 0.001;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::PageDown))
- {
- offsetZ += 0.001;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::PageUp))
- {
- offsetZ -= 0.001;
- }
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT);
- glUniform3f(offsetUniform, offsetX, offsetY, offsetZ);
- glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
- window.display();
- }
- glDeleteProgram(program);
- glDeleteVertexArrays(1, &vao);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement