Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.43 KB | None | 0 0
  1. #include "Player.h"
  2.  
  3. Player::Player(ObjMesh * mesh, Texture2D * texture, float x, float y, float z) : ObjSceneObject(mesh, texture)
  4. {
  5. worldX = x;
  6. worldY = y;
  7. worldZ = z;
  8. }
  9.  
  10.  
  11. void Player::Keyboard(unsigned key, int x, int y)
  12. {
  13. if (key == 's')
  14. {
  15. movePlayer -= 1.0f;
  16. worldX = movePlayer;
  17. }
  18. }
  19.  
  20. void Player::Update()
  21. {
  22. glutKeyboardFunc(Keyboard(unsigned key, int x, int y));
  23.  
  24. }
  25.  
  26.  
  27. void Player::Draw()
  28. {
  29.  
  30. if (_mesh->textureEnabled == true)
  31. {
  32. glBindTexture(GL_TEXTURE_2D, _texture->GetID());
  33.  
  34. glEnableClientState(GL_NORMAL_ARRAY);
  35. glEnableClientState(GL_VERTEX_ARRAY);
  36. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  37.  
  38. glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]);
  39. glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]);
  40. glTexCoordPointer(2, GL_FLOAT, 0, &_mesh->textures[0]);
  41.  
  42. glPushMatrix();
  43. glTranslatef(worldX, worldY, worldZ);
  44.  
  45. glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size());
  46.  
  47. glPopMatrix();
  48.  
  49. glDisableClientState(GL_VERTEX_ARRAY);
  50. glDisableClientState(GL_NORMAL_ARRAY);
  51. glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  52. }
  53. else
  54. {
  55. glEnableClientState(GL_NORMAL_ARRAY);
  56. glEnableClientState(GL_VERTEX_ARRAY);
  57.  
  58.  
  59. glVertexPointer(3, GL_FLOAT, 0, &_mesh->vertices[0]);
  60. glNormalPointer(GL_FLOAT, 0, &_mesh->normals[0]);
  61.  
  62. glPushMatrix();
  63. glTranslatef(-4.0f, -0.0f, -6.0f);
  64.  
  65. glDrawArrays(GL_TRIANGLES, 0, _mesh->vertices.size());
  66.  
  67. glPopMatrix();
  68.  
  69. glDisableClientState(GL_VERTEX_ARRAY);
  70. glDisableClientState(GL_NORMAL_ARRAY);
  71. }
  72. }
  73.  
  74.  
  75.  
  76. //My glut call backs
  77. #include "helloGl.h"
  78. #include "GLUTCallback.h"
  79. #include "Player.h"
  80.  
  81. namespace GLUTCallbacks
  82. {
  83. namespace
  84. {
  85. helloGl* helloGL = nullptr;
  86. }
  87. void Init(helloGl * gl)
  88. {
  89. helloGL = gl;
  90. }
  91. void Display()
  92. {
  93. if (helloGL != nullptr)
  94. helloGL->Display();
  95. }
  96. void Timer(int preferredRefresh)
  97. {
  98. int updateTime = glutGet(GLUT_ELAPSED_TIME);
  99. helloGL->Update();
  100. updateTime = glutGet(GLUT_ELAPSED_TIME) - updateTime;
  101. glutTimerFunc(preferredRefresh - updateTime, GLUTCallbacks::Timer, preferredRefresh);
  102. }
  103. void Keyboard(unsigned char key, int x, int y)
  104. {
  105. helloGL->Keyboard(key, x, y);
  106. }
  107. }
  108.  
  109.  
  110. //My main class where everything is drawn and updated
  111. #include "helloGl.h"
  112.  
  113. //https://www.opengl.org/discussion_boards/showthread.php/142675-Making-the-camera-follow-the-object //camera movement with the player
  114.  
  115. helloGl::helloGl(int argc, char* argv[])
  116. {
  117. InitGL(argc, argv);
  118.  
  119. InitObjects();
  120.  
  121. //glLoadIdentity(); // Reset Matrix
  122. glutMainLoop();
  123. }
  124. void helloGl::LightingForGame()
  125. {
  126. _lightPosition = new Vector4();
  127. _lightPosition->x = 0.0;
  128. _lightPosition->y = 0.0;
  129. _lightPosition->z = 1.0;
  130. _lightPosition->w = 0.0;
  131.  
  132. _lightData = new Lighting();
  133. _lightData->Ambient.x = 0.2;
  134. _lightData->Ambient.y = 0.2;
  135. _lightData->Ambient.z = 0.2;
  136. _lightData->Ambient.w = 1.0;
  137. _lightData->Diffuse.x = 0.8;
  138. _lightData->Diffuse.y = 0.8;
  139. _lightData->Diffuse.z = 0.8;
  140. _lightData->Diffuse.w = 1.0;
  141. _lightData->Specular.x = 0.2;
  142. _lightData->Specular.y = 0.2;
  143. _lightData->Specular.z = 0.2;
  144. _lightData->Specular.w = 1.0;
  145.  
  146. glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.x));
  147. glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.y));
  148. glLightfv(GL_LIGHT0, GL_AMBIENT, &(_lightData->Ambient.z));
  149. glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.x));
  150. glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.y));
  151. glLightfv(GL_LIGHT0, GL_DIFFUSE, &(_lightData->Diffuse.z));
  152. glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.x));
  153. glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.y));
  154. glLightfv(GL_LIGHT0, GL_SPECULAR, &(_lightData->Specular.z));
  155.  
  156. glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->x));
  157. glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->y));
  158. glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->z));
  159. glLightfv(GL_LIGHT0, GL_POSITION, &(_lightPosition->w));
  160.  
  161. _material = new Material();
  162. _material->Ambient.x = 0.8;
  163. _material->Ambient.y = 0.05;
  164. _material->Ambient.z = 0.05;
  165. _material->Ambient.w = 1.0;
  166. _material->Diffuse.x = 0.8;
  167. _material->Diffuse.y = 0.05;
  168. _material->Diffuse.z = 0.05;
  169. _material->Diffuse.w = 1.0;
  170. _material->Specular.x = 1.0;
  171. _material->Specular.y = 1.0;
  172. _material->Specular.z = 1.0;
  173. _material->Specular.w = 1.0;
  174. _material->Shininess = 100.0f;
  175.  
  176. glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.x));
  177. glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.y));
  178. glMaterialfv(GL_FRONT, GL_AMBIENT, &(_material->Ambient.z));
  179. glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.x));
  180. glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.y));
  181. glMaterialfv(GL_FRONT, GL_DIFFUSE, &(_material->Diffuse.z));
  182. glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.x));
  183. glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.y));
  184. glMaterialfv(GL_FRONT, GL_SPECULAR, &(_material->Specular.z));
  185.  
  186. glMaterialf(GL_FRONT, GL_SHININESS, _material->Shininess);
  187. }
  188. void helloGl::InitObjects()
  189. {
  190. Texture2D* texture = new Texture2D();
  191. texture->LoadTextureTGA("GDT.tga");
  192.  
  193. ObjMesh* objMesh = ModelLoader::Load("gunShip1.obj");
  194.  
  195. ObjTest = new Player(objMesh, texture, -2, 0, -10);
  196.  
  197. cameraMove = 5.0f;
  198. camera = new Camera();
  199.  
  200. camera->eye.x = 5.0f; camera->eye.y = 0.0f; camera->eye.z = cameraMove;
  201. camera->center.x = 0.0f; camera->center.y = 0.0f; camera->center.z = 0.0f;
  202. camera->up.x = 0.0f; camera->up.y = 1.0f; camera->up.z = 0.0f;
  203. }
  204.  
  205. void helloGl::InitGL(int argc, char* argv[])
  206. {
  207. GLUTCallbacks::Init(this);
  208. glutInit(&argc, argv);
  209. glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH);
  210. glutInitWindowSize(1920, 1080);
  211. glutInitDisplayMode(GLUT_DOUBLE);
  212. glutCreateWindow("Simple OpenGL Program");
  213. glutDisplayFunc(GLUTCallbacks::Display);
  214. glEnable(GL_DEPTH_TEST);
  215. glutTimerFunc(REFRESHRATE, GLUTCallbacks::Timer, REFRESHRATE);
  216. glutKeyboardFunc(GLUTCallbacks::Keyboard);
  217. glEnable(GL_TEXTURE_2D);
  218.  
  219. glEnable(GL_CULL_FACE);
  220. glCullFace(GL_BACK);
  221. // Use the Projection Matrix
  222. glMatrixMode(GL_PROJECTION);
  223. glLoadIdentity();
  224.  
  225. // Set the viewport to be the entire window
  226. glViewport(0, 0, 800, 800);
  227. gluPerspective(45, 1, 0.1, 1000);
  228.  
  229. glEnable(GL_DEPTH_TEST);
  230. glEnable(GL_TEXTURE_2D);
  231.  
  232. glEnable(GL_LIGHTING);
  233. glEnable(GL_LIGHT0);
  234.  
  235.  
  236. }
  237.  
  238. void helloGl::Display()
  239. {
  240. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //This clears the scene so we start a fresh each time
  241. glPushMatrix();
  242.  
  243. ObjTest->Draw();
  244.  
  245. glEnd(); //Ends the polygon draw
  246. glPopMatrix();
  247. glFlush(); //Flushes the scene we just drew to the graphics card
  248. glutSwapBuffers();
  249. }
  250. void helloGl::Update()
  251. {
  252.  
  253. glMatrixMode(GL_MODELVIEW);
  254. glLoadIdentity();
  255.  
  256. gluLookAt(camera->eye.x, camera->eye.y, camera->eye.z, camera->center.x, camera->center.y, camera->center.z, camera->up.x, camera->up.y, camera->up.z);
  257. ObjTest->Update();
  258.  
  259. glutPostRedisplay();
  260. }
  261.  
  262. void helloGl::Keyboard(unsigned char key, int x, int y)
  263. {
  264. //if (key == 'w')
  265. //{
  266. // direction -= 2.0f;
  267. // camera->eye.z = direction;
  268. //
  269. //}
  270.  
  271. //if (key == 'a')
  272. //{
  273. // direction -= 2.0f;
  274. // camera->eye.x = direction;
  275. //}
  276.  
  277. //if (key == 's')
  278. //{
  279. // direction += 2.0f;
  280. // camera->eye.z = direction;
  281. //}
  282.  
  283. //if (key == 'd')
  284. //{
  285. // direction += 2.0f;
  286. // camera->eye.x = direction;
  287. //}
  288.  
  289. if (key == 'w')
  290. {
  291. direction -= 2.0f;
  292. playerZ = direction;
  293.  
  294. }
  295. if (key == 's')
  296. {
  297. direction += 2.0f;
  298. playerZ = direction;
  299. }
  300. }
  301.  
  302.  
  303. helloGl::~helloGl()
  304. {
  305.  
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement