Advertisement
Guest User

Untitled

a guest
May 25th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <GL/glew.h>
  6. #include <GLFW/glfw3.h>
  7. #include <cmath>
  8. #include <math.h>
  9.  
  10.  
  11. GLfloat fill = GL_LINE;
  12. float Rotate_x = 0.0f, Rotate_y = 0.0f, Rotate_z = 0.0f;
  13. float x = 0, y = 0, z = 0;
  14. float scrollX = 0.4, scrollY = 0.4, scrollZ = 0.4;
  15.  
  16. float fz = 0.5;
  17. GLfloat phi = asin(fz / sqrt(2.0));
  18. GLfloat teta = asin(fz / sqrt(2.0 - fz * fz));
  19.  
  20. GLfloat matrix[4][4] = {
  21. {1,0, 0, 0},
  22. {0,1, 0, 0},
  23. {0,0,1, 0},
  24. {0,0,0,1},
  25. };
  26.  
  27. std::string vertexShader = "#version 430\n"
  28. "in vec3 pos;"
  29. "void main() {"
  30. "gl_Position = vec4(pos, 1);"
  31. "}";
  32.  
  33. std::string fragmentShader = "#version 430\n"
  34. "void main() {"
  35. "gl_FragColor = vec4(1, 0, 0, 1);"
  36. "}";
  37.  
  38. GLuint loadDataInBuffers()
  39. {
  40. GLfloat vertices[] = { // vertex coordinates
  41. 0.5f, -0.5f, -0.5f,
  42. 0.5f, 0.5f, -0.5f,
  43. -0.5f, 0.5f, -0.5f,
  44. -0.5f, -0.5f, -0.5f,
  45.  
  46. 0.5f, -0.5f, 0.5f,
  47. 0.5f, 0.5f, 0.5f,
  48. -0.5f, 0.5f, 0.5f,
  49. -0.5f, -0.5f, 0.5f,
  50.  
  51. 0.5f, -0.5f, -0.5f,
  52. 0.5f, 0.5f, -0.5f,
  53. 0.5f, 0.5f, 0.5f,
  54. 0.5f, -0.5f, 0.5f,
  55.  
  56. -0.5f, -0.5f, 0.5f,
  57. -0.5f, 0.5f, 0.5f,
  58. -0.5f, 0.5f, -0.5f,
  59. -0.5f, -0.5f, -0.5f,
  60.  
  61. 0.5f, 0.5f, 0.5f,
  62. 0.5f, 0.5f, -0.5f,
  63. -0.5f, 0.5f, -0.5f,
  64. -0.5f, 0.5f, 0.5f,
  65.  
  66. 0.5f, -0.5f, -0.5f,
  67. 0.5f, -0.5f, 0.5f,
  68. -0.5f, -0.5f, 0.5f,
  69. -0.5f, -0.5f, -0.5f,
  70.  
  71.  
  72. /* {0.5f, -0.5f, -0.5f},
  73. {0.5f, 0.5f, -0.5f},
  74. {-0.5f, 0.5f, -0.5f},
  75. {-0.5f, -0.5f, -0.5},
  76.  
  77. {0.5, -0.5, 0.5 },
  78. {0.5, 0.5, 0.5},
  79. {-0.5, 0.5, 0.5},
  80. {-0.5, -0.5, 0.5},
  81.  
  82. {0.5, -0.5, -0.5},
  83. {0.5, 0.5, -0.5},
  84. {0.5, 0.5, 0.5},
  85. {0.5, -0.5, 0.5},
  86.  
  87. {-0.5, -0.5, 0.5 },
  88. {-0.5, 0.5, 0.5},
  89. {-0.5, 0.5, -0.5},
  90. {-0.5, -0.5, -0.5},
  91.  
  92. {0.5, 0.5, 0.5 },
  93. {0.5, 0.5, -0.5},
  94. {-0.5, 0.5, -0.5},
  95. {-0.5, 0.5, 0.5},
  96.  
  97. {0.5, -0.5, -0.5 },
  98. {0.5, -0.5, 0.5},
  99. {-0.5, -0.5, 0.5},
  100. {-0.5, -0.5, -0.5},*/
  101. };
  102.  
  103. GLuint vboId;
  104.  
  105. // allocate buffer sapce and pass data to it
  106. glGenBuffers(1, &vboId);
  107. glBindBuffer(GL_ARRAY_BUFFER, vboId);
  108. glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
  109.  
  110. // unbind the active buffer
  111. glBindBuffer(GL_ARRAY_BUFFER, 0);
  112.  
  113. return vboId;
  114. }
  115.  
  116. GLuint linkProgram(GLuint vertexShaderId, GLuint fragmentShaderId)
  117. {
  118. GLuint programId = glCreateProgram(); // crate a program
  119.  
  120. if (programId == 0) {
  121. std::cout << "Error Creating Shader Program";
  122. return 0;
  123. }
  124.  
  125. // Attach both the shaders to it
  126. glAttachShader(programId, vertexShaderId);
  127. glAttachShader(programId, fragmentShaderId);
  128.  
  129. // Create executable of this program
  130. glLinkProgram(programId);
  131.  
  132. GLint linkStatus;
  133.  
  134. // Get the link status for this program
  135. glGetProgramiv(programId, GL_LINK_STATUS, &linkStatus);
  136.  
  137. if (!linkStatus) { // If the linking failed
  138. std::cout << "Error Linking program";
  139. glDetachShader(programId, vertexShaderId);
  140. glDetachShader(programId, fragmentShaderId);
  141. glDeleteProgram(programId);
  142.  
  143. return 0;
  144. }
  145.  
  146. return programId;
  147. }
  148.  
  149. GLuint compileShaders(std::string shader, GLenum type)
  150. {
  151.  
  152. const char* shaderCode = shader.c_str();
  153. GLuint shaderId = glCreateShader(type);
  154.  
  155. if (shaderId == 0) { // Error: Cannot create shader object
  156. std::cout << "Error creating shaders";
  157. return 0;
  158. }
  159.  
  160. // Attach source code to this object
  161. glShaderSource(shaderId, 1, &shaderCode, NULL);
  162. glCompileShader(shaderId); // compile the shader object
  163.  
  164. GLint compileStatus;
  165.  
  166. // check for compilation status
  167. glGetShaderiv(shaderId, GL_COMPILE_STATUS, &compileStatus);
  168.  
  169. if (!compileStatus) { // If compilation was not successfull
  170. int length;
  171. glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &length);
  172. char* cMessage = new char[length];
  173.  
  174. // Get additional information
  175. glGetShaderInfoLog(shaderId, length, &length, cMessage);
  176. std::cout << "Cannot Compile Shader: " << cMessage;
  177. delete[] cMessage;
  178. glDeleteShader(shaderId);
  179. return 0;
  180. }
  181.  
  182. return shaderId;
  183. }
  184.  
  185. void init()
  186. {
  187. // clear the framebuffer each frame with black color
  188. glClearColor(0, 0, 0, 0);
  189.  
  190. GLuint vboId = loadDataInBuffers();
  191.  
  192. GLuint vShaderId = compileShaders(vertexShader, GL_VERTEX_SHADER);
  193. GLuint fShaderId = compileShaders(fragmentShader, GL_FRAGMENT_SHADER);
  194.  
  195. GLuint programId = linkProgram(vShaderId, fShaderId);
  196. //
  197. // Get the 'pos' variable location inside this program
  198. GLuint posAttributePosition = glGetAttribLocation(programId, "pos");
  199.  
  200. GLuint vaoId;
  201. glGenVertexArrays(1, &vaoId); // Generate VAO
  202.  
  203. // Bind it so that rest of vao operations affect this vao
  204. glBindVertexArray(vaoId);
  205.  
  206. // buffer from which 'pos' will recive its data and the format of that data
  207. glBindBuffer(GL_ARRAY_BUFFER, vboId);
  208. glVertexAttribPointer(posAttributePosition, 3, GL_FLOAT, false, 0, 0);
  209.  
  210. // Enable this attribute array linked to 'pos'
  211. glEnableVertexAttribArray(posAttributePosition);
  212. // Use this program for rendering.
  213. //glUseProgram(programId);
  214. }
  215.  
  216.  
  217. void display(GLFWwindow *window)
  218. {
  219. // clear the color buffer before each drawing
  220. glClear(GL_COLOR_BUFFER_BIT);
  221. glPolygonMode(GL_FRONT_AND_BACK, fill);
  222.  
  223. // draw triangles starting from index 0 and
  224. // using 3 indices
  225. glDrawArrays(GL_QUADS, 0, 24);
  226. // swap the buffers and hence show the buffers
  227. // content to the screen
  228. //glfwPollEvents();
  229. //glfwSwapBuffers(window);
  230. }
  231.  
  232. void keyCallback(GLFWwindow * window, int key, int scancode, int action, int mode) {
  233. if (action == GLFW_PRESS || action == GLFW_REPEAT) {
  234. switch (key) {
  235. case GLFW_KEY_RIGHT:
  236. Rotate_x -= 10;
  237. break;
  238. case GLFW_KEY_LEFT:
  239. Rotate_x += 10;
  240. break;
  241. case GLFW_KEY_DOWN:
  242. Rotate_y -= 10;
  243. break;
  244. case GLFW_KEY_UP:
  245. Rotate_y += 10;
  246. break;
  247. case GLFW_KEY_Q:
  248. Rotate_z += 10;
  249. break;
  250. case GLFW_KEY_E:
  251. Rotate_z -= 10;
  252. break;
  253. case GLFW_KEY_A:
  254. x -= 0.1;
  255. break;
  256. case GLFW_KEY_D:
  257. x += 0.1;
  258. break;
  259. case GLFW_KEY_W:
  260. y += 0.1;
  261. break;
  262. case GLFW_KEY_S:
  263. y -= 0.1;
  264. break;
  265. case GLFW_KEY_Z:
  266. scrollX += 0.1;
  267. scrollY += 0.1;
  268. scrollZ += 0.1;
  269. break;
  270. case GLFW_KEY_X:
  271. scrollX -= 0.1;
  272. scrollY -= 0.1;
  273. scrollZ -= 0.1;
  274. break;
  275. case GLFW_KEY_P:
  276. matrix[0][0] = cos(phi); matrix[0][1] = 0; matrix[0][2] = sin(phi); matrix[0][3] = 0;
  277. matrix[1][0] = sin(phi)*sin(teta); matrix[1][1] = cos(teta); matrix[1][2] = -cos(phi)*sin(teta); matrix[1][3] = 0;
  278. matrix[2][0] = sin(phi)*cos(teta); matrix[2][1] = -sin(teta); matrix[2][2] = -cos(phi)*cos(teta); matrix[2][3] = 0;
  279. matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
  280. break;
  281. case GLFW_KEY_O:
  282. matrix[0][0] = 1; matrix[0][1] = 0; matrix[0][2] = 0; matrix[0][3] = 0;
  283. matrix[1][0] = 0; matrix[1][1] = 1; matrix[1][2] = 0; matrix[1][3] = 0;
  284. matrix[2][0] = 0; matrix[2][1] = 0; matrix[2][2] = 1; matrix[2][3] = 0;
  285. matrix[3][0] = 0; matrix[3][1] = 0; matrix[3][2] = 0; matrix[3][3] = 1;
  286. break;
  287. }
  288. }
  289. }
  290.  
  291. void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
  292. if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
  293. fill = GL_FILL;
  294. }
  295.  
  296. if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) {
  297. fill = GL_LINE;
  298. }
  299. }
  300.  
  301.  
  302. int main(void) {
  303. if (!glfwInit()) {
  304. return -1;
  305. }
  306.  
  307. int width = 800, height = 800;
  308. GLFWwindow *window = glfwCreateWindow(800, 800, "Cube", NULL, NULL);
  309. glfwMakeContextCurrent(window);
  310. glViewport(0, 0, width, height);
  311. glEnable(GL_DEPTH_TEST);
  312.  
  313. glfwSetKeyCallback(window, keyCallback);
  314. glfwSetMouseButtonCallback(window, mouse_button_callback);
  315.  
  316.  
  317. while (!glfwWindowShouldClose(window)) {
  318. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  319.  
  320. glMatrixMode(GL_PROJECTION);
  321. glLoadIdentity();
  322. glMultMatrixf(*matrix);
  323. glMatrixMode(GL_MODELVIEW);
  324. glLoadIdentity();
  325.  
  326. // glewInit();
  327. glewInit();
  328. init();
  329.  
  330. glTranslatef(x, y, z);
  331. glRotatef(Rotate_x, 0, 1, 0);
  332. glRotatef(Rotate_y, 1, 0, 0);
  333. glRotatef(Rotate_z, 0, 0, 1);
  334. glScaled(scrollX, scrollY, scrollZ);
  335. //Draw_Tor();
  336.  
  337.  
  338.  
  339. display(window);
  340. //Draw_small();
  341. glfwPollEvents();
  342. glfwSwapBuffers(window);
  343. }
  344.  
  345. glfwDestroyWindow(window);
  346. glfwTerminate();
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement