Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GL_GLEXT_PROTOTYPES
- #include <SFML/Graphics.hpp>
- #include <SFML/OpenGL.hpp>
- #include <stdlib.h>
- #include <string>
- #include <fstream>
- #include <iostream>
- #include <stdio.h>
- #include <vector>
- #include <algorithm>
- #include <sys/time.h>
- #include "utils.h"
- #include "Camera.h"
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- void events();
- void Init(void);
- void input();
- void resize(int w, int h);
- float pitch = 0.0f, yaw = 0.0f;
- glm::vec3 position(0.0f);
- Camera camera(&position, &pitch, &yaw);
- sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
- #define ARRAY_COUNT( array ) (sizeof(array) / sizeof(*array))
- GLuint vao1, vao2, vertexBufferObject, indexBufferObject, program;
- GLuint offsetLoc, perspectiveMatrixLoc, cameraMatrixLoc, modelMatrixLoc;
- float frustumScale = 1.0f;
- float fzNear = 0.01f, fzFar = 300.0f;
- float perspectiveMatrix[16];
- float modelMatrix[16];
- float cameraMatrix[16];
- #define GREEN_COLOR 0.75f, 0.75f, 1.0f, 1.0f
- #define BLUE_COLOR 0.0f, 0.5f, 0.0f, 1.0f
- #define RED_COLOR 1.0f, 0.0f, 0.0f, 1.0f
- #define GREY_COLOR 0.8f, 0.8f, 0.8f, 1.0f
- #define BROWN_COLOR 0.5f, 0.5f, 0.0f, 1.0f
- #define YELLOW_COLOR 1.0f, 1.0f, 0.0f, 1.0f
- #define PINK_COLOR 1.0f, 0.0f, 1.0f, 1.0f
- #define CYAN_COLOR 0.0f, 1.0f, 1.0f, 1.0f
- #define CUBE_SIZE 0.5f
- const float vertexData[] = {
- -CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE,
- -CUBE_SIZE, CUBE_SIZE, CUBE_SIZE,
- CUBE_SIZE, -CUBE_SIZE, CUBE_SIZE,
- CUBE_SIZE, CUBE_SIZE, CUBE_SIZE,
- CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
- CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE,
- -CUBE_SIZE, -CUBE_SIZE, -CUBE_SIZE,
- -CUBE_SIZE, CUBE_SIZE, -CUBE_SIZE,
- BLUE_COLOR,
- GREEN_COLOR,
- RED_COLOR,
- YELLOW_COLOR,
- PINK_COLOR,
- GREY_COLOR,
- BROWN_COLOR,
- CYAN_COLOR
- };
- const GLubyte indexData[] =
- {
- 0, 1, 2, 3,
- 4, 5, 6, 7,
- 0, 1,
- 1, 7, 3, 5,
- 6, 0, 4, 2
- };
- float r = 0.0f;
- float radius = 5.0f;
- float tmpMtx1[16], tmpMtx2[16], rotMatrix[16];
- static void display(void)
- {
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- glClearDepth(1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- IdentityMatrix(modelMatrix);
- IdentityMatrix(cameraMatrix);
- /*RotationMatrix_Y(rotMatrix, r);
- CopyMatrix(modelMatrix, tmpMatrix);
- MultiplyMatrix(modelMatrix, tmpMatrix, rotMatrix);
- RotationMatrix_X(rotMatrix, r);
- CopyMatrix(modelMatrix, tmpMatrix);
- MultiplyMatrix(modelMatrix, tmpMatrix, rotMatrix);
- */
- TranslationMatrix(cameraMatrix, position.x, position.y, position.z);
- glUseProgram(program);
- glUniformMatrix4fv(perspectiveMatrixLoc, 1, GL_FALSE, perspectiveMatrix);
- glUniformMatrix4fv(cameraMatrixLoc, 1, GL_FALSE, cameraMatrix);
- glUniformMatrix4fv(modelMatrixLoc, 1, GL_FALSE, modelMatrix);
- glBindVertexArray(vao1);
- glDrawElements(GL_TRIANGLE_STRIP, 10, GL_UNSIGNED_BYTE, (void*)0);
- glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, (void*)10);
- glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, (void*)14);
- glBindVertexArray(0);
- glUseProgram(0);
- r+=0.025f;
- window.display();
- }
- int main(void)
- {
- Init();
- while (window.isOpen()) {
- events();
- input();
- display();
- }
- return EXIT_SUCCESS;
- }
- void input()
- {
- #define SPEED 0.01f
- // glm::vec3 look = camera.getLookVector();
- // glm::vec3 right = camera.getRightVector();
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)) {
- //pitch += SPEED;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) {
- //pitch -= SPEED;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
- //yaw += SPEED;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
- //yaw -= SPEED;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
- position.x += SPEED;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
- position.x -= SPEED;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
- position.z += SPEED;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
- position.z -= SPEED;
- }
- if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) {
- position.y += SPEED;
- } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)) {
- position.y -= SPEED;
- }
- if(pitch > PI_2) pitch -= PI_2;
- if(yaw > PI_2) yaw -= PI_2;
- if(pitch < 0) pitch += PI_2;
- if(yaw < 0) yaw += PI_2;
- //camera.update();
- }
- void events()
- {
- sf::Event event;
- while (window.pollEvent(event)) {
- switch (event.type)
- {
- case sf::Event::Closed:
- window.close();
- break;
- case sf::Event::Resized:
- resize(event.size.width, event.size.height);
- break;
- default:
- break;
- }
- }
- }
- void resize(int w, int h)
- {
- perspectiveMatrix[0] = frustumScale / (w / (float)h);
- perspectiveMatrix[5] = frustumScale;
- glViewport(0, 0, (GLsizei) w, (GLsizei) h);
- }
- void Init(void)
- {
- program = InitializeProgram();
- glGenBuffers(1, &vertexBufferObject);
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, 0);
- glGenBuffers(1, &indexBufferObject);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
- glGenVertexArrays(1, &vao1);
- glBindVertexArray(vao1);
- glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
- glEnableVertexAttribArray(0);
- glEnableVertexAttribArray(1);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
- glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(float) * 3 * 8));
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject);
- glBindVertexArray(0);
- offsetLoc = glGetUniformLocation(program, "offset");
- modelMatrixLoc = glGetUniformLocation(program, "modelMatrix");
- perspectiveMatrixLoc = glGetUniformLocation(program, "perspectiveMatrix");
- cameraMatrixLoc = glGetUniformLocation(program, "cameraMatrix");
- memset(perspectiveMatrix, 0, sizeof(float) * 16);
- IdentityMatrix(modelMatrix);
- perspectiveMatrix[0] = frustumScale;
- perspectiveMatrix[5] = frustumScale;
- perspectiveMatrix[10] = (fzFar + fzNear) / (fzNear - fzFar);
- perspectiveMatrix[14] = (2 * fzFar * fzNear) / (fzNear - fzFar);
- perspectiveMatrix[11] = -1.0f;
- glEnable(GL_DEPTH_TEST);
- glDepthMask(GL_TRUE);
- glDepthFunc(GL_LEQUAL);
- glDepthRange(0.0f, 1.0f);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement