Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GL/freeglut.h>
- #include <stdio.h>
- #include <stack>
- #include <iostream>
- #include <glm/mat4x4.hpp>
- #include <glm/gtx/transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <glm/gtc/constants.hpp>
- #include <vector>
- #include <fstream>
- #include <string>
- #define PI glm::pi<float>()
- glm::mat4 projectionMatrix, viewMatrix, modelMatrix;
- std::vector<GLfloat> vertices;
- std::vector<GLuint> indices;
- GLuint vao_sphere;
- GLuint vboV_sphere; //VBO - varfurile ce formeaza poligoanele sferei
- GLuint vboI_sphere; //IBO – indicii varfurilor din VBO care formeaza poligoanele
- //varfurile triunghiului
- GLuint shader_programme, vao;
- void GenSphere(std::vector<GLfloat> &vertices, std::vector<GLuint> &indices, int slices = 40, int stacks = 40) {
- int i, j;
- int indicator = 0;
- for (i = 0; i <= stacks; i++) {
- double lat0 = glm::pi<double>() * (-0.5 + (double)(i - 1) / stacks);
- double z0 = sin(lat0);
- double zr0 = cos(lat0);
- double lat1 = glm::pi<double>() * (-0.5 + (double)i / stacks);
- double z1 = sin(lat1);
- double zr1 = cos(lat1);
- for (j = 0; j <= slices; j++) {
- double lng = 2 * glm::pi<double>() * (double)(j - 1) / slices;
- double x = cos(lng);
- double y = sin(lng);
- vertices.push_back(x * zr0);
- vertices.push_back(y * zr0);
- vertices.push_back(z0);
- indices.push_back(indicator);
- indicator++;
- vertices.push_back(x * zr1);
- vertices.push_back(y * zr1);
- vertices.push_back(z1);
- indices.push_back(indicator);
- indicator++;
- }
- indices.push_back(GL_PRIMITIVE_RESTART_FIXED_INDEX);
- }
- }
- void display()
- {
- glBindVertexArray(vao_sphere);
- glEnable(GL_PRIMITIVE_RESTART);
- glPrimitiveRestartIndex(GL_PRIMITIVE_RESTART_FIXED_INDEX);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboI_sphere);
- glDrawElements(GL_QUAD_STRIP, indices.size(), GL_UNSIGNED_INT, NULL);
- }
- void init()
- {
- glGenVertexArrays(1, &vao_sphere);
- glBindVertexArray(vao_sphere);
- GenSphere(vertices, indices);
- glGenBuffers(1, &vboV_sphere);
- glBindBuffer(GL_ARRAY_BUFFER, vboV_sphere);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0],GL_STATIC_DRAW);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- glEnableVertexAttribArray(0);
- glGenBuffers(1, &vboI_sphere);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboI_sphere);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0],GL_STATIC_DRAW);
- GLuint shader_programme;
- std::string vstext = textFileRead("vertex.vert");
- std::string fstext = textFileRead("fragment.frag");
- const char* vertex_shader = vstext.c_str();
- const char* fragment_shader = fstext.c_str();
- GLuint vs = glCreateShader(GL_VERTEX_SHADER);
- glShaderSource(vs, 1, &vertex_shader, NULL);
- glCompileShader(vs);
- GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderSource(fs, 1, &fragment_shader, NULL);
- glCompileShader(fs);
- shader_programme = glCreateProgram();
- glAttachShader(shader_programme, fs);
- glAttachShader(shader_programme, vs);
- glLinkProgram(shader_programme);
- }
- std::string textFileRead(char *fn) {
- std::ifstream ifile(fn);
- std::string filetext;
- while (ifile.good()) {
- std::string line;
- std::getline(ifile, line);
- filetext.append(line + "\n");
- }
- return filetext;
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
- glutInitWindowPosition(200, 200);
- glutInitWindowSize(512, 512);
- glutCreateWindow("SPG");
- init();
- glutDisplayFunc(display);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement