fork download
  1. // NewTrainingFramework.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "../Utilities/utilities.h"
  6. #include "Vertex.h"
  7. #include "Shaders.h"
  8. #include "Globals.h"
  9. #include <conio.h>
  10.  
  11. int COLOR = 3;
  12. GLuint vboId;
  13. Shaders myShaders;
  14. Matrix m;
  15. float m_time;
  16. GLuint textureHandle;
  17. int uniformLocation;
  18.  
  19. int Init ( ESContext *esContext )
  20. {
  21. m.SetIdentity();
  22. glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
  23.  
  24. //triangle data (heap)
  25. Vertex verticesData[3];
  26.  
  27. verticesData[0].pos.x = 0.0f; verticesData[0].pos.y = 0.5f; verticesData[0].pos.z = 0.0f;
  28. verticesData[0].color.x = 0.5f; verticesData[0].color.y = 0.0f; verticesData[0].color.z = 0.0f;
  29. verticesData[0].uv.x = 0.0f; //u
  30. verticesData[0].uv.y = 0.0f; //v
  31.  
  32. verticesData[1].pos.x = -0.5f; verticesData[1].pos.y = -0.5f; verticesData[1].pos.z = 0.0f;
  33. verticesData[1].color.x = 0.0f; verticesData[1].color.y = 0.5f; verticesData[1].color.z = 0.0f;
  34. verticesData[1].uv.x = 0.5f;
  35. verticesData[1].uv.y = 1.0f;
  36.  
  37. verticesData[2].pos.x = 0.5f; verticesData[2].pos.y = -0.5f; verticesData[2].pos.z = 0.0f;
  38. verticesData[2].color.x = 0.0f; verticesData[2].color.y = 0.0f; verticesData[2].color.z = 0.5f;
  39. verticesData[2].uv.x = 1.0f;
  40. verticesData[2].uv.y = 0.0f;
  41.  
  42.  
  43. //buffer object
  44. glGenBuffers(1, &vboId); //buffer object name generation
  45. glBindBuffer(GL_ARRAY_BUFFER, vboId); //buffer object binding
  46. glBufferData(GL_ARRAY_BUFFER, sizeof(verticesData), verticesData, GL_STATIC_DRAW); //creation and initializion of buffer onject storage
  47. glBindBuffer(GL_ARRAY_BUFFER, 0);
  48.  
  49. //creation of shaders and program
  50. return myShaders.Init("../Resources/Shaders/TriangleShaderVS.vs", "../Resources/Shaders/TriangleShaderFS.fs");
  51.  
  52. }
  53. void InitTexture ( char *filepath, char * wrapmode)
  54. {
  55.  
  56. // create the OpenGL ES texture resource and get the handle
  57. glGenTextures(1, &textureHandle);
  58. // bind the texture to the 2D texture type
  59. glBindTexture(GL_TEXTURE_2D, textureHandle);
  60. // create CPU buffer and load it with the image data
  61. int width, height, bpp;
  62. char * bufferTGA = LoadTGA(filepath, &width, &height, &bpp );
  63. // load the image data into OpenGL ES texture resource
  64. if (bpp == 24)
  65. { //bpp = bits per pixel (8*number of channels, for RGBA = 8*4=32)
  66. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, bufferTGA);
  67. }
  68. else
  69. {
  70. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, bufferTGA);
  71. }
  72. // free the client memory
  73. delete [] bufferTGA;
  74.  
  75. //set the filters for minification and magnification
  76. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
  77. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  78.  
  79. // generate the mipmap chain
  80. glGenerateMipmap(GL_TEXTURE_2D);
  81.  
  82. if (wrapmode == "REPEAT")
  83. {
  84. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  85. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  86. }
  87.  
  88. if (wrapmode == "CLAMP_TO_EDGE")
  89. {
  90. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  91. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  92. }
  93.  
  94. uniformLocation = glGetUniformLocation(myShaders.program, filepath);
  95.  
  96. }
  97.  
  98. void Draw ( ESContext *esContext )
  99. {
  100. glUseProgram(myShaders.program);
  101. glUniformMatrix4fv( myShaders.matrixTransform, 1, false, (GLfloat *)&m );
  102. glClear(GL_COLOR_BUFFER_BIT);
  103. glBindBuffer(GL_ARRAY_BUFFER, vboId);
  104.  
  105. GLfloat* ptr = (GLfloat *)0;
  106. if(myShaders.positionAttribute != -1) //attribute passing to shader, for uniforms use glUniform1f(time, deltaT); glUniformMatrix4fv( m_pShader->matrixWVP, 1, false, (GLfloat *)&rotationMat );
  107. {
  108. glEnableVertexAttribArray(myShaders.positionAttribute);
  109. glVertexAttribPointer(myShaders.positionAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), ptr);
  110. }
  111.  
  112. if(myShaders.colorAttribute != -1)
  113. {
  114. glEnableVertexAttribArray(myShaders.colorAttribute);
  115.  
  116. glVertexAttribPointer(myShaders.colorAttribute, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), ptr+COLOR);
  117. }
  118.  
  119. if(myShaders.uvAttrib != -1)
  120. {
  121. glEnableVertexAttribArray(myShaders.uvAttrib);
  122.  
  123. glVertexAttribPointer(myShaders.uvAttrib, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), ptr+uvCOOR);
  124. }
  125.  
  126. glDrawArrays(GL_TRIANGLES, 0, 3);
  127. glBindBuffer(GL_ARRAY_BUFFER, 0);
  128. eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );
  129.  
  130. unsigned short textureUnit = 0;
  131. // make active a texture unit
  132. glActiveTexture(GL_TEXTURE0 + textureUnit);
  133. // bind the texture to the currently active texture unit
  134. glBindTexture(GL_TEXTURE_2D, textureHandle);
  135. // set the uniform sampler
  136. glUniform1i(uniformLocation, textureHandle);
  137.  
  138. }
  139.  
  140.  
  141. void CleanUp()
  142. {
  143. glDeleteBuffers(1, &vboId);
  144. }
  145.  
  146. int _tmain(int argc, _TCHAR* argv[])
  147. {
  148. ESContext esContext;
  149.  
  150. esInitContext ( &esContext );
  151.  
  152. esCreateWindow ( &esContext, "Hello Triangle", Globals::screenWidth, Globals::screenHeight, ES_WINDOW_RGB | ES_WINDOW_DEPTH);
  153.  
  154. if ( Init ( &esContext ) != 0 )
  155. return 0;
  156.  
  157.  
  158. InitTexture("../Resources/Textures/Rock.tga", "REPEAT" );
  159.  
  160. esRegisterDrawFunc ( &esContext, Draw );
  161. esMainLoop ( &esContext );
  162.  
  163. //releasing OpenGL resources
  164. CleanUp();
  165.  
  166. //identifying memory leaks
  167. MemoryDump();
  168. printf("Press any key...\n");
  169. _getch();
  170.  
  171. return 0;
  172. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:20: fatal error: stdafx.h: No such file or directory
 #include "stdafx.h"
                    ^
compilation terminated.
stdout
Standard output is empty