Advertisement
pierdziadek

grafika lab6 zrobione

Jan 14th, 2025
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.73 KB | None | 0 0
  1. #include <GL/glew.h>
  2.  
  3. #include "glm/glm.hpp"
  4. #include "glm/gtc/matrix_transform.hpp"
  5. #include "glm/gtc/type_ptr.hpp"
  6.  
  7. #include <SFML/System.hpp>
  8. #include <SFML/Graphics.hpp>
  9. #include <SFML/OpenGL.hpp>
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <chrono>
  14. #include "OBJ_Loader.h"
  15.  
  16. const GLchar* vertexSource = R"glsl(
  17. #version 150 core
  18.  
  19. uniform float time;
  20. uniform mat4 model;
  21. uniform mat4 view;
  22. uniform mat4 projection;
  23.  
  24. in vec3 position;
  25. in vec2 texture;
  26. out vec2 vTexture;
  27.  
  28. void main()
  29. {
  30. vTexture = texture;
  31. gl_Position = projection * view * model * vec4(position, 1.0);
  32. }
  33. )glsl";
  34. const GLchar* fragmentSource = R"glsl(
  35. #version 150 core
  36.  
  37. uniform float time;
  38.  
  39. uniform sampler2D tex;
  40.  
  41. in vec2 vTexture;
  42.  
  43. out vec4 outColor;
  44.  
  45. void main()
  46. {
  47. outColor = texture(tex, vTexture);
  48. }
  49. )glsl";
  50.  
  51. void modelViewProjection(GLuint shaderProgram, float time) {
  52.  
  53. // Model matrix
  54. glm::mat4 model = glm::mat4(1.0f);
  55. model = glm::scale(model, glm::vec3(0.5f, 0.5f, 0.5f));
  56. model = glm::translate(model, glm::vec3(1.5f, 0.0f, 0.0f));
  57. model = glm::rotate(model, glm::radians(time * 5.0f), glm::vec3(0.0f, 1.0f, 0.0f));
  58.  
  59. GLint uniformTranslation = glGetUniformLocation(shaderProgram, "model");
  60. glUniformMatrix4fv(uniformTranslation, 1, GL_FALSE, glm::value_ptr(model));
  61.  
  62. // View matrix
  63. glm::mat4 view = glm::lookAt(
  64. glm::vec3(5.0f, 5.0f, 5.0f),
  65. glm::vec3(0.0f, 0.0f, 0.0f),
  66. glm::vec3(0.0f, 1.0f, 0.0f)
  67. );
  68. GLint uniView = glGetUniformLocation(shaderProgram, "view");
  69. glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  70.  
  71. // Projection matrix
  72. glm::mat4 proj = glm::perspective(glm::radians(45.0f), 800.0f / 600.0f, 1.0f, 10.0f);
  73. GLint uniProj = glGetUniformLocation(shaderProgram, "projection");
  74. glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
  75. }
  76.  
  77. int main()
  78. {
  79. // Configure OpenGL Context through SFML
  80. sf::ContextSettings settings;
  81. settings.depthBits = 24;
  82. settings.stencilBits = 8;
  83. settings.antialiasingLevel = 2;
  84. settings.majorVersion = 4;
  85. settings.minorVersion = 0;
  86. settings.attributeFlags = sf::ContextSettings::Core;
  87.  
  88. // Create the main window
  89. sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Resize | sf::Style::Close, settings);
  90. window.setVerticalSyncEnabled(true);
  91.  
  92. settings = window.getSettings();
  93.  
  94. std::cout << "depth bits:" << settings.depthBits << std::endl;
  95. std::cout << "stencil bits:" << settings.stencilBits << std::endl;
  96. std::cout << "antialiasing level:" << settings.antialiasingLevel << std::endl;
  97. std::cout << "version:" << settings.majorVersion << "." << settings.minorVersion << std::endl;
  98.  
  99. // start glew
  100. glewExperimental = GL_TRUE;
  101. glewInit();
  102.  
  103. // load 3D model
  104. objl::Loader loader;
  105. loader.LoadFile("cube.obj");
  106.  
  107. GLuint vertexCount = loader.LoadedVertices.size();
  108. GLuint indexCount = loader.LoadedIndices.size();
  109.  
  110. std::vector<float> vertexAttributes(vertexCount * 5);
  111.  
  112. // Create a vertex array object
  113. GLuint vao;
  114. glGenVertexArrays(1, &vao);
  115. glBindVertexArray(vao);
  116.  
  117. // Create vertex buffer
  118. GLuint vbo;
  119. glGenBuffers(1, &vbo); // Generate 1 buffer
  120. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  121.  
  122. for (int i = 0; i < vertexCount; i++) {
  123. vertexAttributes[ i * 5 + 0 ] = loader.LoadedVertices[ i ].Position.X;
  124. vertexAttributes[ i * 5 + 1 ] = loader.LoadedVertices[ i ].Position.Y;
  125. vertexAttributes[ i * 5 + 2 ] = loader.LoadedVertices[ i ].Position.Z;
  126. vertexAttributes[ i * 5 + 3 ] = loader.LoadedVertices[ i ].TextureCoordinate.X;
  127. vertexAttributes[ i * 5 + 4 ] = loader.LoadedVertices[ i ].TextureCoordinate.Y;
  128. }
  129.  
  130. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertexCount * 5, vertexAttributes.data(), GL_STATIC_DRAW);
  131.  
  132. // load faces / indices
  133. GLuint ebo;
  134. glGenBuffers(1, &ebo);
  135. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  136.  
  137. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * indexCount, loader.LoadedIndices.data(), GL_STATIC_DRAW);
  138.  
  139.  
  140. // Load texture
  141. /*GLuint tex;
  142. glGenTextures( 1, &tex );
  143. glActiveTexture( GL_TEXTURE0 );
  144. glBindTexture( GL_TEXTURE_2D, tex );
  145.  
  146. float pixels [ ] = {
  147. 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
  148. 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f
  149. };
  150. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels );*/
  151.  
  152. sf::Image img_data;
  153. std::string filePath = "cube.png"; // Make sure this is not empty
  154. if ( !img_data.loadFromFile( filePath ) ) {
  155. std::cout << "Texture not loaded!" << std::endl;
  156. }
  157. sf::Vector2u texSize = img_data.getSize( );
  158. glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texSize.x, texSize.y,
  159. 0, GL_RGBA, GL_UNSIGNED_BYTE, img_data.getPixelsPtr( ) );
  160.  
  161. float borderColor [ ] = { 1.0f, 0.0f, 1.0f, 1.0f };
  162. glTexParameterfv( GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, borderColor );
  163. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER );
  164. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER );
  165.  
  166. glGenerateMipmap( GL_TEXTURE_2D );
  167. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
  168. glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR );
  169.  
  170. glEnable( GL_DEPTH_TEST );
  171. glEnable( GL_CULL_FACE );
  172. glFrontFace( GL_CW );
  173.  
  174. // Create and compile the vertex shader
  175. GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
  176. glShaderSource(vertexShader, 1, &vertexSource, NULL);
  177. glCompileShader(vertexShader);
  178.  
  179. // Create and compile the fragment shader
  180. GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
  181. glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  182. glCompileShader(fragmentShader);
  183.  
  184.  
  185. // combine shaders into a program
  186. GLuint shaderProgram = glCreateProgram();
  187. glAttachShader(shaderProgram, vertexShader);
  188. glAttachShader(shaderProgram, fragmentShader);
  189.  
  190. glBindFragDataLocation(shaderProgram, 0, "outColor");
  191. glLinkProgram(shaderProgram);
  192. glUseProgram(shaderProgram);
  193.  
  194. // Specify the layout of the vertex data
  195. GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
  196. glEnableVertexAttribArray(posAttrib);
  197. glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), 0);
  198.  
  199. GLint texAttrib = glGetAttribLocation( shaderProgram, "texture" );
  200. glEnableVertexAttribArray( texAttrib );
  201. glVertexAttribPointer( texAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof( float ), ( void* ) ( 3 * sizeof( float ) ) );
  202.  
  203. // Set uniforms
  204. auto t_start = std::chrono::high_resolution_clock::now();
  205. GLint timeUni = glGetUniformLocation(shaderProgram, "time");
  206.  
  207. // main loop
  208. bool running = true;
  209. while (running)
  210. {
  211. // handle events
  212. sf::Event event;
  213. while (window.pollEvent(event))
  214. {
  215. if (event.type == sf::Event::Closed)
  216. {
  217. // end the program
  218. running = false;
  219. }
  220. else if (event.type == sf::Event::Resized)
  221. {
  222. // adjust the viewport when the window is resized
  223. glViewport(0, 0, event.size.width, event.size.height);
  224. }
  225. else if (event.type == sf::Event::KeyPressed) {
  226. if (event.key.code == sf::Keyboard::Escape)
  227. running = false;
  228. break;
  229. }
  230. }
  231.  
  232. // make sure the graphic context is active
  233. window.setActive();
  234.  
  235. // clear the buffers
  236. glClearColor(0.0f, 0.0f, 0.4f, 1.0f);
  237. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  238.  
  239. // set uniforms
  240. auto t_now = std::chrono::high_resolution_clock::now();
  241. float time = std::chrono::duration_cast<std::chrono::duration<float>>(t_now - t_start).count();
  242. glUniform1f(timeUni, time);
  243.  
  244. // Set Model View Projection matrices
  245. modelViewProjection(shaderProgram, time);
  246.  
  247. // draw...
  248. //glDrawArrays(GL_TRIANGLES, 0, 10);
  249. glDrawElements(GL_TRIANGLES, indexCount * 3, GL_UNSIGNED_INT, 0);
  250.  
  251. // end the current frame (internally swaps the front and back buffers)
  252. window.display();
  253. sf::sleep(sf::seconds(0.01f));
  254. }
  255.  
  256. // release resources...
  257.  
  258. glDeleteProgram(shaderProgram);
  259. glDeleteShader(fragmentShader);
  260. glDeleteShader(vertexShader);
  261.  
  262. glDeleteBuffers(1, &vbo);
  263.  
  264. glDeleteVertexArrays(1, &vao);
  265.  
  266. window.close();
  267.  
  268. return 0;
  269. }
  270.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement