Advertisement
Guest User

Teapot

a guest
Oct 17th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.40 KB | None | 0 0
  1. /*
  2. Had to change the params in antons math class because near and far are already defined somewhere else..i think. Also to get rid of that stupid ambiguous variable error I got rid of the "using namespace std".
  3. Will just use "std::" if i need anything.
  4.  
  5. */
  6.  
  7.  
  8. #define _CRT_SECURE_NO_DEPRECATE
  9. //Some Windows Headers (For Time, IO, etc.)
  10. #include <windows.h>
  11. #include <mmsystem.h>
  12.  
  13. #include <GL/glew.h>
  14. #include <GL/freeglut.h>
  15. #include <iostream>
  16.  
  17. #include "maths_funcs.h" //Anton's math class
  18. #include "teapot.h" // teapot mesh
  19.  
  20. // Macro for indexing vertex buffer
  21. #define BUFFER_OFFSET(i) ((char *)NULL + (i))
  22.  
  23. //using namespace std;
  24. GLuint shaderProgramID;
  25. GLfloat rotateSpeed = 0.0f, move = -40.0f;
  26. unsigned int teapot_vao = 0;
  27. int width = 800.0;
  28. int height = 600.0;
  29.  
  30. // Shader Functions- click on + to expand
  31. #pragma region SHADER_FUNCTIONS
  32.  
  33. // Create a NULL-terminated string by reading the provided file
  34. char* readShaderSource(const char* shaderFile) {  
  35.     FILE* fp = fopen(shaderFile, "rb"); //!->Why does binary flag "RB" work and not "R"... wierd msvc thing?
  36.  
  37.     if ( fp == NULL ) { return NULL; }
  38.  
  39.     fseek(fp, 0L, SEEK_END);
  40.     long size = ftell(fp);
  41.  
  42.     fseek(fp, 0L, SEEK_SET);
  43.     char* buf = new char[size + 1];
  44.     fread(buf, 1, size, fp);
  45.     buf[size] = '\0';
  46.  
  47.     fclose(fp);
  48.  
  49.     return buf;
  50. }
  51.  
  52.  
  53. static void AddShader(GLuint ShaderProgram, const char* pShaderText, GLenum ShaderType)
  54. {
  55.     // create a shader object
  56.     GLuint ShaderObj = glCreateShader(ShaderType);
  57.    
  58.     if (ShaderObj == 0) {
  59.         fprintf(stderr, "Error creating shader type %d\n", ShaderType);
  60.         exit(0);
  61.     }
  62.  
  63.     /**
  64.     pShaderText is the problem....
  65.    
  66.     **/
  67.  
  68.     const char* pShaderSource = readShaderSource(pShaderText);
  69.    
  70.     // Bind the source code to the shader, this happens before compilation
  71.     glShaderSource(ShaderObj, 1, (const GLchar**)&pShaderSource, NULL);
  72.     // compile the shader and check for errors
  73.     glCompileShader(ShaderObj);
  74.     GLint success;
  75.     // check for shader related errors using glGetShaderiv
  76.     glGetShaderiv(ShaderObj, GL_COMPILE_STATUS, &success);
  77.     if (!success) {
  78.         GLchar InfoLog[1024];
  79.         glGetShaderInfoLog(ShaderObj, 1024, NULL, InfoLog);
  80.         fprintf(stderr, "Error compiling shader type %d: '%s'\n", ShaderType, InfoLog);
  81.         exit(1);
  82.     }
  83.     // Attach the compiled shader object to the program object
  84.     glAttachShader(ShaderProgram, ShaderObj);
  85. }
  86.  
  87. GLuint CompileShaders()
  88. {
  89.     //Start the process of setting up our shaders by creating a program ID
  90.     //Note: we will link all the shaders together into this ID
  91.     shaderProgramID = glCreateProgram();
  92.     if (shaderProgramID == 0) {
  93.         fprintf(stderr, "Error creating shader program\n");
  94.         exit(1);
  95.     }
  96.  
  97.     // Create two shader objects, one for the vertex, and one for the fragment shader
  98.     AddShader(shaderProgramID, "../Shaders/simpleVertexShader.txt", GL_VERTEX_SHADER);
  99.     AddShader(shaderProgramID, "../Shaders/simpleFragmentShader.txt", GL_FRAGMENT_SHADER);
  100.  
  101.     GLint Success = 0;
  102.     GLchar ErrorLog[1024] = { 0 };
  103.  
  104.  
  105.     // After compiling all shader objects and attaching them to the program, we can finally link it
  106.     glLinkProgram(shaderProgramID);
  107.     // check for program related errors using glGetProgramiv
  108.     glGetProgramiv(shaderProgramID, GL_LINK_STATUS, &Success);
  109.     if (Success == 0) {
  110.         glGetProgramInfoLog(shaderProgramID, sizeof(ErrorLog), NULL, ErrorLog);
  111.         fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
  112.         exit(1);
  113.     }
  114.  
  115.     // program has been successfully linked but needs to be validated to check whether the program can execute given the current pipeline state
  116.     glValidateProgram(shaderProgramID);
  117.     // check for program related errors using glGetProgramiv
  118.     glGetProgramiv(shaderProgramID, GL_VALIDATE_STATUS, &Success);
  119.     if (!Success) {
  120.         glGetProgramInfoLog(shaderProgramID, sizeof(ErrorLog), NULL, ErrorLog);
  121.         fprintf(stderr, "Invalid shader program: '%s'\n", ErrorLog);
  122.         exit(1);
  123.     }
  124.     // Finally, use the linked shader program
  125.     // Note: this program will stay in effect for all draw calls until you replace it with another or explicitly disable its use
  126.     glUseProgram(shaderProgramID);
  127.  
  128.     return shaderProgramID;
  129. }
  130. #pragma endregion SHADER_FUNCTIONS
  131.  
  132. // VBO Functions - click on + to expand
  133. #pragma region VBO_FUNCTIONS
  134.  
  135. void generateObjectBufferTeapot () {
  136.     GLuint vp_vbo = 0;
  137.     glGenBuffers (1, &vp_vbo);
  138.     glBindBuffer (GL_ARRAY_BUFFER, vp_vbo);
  139.     glBufferData (GL_ARRAY_BUFFER, 3 * teapot_vertex_count * sizeof (float), teapot_vertex_points, GL_STATIC_DRAW);
  140.     GLuint vn_vbo = 0;
  141.     glGenBuffers (1, &vn_vbo);
  142.     glBindBuffer (GL_ARRAY_BUFFER, vn_vbo);
  143.     glBufferData (GL_ARRAY_BUFFER, 3 * teapot_vertex_count * sizeof (float), teapot_normals, GL_STATIC_DRAW);
  144.  
  145.     glGenVertexArrays (1, &teapot_vao);
  146.     glBindVertexArray (teapot_vao);
  147.     glEnableVertexAttribArray (0);
  148.     glBindBuffer (GL_ARRAY_BUFFER, vp_vbo);
  149.     glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  150.     glEnableVertexAttribArray (1);
  151.     glBindBuffer (GL_ARRAY_BUFFER, vn_vbo);
  152.     glVertexAttribPointer (1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
  153. }
  154.  
  155. void linkCurrentBuffertoShader(GLuint shaderProgramID){
  156.  
  157.     glBindAttribLocation (shaderProgramID, 0, "vertex_position");
  158.     glBindAttribLocation (shaderProgramID, 1, "vertex_normals");
  159.    
  160. }
  161. #pragma endregion VBO_FUNCTIONS
  162.  
  163.  
  164. void display(){
  165.  
  166.     // tell GL to only draw onto a pixel if the shape is closer to the viewer
  167.     glEnable (GL_DEPTH_TEST); // enable depth-testing
  168.     glDepthFunc (GL_LESS); // depth-testing interprets a smaller value as "closer"
  169.     glClearColor (0.5f, 0.5f, 0.5f, 1.0f);
  170.    
  171.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  172.     glUseProgram (shaderProgramID);
  173.  
  174.     //Declare your uniform variables that will be used in your shader
  175.     int matrix_location = glGetUniformLocation (shaderProgramID, "model");
  176.     int view_mat_location = glGetUniformLocation (shaderProgramID, "view");
  177.     int proj_mat_location = glGetUniformLocation (shaderProgramID, "proj");
  178.    
  179.    
  180.     rotateSpeed = rotateSpeed + 0.4f;
  181.    
  182.    
  183.     if (move > 40.0f){
  184.         move = -40.0f;
  185.     }
  186.     move = move + 0.4f;
  187.  
  188.     //Here is where the code for the viewport lab will go, to get you started I have drawn a t-pot in the bottom left
  189.     //The model transform rotates the object by 45 degrees, the view transform sets the camera at -40 on the z-axis, and the perspective projection is setup using Antons method
  190. //******************************************************************************************************************************************************************************
  191.     // bottom-left
  192.     mat4 view = translate (identity_mat4 (), vec3 (0.0, 0.0, -40.0));
  193.     mat4 persp_proj = perspective(50.0, (float)width/(float)height, 0.1, 100.0);
  194.     mat4 model = rotate_x_deg (identity_mat4 (), 40);
  195.    
  196.     glViewport (0, 0, width / 2, height / 2);
  197.     GLfloat aspect = (width/2) / (height/2);
  198.  
  199.     glOrtho(-aspect,aspect,-1,1,-1,1000);
  200.  
  201.     glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, persp_proj.m);
  202.     glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view.m);
  203.     glUniformMatrix4fv (matrix_location, 1, GL_FALSE, model.m);
  204.     glDrawArrays (GL_TRIANGLES, 0, teapot_vertex_count);
  205.  
  206.  
  207.    
  208. //******************************************************************************************************************************************************************************
  209.     // bottom-right
  210.    
  211.     mat4 view1 = translate (identity_mat4 (), vec3 (move, 0.0, -40.0));
  212.     mat4 persp_proj1 = perspective(50.0, (float)width/(float)height, 0.1, 100);//   mat4 persp_proj1 = perspective(50.0, (float)width/(float)height, 0.1, move); this will allow the object to fade in and out.
  213.     mat4 model1 = rotate_x_deg (identity_mat4 (), rotateSpeed);
  214.     model1 = model1 * rotate_y_deg(model1, rotateSpeed*10);
  215.     model1 = model1 * rotate_x_deg(model1, rotateSpeed);
  216.    
  217.  
  218.     glViewport (400, 0, width / 2, height / 2);
  219.     glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, persp_proj1.m);
  220.     glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view1.m);
  221.     glUniformMatrix4fv (matrix_location, 1, GL_FALSE, model1.m);
  222.     glDrawArrays (GL_TRIANGLES, 0, teapot_vertex_count);
  223.  
  224.  
  225. //******************************************************************************************************************************************************************************
  226.     // top-left
  227.  
  228.    
  229.    
  230.     mat4 view2 = translate (identity_mat4 (), vec3 (0.0, 0.0, -40.0));
  231.     mat4 persp_proj2 = perspective(50.0, (float)width/(float)height, 0.1, 100.0);
  232.     mat4 model2 = rotate_x_deg (identity_mat4 (), 0);
  233.     //model2 = model2 * rotate_x_deg(model, );
  234.  
  235.     glViewport (0, 300, width / 2, height / 2);
  236.     glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, persp_proj2.m);
  237.     glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view2.m);
  238.     glUniformMatrix4fv (matrix_location, 1, GL_FALSE, model2.m);
  239.     glDrawArrays (GL_TRIANGLES, 0, teapot_vertex_count);
  240.  
  241. //******************************************************************************************************************************************************************************
  242.     // top-right
  243.  
  244.     mat4 view3 = translate (identity_mat4 (), vec3 (0.0, 0.0, -40.0));
  245.     mat4 persp_proj3 = perspective(50.0, (float)width/(float)height, 0.1, 100.0);
  246.     mat4 model3 = rotate_y_deg (identity_mat4 (), 90);
  247.     //model = model * rotate_x_deg(model, );
  248.  
  249.    
  250.  
  251.     glViewport (400, 300, width / 2, height / 2);
  252.     glUniformMatrix4fv (proj_mat_location, 1, GL_FALSE, persp_proj3.m);
  253.     glUniformMatrix4fv (view_mat_location, 1, GL_FALSE, view3.m);
  254.     glUniformMatrix4fv (matrix_location, 1, GL_FALSE, model3.m);
  255.     glDrawArrays (GL_TRIANGLES, 0, teapot_vertex_count);
  256.  
  257.     glutSwapBuffers();
  258. }
  259.  
  260.  
  261. void updateScene() {   
  262.  
  263.         // Wait until at least 16ms passed since start of last frame (Effectively caps framerate at ~60fps)
  264.     static DWORD  last_time = 0;
  265.     DWORD  curr_time = timeGetTime();
  266.     float  delta = (curr_time - last_time) * 0.001f;
  267.     if (delta > 0.03f)
  268.         delta = 0.03f;
  269.     last_time = curr_time;
  270.  
  271.     // Draw the next frame
  272.     glutPostRedisplay();
  273. }
  274.  
  275.  
  276. void init()
  277. {
  278.     // Create 3 vertices that make up a triangle that fits on the viewport
  279.     GLfloat vertices[] = {-1.0f, -1.0f, 0.0f, 1.0,
  280.             1.0f, -1.0f, 0.0f, 1.0,
  281.             0.0f, 1.0f, 0.0f, 1.0};
  282.     // Create a color array that identfies the colors of each vertex (format R, G, B, A)
  283.     GLfloat colors[] = {0.0f, 1.0f, 0.0f, 1.0f,
  284.             1.0f, 0.0f, 0.0f, 1.0f,
  285.             0.0f, 0.0f, 1.0f, 1.0f};
  286.     // Set up the shaders
  287.     GLuint shaderProgramID = CompileShaders();
  288.     // load teapot mesh into a vertex buffer array
  289.     generateObjectBufferTeapot ();
  290.     // Link the current buffer to the shader
  291.     linkCurrentBuffertoShader(shaderProgramID);
  292. }
  293.  
  294.  
  295. void keyPressed(unsigned char key, int x, int y){
  296.      
  297.     switch(key){
  298.     case 27:
  299.         exit(0);
  300.         break;
  301.     default:
  302.         break;
  303.     }
  304.  
  305.  
  306.  
  307. }
  308.  
  309.  
  310. int main(int argc, char** argv){
  311.  
  312.     // Set up the window
  313.     glutInit(&argc, argv);
  314.     glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
  315.     glutInitWindowSize(width, height);
  316.     glutCreateWindow("Viewport Teapots");
  317.     glutKeyboardFunc(keyPressed);
  318.     // Tell glut where the display function is
  319.    
  320.     glutDisplayFunc(display);
  321.    
  322.     glutIdleFunc(updateScene);
  323.  
  324.      // A call to glewInit() must be done after glut is initialized!
  325.     GLenum res = glewInit();
  326.     // Check for any errors
  327.     if (res != GLEW_OK) {
  328.       fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
  329.       return 1;
  330.     }
  331.     // Set up your objects and shaders
  332.     init();
  333.     // Begin infinite event loop
  334.     glutMainLoop();
  335.     return 0;
  336. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement