Advertisement
Auios

Untitled

Dec 5th, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.67 KB | None | 0 0
  1.  
  2. ///////////////////////////////////////////////////////////////////////
  3. //
  4. // triangles.cpp
  5. //
  6. ///////////////////////////////////////////////////////////////////////
  7.  
  8. using namespace std;
  9.  
  10. #include "vgl.h"
  11. #include "LoadShaders.h"
  12. #include "glm\glm.hpp"
  13. #include "glm\gtc\matrix_transform.hpp"
  14. #include "Angel.h"
  15. #include "mat.h"
  16. #include <vector>
  17.  
  18. using namespace std;
  19.  
  20.  
  21. GLuint program;
  22. const int NumVertices = 36;
  23. GLfloat theta = 0.0f;
  24. GLfloat speed = 0.0F;
  25. bool stop = false;
  26. typedef glm::vec4 point4;
  27. typedef glm::vec4 color4;
  28. GLfloat u, v = 0.0f;
  29. GLfloat w = 1.0f;
  30. point4 light_position = point4(u, v, w, 1.0);
  31. glm::vec3 normals[NumVertices];
  32. glm::vec3 rotation_axis = glm::vec3(0, 0, 1);
  33.  
  34. // Model-view and projection matrices uniform location
  35. GLuint  ModelView, Projection;
  36.  
  37. //----------------------------------------------------------------------------
  38.  
  39. bool loadOBJ(const char * path, std::vector<glm::vec3> & out_vertices, std::vector<glm::vec3> & out_normals);
  40.  
  41. //----------------------------------------------------------------------------
  42.  
  43. // OpenGL initialization
  44. void
  45. init()
  46. {
  47.     vector<glm::vec3> vertices;
  48.     vector<glm::vec2> uvs;
  49.     vector<glm::vec3> normals;
  50.  
  51.     loadOBJ("plane.txt", vertices, normals);
  52.  
  53.     ShaderInfo shaders[] = {
  54.         { GL_VERTEX_SHADER, "triangles.vert" },
  55.         { GL_FRAGMENT_SHADER, "triangles.frag" },
  56.         { GL_NONE, NULL }
  57.     };
  58.  
  59.     program = LoadShaders(shaders);
  60.     glUseProgram(program);
  61.  
  62.     // Create a vertex array object
  63.     GLuint vao;
  64.     glGenVertexArrays(1, &vao);
  65.     glBindVertexArray(vao);
  66.  
  67.     // Create and initialize a buffer object
  68.     GLuint buffer[2];
  69.     glGenBuffers(2, buffer);
  70.     glBindBuffer(GL_ARRAY_BUFFER, buffer[0]);
  71.     glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices, GL_STATIC_DRAW);
  72.     glBindAttribLocation(program, 0, "vPosition");
  73.     glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
  74.     glEnableVertexAttribArray(0);
  75.  
  76.     glBindBuffer(GL_ARRAY_BUFFER, buffer[1]);
  77.     glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(glm::vec3), &normals, GL_STATIC_DRAW);
  78.     glBindAttribLocation(program, 1, "vNormal");
  79.     glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
  80.     glEnableVertexAttribArray(1);
  81.  
  82.     // Initialize shader lighting parameters
  83.     point4 light_position(0.0, 0.0, 2.0, 0.0); //Position of diffuse light
  84.                                                //color4 light_ambient(0.2, 0.2, 0.2, 1.0);
  85.     color4 light_diffuse(1.0, 1.0, 0.0, 1.0); //Intensity or Lumination
  86.                                               //    color4 light_specular(1.0, 1.0, 1.0, 1.0);
  87.  
  88.                                               //    color4 material_ambient(1.0, 0.0, 1.0, 1.0);
  89.                                               //color4 material_diffuse(1.0, 0.8, 0.0, 1.0);
  90.     color4 material_diffuse(1.0, 1.0, 1.0, 1.0);
  91.     //  color4 material_specular(1.0, 0.0, 1.0, 1.0);
  92.     float  material_shininess = 5.0;
  93.  
  94.     //  color4 ambient_product = light_ambient * material_ambient;
  95.     color4 diffuse_product = light_diffuse * material_diffuse;
  96.     //  color4 specular_product = light_specular * material_specular;
  97.  
  98.     //  GLfloat tmp1[4] = {ambient_product.r, ambient_product.g, ambient_product.b, ambient_product.a };
  99.     GLfloat tmp2[4] = { diffuse_product.r, diffuse_product.g, diffuse_product.b, diffuse_product.a };
  100.     //  GLfloat tmp3[4] = {specular_product.r, specular_product.g, specular_product.b, specular_product.a};
  101.  
  102.     //  glUniform4fv(glGetUniformLocation(program, "AmbientProduct"),1, tmp1);
  103.     glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, tmp2);
  104.     //  glUniform4fv(glGetUniformLocation(program, "SpecularProduct"),1, tmp3);
  105.  
  106.     GLfloat tmp_p[4] = { light_position.x, light_position.y, light_position.z, light_position.w };
  107.  
  108.     glUniform4fv(glGetUniformLocation(program, "LightPosition"), 1, tmp_p);
  109.  
  110.     glUniform1f(glGetUniformLocation(program, "Shininess"), material_shininess);
  111.  
  112.     // Retrieve transformation uniform variable locations
  113.     ModelView = glGetUniformLocation(program, "ModelView");
  114.     Projection = glGetUniformLocation(program, "Projection");
  115.  
  116.     glEnable(GL_DEPTH_TEST);
  117.  
  118.     glClearColor(0.0, 0.0, 0.0, 1.0); /* white background */
  119.  
  120.  
  121. }
  122.  
  123. //----------------------------------------------------------------------------
  124.  
  125. void
  126. display(void)
  127. {
  128.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  129.  
  130.     glm::vec3 at(0.0, 0.0, 0.0);
  131.     glm::vec3 eye(0.0, 0.0, 0.5);
  132.     glm::vec3 up(0.0, 1.0, 0.0);
  133.  
  134.     glm::mat4 model_view = glm::lookAt(eye, at, up);
  135.  
  136.     model_view = glm::rotate(model_view, theta, rotation_axis);
  137.  
  138.     glUniformMatrix4fv(ModelView, 1, GL_TRUE, &model_view[0][0]);
  139.  
  140.     light_position = point4(u, v, w, 1.0);
  141.     GLfloat tmp_p[4] = { light_position.x, light_position.y, light_position.z, light_position.w };
  142.  
  143.     glUniform4fv(glGetUniformLocation(program, "LightPosition"), 1, tmp_p);
  144.  
  145.  
  146.  
  147.     glDrawArrays(GL_QUADS, 0, NumVertices);
  148.  
  149.  
  150.  
  151.     glutSwapBuffers();
  152.  
  153.  
  154. }
  155.  
  156. void idle()
  157. {
  158.     if (!stop)
  159.     {
  160.         theta += speed;
  161.     }
  162.     glutPostRedisplay();
  163. }
  164.  
  165. //----------------------------------------------------------------------------
  166.  
  167. void
  168. keyboard(unsigned char key, int x, int y)
  169. {
  170.     switch (key)
  171.     {
  172.  
  173.     case 'x':
  174.         rotation_axis = glm::vec3(1, 0, 0);
  175.         break;
  176.     case 'y':
  177.         rotation_axis = glm::vec3(0, 1, 0);
  178.         break;
  179.     case 'z':
  180.         rotation_axis = glm::vec3(0, 0, 1);
  181.         break;
  182.     case 's':
  183.         stop = !stop;
  184.         break;
  185.     case '=':
  186.         speed += 0.001;
  187.         break;
  188.     case '-':
  189.         speed -= 0.001;
  190.         if (speed <= 0) speed = 0.01;
  191.         break;
  192.     case 'f':
  193.         w += 0.05;
  194.         break;
  195.  
  196.     case 'n':
  197.         w -= 0.05;
  198.         break;
  199.  
  200.  
  201.     case 'a':
  202.         u += 0.05;
  203.         break;
  204.  
  205.     case 'd':
  206.         u -= 0.05;
  207.         break;
  208.  
  209.     case 'j':
  210.         v += 0.05;
  211.         break;
  212.  
  213.     case 'l':
  214.         v -= 0.05;
  215.         break;
  216.     }
  217.     glutPostRedisplay();
  218. }
  219.  
  220. //----------------------------------------------------------------------------
  221.  
  222. void
  223. reshape(int width, int height)
  224. {
  225.     glViewport(0, 0, width, height);
  226.  
  227.     GLfloat left = -2.0, right = 2.0;
  228.     GLfloat top = 2.0, bottom = -2.0;
  229.     GLfloat zNear = 0.001, zFar = 20.0;
  230.  
  231.     GLfloat aspect = GLfloat(width) / height;
  232.  
  233.     if (aspect > 1.0) {
  234.         left *= aspect;
  235.         right *= aspect;
  236.     }
  237.     else {
  238.         top /= aspect;
  239.         bottom /= aspect;
  240.     }
  241.  
  242.     glm::mat4 projection = glm::ortho(left, right, bottom, top, zNear, zFar);
  243.  
  244.     glUniformMatrix4fv(Projection, 1, GL_TRUE, &projection[0][0]);
  245. }
  246.  
  247. //----------------------------------------------------------------------------
  248.  
  249. int
  250. main(int argc, char **argv)
  251. {
  252.  
  253.     glutInit(&argc, argv);
  254.     glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH);
  255.     glutInitWindowSize(512, 512);
  256.     glutInitContextVersion(3, 2);
  257.     glutInitContextProfile(GLUT_CORE_PROFILE);
  258.     glutCreateWindow("Sphere");
  259.     glewExperimental = GL_TRUE;
  260.     glewInit();
  261.  
  262.     GLenum err = glGetError();
  263.     std::cout << err;
  264.  
  265.     init();
  266.  
  267.     glutDisplayFunc(display);
  268.     glutReshapeFunc(reshape);
  269.     glutKeyboardFunc(keyboard);
  270.     glutIdleFunc(idle);
  271.     glutMainLoop();
  272.     return 0;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement