Advertisement
d10070dd

Chapter_3.2

Oct 19th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.81 KB | None | 0 0
  1. /*  Copyright (C) 2011 by Eddy Luten
  2.  
  3.     Permission is hereby granted, free of charge, to any person obtaining a copy
  4.     of this software and associated documentation files (the "Software"), to deal
  5.     in the Software without restriction, including without limitation the rights
  6.     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7.     copies of the Software, and to permit persons to whom the Software is
  8.     furnished to do so, subject to the following conditions:
  9.  
  10.     The above copyright notice and this permission notice shall be included in
  11.     all copies or substantial portions of the Software.
  12.  
  13.     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14.     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15.     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16.     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17.     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18.     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19.     THE SOFTWARE.
  20. */
  21.  
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25. #include <GL/glew.h>
  26. #include <GL/freeglut.h>
  27. #define WINDOW_TITLE_PREFIX "Chapter 3"
  28.  
  29. typedef struct
  30. {
  31.     float XYZW[4];
  32.     float RGBA[4];
  33. } Vertex;
  34.  
  35. int CurrentWidth = 600,
  36.     CurrentHeight = 600,
  37.     WindowHandle = 0;
  38.  
  39. unsigned FrameCount = 0;
  40.  
  41. GLuint
  42.     VertexShaderId,
  43.     FragmentShaderId,
  44.     ProgramId,
  45.     VaoId,
  46.     BufferId,
  47.     IndexBufferId[2],
  48.     ActiveIndexBuffer = 0;
  49.  
  50. const GLchar* VertexShader =
  51. {
  52.     "#version 400\n"\
  53.  
  54.     "layout(location=0) in vec4 in_Position;\n"\
  55.     "layout(location=1) in vec4 in_Color;\n"\
  56.     "out vec4 ex_Color;\n"\
  57.  
  58.     "void main(void)\n"\
  59.     "{\n"\
  60.     "   gl_Position = in_Position;\n"\
  61.     "   ex_Color = in_Color;\n"\
  62.     "}\n"
  63. };
  64.  
  65. const GLchar* FragmentShader =
  66. {
  67.     "#version 400\n"\
  68.  
  69.     "in vec4 ex_Color;\n"\
  70.     "out vec4 out_Color;\n"\
  71.  
  72.     "void main(void)\n"\
  73.     "{\n"\
  74.     "   out_Color = ex_Color;\n"\
  75.     "}\n"
  76. };
  77.  
  78. void Initialize(int, char*[]);
  79. void InitWindow(int, char*[]);
  80. void ResizeFunction(int, int);
  81. void RenderFunction(void);
  82. void TimerFunction(int);
  83. void IdleFunction(void);
  84. void KeyboardFunction(unsigned char, int, int);
  85. void Cleanup(void);
  86. void CreateVBO(void);
  87. void DestroyVBO(void);
  88. void CreateShaders(void);
  89. void DestroyShaders(void);
  90.  
  91. int main(int argc, char* argv[])
  92. {
  93.     Initialize(argc, argv);
  94.  
  95.     glutMainLoop();
  96.    
  97.     exit(EXIT_SUCCESS);
  98. }
  99.  
  100. void Initialize(int argc, char* argv[])
  101. {
  102.     GLenum GlewInitResult;
  103.  
  104.     InitWindow(argc, argv);
  105.  
  106.     glewExperimental = GL_TRUE;
  107.     GlewInitResult = glewInit();
  108.  
  109.     if (GLEW_OK != GlewInitResult) {
  110.         fprintf(
  111.             stderr,
  112.             "ERROR: %s\n",
  113.             glewGetErrorString(GlewInitResult)
  114.         );
  115.         exit(EXIT_FAILURE);
  116.     }
  117.    
  118.     fprintf(
  119.         stdout,
  120.         "INFO: OpenGL Version: %s\n",
  121.         glGetString(GL_VERSION)
  122.     );
  123.  
  124.     CreateShaders();
  125.     CreateVBO();
  126.  
  127.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  128. }
  129.  
  130. void InitWindow(int argc, char* argv[])
  131. {
  132.     glutInit(&argc, argv);
  133.    
  134.     glutInitContextVersion(4, 0);
  135.     glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
  136.     glutInitContextProfile(GLUT_CORE_PROFILE);
  137.  
  138.     glutSetOption(
  139.         GLUT_ACTION_ON_WINDOW_CLOSE,
  140.         GLUT_ACTION_GLUTMAINLOOP_RETURNS
  141.     );
  142.    
  143.     glutInitWindowSize(CurrentWidth, CurrentHeight);
  144.  
  145.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  146.  
  147.     WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
  148.  
  149.     if(WindowHandle < 1) {
  150.         fprintf(
  151.             stderr,
  152.             "ERROR: Could not create a new rendering window.\n"
  153.         );
  154.         exit(EXIT_FAILURE);
  155.     }
  156.    
  157.     glutReshapeFunc(ResizeFunction);
  158.     glutDisplayFunc(RenderFunction);
  159.     glutIdleFunc(IdleFunction);
  160.     glutTimerFunc(0, TimerFunction, 0);
  161.     glutCloseFunc(Cleanup);
  162.     glutKeyboardFunc(KeyboardFunction);
  163. }
  164.  
  165. void KeyboardFunction(unsigned char Key, int X, int Y)
  166. {
  167.     X; Y; // Resolves warning C4100: unreferenced formal parameter
  168.  
  169.     switch (Key)
  170.     {
  171.     case 'T':
  172.     case 't':
  173.         {
  174.             ActiveIndexBuffer = (ActiveIndexBuffer == 1 ? 0 : 1);
  175.             glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId[ActiveIndexBuffer]);
  176.             break;
  177.         }
  178.     default:
  179.         break;
  180.     }
  181. }
  182.  
  183. void ResizeFunction(int Width, int Height)
  184. {
  185.     CurrentWidth = Width;
  186.     CurrentHeight = Height;
  187.     glViewport(0, 0, CurrentWidth, CurrentHeight);
  188. }
  189.  
  190. void RenderFunction(void)
  191. {
  192.     ++FrameCount;
  193.  
  194.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  195.  
  196.     if (ActiveIndexBuffer == 0) {
  197.         glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, NULL);
  198.     } else {
  199.         glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, NULL);
  200.     }
  201.  
  202.     glutSwapBuffers();
  203.     glutPostRedisplay();
  204. }
  205.  
  206. void IdleFunction(void)
  207. {
  208.     glutPostRedisplay();
  209. }
  210.  
  211. void TimerFunction(int Value)
  212. {
  213.     if (0 != Value) {
  214.         char* TempString = (char*)
  215.             malloc(512 + strlen(WINDOW_TITLE_PREFIX));
  216.  
  217.         sprintf(
  218.             TempString,
  219.             "%s: %d Frames Per Second @ %d x %d",
  220.             WINDOW_TITLE_PREFIX,
  221.             FrameCount * 4,
  222.             CurrentWidth,
  223.             CurrentHeight
  224.         );
  225.  
  226.         glutSetWindowTitle(TempString);
  227.         free(TempString);
  228.     }
  229.    
  230.     FrameCount = 0;
  231.     glutTimerFunc(250, TimerFunction, 1);
  232. }
  233.  
  234. void Cleanup(void)
  235. {
  236.     DestroyShaders();
  237.     DestroyVBO();
  238. }
  239.  
  240. void CreateVBO(void)
  241. {
  242.     Vertex Vertices[] =
  243.     {
  244.         { { 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }, // 0
  245.  
  246.         // Top
  247.         { { -0.2f, 0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 1
  248.         { { 0.2f, 0.8f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 2
  249.         { { 0.0f, 0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //3
  250.         { { 0.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } }, // 4
  251.  
  252.         // Bottom
  253.         { { -0.2f, -0.8f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 5
  254.         { { 0.2f, -0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 6
  255.         { { 0.0f, -0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //7
  256.         { { 0.0f, -1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },    // 8
  257.  
  258.         // Left
  259.         { { -0.8f, -0.2f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 9
  260.         { { -0.8f, 0.2f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 10
  261.         { { -0.8f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //11
  262.         { { -1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },    // 12
  263.  
  264.         // Right
  265.         { { 0.8f, -0.2f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 13
  266.         { { 0.8f, 0.2f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 14
  267.         { { 0.8f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //15
  268.         { { 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } }  // 16
  269.     };
  270.  
  271.     GLubyte Indices[] = {
  272.         // Top
  273.         0, 1, 3,
  274.         0, 3, 2,
  275.         3, 1, 4,
  276.         3, 4, 2,
  277.  
  278.         // Bottom
  279.         0, 5, 7,
  280.         0, 7, 6,
  281.         7, 5, 8,
  282.         7, 8, 6,
  283.  
  284.         // Left
  285.         0, 9, 11,
  286.         0, 11, 10,
  287.         11, 9, 12,
  288.         11, 12, 10,
  289.  
  290.         // Right
  291.         0, 13, 15,
  292.         0, 15, 14,
  293.         15, 13, 16,
  294.         15, 16, 14
  295.     };
  296.  
  297.     GLubyte AlternateIndices[] = {
  298.         // Outer square border:
  299.         3, 4, 16,
  300.         3, 15, 16,
  301.         15, 16, 8,
  302.         15, 7, 8,
  303.         7, 8, 12,
  304.         7, 11, 12,
  305.         11, 12, 4,
  306.         11, 3, 4,
  307.  
  308.         // Inner square
  309.         0, 11, 3,
  310.         0, 3, 15,
  311.         0, 15, 7,
  312.         0, 7, 11
  313.     };
  314.  
  315.     GLenum ErrorCheckValue = glGetError();
  316.     const size_t BufferSize = sizeof(Vertices);
  317.     const size_t VertexSize = sizeof(Vertices[0]);
  318.     const size_t RgbOffset = sizeof(Vertices[0].XYZW);
  319.    
  320.     glGenVertexArrays(1, &VaoId);
  321.     glBindVertexArray(VaoId);
  322.    
  323.     glGenBuffers(1, &BufferId);
  324.     glBindBuffer(GL_ARRAY_BUFFER, BufferId);
  325.     glBufferData(GL_ARRAY_BUFFER, BufferSize, Vertices, GL_STATIC_DRAW);
  326.  
  327.     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, VertexSize, 0);
  328.     glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, VertexSize, (GLvoid*)RgbOffset);
  329.  
  330.     glEnableVertexAttribArray(0);
  331.     glEnableVertexAttribArray(1);
  332.  
  333.     glGenBuffers(2, IndexBufferId);
  334.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId[0]);
  335.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
  336.  
  337.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId[1]);
  338.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(AlternateIndices), AlternateIndices, GL_STATIC_DRAW);
  339.  
  340.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId[0]);
  341.  
  342.     ErrorCheckValue = glGetError();
  343.     if (ErrorCheckValue != GL_NO_ERROR)
  344.     {
  345.         fprintf(
  346.             stderr,
  347.             "ERROR: Could not create a VBO: %s \n",
  348.             gluErrorString(ErrorCheckValue)
  349.         );
  350.  
  351.         exit(-1);
  352.     }
  353. }
  354.  
  355. void DestroyVBO(void)
  356. {
  357.     GLenum ErrorCheckValue = glGetError();
  358.  
  359.     glDisableVertexAttribArray(1);
  360.     glDisableVertexAttribArray(0);
  361.    
  362.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  363.     glDeleteBuffers(1, &BufferId);
  364.  
  365.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  366.     glDeleteBuffers(2, IndexBufferId);
  367.  
  368.     glBindVertexArray(0);
  369.     glDeleteVertexArrays(1, &VaoId);
  370.  
  371.     ErrorCheckValue = glGetError();
  372.     if (ErrorCheckValue != GL_NO_ERROR)
  373.     {
  374.         fprintf(
  375.             stderr,
  376.             "ERROR: Could not destroy the VBO: %s \n",
  377.             gluErrorString(ErrorCheckValue)
  378.         );
  379.  
  380.         exit(-1);
  381.     }
  382. }
  383.  
  384. void CreateShaders(void)
  385. {
  386.     GLenum ErrorCheckValue = glGetError();
  387.    
  388.     VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
  389.     glShaderSource(VertexShaderId, 1, &VertexShader, NULL);
  390.     glCompileShader(VertexShaderId);
  391.  
  392.     FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
  393.     glShaderSource(FragmentShaderId, 1, &FragmentShader, NULL);
  394.     glCompileShader(FragmentShaderId);
  395.  
  396.     ProgramId = glCreateProgram();
  397.         glAttachShader(ProgramId, VertexShaderId);
  398.         glAttachShader(ProgramId, FragmentShaderId);
  399.     glLinkProgram(ProgramId);
  400.     glUseProgram(ProgramId);
  401.  
  402.     ErrorCheckValue = glGetError();
  403.     if (ErrorCheckValue != GL_NO_ERROR)
  404.     {
  405.         fprintf(
  406.             stderr,
  407.             "ERROR: Could not create the shaders: %s \n",
  408.             gluErrorString(ErrorCheckValue)
  409.         );
  410.  
  411.         exit(-1);
  412.     }
  413. }
  414.  
  415. void DestroyShaders(void)
  416. {
  417.     GLenum ErrorCheckValue = glGetError();
  418.  
  419.     glUseProgram(0);
  420.  
  421.     glDetachShader(ProgramId, VertexShaderId);
  422.     glDetachShader(ProgramId, FragmentShaderId);
  423.  
  424.     glDeleteShader(FragmentShaderId);
  425.     glDeleteShader(VertexShaderId);
  426.  
  427.     glDeleteProgram(ProgramId);
  428.  
  429.     ErrorCheckValue = glGetError();
  430.     if (ErrorCheckValue != GL_NO_ERROR)
  431.     {
  432.         fprintf(
  433.             stderr,
  434.             "ERROR: Could not destroy the shaders: %s \n",
  435.             gluErrorString(ErrorCheckValue)
  436.         );
  437.  
  438.         exit(-1);
  439.     }
  440. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement