Advertisement
Guest User

main file

a guest
Jun 18th, 2021
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.06 KB | None | 0 0
  1.  
  2. #include "Dependencies\glew\glew.h"
  3. #include "Dependencies\freeglut\freeglut.h"
  4. #include "Dependencies\soil\SOIL.h"
  5.  
  6. #include "glm/glm.hpp"
  7. #include "glm/gtc/matrix_transform.hpp"
  8. #include "glm/gtc/type_ptr.hpp"
  9.  
  10. #include <iostream>
  11.  
  12. #include "ShaderLoader.h";
  13. #include "Camera.h"
  14.  
  15. #define BUTTON_UP 0
  16. #define BUTTON_DOWN 1
  17.  
  18. GLuint program;
  19. GLuint vao, vbo, ebo;
  20. GLuint texture, texture2;
  21.  
  22. unsigned char keyState[255];
  23.  
  24.  
  25. glm::vec3 vPosTrans = glm::vec3(-0.50f, -0.50f, 0.0f);//source
  26. glm::vec3 vPosInit = vPosTrans;
  27. glm::vec3 vPosDest = glm::vec3(0.50f, 0.50f, 0.0f);//destination
  28. glm::vec3 vCurrentPos = vPosTrans;
  29.  
  30. bool Dest1or2 = true;
  31.  
  32. const GLfloat WIDTH = 800.0f, HEIGHT = 600.0f;
  33.  
  34. Camera* camera;
  35.  
  36. void init(){
  37. camera = new Camera(180.0f, WIDTH, HEIGHT, 0.0f, 100.0f);
  38.  
  39. ShaderLoader shaderLoader;
  40. program = shaderLoader.CreateProgram("CoordSystem_Texture_QUAD.vs", "CoordSystem_Texture_QUAD.fs");
  41.  
  42. glEnable(GL_DEPTH_TEST);
  43.  
  44. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
  45.  
  46. // Set up vertex data (and buffer(s)) and attribute pointers
  47. GLfloat vertices[] = {
  48.  
  49. //position //color //texture coord //Normals
  50. -1.01f, -1.01f, -1.01f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
  51. -1.01f, 1.01f, -1.01f, 0.0f, 1.0f, -0.0f, 0.0f, 0.0f,
  52. 1.01f, 1.01f, -1.01f, 1.0f, 0.0f, -0.0f, 1.0f, 0.0f,
  53. 1.01f, -1.01f, -1.01f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  54.  
  55. // Fill in the back face vertex data.
  56. -1.01f, -1.01f, 1.01f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  57. 1.01f, -1.01f, 1.01f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f,
  58. 1.01f, 1.01f, 1.01f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  59. -1.01f, 1.01f, 1.01f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  60.  
  61. // Fill in the top face vertex data.
  62. -1.01f, 1.01f, -1.01f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  63. -1.01f, 1.01f, 1.01f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  64. 1.01f, 1.01f, 1.01f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
  65. 1.01f, 1.01f, -1.01f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  66.  
  67. // Fill in the bottom face vertex data.
  68. -1.01f, -1.01f, -1.01f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  69. 1.01f, -1.01f, -1.01f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f,
  70. 1.01f, -1.01f, 1.01f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  71. -1.01f, -1.01f, 1.01f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f,
  72.  
  73. // Fill in the left face vertex data.
  74. -1.01f, -1.01f, 1.01f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  75. -1.01f, 1.01f, 1.01f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  76. -1.01f, 1.01f, -1.01f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
  77. -1.01f, -1.01f, -1.01f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f,
  78.  
  79. // Fill in the right face vertex data.
  80. 1.01f, -1.01f, -1.01f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f,
  81. 1.01f, 1.01f, -1.01f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  82. 1.01f, 1.01f, 1.01f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f,
  83. 1.01f, -1.01f, 1.01f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
  84. };
  85.  
  86. GLuint indices[] = {
  87. // front
  88. 0, 1, 2,
  89. 0, 2, 3,
  90. // top
  91. 4, 5, 6,
  92. 4, 6, 7,
  93. // back
  94. 8, 9, 10,
  95. 8, 10, 11,
  96. // bottom
  97. 12, 13, 14,
  98. 12, 14, 15,
  99. // left
  100. 16, 17, 18,
  101. 16, 18, 19,
  102. // right
  103. 20, 21, 22,
  104. 20, 22, 23,
  105. };
  106.  
  107. //** VAO **
  108. // Generate vertex arrow object
  109. glGenVertexArrays(1, &vao);
  110. // Bind the Vertex Array Object to openGl context
  111. glBindVertexArray(vao);//otherwise glVertexAttribPointer
  112.  
  113. //** VBO **
  114. // Then bind and set vertex buffer(s).
  115. // First paramter is how many buffers we have to create
  116. glGenBuffers(1, &vbo);
  117.  
  118. // bind VBO to binding point, here it is GL_ARRAY_BUFFER
  119. // there are other binding points
  120. glBindBuffer(GL_ARRAY_BUFFER, vbo);//bind to context
  121. glBufferData(GL_ARRAY_BUFFER,
  122. sizeof(vertices),// GPU need to know how much memory needs to be allocated
  123. vertices,//copy data to GPU
  124. GL_STATIC_DRAW);// How to use the buffer - buffer is created and modified once
  125.  
  126. //** EBO **
  127. glGenBuffers(1, &ebo);
  128. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
  129. glBufferData(GL_ELEMENT_ARRAY_BUFFER,
  130. sizeof(indices),
  131. indices,
  132. GL_STATIC_DRAW);
  133.  
  134.  
  135. // ** Attributes **
  136.  
  137. //** Vertex Attribute **
  138. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
  139. glEnableVertexAttribArray(0);
  140.  
  141. //** Color Attribute **
  142. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
  143. glEnableVertexAttribArray(1);
  144.  
  145. //** TexCoord attribute **
  146. glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
  147. glEnableVertexAttribArray(2);
  148.  
  149. // It's always a good thing to unbind any buffer/array to prevent strange bugs
  150. glBindBuffer(GL_ARRAY_BUFFER, 0);
  151. glBindVertexArray(0);
  152.  
  153. //** Load and bind texture 1
  154. //--------------------------
  155. glGenTextures(1, &texture);
  156. glBindTexture(GL_TEXTURE_2D, texture);
  157.  
  158.  
  159. //** loadImage and create texture
  160. // Load image, create texture and generate mipmaps
  161. int width, height;
  162. unsigned char* image = SOIL_load_image("wall.jpg", &width, &height, 0, SOIL_LOAD_RGB);
  163. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
  164. glGenerateMipmap(GL_TEXTURE_2D);
  165. SOIL_free_image_data(image);
  166. glBindTexture(GL_TEXTURE_2D, 0);
  167.  
  168.  
  169. // ** Load and Bind Texture 2
  170. //---------------------------
  171. glGenTextures(1, &texture2);
  172. glBindTexture(GL_TEXTURE_2D, texture2);
  173.  
  174.  
  175. //** loadImage and create texture
  176. // Load image, create texture and generate mipmaps
  177. unsigned char* image2 = SOIL_load_image("awesomeface.png", &width, &height, 0, SOIL_LOAD_RGB);
  178. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
  179. glGenerateMipmap(GL_TEXTURE_2D);
  180. SOIL_free_image_data(image2);
  181. glBindTexture(GL_TEXTURE_2D, 0);
  182.  
  183.  
  184. // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
  185. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  186. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  187. // Set texture filtering parameters
  188. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  189. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  190.  
  191. }
  192.  
  193. void render(){
  194.  
  195. glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
  196. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  197.  
  198. // Draw our first triangle
  199. glUseProgram(program);
  200.  
  201. //bind
  202. glActiveTexture(GL_TEXTURE0);
  203. glBindTexture(GL_TEXTURE_2D, texture);
  204. glUniform1i(glGetUniformLocation(program, "Texture"), 0);
  205.  
  206. glActiveTexture(GL_TEXTURE1);
  207. glBindTexture(GL_TEXTURE_2D, texture2);
  208. glUniform1i(glGetUniformLocation(program, "Texture2"), 1);
  209.  
  210. glBindVertexArray(vao);
  211.  
  212. // Draw Elements intead of vertex array
  213. glDrawArrays(GL_QUADS, 0, 24);
  214. //glDrawElements(GL_TRIANGLES, 24, GL_UNSIGNED_INT, 0);
  215. glBindVertexArray(0);
  216.  
  217. glutSwapBuffers();
  218. }
  219.  
  220. void update(){
  221. GLfloat cameraSpeed = 0.05f;
  222. camera->setCameraSpeed(cameraSpeed);
  223.  
  224. //currentTime uniform
  225. GLfloat currentTime = glutGet(GLUT_ELAPSED_TIME);
  226. currentTime = currentTime / 1000;
  227. GLint currentTimeLocation = glGetUniformLocation(program, "currentTime");
  228. glUniform1f(currentTimeLocation, currentTime);
  229.  
  230. //transforms
  231. glm::mat4 transform = glm::mat4(1.0f);
  232. glm::vec3 vScaleVec = glm::vec3(0.51f, 0.51f, 0.0f);
  233.  
  234. glm::mat4 model = glm::mat4(1.0);
  235.  
  236. if (keyState[(unsigned char)'w'] == BUTTON_DOWN) {
  237. camera->moveForward();
  238. std::cout << "e";
  239. }
  240. if (keyState[(unsigned char)'s'] == BUTTON_DOWN) {
  241. camera->moveBack();
  242. std::cout << "A";
  243. }
  244.  
  245. camera->Update();
  246.  
  247. //glm::mat4 projection = glm::perspective(45.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 0.1f, 100.0f);
  248. glm::mat4 projection = camera->getProjectionMatrix();
  249.  
  250. glm::mat4 view = glm::lookAt(camera->getCameraPos(), camera->getCameraPos() + camera->getCameraFront(), camera->getCameraUp());
  251.  
  252. //glm::mat4 view = glm::lookAt(glm::vec3(1, 1, 0), glm::vec3(0, 1, 0), glm::vec3(0, 0, 1));
  253.  
  254. GLfloat radius = 3.5f;
  255. GLfloat camX = sin(currentTime) * radius;
  256. GLfloat camZ = cos(currentTime) * radius;
  257.  
  258. //glm::mat4 view = glm::lookAt(
  259. // glm::vec3(camX, 0.0, camZ),
  260. // glm::vec3(0.0, 0.0, 0.0),
  261. // glm::vec3(0.0, 1.0, 0.0));
  262.  
  263. transform = glm::scale(transform, vScaleVec);
  264. glm::vec3(0.65f, 0.65f, 0.65f);
  265. //transform = glm::rotate(transform, 90.0f, glm::vec3(0.0, 0.0, 1.0));
  266. if (vCurrentPos == vPosDest && Dest1or2 == true )
  267. {
  268. Dest1or2 = false;
  269. }
  270. else if (vCurrentPos == vPosTrans && Dest1or2 == false)
  271. {
  272. Dest1or2 = true;
  273. }
  274. if(Dest1or2)
  275. vCurrentPos = glm::mix(vCurrentPos, vPosDest, currentTime * 0.01f);
  276. else
  277. vCurrentPos = glm::mix(vCurrentPos, vPosTrans, currentTime*0.01f);
  278.  
  279. //transform = glm::translate(transform, vCurrentPos);
  280. //transform = glm::translate(transform, glm::vec3(-0.51f, -0.51f, 0.0f));
  281. //transform = glm::rotate(transform, ((GLfloat)currentTime / 100) * 90.0f, glm::vec3(0.0f, 0.0f, 1.0f));
  282.  
  283. // RTS
  284. GLuint transformLoc = glGetUniformLocation(program, "transform");
  285. glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(transform));
  286.  
  287. GLint modelLoc = glGetUniformLocation(program, "model");
  288. GLint viewLoc = glGetUniformLocation(program, "view");
  289. GLint projLoc = glGetUniformLocation(program, "projection");
  290.  
  291. // Pass them to the shaders
  292. glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
  293. glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(camera->getViewMatrix()));
  294. glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(camera->getProjectionMatrix()));
  295. //glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(view));
  296. //glUniformMatrix4fv(projLoc, 1, GL_FALSE, glm::value_ptr(projection));
  297.  
  298.  
  299. glutPostRedisplay();
  300. }
  301.  
  302. void keyboard(unsigned char key, int x, int y) {
  303. keyState[key] = BUTTON_DOWN;
  304.  
  305. //printf("key pressed: %d \n", key);
  306. }
  307.  
  308. void keyboard_up(unsigned char key, int x, int y) {
  309. keyState[key] = BUTTON_UP;
  310. }
  311.  
  312. int main(int argc, char **argv){
  313.  
  314. // init glut
  315. glutInit(&argc, argv);
  316. glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
  317.  
  318. glutInitWindowPosition(300, 200);
  319. glutInitWindowSize(800, 600);
  320. glutCreateWindow("QUAD EBO");
  321.  
  322. //init GLEW
  323. glewInit();
  324.  
  325. init();
  326.  
  327. //clear
  328. glClearColor(1.0, 0.0, 0.0, 1.0);//clear red
  329.  
  330. // register callbacks
  331. glutDisplayFunc(render);
  332.  
  333. glutKeyboardFunc(keyboard);
  334. glutKeyboardUpFunc(keyboard_up);
  335.  
  336. glutIdleFunc(update);
  337.  
  338. glutMainLoop();
  339.  
  340. return 0;
  341. }
  342.  
  343.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement