Guest User

Untitled

a guest
Nov 20th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.15 KB | None | 0 0
  1. #include<windows.h>
  2. #include <GLTools.h>
  3. #include <GLShaderManager.h>
  4. #include <GL/glut.h>
  5. #include <math.h>
  6. #include <GLMatrixStack.h>
  7. #include <GLFrame.h>
  8. #include <GLBatch.h>
  9. #include <GLFrustum.h>
  10. #include <GLGeometryTransform.h>
  11. #include <stdio.h>
  12. #include <math3d.h>
  13.  
  14.  
  15. GLFrame viewFrame;
  16. GLFrustum viewFrustum;
  17. GLTriangleBatch sphereBatch_;
  18. GLMatrixStack modelViewMatrix;
  19. GLMatrixStack projectionMatrix;
  20. GLGeometryTransform transformPipeline;
  21. GLShaderManager shaderManager;
  22.  
  23. GLuint ADSLightShader; // The diffuse light shader
  24. GLint locAmbient; // The location of the ambient color
  25. GLint locDiffuse; // The location of the diffuse color
  26. GLint locSpecular; // The location of the specular color
  27. GLint locLight; // The location of the Light in eye coordinates
  28. GLint locMVP; // The location of the ModelViewProjection matrix uniform
  29. GLint locMV; // The location of the ModelView matrix uniform
  30. GLint locNM; // The location of the Normal matrix uniform
  31.  
  32.  
  33. GLTriangleBatch sphereBatch;
  34. GLfloat fRadius = 30;
  35. GLint iSlices = 20;
  36. GLint iStacks = 20;
  37.  
  38. // Texture objects
  39. #define TEXTURE_EARTH 0
  40. #define TEXTURE_COUNT 1
  41.  
  42. GLuint textures[TEXTURE_COUNT];
  43. const char *szTextureFiles[TEXTURE_COUNT] = {"d:\project\AAAAAA\texturemap.tga"};
  44.  
  45.  
  46. // This function does any needed initialization on the rendering
  47. // context.
  48. void SetupRC(void)
  49. {
  50. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  51.  
  52. glEnable(GL_DEPTH_TEST);
  53. glEnable(GL_CULL_FACE);
  54.  
  55. shaderManager.InitializeStockShaders();
  56. viewFrame.MoveForward(4.0f);
  57.  
  58. gltMakeSphere(sphereBatch_, 1.0f, 26, 13);
  59.  
  60. ADSLightShader = shaderManager.LoadShaderPairWithAttributes("d:\project\AAAAAA\ADSPhong.vp", "d:\project\AAAAAA\ADSPhong.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex",
  61. GLT_ATTRIBUTE_NORMAL, "vNormal");
  62.  
  63. locAmbient = glGetUniformLocation(ADSLightShader, "ambientColor");
  64. locDiffuse = glGetUniformLocation(ADSLightShader, "diffuseColor");
  65. locSpecular = glGetUniformLocation(ADSLightShader, "specularColor");
  66. locLight = glGetUniformLocation(ADSLightShader, "vLightPosition");
  67. locMVP = glGetUniformLocation(ADSLightShader, "mvpMatrix");
  68. locMV = glGetUniformLocation(ADSLightShader, "mvMatrix");
  69. locNM = glGetUniformLocation(ADSLightShader, "normalMatrix");
  70.  
  71. GLbyte *pBytes;
  72. GLint iWidth, iHeight, iComponents;
  73. GLenum eFormat;
  74. GLint iLoop_;
  75.  
  76. // Load textures
  77. glGenTextures(TEXTURE_COUNT, textures);
  78. for(iLoop_ = 0; iLoop_ < TEXTURE_COUNT; iLoop_++)
  79. {
  80. // Bind to next texture object
  81. glBindTexture(GL_TEXTURE_2D, textures[iLoop_]);
  82.  
  83. // Load texture, set filter and wrap modes
  84. pBytes = gltReadTGABits(szTextureFiles[iLoop_],&iWidth, &iHeight,&iComponents,&eFormat);
  85.  
  86. // Load texture, set filter and wrap modes
  87. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  88. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  89. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  90. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  91. glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes);
  92. glGenerateMipmap(GL_TEXTURE_2D);
  93. // Don't need original texture data any more
  94. free(pBytes);
  95. }
  96.  
  97. GLfloat drho = (GLfloat)(3.141592653589) / (GLfloat) iStacks;
  98. GLfloat dtheta = 2.0f * (GLfloat)(3.141592653589) / (GLfloat) iSlices;
  99. GLfloat ds = 1.0f / (GLfloat) iSlices;
  100. GLfloat dt = 1.0f / (GLfloat) iStacks;
  101. GLfloat t = 1.0f;
  102. GLfloat s = 0.0f;
  103. GLint i, j; // Looping variables
  104.  
  105. sphereBatch.BeginMesh(iSlices * iStacks * 6);
  106. for (i = 0; i < iStacks; i++)
  107. {
  108. GLfloat rho = (GLfloat)i * drho;
  109. GLfloat srho = (GLfloat)(sin(rho));
  110. GLfloat crho = (GLfloat)(cos(rho));
  111. GLfloat srhodrho = (GLfloat)(sin(rho + drho));
  112. GLfloat crhodrho = (GLfloat)(cos(rho + drho));
  113.  
  114. // Many sources of OpenGL sphere drawing code uses a triangle fan
  115. // for the caps of the sphere. This however introduces texturing
  116. // artifacts at the poles on some OpenGL implementations
  117. s = 0.0f;
  118. M3DVector3f vVertex[4];
  119. M3DVector3f vNormal[4];
  120. M3DVector2f vTexture[4];
  121.  
  122. for ( j = 0; j < iSlices; j++)
  123. {
  124. GLfloat theta = (j == iSlices) ? 0.0f : j * dtheta;
  125. GLfloat stheta = (GLfloat)(-sin(theta));
  126. GLfloat ctheta = (GLfloat)(cos(theta));
  127.  
  128. GLfloat x = stheta * srho;
  129. GLfloat y = ctheta * srho;
  130. GLfloat z = crho;
  131.  
  132. vTexture[0][0] = s;
  133. vTexture[0][1] = t;
  134. vNormal[0][0] = x;
  135. vNormal[0][1] = y;
  136. vNormal[0][2] = z;
  137. vVertex[0][0] = x * fRadius;
  138. vVertex[0][1] = y * fRadius;
  139. vVertex[0][2] = z * fRadius;
  140.  
  141. x = stheta * srhodrho;
  142. y = ctheta * srhodrho;
  143. z = crhodrho;
  144.  
  145. vTexture[1][0] = s;
  146. vTexture[1][1] = t - dt;
  147. vNormal[1][0] = x;
  148. vNormal[1][1] = y;
  149. vNormal[1][2] = z;
  150. vVertex[1][0] = x * fRadius;
  151. vVertex[1][1] = y * fRadius;
  152. vVertex[1][2] = z * fRadius;
  153.  
  154.  
  155. theta = ((j+1) == iSlices) ? 0.0f : (j+1) * dtheta;
  156. stheta = (GLfloat)(-sin(theta));
  157. ctheta = (GLfloat)(cos(theta));
  158.  
  159. x = stheta * srho;
  160. y = ctheta * srho;
  161. z = crho;
  162.  
  163. s += ds;
  164. vTexture[2][0] = s;
  165. vTexture[2][1] = t;
  166. vNormal[2][0] = x;
  167. vNormal[2][1] = y;
  168. vNormal[2][2] = z;
  169. vVertex[2][0] = x * fRadius;
  170. vVertex[2][1] = y * fRadius;
  171. vVertex[2][2] = z * fRadius;
  172.  
  173. x = stheta * srhodrho;
  174. y = ctheta * srhodrho;
  175. z = crhodrho;
  176.  
  177. vTexture[3][0] = s;
  178. vTexture[3][1] = t - dt;
  179. vNormal[3][0] = x;
  180. vNormal[3][1] = y;
  181. vNormal[3][2] = z;
  182. vVertex[3][0] = x * fRadius;
  183. vVertex[3][1] = y * fRadius;
  184. vVertex[3][2] = z * fRadius;
  185.  
  186. sphereBatch.AddTriangle(vVertex, vNormal, vTexture);
  187.  
  188. // Rearrange for next triangle
  189. memcpy(vVertex[0], vVertex[1], sizeof(M3DVector3f));
  190. memcpy(vNormal[0], vNormal[1], sizeof(M3DVector3f));
  191. memcpy(vTexture[0], vTexture[1], sizeof(M3DVector2f));
  192.  
  193. memcpy(vVertex[1], vVertex[3], sizeof(M3DVector3f));
  194. memcpy(vNormal[1], vNormal[3], sizeof(M3DVector3f));
  195. memcpy(vTexture[1], vTexture[3], sizeof(M3DVector2f));
  196.  
  197. sphereBatch.AddTriangle(vVertex, vNormal, vTexture);
  198. }
  199. t -= dt;
  200. }
  201. sphereBatch.End();
  202. }
  203.  
  204. // Cleanup
  205. void ShutdownRC(void)
  206. {
  207. glDeleteTextures(TEXTURE_COUNT, textures);
  208. }
  209.  
  210.  
  211. // Called to draw scene
  212. void RenderScene(void)
  213. {
  214.  
  215. // Clear the window and the depth buffer
  216. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  217.  
  218. modelViewMatrix.PushMatrix(viewFrame);
  219. //modelViewMatrix.PushMatrix();
  220.  
  221. GLfloat vEyeLight[] = { -100.0f, 100.0f, 100.0f };
  222. GLfloat vAmbientColor[] = { 0.1f, 0.1f, 0.1f, 1.0f };
  223. GLfloat vDiffuseColor[] = { 0.0f, 0.0f, 1.0f, 1.0f };
  224. GLfloat vSpecularColor[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  225.  
  226. glUseProgram(ADSLightShader);
  227. glUniform4fv(locAmbient, 1, vAmbientColor);
  228. glUniform4fv(locDiffuse, 1, vDiffuseColor);
  229. glUniform4fv(locSpecular, 1, vSpecularColor);
  230. glUniform3fv(locLight, 1, vEyeLight);
  231. glUniformMatrix4fv(locMVP, 1, GL_FALSE, transformPipeline.GetModelViewProjectionMatrix());
  232. glUniformMatrix4fv(locMV, 1, GL_FALSE, transformPipeline.GetModelViewMatrix());
  233. glUniformMatrix3fv(locNM, 1, GL_FALSE, transformPipeline.GetNormalMatrix());
  234.  
  235. shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE,transformPipeline.GetModelViewProjectionMatrix(), 0);
  236. glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_EARTH]);
  237. sphereBatch.Draw();
  238.  
  239. sphereBatch_.Draw();
  240.  
  241. modelViewMatrix.PopMatrix();
  242.  
  243.  
  244. glutSwapBuffers();
  245. glutPostRedisplay();
  246. }
  247.  
  248. void ChangeSize(int w, int h)
  249. {
  250. // Prevent a divide by zero
  251. if (h == 0)
  252. h = 1;
  253.  
  254. // Set Viewport to window dimensions
  255. glViewport(0, 0, w, h);
  256.  
  257. viewFrustum.SetPerspective(35.0f, float(w) / float(h), 1.0f, 100.0f);
  258.  
  259. projectionMatrix.LoadMatrix(viewFrustum.GetProjectionMatrix());
  260. transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix);
  261. }
  262.  
  263. ///////////////////////////////////////////////////////////////////////////////
  264. // Main entry point for GLUT based programs
  265. int main(int argc, char* argv[])
  266. {
  267. gltSetWorkingDirectory(argv[0]);
  268.  
  269. glutInit(&argc, argv);
  270. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
  271. glutInitWindowSize(800, 600);
  272. glutCreateWindow("ADS Lighting, Phong Shading");
  273. glutReshapeFunc(ChangeSize);
  274. glutDisplayFunc(RenderScene);
  275.  
  276. GLenum err = glewInit();
  277. if (GLEW_OK != err)
  278. {
  279. fprintf(stderr, "GLEW Error: %sn", glewGetErrorString(err));
  280. return 1;
  281. }
  282.  
  283. SetupRC();
  284. glutMainLoop();
  285. ShutdownRC();
  286. return 0;
  287. }
Add Comment
Please, Sign In to add comment