Advertisement
d10070dd

Chapter_3.1

Oct 19th, 2013
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.79 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;
  48.  
  49. const GLchar* VertexShader =
  50. {
  51.     "#version 400\n"\
  52.  
  53.     "layout(location=0) in vec4 in_Position;\n"\
  54.     "layout(location=1) in vec4 in_Color;\n"\
  55.     "out vec4 ex_Color;\n"\
  56.  
  57.     "void main(void)\n"\
  58.     "{\n"\
  59.     "   gl_Position = in_Position;\n"\
  60.     "   ex_Color = in_Color;\n"\
  61.     "}\n"
  62. };
  63.  
  64. const GLchar* FragmentShader =
  65. {
  66.     "#version 400\n"\
  67.  
  68.     "in vec4 ex_Color;\n"\
  69.     "out vec4 out_Color;\n"\
  70.  
  71.     "void main(void)\n"\
  72.     "{\n"\
  73.     "   out_Color = ex_Color;\n"\
  74.     "}\n"
  75. };
  76.  
  77. void Initialize(int, char*[]);
  78. void InitWindow(int, char*[]);
  79. void ResizeFunction(int, int);
  80. void RenderFunction(void);
  81. void TimerFunction(int);
  82. void IdleFunction(void);
  83. void Cleanup(void);
  84. void CreateVBO(void);
  85. void DestroyVBO(void);
  86. void CreateShaders(void);
  87. void DestroyShaders(void);
  88.  
  89. int main(int argc, char* argv[])
  90. {
  91.     Initialize(argc, argv);
  92.  
  93.     glutMainLoop();
  94.    
  95.     exit(EXIT_SUCCESS);
  96. }
  97.  
  98. void Initialize(int argc, char* argv[])
  99. {
  100.     GLenum GlewInitResult;
  101.  
  102.     InitWindow(argc, argv);
  103.  
  104.     glewExperimental = GL_TRUE;
  105.     GlewInitResult = glewInit();
  106.  
  107.     if (GLEW_OK != GlewInitResult) {
  108.         fprintf(
  109.             stderr,
  110.             "ERROR: %s\n",
  111.             glewGetErrorString(GlewInitResult)
  112.         );
  113.         exit(EXIT_FAILURE);
  114.     }
  115.    
  116.     fprintf(
  117.         stdout,
  118.         "INFO: OpenGL Version: %s\n",
  119.         glGetString(GL_VERSION)
  120.     );
  121.  
  122.     CreateShaders();
  123.     CreateVBO();
  124.  
  125.     glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  126. }
  127.  
  128. void InitWindow(int argc, char* argv[])
  129. {
  130.     glutInit(&argc, argv);
  131.    
  132.     glutInitContextVersion(4, 0);
  133.     glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
  134.     glutInitContextProfile(GLUT_CORE_PROFILE);
  135.  
  136.     glutSetOption(
  137.         GLUT_ACTION_ON_WINDOW_CLOSE,
  138.         GLUT_ACTION_GLUTMAINLOOP_RETURNS
  139.     );
  140.    
  141.     glutInitWindowSize(CurrentWidth, CurrentHeight);
  142.  
  143.     glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  144.  
  145.     WindowHandle = glutCreateWindow(WINDOW_TITLE_PREFIX);
  146.  
  147.     if(WindowHandle < 1) {
  148.         fprintf(
  149.             stderr,
  150.             "ERROR: Could not create a new rendering window.\n"
  151.         );
  152.         exit(EXIT_FAILURE);
  153.     }
  154.    
  155.     glutReshapeFunc(ResizeFunction);
  156.     glutDisplayFunc(RenderFunction);
  157.     glutIdleFunc(IdleFunction);
  158.     glutTimerFunc(0, TimerFunction, 0);
  159.     glutCloseFunc(Cleanup);
  160. }
  161.  
  162. void ResizeFunction(int Width, int Height)
  163. {
  164.     CurrentWidth = Width;
  165.     CurrentHeight = Height;
  166.     glViewport(0, 0, CurrentWidth, CurrentHeight);
  167. }
  168.  
  169. void RenderFunction(void)
  170. {
  171.     ++FrameCount;
  172.  
  173.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  174.    
  175.     glDrawElements(GL_TRIANGLES, 48, GL_UNSIGNED_BYTE, NULL);
  176.  
  177.     glutSwapBuffers();
  178.     glutPostRedisplay();
  179. }
  180.  
  181. void IdleFunction(void)
  182. {
  183.     glutPostRedisplay();
  184. }
  185.  
  186. void TimerFunction(int Value)
  187. {
  188.     if (0 != Value) {
  189.         char* TempString = (char*)
  190.             malloc(512 + strlen(WINDOW_TITLE_PREFIX));
  191.  
  192.         sprintf(
  193.             TempString,
  194.             "%s: %d Frames Per Second @ %d x %d",
  195.             WINDOW_TITLE_PREFIX,
  196.             FrameCount * 4,
  197.             CurrentWidth,
  198.             CurrentHeight
  199.         );
  200.  
  201.         glutSetWindowTitle(TempString);
  202.         free(TempString);
  203.     }
  204.    
  205.     FrameCount = 0;
  206.     glutTimerFunc(250, TimerFunction, 1);
  207. }
  208.  
  209. void Cleanup(void)
  210. {
  211.     DestroyShaders();
  212.     DestroyVBO();
  213. }
  214.  
  215. void CreateVBO(void)
  216. {
  217.     Vertex Vertices[] =
  218.     {
  219.         { { 0.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } }, // 0
  220.  
  221.         // Top
  222.         { { -0.2f, 0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 1
  223.         { { 0.2f, 0.8f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 2
  224.         { { 0.0f, 0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //3
  225.         { { 0.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } }, // 4
  226.  
  227.         // Bottom
  228.         { { -0.2f, -0.8f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 5
  229.         { { 0.2f, -0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 6
  230.         { { 0.0f, -0.8f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //7
  231.         { { 0.0f, -1.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },    // 8
  232.  
  233.         // Left
  234.         { { -0.8f, -0.2f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 9
  235.         { { -0.8f, 0.2f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 10
  236.         { { -0.8f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //11
  237.         { { -1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } },    // 12
  238.  
  239.         // Right
  240.         { { 0.8f, -0.2f, 0.0f, 1.0f }, { 0.0f, 0.0f, 1.0f, 1.0f } }, // 13
  241.         { { 0.8f, 0.2f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f } }, // 14
  242.         { { 0.8f, 0.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 1.0f, 1.0f } }, //15
  243.         { { 1.0f, 0.0f, 0.0f, 1.0f }, { 1.0f, 0.0f, 0.0f, 1.0f } }  // 16
  244.     };
  245.  
  246.     GLubyte Indices[] = {
  247.         // Top
  248.         0, 1, 3,
  249.         0, 3, 2,
  250.         3, 1, 4,
  251.         3, 4, 2,
  252.  
  253.         // Bottom
  254.         0, 5, 7,
  255.         0, 7, 6,
  256.         7, 5, 8,
  257.         7, 8, 6,
  258.  
  259.         // Left
  260.         0, 9, 11,
  261.         0, 11, 10,
  262.         11, 9, 12,
  263.         11, 12, 10,
  264.  
  265.         // Right
  266.         0, 13, 15,
  267.         0, 15, 14,
  268.         15, 13, 16,
  269.         15, 16, 14
  270.     };
  271.  
  272.     GLenum ErrorCheckValue = glGetError();
  273.     const size_t BufferSize = sizeof(Vertices);
  274.     const size_t VertexSize = sizeof(Vertices[0]);
  275.     const size_t RgbOffset = sizeof(Vertices[0].XYZW);
  276.    
  277.     glGenVertexArrays(1, &VaoId);
  278.     glBindVertexArray(VaoId);
  279.    
  280.     glGenBuffers(1, &BufferId);
  281.     glBindBuffer(GL_ARRAY_BUFFER, BufferId);
  282.     glBufferData(GL_ARRAY_BUFFER, BufferSize, Vertices, GL_STATIC_DRAW);
  283.  
  284.     glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, VertexSize, 0);
  285.     glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, VertexSize, (GLvoid*)RgbOffset);
  286.  
  287.     glEnableVertexAttribArray(0);
  288.     glEnableVertexAttribArray(1);
  289.  
  290.     glGenBuffers(1, &IndexBufferId);
  291.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId);
  292.     glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW);
  293.  
  294.     ErrorCheckValue = glGetError();
  295.     if (ErrorCheckValue != GL_NO_ERROR)
  296.     {
  297.         fprintf(
  298.             stderr,
  299.             "ERROR: Could not create a VBO: %s \n",
  300.             gluErrorString(ErrorCheckValue)
  301.         );
  302.  
  303.         exit(-1);
  304.     }
  305. }
  306.  
  307. void DestroyVBO(void)
  308. {
  309.     GLenum ErrorCheckValue = glGetError();
  310.  
  311.     glDisableVertexAttribArray(1);
  312.     glDisableVertexAttribArray(0);
  313.    
  314.     glBindBuffer(GL_ARRAY_BUFFER, 0);
  315.     glDeleteBuffers(1, &BufferId);
  316.  
  317.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
  318.     glDeleteBuffers(1, &IndexBufferId);
  319.  
  320.     glBindVertexArray(0);
  321.     glDeleteVertexArrays(1, &VaoId);
  322.  
  323.     ErrorCheckValue = glGetError();
  324.     if (ErrorCheckValue != GL_NO_ERROR)
  325.     {
  326.         fprintf(
  327.             stderr,
  328.             "ERROR: Could not destroy the VBO: %s \n",
  329.             gluErrorString(ErrorCheckValue)
  330.         );
  331.  
  332.         exit(-1);
  333.     }
  334. }
  335.  
  336. void CreateShaders(void)
  337. {
  338.     GLenum ErrorCheckValue = glGetError();
  339.    
  340.     VertexShaderId = glCreateShader(GL_VERTEX_SHADER);
  341.     glShaderSource(VertexShaderId, 1, &VertexShader, NULL);
  342.     glCompileShader(VertexShaderId);
  343.  
  344.     FragmentShaderId = glCreateShader(GL_FRAGMENT_SHADER);
  345.     glShaderSource(FragmentShaderId, 1, &FragmentShader, NULL);
  346.     glCompileShader(FragmentShaderId);
  347.  
  348.     ProgramId = glCreateProgram();
  349.         glAttachShader(ProgramId, VertexShaderId);
  350.         glAttachShader(ProgramId, FragmentShaderId);
  351.     glLinkProgram(ProgramId);
  352.     glUseProgram(ProgramId);
  353.  
  354.     ErrorCheckValue = glGetError();
  355.     if (ErrorCheckValue != GL_NO_ERROR)
  356.     {
  357.         fprintf(
  358.             stderr,
  359.             "ERROR: Could not create the shaders: %s \n",
  360.             gluErrorString(ErrorCheckValue)
  361.         );
  362.  
  363.         exit(-1);
  364.     }
  365. }
  366.  
  367. void DestroyShaders(void)
  368. {
  369.     GLenum ErrorCheckValue = glGetError();
  370.  
  371.     glUseProgram(0);
  372.  
  373.     glDetachShader(ProgramId, VertexShaderId);
  374.     glDetachShader(ProgramId, FragmentShaderId);
  375.  
  376.     glDeleteShader(FragmentShaderId);
  377.     glDeleteShader(VertexShaderId);
  378.  
  379.     glDeleteProgram(ProgramId);
  380.  
  381.     ErrorCheckValue = glGetError();
  382.     if (ErrorCheckValue != GL_NO_ERROR)
  383.     {
  384.         fprintf(
  385.             stderr,
  386.             "ERROR: Could not destroy the shaders: %s \n",
  387.             gluErrorString(ErrorCheckValue)
  388.         );
  389.  
  390.         exit(-1);
  391.     }
  392. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement