Advertisement
adwas33

Untitled

May 19th, 2022
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.11 KB | None | 0 0
  1. #pragma warning(disable : 4996)
  2. #pragma warning(suppress : 4996)
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <GL/glew.h>
  6. #include <SFML/Window.hpp>
  7. #include <SFML/System/Time.hpp>
  8.  
  9. #include <glm/glm.hpp>
  10. #include <glm/gtc/matrix_transform.hpp>
  11. #include <glm/gtc/type_ptr.hpp>
  12.  
  13. #define STB_IMAGE_IMPLEMENTATION
  14. #include "stb_image.h"
  15.  
  16. // Kody shaderów
  17. const GLchar* vertexSource = R"glsl(
  18. #version 150 core
  19. in vec3 position;
  20. in vec3 color;
  21. in vec3 aNormal;
  22. out vec3 Normal;
  23. out vec3 FragPos;
  24. uniform mat4 model;
  25. uniform mat4 view;
  26. uniform mat4 proj;
  27. out vec3 Color;
  28. in vec2 aTexCoord;
  29. out vec2 TexCoord;
  30. void main(){
  31.  
  32. Color = color;
  33.  
  34. gl_Position = proj * view * model * vec4(position, 1.0);
  35. TexCoord = aTexCoord;
  36. }
  37. )glsl";
  38.  
  39. const GLchar* fragmentSource = R"glsl(
  40. #version 150 core
  41. in vec3 Color;
  42. out vec4 outColor;
  43. in vec2 TexCoord;
  44. in vec3 Normal;
  45. in vec3 FragPos;
  46. in vec3 lightPos;
  47. uniform sampler2D texture1;
  48. void main()
  49. {
  50. float ambientStrength = 0.4; // ambient
  51. vec3 ambientlightColor = vec3(0.8,0.8,0.8);
  52. vec4 ambient = ambientStrength * vec4(ambientlightColor,0.7);
  53. vec3 difflightColor = vec3(0.8,0.8,0.8);
  54.  
  55. vec3 norm = normalize(Normal);
  56. vec3 lightDir = normalize(lightPos - FragPos);
  57.  
  58. float diff = max(dot(norm, lightDir), 0.0);
  59. vec3 diffuse = diff * difflightColor;
  60.  
  61. //outColor = vec4(Color, 1.0);
  62.  
  63. //outColor=texture(texture1, TexCoord);
  64.  
  65. outColor = (ambient+vec4(diffuse,0.3)) * texture(texture1, TexCoord);
  66. }
  67. )glsl";
  68. //ZMIENNE OPISUJĄCE KAMERE
  69. glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
  70. glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
  71. glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
  72. float obrot = 0;
  73.  
  74. //
  75.  
  76. bool loadOBJ(
  77. const char* path,
  78. std::vector < glm::vec3 >& out_vertices,
  79. std::vector < glm::vec2 >& out_uvs,
  80. std::vector < glm::vec3 >& out_normals
  81. )
  82. {
  83. std::vector< unsigned int > vertexIndices, uvIndices, normalIndices;
  84. std::vector< glm::vec3 > temp_vertices;
  85. std::vector< glm::vec2 > temp_uvs;
  86. std::vector< glm::vec3 > temp_normals;
  87. FILE* file = fopen("D:\\GrafikaKomputerowa\\GKPomocOlka\\Grass_block.obj","r");
  88. if (file == NULL) {
  89. printf("Impossible to open the file !\n");
  90. return false;
  91. }
  92. while (1) {
  93.  
  94. char lineHeader[128];
  95. // read the first word of the line
  96. int res = fscanf(file, "%s", lineHeader);
  97. if (res == EOF)
  98. break; // EOF = End Of File. Quit the loop.
  99.  
  100. // else : parse lineHeader
  101. if (strcmp(lineHeader, "v") == 0) {
  102. glm::vec3 vertex;
  103. fscanf(file, "%f %f %f\n", &vertex.x, &vertex.y, &vertex.z);
  104. temp_vertices.push_back(vertex);
  105. }
  106. else if (strcmp(lineHeader, "vt") == 0) {
  107. glm::vec2 uv;
  108. fscanf(file, "%f %f\n", &uv.x, &uv.y);
  109. temp_uvs.push_back(uv);
  110. }
  111. else if (strcmp(lineHeader, "vn") == 0) {
  112. glm::vec3 normal;
  113. fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z);
  114. temp_normals.push_back(normal);
  115. }
  116. else if (strcmp(lineHeader, "f") == 0) {
  117. std::string vertex1, vertex2, vertex3;
  118. unsigned int vertexIndex[3], uvIndex[3], normalIndex[3];
  119. int matches = fscanf(file, "%d/%d/%d %d/%d/%d %d/%d/%d\n", &vertexIndex[0], &uvIndex[0], &normalIndex[0], &vertexIndex[1], &uvIndex[1], &normalIndex[1], &vertexIndex[2], &uvIndex[2], &normalIndex[2]);
  120. if (matches != 9) {
  121. printf("File can't be read by our simple parser : ( Try exporting with other options\n");
  122. return false;
  123. }
  124. vertexIndices.push_back(vertexIndex[0]);
  125. vertexIndices.push_back(vertexIndex[1]);
  126. vertexIndices.push_back(vertexIndex[2]);
  127. uvIndices.push_back(uvIndex[0]);
  128. uvIndices.push_back(uvIndex[1]);
  129. uvIndices.push_back(uvIndex[2]);
  130. normalIndices.push_back(normalIndex[0]);
  131. normalIndices.push_back(normalIndex[1]);
  132. normalIndices.push_back(normalIndex[2]);
  133. }
  134. // For each vertex of each triangle
  135. for (unsigned int i = 0; i < vertexIndices.size(); i++) {
  136. unsigned int vertexIndex = vertexIndices[i];
  137. glm::vec3 vertex = temp_vertices[vertexIndex - 1];
  138. out_vertices.push_back(vertex);
  139. }
  140. }
  141. }
  142.  
  143. bool firstMouse = true;
  144. //
  145. glm::vec3 ustawKamereMysz(sf::Window* window, GLfloat xpos, GLfloat ypos)
  146. {
  147. GLfloat lastX;
  148. GLfloat lastY;
  149.  
  150. if (firstMouse) // initially set to true
  151. {
  152. lastX = xpos;
  153. lastY = ypos;
  154. firstMouse = false;
  155. }
  156.  
  157. sf::Vector2i localPosition = sf::Mouse::getPosition();
  158. lastX = localPosition.x;
  159. lastY = localPosition.y;
  160.  
  161. GLfloat xoffset = xpos - lastX;
  162. GLfloat yoffset = lastY - ypos;
  163.  
  164. //sf::Vector2i localPosition = sf::Mouse::getPosition();
  165. //lastX = localPosition.x;
  166. //lastY = localPosition.y;
  167.  
  168. GLdouble yaw = -90; //obrót względem osi Y
  169. GLdouble pitch = 0; //obrót względem osi X
  170.  
  171. GLfloat sensitivity = 0.1f;
  172. xoffset *= sensitivity;
  173. yoffset *= sensitivity;
  174.  
  175. yaw += xoffset;
  176. pitch += yoffset;
  177.  
  178. if (pitch > 89.0f)
  179. pitch = 89.0f;
  180. if (pitch < -89.0f)
  181. pitch = -89.0f;
  182.  
  183. glm::vec3 direction;
  184. direction.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
  185. direction.y = sin(glm::radians(pitch));
  186. direction.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
  187. return direction;
  188. }
  189. //
  190.  
  191.  
  192. //ZAJ4
  193. unsigned int texture1;
  194.  
  195. int main()
  196. {
  197.  
  198.  
  199.  
  200.  
  201.  
  202. sf::ContextSettings settings;
  203. settings.depthBits = 24;
  204. settings.stencilBits = 8;
  205.  
  206. // Okno renderingu
  207. sf::Window window(sf::VideoMode(800, 600, 32), "OpenGL", sf::Style::Titlebar | sf::Style::Close, settings);
  208. window.setMouseCursorGrabbed(true); //przechwycenie kursora myszy w oknie ------------
  209. window.setMouseCursorVisible(false); //ukrycie kursora myszy ---------------------
  210. window.setFramerateLimit(20);
  211.  
  212.  
  213. // Inicjalizacja GLEW
  214. glewExperimental = GL_TRUE;
  215. glewInit();
  216.  
  217. // Utworzenie VAO (Vertex Array Object)
  218. GLuint vao;
  219. glGenVertexArrays(1, &vao);
  220. glBindVertexArray(vao);
  221.  
  222. // Utworzenie VBO (Vertex Buffer Object)
  223. // i skopiowanie do niego danych wierzchołkowych
  224. GLuint vbo;
  225. glGenBuffers(1, &vbo);
  226.  
  227.  
  228.  
  229. glGenTextures(1, &texture1); // Generuje tekstury
  230. glBindTexture(GL_TEXTURE_2D, texture1); //Ustawienie tekstury jako bieżącej (powiązanie)
  231. // set the texture wrapping parameters
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  233. // set texture wrapping to GL_REPEAT (default wrapping method)
  234. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  235. // set texture filtering parameters
  236. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  237. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  238. // load image, create texture and generate mipmaps
  239. int width, height, nrChannels;
  240. stbi_set_flip_vertically_on_load(true); // tell stb_image.h to flip loaded texture's on the y-axis.
  241. unsigned char* data = stbi_load("D:\\GrafikaKomputerowa\\GKPomocOlka\\Java.png", &width, &height, &nrChannels, 0);
  242. if (data)
  243. {
  244. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
  245. glGenerateMipmap(GL_TEXTURE_2D);
  246. }
  247. else
  248. {
  249. std::cout << "Failed to load texture" << std::endl;
  250. }
  251. stbi_image_free(data);
  252.  
  253.  
  254. /*
  255.  
  256. ///Zmiana 2) - odwrócony trójkąt
  257. GLfloat* vertices = new GLfloat[]{
  258. -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,// Top-left
  259. 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,// Top-right
  260. 0.5f, -0.5f, 0.0f, 0.0f, 1.0f, // Bottom-right
  261.  
  262. 0.5f, -0.5f, 0.0f, 0.0f, 1.0f,// Bottom-right
  263. -0.5f, -0.5f, 1.0f, 1.0f, 1.0f,// Bottom-left
  264. -0.5f, 0.5f, 1.0f, 0.0f, 0.0f // Top-left
  265.  
  266. };
  267. */
  268. /*
  269.  
  270. float vertices[] = {
  271. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,0.0f,0.0f,
  272. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,1.0f,0.0f,
  273.  
  274. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,1.0f,1.0f,
  275. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,1.0f,1.0f,
  276.  
  277. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.0f,1.0f,
  278. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f,0.0f,0.0f,
  279.  
  280. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,0.0f,
  281. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,1.0f,0.0f,
  282. 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,1.0f,1.0f,
  283. 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, 0.0f,1.0f,1.0f,
  284. -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.0f,0.0f,1.0f,
  285. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,0.0f,
  286.  
  287. -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.0f,0.0f,
  288. -0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,
  289. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,1.0f,1.0f,
  290. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,1.0f,1.0f,
  291. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
  292. -0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.0f,0.0f,
  293.  
  294. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.0f,0.0f,
  295. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,
  296. 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,1.0f,1.0f,
  297. 0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,1.0f,1.0f,
  298. 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
  299. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,0.0f,0.0f,
  300.  
  301. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.0f,0.0f,
  302. 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,
  303. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,1.0f,1.0f,
  304. 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.0f,1.0f,1.0f,
  305. -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
  306. -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.0f,0.0f,
  307.  
  308. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.0f,0.0f,
  309. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f,1.0f,0.0f,
  310. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,1.0f,1.0f,
  311. 0.5f, 0.5f, 0.5f, 1.0f, 0.0f, 0.0f,1.0f,1.0f,
  312. -0.5f, 0.5f, 0.5f, 0.0f, 0.0f, 0.0f,0.0f,1.0f,
  313. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,0.0f,0.0f
  314. };
  315.  
  316.  
  317. */
  318. /*
  319. float vertices[] = {
  320. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  321. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,0.0f,
  322. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  323. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  324. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f,
  325. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  326.  
  327.  
  328. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  329. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,0.0f,
  330. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  331. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  332. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f,
  333. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  334.  
  335. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  336. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,0.0f,
  337. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  338. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  339. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f,
  340. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  341.  
  342. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  343. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,0.0f,
  344. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  345. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  346. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f,
  347. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  348.  
  349. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  350. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,0.0f,
  351. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  352. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  353. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f,
  354. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  355.  
  356. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f,
  357. 0.5f, -0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f,0.0f,
  358. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  359. 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.0f, 1.0f,1.0f,
  360. -0.5f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 0.0f,1.0f,
  361. -0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 0.0f,0.0f
  362. };
  363.  
  364.  
  365. */
  366.  
  367. // Read our .obj file
  368. std::vector< glm::vec3 > vertices;
  369. std::vector< glm::vec2 > uvs;
  370. std::vector< glm::vec3 > normals; // Won't be used at the moment.
  371. bool res = loadOBJ("Grass_block.obj", vertices, uvs, normals);
  372.  
  373. glBindBuffer(GL_ARRAY_BUFFER, vbo);
  374. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
  375.  
  376. // Utworzenie i skompilowanie shadera wierzchołków
  377. GLuint vertexShader =
  378. glCreateShader(GL_VERTEX_SHADER);
  379. glShaderSource(vertexShader, 1, &vertexSource, NULL);
  380. glCompileShader(vertexShader);
  381.  
  382. // Utworzenie i skompilowanie shadera fragmentów
  383. GLuint fragmentShader =
  384. glCreateShader(GL_FRAGMENT_SHADER);
  385. glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
  386. glCompileShader(fragmentShader);
  387.  
  388. // Zlinkowanie obu shaderów w jeden wspólny program
  389. GLuint shaderProgram = glCreateProgram();
  390. glAttachShader(shaderProgram, vertexShader);
  391. glAttachShader(shaderProgram, fragmentShader);
  392. glBindFragDataLocation(shaderProgram, 0, "outColor");
  393. glLinkProgram(shaderProgram);
  394. glUseProgram(shaderProgram);
  395. ///
  396. glm::vec3 lightPos(1.2f, 1.0f, 2.0f);
  397. GLint uniLightPos = glGetUniformLocation(shaderProgram, "lightPos");
  398. glUniform3fv(uniLightPos, 1, &lightPos[0]);
  399. //
  400. // Specifikacja formatu danych wierzchołkowych
  401. GLint posAttrib = glGetAttribLocation(shaderProgram, "position");
  402. glEnableVertexAttribArray(posAttrib);
  403. glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), 0);
  404. GLint colAttrib = glGetAttribLocation(shaderProgram, "color");
  405. glEnableVertexAttribArray(colAttrib);
  406. ///
  407. GLint NorAttrib = glGetAttribLocation(shaderProgram, "aNormal");
  408. glEnableVertexAttribArray(NorAttrib);
  409. glVertexAttribPointer(
  410. NorAttrib, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat)));
  411.  
  412.  
  413.  
  414. //3)Zepsuje tutaj w tym miejscu ostatni parametr i program zamiast się wysypać zmieni kolor figury w środku
  415.  
  416. glVertexAttribPointer(colAttrib, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (void*)(2 * sizeof(GLfloat)));
  417.  
  418. // Rozpoczęcie pętli zdarzeń
  419. bool running = true;
  420. //ZMIANY
  421. int punkty_ = 6;
  422. long prymityw = GL_TRIANGLES;
  423. GLfloat mouse_y = 500;
  424. GLfloat mouse_x = 600;
  425. //ZMIANY
  426.  
  427.  
  428.  
  429. //
  430. GLint vStatus;
  431. glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &vStatus);
  432. std::cout << "Vertex shader: ";
  433. if (vStatus == GL_TRUE) {
  434. std::cout << "OK" << std::endl;
  435. }
  436. else {
  437. std::cout << "EROOR" << std::endl;
  438. char vBuffer[128];
  439. glGetShaderInfoLog(vertexShader, 64, NULL, vBuffer);
  440. std::cout << vBuffer << std::endl;
  441. }
  442.  
  443. //
  444.  
  445.  
  446. //
  447.  
  448. glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &vStatus);
  449. std::cout << "Fragment shader: ";
  450. if (vStatus == GL_TRUE) {
  451. std::cout << "OK" << std::endl;
  452. }
  453. else {
  454. std::cout << "EROOR" << std::endl;
  455. char vBuffer[128];
  456. glGetShaderInfoLog(fragmentShader, 64, NULL, vBuffer);
  457. std::cout << vBuffer << std::endl;
  458. }
  459.  
  460. //
  461.  
  462. glm::mat4 model = glm::mat4(1.0f);
  463.  
  464. model = glm::rotate(model, glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f));
  465.  
  466. GLint uniTrans = glGetUniformLocation(shaderProgram, "model");
  467. glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
  468.  
  469. glm::mat4 view;
  470. view = glm::lookAt(glm::vec3(0.0f, 0.0f, 3.0f),
  471. glm::vec3(0.0f, 0.0f, 0.0f),
  472. glm::vec3(0.0f, 1.0f, 0.0f));
  473.  
  474. GLint uniView = glGetUniformLocation(shaderProgram, "view");
  475. glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  476. glm::mat4 proj = glm::perspective(glm::radians(45.0f), 800.0f / 800.0f, 0.06f, 100.0f);
  477. GLint uniProj = glGetUniformLocation(shaderProgram, "proj");
  478. glUniformMatrix4fv(uniProj, 1, GL_FALSE, glm::value_ptr(proj));
  479.  
  480. view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  481. float cameraSpeed = 0.1f;
  482. float obrot = 0.3f;
  483.  
  484.  
  485.  
  486. sf::Clock clock;
  487. sf::Time time;
  488.  
  489.  
  490.  
  491. while (running) {
  492. sf::Event windowEvent;
  493. time = clock.getElapsedTime();
  494. clock.restart();
  495.  
  496. float cameraSpeed = 0.02f * time.asMicroseconds();
  497.  
  498. while (window.pollEvent(windowEvent)) {
  499. switch (windowEvent.type) {
  500. case sf::Event::Closed:
  501. running = false;
  502. break;
  503.  
  504. case sf::Event::KeyPressed:
  505. {
  506. switch (windowEvent.key.code)
  507. {
  508. case sf::Keyboard::Num1:
  509. prymityw = GL_POINTS;
  510. break;
  511. case sf::Keyboard::Num2:
  512. prymityw = GL_LINES;
  513. break;
  514. case sf::Keyboard::Num3:
  515. prymityw = GL_LINE_STRIP;
  516. break;
  517. case sf::Keyboard::Num4:
  518. prymityw = GL_LINE_LOOP;
  519. break;
  520. case sf::Keyboard::Num5:
  521. prymityw = GL_TRIANGLES;
  522. break;
  523. case sf::Keyboard::Num6:
  524. prymityw = GL_TRIANGLE_STRIP;
  525. break;
  526. case sf::Keyboard::Num7:
  527. prymityw = GL_TRIANGLE_FAN;
  528. break;
  529. case sf::Keyboard::Num8:
  530. prymityw = GL_QUADS;
  531. break;
  532. case sf::Keyboard::Num9:
  533. prymityw = GL_QUAD_STRIP;
  534. break;
  535. case sf::Keyboard::Num0:
  536. prymityw = GL_POLYGON;
  537. break;
  538. case sf::Keyboard::Left:
  539. cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  540. view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  541. glGetUniformLocation(shaderProgram, "view");
  542. glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  543. break;
  544. case sf::Keyboard::Right:
  545. cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * cameraSpeed;
  546. view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  547. glGetUniformLocation(shaderProgram, "view");
  548. glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  549. break;
  550. case sf::Keyboard::Up:
  551. cameraPos -= cameraSpeed * cameraUp;
  552. view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  553. glGetUniformLocation(shaderProgram, "view");
  554. glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  555. break;
  556.  
  557. case sf::Keyboard::Down:
  558. cameraPos += cameraSpeed * cameraUp;
  559. view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  560. glGetUniformLocation(shaderProgram, "view");
  561. glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  562. break;
  563. case sf::Keyboard::R:
  564. model = glm::rotate(model, glm::radians(30.0f) + obrot, glm::vec3(0.0f, 0.0f, 1.0f));
  565. glGetUniformLocation(shaderProgram, "model");
  566. glUniformMatrix4fv(uniTrans, 1, GL_FALSE, glm::value_ptr(model));
  567. break;
  568. case sf::Keyboard::Escape:
  569. window.close();
  570. return 0;
  571. break;
  572.  
  573.  
  574.  
  575.  
  576.  
  577. }
  578. case sf::Event::MouseMoved:
  579. glm::vec3 direction;
  580. direction = ustawKamereMysz(&window, mouse_x, mouse_y);
  581. cameraFront = glm::normalize(direction);
  582. view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
  583. glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view));
  584.  
  585. break;
  586. }
  587. }
  588. // Nadanie scenie koloru czarnego
  589. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  590. glClear(GL_COLOR_BUFFER_BIT);
  591.  
  592. // Narysowanie trójkąta na podstawie 3 wierzchołków
  593. ///Zmiana 2.1 z 3 wierzchołków na 6 wierzchołków
  594. ///
  595. //glBindBuffer(GL_ARRAY_BUFFER, buffer);
  596.  
  597.  
  598. GLint TexCoord = glGetAttribLocation(shaderProgram, "aTexCoord");
  599. glEnableVertexAttribArray(TexCoord);
  600. glVertexAttribPointer(TexCoord, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (void*)(6 *
  601. sizeof(GLfloat)));
  602.  
  603. glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
  604. glBindTexture(GL_TEXTURE_2D, texture1);
  605. glDrawArrays(prymityw, 0, punkty_);
  606.  
  607. // Wymiana buforów tylni/przedni
  608. window.display();
  609. }
  610. }
  611. // Kasowanie programu i czyszczenie buforów
  612. glDeleteProgram(shaderProgram);
  613. glDeleteShader(fragmentShader);
  614. glDeleteShader(vertexShader);
  615. glDeleteBuffers(1, &vbo);
  616. glDeleteVertexArrays(1, &vao);
  617. // Zamknięcie okna renderingu
  618. window.close();
  619. return 0;
  620.  
  621. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement