Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <glew.h>
- #include <glfw3.h>
- #include <iostream>
- #include <vector>
- #include <math.h>
- #include "shaders.h"
- #define radius 1.0f
- #define zmin -1.0f
- #define zmax 1.0f
- #define theta_max 360.0f
- constexpr int WIDTH = 600; // szerokosc okna
- constexpr int HEIGHT = 600; // wysokosc okna
- //******************************************************************************************
- GLuint shaderProgram; // identyfikator programu cieniowania
- GLuint vertexLoc; // lokalizacja atrybutu wierzcholka - wspolrzedne wierzcholkow
- GLuint colorLoc; // lokalizacja zmiennej jednorodnej - kolor rysowania prymitywu
- GLuint vao; // identyfikatory VAO
- GLuint buffers[2]; // identyfikatory VBO
- GLint indicesNumber = 0; // liczba indeksow definiujacych obiekt
- float color[] = { 0.0f, 1.0f, 0.0f, 1.0f }; // kolor jakim rysowac siatke
- float lineWidth = 3.0f; // grubosc linii
- //******************************************************************************************
- void errorCallback( int error, const char* description );
- void keyCallback( GLFWwindow* window, int key, int scancode, int action, int mods );
- void onShutdown();
- void initGL();
- void setupShaders();
- void setupBuffers();
- void renderScene();
- void renderSphere(float zMin, float zMax, float rad, float tetaMax);
- int main(int argc, char* argv[])
- {
- atexit( onShutdown );
- GLFWwindow* window;
- glfwSetErrorCallback( errorCallback );
- if( !glfwInit() )
- exit( EXIT_FAILURE );
- glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
- glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
- glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
- window = glfwCreateWindow( WIDTH, HEIGHT, "OpenGL (Core Profile) - primitive restart", nullptr, nullptr );
- if( !window )
- {
- glfwTerminate();
- exit( EXIT_FAILURE );
- }
- glfwSetKeyCallback( window, keyCallback );
- glfwMakeContextCurrent( window );
- glewExperimental = GL_TRUE;
- GLenum err = glewInit();
- if( err != GLEW_OK )
- {
- std::cerr << "Blad: " << glewGetErrorString( err ) << std::endl;
- exit( 1 );
- }
- if( !GLEW_VERSION_3_3 )
- {
- std::cerr << "Brak obslugi OpenGL 3.3\n";
- exit( 2 );
- }
- glfwSwapInterval( 1 ); // v-sync on
- initGL();
- while( !glfwWindowShouldClose( window ) )
- {
- renderScene();
- glfwSwapBuffers( window );
- glfwPollEvents();
- }
- glfwDestroyWindow( window );
- glfwTerminate();
- exit( EXIT_SUCCESS );
- return 0;
- }
- /*------------------------------------------------------------------------------------------
- ** funkcja zwrotna do obslugi bledow biblioteki GLFW
- ** error - kod bledu
- ** description - opis bledu
- **------------------------------------------------------------------------------------------*/
- void errorCallback( int error, const char* description )
- {
- std::cerr << "Error: " << description << std::endl;
- }
- /*------------------------------------------------------------------------------------------
- ** funkcja zwrotna do obslugi klawiatury
- ** window - okno, które otrzymalo zdarzenie
- ** key - klawisz jaki zostal nacisniety lub zwolniony
- ** scancode - scancode klawisza specyficzny dla systemu
- ** action - zachowanie klawisza (GLFW_PRESS, GLFW_RELEASE or GLFW_REPEAT)
- ** mods - pole bitowe zawierajace informacje o nacisnietych modyfikatorach (GLFW_MOD_SHIFT, GLFW_MOD_CONTROL, GLFW_MOD_ALT, GLFW_MOD_SUPER)
- **------------------------------------------------------------------------------------------*/
- void keyCallback( GLFWwindow* window, int key, int scancode, int action, int mods )
- {
- if( key == GLFW_KEY_ESCAPE && action == GLFW_PRESS )
- glfwSetWindowShouldClose( window, GLFW_TRUE );
- }
- /*------------------------------------------------------------------------------------------
- ** funkcja wykonywana przed zamknieciem programu
- **------------------------------------------------------------------------------------------*/
- void onShutdown()
- {
- glDeleteBuffers( 2, buffers );
- glDeleteVertexArrays( 1, &vao );
- glDeleteProgram( shaderProgram );
- }
- /*------------------------------------------------------------------------------------------
- ** funkcja inicjujaca ustawienia OpenGL
- **------------------------------------------------------------------------------------------*/
- void initGL()
- {
- std::cout << "GLEW = " << glewGetString( GLEW_VERSION ) << std::endl;
- std::cout << "GL_VENDOR = " << glGetString( GL_VENDOR ) << std::endl;
- std::cout << "GL_RENDERER = " << glGetString( GL_RENDERER ) << std::endl;
- std::cout << "GL_VERSION = " << glGetString( GL_VERSION ) << std::endl;
- std::cout << "GLSL = " << glGetString( GL_SHADING_LANGUAGE_VERSION ) << std::endl;
- glClearColor( 0.0f, 0.0f, 0.0f, 1.0f );
- setupShaders();
- setupBuffers();
- }
- /*------------------------------------------------------------------------------------------
- ** funkcja tworzaca program cieniowania skladajacy sie z shadera wierzcholkow i fragmentow
- **------------------------------------------------------------------------------------------*/
- void setupShaders()
- {
- if( !setupShaders( "shaders/vertex.vert", "shaders/fragment.frag", shaderProgram ) )
- exit( 3 );
- vertexLoc = glGetAttribLocation( shaderProgram, "vPosition" );
- colorLoc = glGetUniformLocation( shaderProgram, "color" );
- }
- /*------------------------------------------------------------------------------------------
- ** funkcja inicjujaca VAO oraz zawarte w nim VBO z danymi o modelu
- **------------------------------------------------------------------------------------------*/
- void setupBuffers()
- {
- renderSphere(radius, zmin, zmax, theta_max);
- }
- void renderSphere(float zMin, float zMax, float rad, float thetaMax)
- {
- glGenVertexArrays(1, &vao);
- glGenBuffers(2, buffers);
- glBindVertexArray(vao); //powiazanie vao, reprezentuje sfere
- const float PI = 3.14159265359;
- int horizontalVertices = 16;
- int verticalVertices = 8;
- float phi_min = -90.0f, phi_max = 90.0f;
- /*if (zMin > (-rad))
- phi_min = asin(zMin / rad);
- if (zMax < rad)
- phi_max = asin(zMax / rad);*/
- float phi, theta;
- float x, y, z, u, v;
- std::vector<GLfloat> vertices;
- std::vector<GLuint> indices;
- for (int i = 0; i < verticalVertices; i++)
- {
- for (int j = 0; j < horizontalVertices; j++)
- {
- u = (float)j / (horizontalVertices - 1);
- v = (float) i / (verticalVertices - 1);
- phi = (phi_min + v * (phi_max - phi_min)) * PI / 180;
- theta = (u * thetaMax) * PI / 180;
- x = rad * cos(theta) * cos(phi);
- y = rad * sin(theta) * cos(phi);
- z = rad * sin(phi);
- vertices.push_back(x);
- vertices.push_back(y);
- vertices.push_back(z);
- if (i != (verticalVertices - 1) && j != (horizontalVertices - 1)) //rysowanie
- { //bez uwzglednienia ostatniego wierzcholka, bez sensu rysowac go dwa razy
- indices.push_back(i * horizontalVertices + horizontalVertices + j);
- indices.push_back(i * horizontalVertices + j + 1);
- indices.push_back(i * horizontalVertices + j);
- //wrzucenie kolejnych wierzcholkow do wektora z trojkatami w odpowiedniej
- //kolejnosci zeby je dobrze polaczyc
- indices.push_back(i * horizontalVertices + j + 1);
- indices.push_back(i * horizontalVertices + horizontalVertices + j);
- indices.push_back(i * horizontalVertices + horizontalVertices + j + 1);
- }
- }
- }
- indicesNumber = 3 * indices.size();
- //wrzucenie wierzcholkow do pierwszego bufora przy vao
- glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(GLfloat), &vertices[0], GL_STATIC_DRAW);
- glEnableVertexAttribArray(vertexLoc);
- glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, GL_FALSE, 0, 0);
- //wrzucenie trojkatow do drugiego bufora przy vao
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
- glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLuint), &indices[0], GL_STATIC_DRAW);
- glBindVertexArray(0);
- }
- /*------------------------------------------------------------------------------------------
- ** funkcja rysujaca scene
- **------------------------------------------------------------------------------------------*/
- void renderScene()
- {
- glClear( GL_COLOR_BUFFER_BIT );
- glUseProgram( shaderProgram );
- glPolygonMode( GL_FRONT_AND_BACK, GL_LINE);
- glLineWidth( lineWidth );
- glBindVertexArray( vao );
- glUniform4fv( colorLoc, 1, color );
- glDrawElements( GL_TRIANGLES, indicesNumber, GL_UNSIGNED_INT, 0 );
- glBindVertexArray( 0 );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement