Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #define GLEW_STATIC
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <SOIL.h>
- #include <glm/glm.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include "Shader.h"
- #include <vector>
- using namespace std;
- // INPUT FUNCTIONS
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode);
- void do_movement();
- void makeSphere();
- // THE SIZE OF THE WINDOW
- const GLuint WIDTH = 800, HEIGHT = 600;
- // CAMERA
- glm::vec3 cameraPos = glm::vec3(0.0f, 5.0f, 5.0f); // Position of the camera
- glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f); // Front vector of the camera
- glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f); // Up vector of the camera
- glm::vec3 modelpos = glm::vec3(0.0f, 0.0f, 0.0f); // Position of the model
- GLfloat pitch = 0.0f;
- GLfloat yaw = 90.0f;
- GLfloat fov = 45.0f;
- bool keys[1024];
- //GLfloat sphere[144]; // VERTICES FOR A SPHERE
- //GLuint indices[156];
- vector <float> vertices;
- vector <int> indices;
- // TIME VALUES
- GLfloat deltaTime = 0.0f; // Time between current frame and last frame
- GLfloat lastFrame = 0.0f; // Time of last frame since start
- int main()
- {
- // Initializing GLFW window:
- glfwInit();
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
- GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Best Pool Game EVER", nullptr, nullptr);
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
- // TELLING GLEW TO BE MODERN
- glewExperimental = GL_TRUE;
- glewInit();
- // MATCHING THE VIEWPORT SIZE TO THE WINDOW
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
- //OPENGL SETTINGS
- glEnable(GL_DEPTH_TEST);
- // Build and compile our shader program
- Shader ourShader("shader.vs", "shader.frag");
- // SETTING VERTICES AND INDICES
- //GLfloat verticesGone[] = {
- // -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
- // 0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
- // 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- // 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- // -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
- // -0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
- // -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- // 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- // 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
- // 0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
- // -0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
- // -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- // -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- // -0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- // -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- // -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- // -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- // -0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- // 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- // 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- // 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- // 0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- // 0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- // 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- // -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- // 0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
- // 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- // 0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
- // -0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
- // -0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
- // -0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
- // 0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
- // 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- // 0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
- // -0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
- // -0.5f, 0.5f, -0.5f, 0.0f, 1.0f
- //};
- //GLfloat texCoords[] = {
- // 0.0f, 0.0f, // Lower-left corner
- // 1.0f, 0.0f, // Lower-right corner
- // 0.5f, 1.0f // Top-center corner
- //};
- // SETTING VAO AND BINDING VBO, EBO TO IT
- GLuint VAO, ballCordVBO, ballTextVBO, EBO ;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &ballCordVBO);
- glGenBuffers(1, &ballTextVBO);
- glGenBuffers(1, &EBO);
- glBindVertexArray(VAO);
- makeSphere();
- //NEXT 4 LINES - defining VBO
- glBindBuffer(GL_ARRAY_BUFFER, ballCordVBO);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, ballTextVBO);
- glBufferData(GL_ARRAY_BUFFER, vertices .size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], GL_STATIC_DRAW);
- glBindVertexArray(0);
- // TEXTURE LOADING
- GLuint texture;
- glGenTextures(1, &texture);
- glBindTexture(GL_TEXTURE_2D, texture);
- int text_w, text_h;
- unsigned char* image = SOIL_load_image("13ball.jpg", &text_w, &text_h, 0, SOIL_LOAD_RGB);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, text_w, text_h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- glGenerateMipmap(GL_TEXTURE_2D);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0);
- // MAIN GAME LOOP
- while (!glfwWindowShouldClose(window)) // check if we should close the window
- {
- // Calculate deltatime of current frame
- GLfloat currentFrame = glfwGetTime();
- deltaTime = currentFrame - lastFrame;
- lastFrame = currentFrame;
- glfwPollEvents();
- do_movement();
- glClearColor(0.2f, 0.2f, 0.2f, 0.0f); // Clear the screen after rendering
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the Color Buffer
- glBindTexture(GL_TEXTURE_2D, texture);
- ourShader.Use();
- //CAMERA DECLARATION
- cameraFront = glm::normalize(modelpos - cameraPos); // Front vector of the camera
- glm::mat4 projection;
- projection = glm::perspective(glm::radians(fov), (GLfloat)width / (GLfloat)height, 0.1f, 100.0f);
- //PASSING TO SHADERS
- GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
- GLint viewLoc = glGetUniformLocation(ourShader.Program, "view");
- GLint projLoc = glGetUniformLocation(ourShader.Program, "projection");
- glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
- glm::mat4 view;
- view = glm::lookAt(cameraPos, modelpos, cameraUp);
- glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
- // DRAWING
- glBindVertexArray(VAO);
- glm::mat4 model;
- glm::vec3 forward(0, 0, -1);
- /*modelpos = modelpos + forward;
- cout << modelpos[2];
- model = glm::translate(model, modelpos);
- */glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- //glDrawArrays(GL_TRIANGLES, 0, 36);
- glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- glfwSwapBuffers(window); // call second buffer
- }
- // END OF GAME LOOP
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &ballCordVBO);
- glDeleteBuffers(1, &EBO);
- glfwTerminate();
- return 0;
- }
- void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (key >= 0 && key < 1024)
- {
- if (action == GLFW_PRESS)
- keys[key] = true;
- else if (action == GLFW_RELEASE)
- keys[key] = false;
- }
- }
- void do_movement()
- {
- glm::vec3 cameraRight = glm::normalize(glm::cross(cameraFront, cameraUp));
- GLfloat cameraSpeed = 0.005f;
- if (keys[GLFW_KEY_W])
- cameraPos += cameraFront * cameraSpeed;
- if (keys[GLFW_KEY_S])
- cameraPos -= cameraFront * cameraSpeed;
- if (keys[GLFW_KEY_A])
- cameraPos -= cameraRight * cameraSpeed;
- if (keys[GLFW_KEY_D])
- cameraPos += cameraRight * cameraSpeed;
- }
- void makeSphere() {
- int Stacks = 20;
- int Slices = 20;
- float Radius = 1;
- // Calc The Vertices
- for (int i = 0; i <= Stacks; ++i) {
- float V = i / (float)Stacks;
- float phi = V * glm::pi <float>();
- // Loop Through Slices
- for (int j = 0; j <= Slices; ++j) {
- float U = j / (float)Slices;
- float theta = U * (glm::pi <float>() * 2);
- // Calc The Vertex Positions
- float x = cosf(theta) * sinf(phi);
- float y = cosf(phi);
- float z = sinf(theta) * sinf(phi);
- // Push Back Vertex Data
- vertices.push_back(x * Radius);
- vertices.push_back(y * Radius);
- vertices.push_back(z * Radius);
- }
- }
- // Calc The Index Positions
- for (int i = 0; i < Slices * Stacks + Slices; ++i) {
- indices.push_back(i);
- indices.push_back(i + Slices + 1);
- indices.push_back(i + Slices);
- indices.push_back(i + Slices + 1);
- indices.push_back(i);
- indices.push_back(i + 1);
- }
- }
Add Comment
Please, Sign In to add comment