Advertisement
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>
- #include <string>
- using namespace std;
- class Ball {
- public:
- glm::vec3 ballPos;
- vector <float> vertices;
- vector <int> indices;
- vector <float> UV;
- string texturePath;
- GLuint textures[16] = { 1, 2, 3,
- 4, 5, 6,
- 7, 8, 9,
- 10, 11, 12,
- 13, 14, 15, 100 };
- // TEXTURES
- string texturePaths[16] = { "1.jpg","2.jpg","3.jpg","4.jpg",
- "5.jpg","6.jpg","7.jpg","8.jpg",
- "9.jpg","10.jpg","11.jpg","12.jpg",
- "13.jpg","14.jpg","15.jpg","0White.jpg" };
- int textID;
- Ball(glm::vec3 position, int textID) {
- this->ballPos = position;
- this->texturePath = texturePath;
- this->textID = textID;
- }
- glm::vec3 getPos() {
- return ballPos;
- }
- void makeSphere() {
- int Stacks = 50;
- int Slices = 50;
- float Radius = 1;
- // CALCULATING VERTICES
- for (int i = 0; i <= Stacks; ++i) {
- float V = i / (float)Stacks;
- float phi = V * glm::pi <float>();
- for (int j = 0; j <= Slices; ++j) {
- float U = j / (float)Slices;
- float theta = U * (glm::pi <float>() * 2);
- float x = cosf(theta) * sinf(phi);
- float y = cosf(phi);
- float z = sinf(theta) * sinf(phi);
- vertices.push_back(x * Radius);
- vertices.push_back(y * Radius);
- vertices.push_back(z * Radius);
- }
- }
- // CALCULATING 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);
- }
- // CALCULATING UV COORDS
- // u = 0.5 + arctan2(dz,dx)/(2*pi)
- // v = 0.5 - arcsin(dy)/pi
- // where d is the unit vector from the point to the origin
- // 1. calculate unit vector at each point
- // 2. calculate u
- // 3. calculate v
- // 4. create uv vector
- for (int i = 0; i < vertices.size(); i++) {
- if (i % 3 == 0) {
- glm::vec3 d = glm::vec3(vertices[i] - ballPos[0], vertices[i + 1] - ballPos[1], vertices[i + 2] - ballPos[2]);
- d = glm::normalize(d);
- float u = 1 - (0.5 + (glm::atan(d[2], d[0]) / (glm::pi<float>() * 2)));
- float v = 0.5 - (glm::asin(d[1])) / glm::pi<float>();
- UV.push_back(u);
- UV.push_back(v);
- }
- }
- }
- void drawBall(Shader ourShader) {
- // Build and compile our shader program
- // Shader ourShader("shader.vs", "shader.frag");
- GLuint VAO, VBO, EBO, VBO2;
- glGenVertexArrays(1, &VAO);
- glGenBuffers(1, &VBO);
- glGenBuffers(1, &EBO);
- glGenBuffers(1, &VBO2);
- // BIND VAO
- glBindVertexArray(VAO);
- // CREATE THE SPHERE
- //makeSphere();
- // ASSIGNING VBO AND EBO, POINTERS
- glBindBuffer(GL_ARRAY_BUFFER, VBO);
- 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_ELEMENT_ARRAY_BUFFER, EBO);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], GL_STATIC_DRAW);
- glBindBuffer(GL_ARRAY_BUFFER, VBO2);
- glBufferData(GL_ARRAY_BUFFER, UV.size() * sizeof(GLfloat), &UV[0], GL_STATIC_DRAW);
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
- glEnableVertexAttribArray(2);
- // UNBIND VAO
- glBindVertexArray(0);
- glBindTexture(GL_TEXTURE_2D, textures[textID]);
- GLint modelLoc = glGetUniformLocation(ourShader.Program, "model");
- //ACTUAL DRAWING
- glBindVertexArray(VAO);
- glm::mat4 model;
- model = glm::translate(model, ballPos);
- glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
- glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
- glBindVertexArray(0);
- // GETTING RID OF EVERYTHING BEFORE NEXT GAME LOOP
- glDeleteVertexArrays(1, &VAO);
- glDeleteBuffers(1, &VBO);
- glDeleteBuffers(1, &EBO);
- glDeleteBuffers(1, &VBO2);
- }
- void loadTexture() {
- glGenTextures(1, &textures[textID]);
- glBindTexture(GL_TEXTURE_2D, textures[textID]);
- int text_w, text_h;
- unsigned char* image = SOIL_load_image(texturePaths[textID].c_str(), &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);
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement