Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <stdio.h>
- #include <GL/glew.h>
- #include <GL/freeglut.h>
- #include <glm/mat4x4.hpp>
- #include <glm/gtx/transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- #include <glm/gtc/constants.hpp>
- #define PI glm::pi<float>()
- GLuint shader_programme, vao;
- glm::mat4 projectionMatrix, viewMatrix;
- std::vector<float> points;
- 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;
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glUseProgram(shader_programme);
- glm::mat4 modelMatrix;
- glBindVertexArray(vao);
- GLuint matrixID = glGetUniformLocation(shader_programme, "modelViewProjectionMatrix");
- glUniformMatrix4fv(matrixID, 1, GL_FALSE, glm::value_ptr(projectionMatrix * viewMatrix * modelMatrix));
- glDrawArrays(GL_LINE_STRIP, 0, points.size() / 3);
- glFlush();
- }
- void createPointsVector()
- {
- /*
- //primul punct
- points.push_back(0.0f);
- points.push_back(0.0f);
- points.push_back(0.0f);
- //al doilea punct
- points.push_back(1.0f);
- points.push_back(0.0f);
- points.push_back(0.0f);
- //al treilea punct
- points.push_back(2.0f);
- points.push_back(1.0f);
- points.push_back(0.0f);
- */
- //conditiile geometrice ale curbei
- float Geometry[4][3] = {
- { 10,10,0 }, // P0
- { -10,5,-2 }, // P1
- { 5,-5,0 }, // Tangenta in P0
- { 5,10,0 } // Tangenta in P1
- };
- unsigned int LOD = 20;
- for (int i = 0;i<LOD;i++) {
- float u = (float)i / (LOD - 1);
- double u2 = u*u;
- double u3 = u2*u;
- // calcularea functiilor de amestec
- float f0 = 2*u3-3*u2+1;
- float f1 = -2*u3+3*u2;
- float f2 = u3-2*u2+u;
- float f3 = u3-u2;
- // calcularea coordonatelor x,y,z ale punctului curent de pe curba
- float x = f0 * Geometry[0][0] + f1 * Geometry[1][0] + f2 * Geometry[2][0] + f3 * Geometry[3][0];
- float y = f0 * Geometry[0][1] + f1 * Geometry[1][1] + f2 * Geometry[2][1]+ f3 * Geometry[3][1];
- float z = f0 * Geometry[0][2] + f1 * Geometry[1][2] + f2 * Geometry[2][2]+ f3 * Geometry[3][2];
- // specificarea punctului pe pozitia i in VBO
- points.push_back(x);
- points.push_back(y);
- points.push_back(z);
- }
- }
- void init()
- {
- // get version info
- const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
- const GLubyte* version = glGetString(GL_VERSION); // version as a string
- printf("Renderer: %s\n", renderer);
- printf("OpenGL version supported %s\n", version);
- glClearColor(1, 1, 1, 0);
- glewInit();
- GLuint vbo = 1;
- glGenBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glBufferData(GL_ARRAY_BUFFER, points.size() * sizeof(float), &points[0], GL_STATIC_DRAW);
- vao = 0;
- glGenVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
- 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);
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- projectionMatrix = glm::perspective(PI / 3, (float)w / h, 0.1f, 100.0f);
- /*
- viewMatrix este matricea transformarii de observare. Parametrii functiei
- lookAt sunt trei vectori ce reprezinta, in ordine:
- - pozitia observatorului
- - punctul catre care priveste observatorul
- - directia dupa care este orientat observatorul
- */
- viewMatrix = glm::lookAt(glm::vec3(0, 0, 20), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
- glutInitWindowPosition(200, 200);
- glutInitWindowSize(700, 700);
- glutCreateWindow("SPG");
- createPointsVector();
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement