Guest User

Untitled

a guest
Jul 7th, 2012
408
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SFML/System.hpp>
  2. #include <SFML/Window.hpp>
  3. #include <SFML/Graphics.hpp>
  4. #include <iostream>
  5.  
  6. void processEvents(sf::Window *app);
  7. void processInput(sf::Window *app, const sf::Input &input);
  8.  
  9. void renderCube(sf::Window *app, sf::Clock *clock);
  10. void renderGlScene(sf::Window *app);
  11.  
  12. int loadResources();
  13.  
  14. void init();
  15.  
  16. void update();
  17.  
  18. GLuint texture_handle;
  19.  
  20. float xRot = 0.0f;
  21. float yRot = 0.0f;
  22. float zRot = 0.0f;
  23.  
  24. int main() {
  25. sf::Window app(sf::VideoMode(800, 600, 32), "Lesson 6");
  26.  
  27. app.UseVerticalSync(false);
  28.  
  29. init();
  30.  
  31. if (loadResources() == -1) {
  32. return EXIT_FAILURE;
  33. }
  34.  
  35. while (app.IsOpened()) {
  36. update();
  37.  
  38. processEvents(&app);
  39.  
  40. //renderCube(&app, &clock);
  41. renderGlScene(&app);
  42.  
  43. app.Display() ;
  44. }
  45.  
  46. return EXIT_SUCCESS;
  47. }
  48.  
  49. int loadResources() {
  50. sf::Image img_data;
  51.  
  52. // Load Texture
  53. if (!img_data.LoadFromFile("data/images/crate.jpg")) {
  54. std::cout << "Could not load data/images/crate.jpg";
  55.  
  56. return -1;
  57. }
  58.  
  59. // Give the texture an id
  60. glGenTextures(1, &texture_handle);
  61.  
  62. // Bind the id to a texture (i.e: associate the id with a texture)
  63. glBindTexture(GL_TEXTURE_2D, texture_handle);
  64.  
  65. //
  66. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_data.GetWidth(), img_data.GetHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data.GetPixelsPtr());
  67.  
  68. //
  69. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  70. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  71.  
  72. return 0;
  73. }
  74.  
  75. void update() {
  76. xRot += 0.003f;
  77. yRot += 0.002f;
  78. zRot += 0.004f;
  79. }
  80.  
  81. void init() {
  82. glClearDepth(1.f);
  83. glClearColor(0.f, 0.f, 0.f, 0.f);
  84.  
  85. // Enable z-buffer and read and write
  86. glEnable(GL_DEPTH_TEST);
  87. glDepthMask(GL_TRUE);
  88.  
  89. // Setup a perpective projection
  90. glMatrixMode(GL_PROJECTION);
  91. glLoadIdentity();
  92. gluPerspective(45.f, 1.f, 1.f, 500.f);
  93. }
  94.  
  95. void processEvents(sf::Window *app) {
  96. sf::Event event;
  97.  
  98. while (app->GetEvent(event)) {
  99. if (event.Type == sf::Event::Closed) {
  100. app->Close();
  101. }
  102.  
  103. if (event.Type == sf::Event::KeyPressed &&
  104. event.Key.Code == sf::Key::Escape) {
  105. app->Close();
  106. }
  107. }
  108. }
  109.  
  110. void renderGlScene(sf::Window *app) {
  111. app->SetActive();
  112.  
  113. // Clear color depth buffer
  114. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  115.  
  116. // Apply some transformations
  117. glMatrixMode(GL_MODELVIEW);
  118. glLoadIdentity();
  119.  
  120. glTranslatef(0.0f,0.0f,-5.0f);
  121.  
  122. glRotatef(xRot,1.0f,0.0f,0.0f); // Rotate On The X Axis
  123. glRotatef(yRot,0.0f,1.0f,0.0f); // Rotate On The Y Axis
  124. glRotatef(zRot,0.0f,0.0f,1.0f); // Rotate On The Z Axis
  125.  
  126. glBindTexture(GL_TEXTURE_2D, texture_handle);
  127.  
  128. glBegin(GL_QUADS);
  129. // Front Face
  130. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  131. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  132. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
  133. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
  134. // Back Face
  135. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
  136. glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
  137. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
  138. glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
  139. // Top Face
  140. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
  141. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  142. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  143. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
  144. // Bottom Face
  145. glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
  146. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
  147. glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  148. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  149. // Right face
  150. glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
  151. glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
  152. glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
  153. glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
  154. // Left Face
  155. glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
  156. glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
  157. glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
  158. glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
  159. glEnd();
  160. }
RAW Paste Data