Guest User

Untitled

a guest
Jan 19th, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.27 KB | None | 0 0
  1. /* put these on vShader.vs & fShader.fs
  2. ________________________________________________________________
  3. vShader.vs :
  4. #version 330 core
  5. layout (location = 0) in vec3 aPos;
  6. layout (location = 1) in vec3 aColor;
  7.  
  8. uniform float xPos;
  9. uniform float yPos;
  10. out vec3 ourColor;
  11.  
  12. void main()
  13. {
  14.  
  15. gl_Position = vec4(aPos.x + xPos,aPos.y + yPos,aPos.z, 1.0);
  16. ourColor = aColor;
  17. }
  18. ________________________________________________________________
  19. fShader.fs :
  20. #version 330 core
  21. out vec4 FragColor;
  22.  
  23. in vec3 ourColor;
  24.  
  25. void main()
  26. {
  27. FragColor = vec4(ourColor, 1.0f);
  28. }
  29. ________________________________________________________________
  30. */
  31. #include <iostream>
  32. #include <glad\glad.h>
  33. #include <GLFW\glfw3.h>
  34. #include <GLFW\shader_s.h>
  35. bool pressedOnce = false;
  36. float xOffset = 0.0f;
  37. float yOffset = 0.0f;
  38. void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  39. {
  40. if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_PRESS)
  41. pressedOnce = true;
  42. if (glfwGetKey(window, GLFW_KEY_ENTER) == GLFW_RELEASE && pressedOnce == true)
  43. glfwSetWindowShouldClose(window, true);
  44. if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
  45. yOffset += 5;
  46. if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
  47. yOffset -= 5;
  48. if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
  49. xOffset += 5;
  50. if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
  51. xOffset -= 5;
  52. }
  53. void GLAD_Check()
  54. {
  55. if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
  56. std::cout << "Failed to initialize GLAD" << std::endl;
  57. }
  58. void Resizer(GLFWwindow *window, int width , int height)
  59. {
  60. glViewport(0, 0, width, height);
  61. }
  62.  
  63. int main()
  64. {
  65. // Setup ------------------------------------
  66. // window*********
  67. glfwInit();
  68. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  69. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  70. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  71. GLFWwindow *window = glfwCreateWindow(800, 600, "Shaders", NULL, NULL);
  72. glfwMakeContextCurrent(window);
  73. GLAD_Check();
  74. glViewport(0, 0, 800, 600);
  75. glfwSetFramebufferSizeCallback(window, Resizer);
  76.  
  77. float vertcies[] =
  78. { // vertcies // color
  79. 0.0f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, // top middle
  80. - 0.5f, -0.5f, 1.0f, 0.0f, 1.0f, 0.0f,// bot left
  81. 0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 1.0f// bot right
  82. };
  83.  
  84. // VBO & VAO ********
  85. unsigned int VBO;
  86. glGenBuffers(1, &VBO);
  87. glBindBuffer(GL_ARRAY_BUFFER, VBO);
  88. glBufferData(GL_ARRAY_BUFFER, sizeof(vertcies), vertcies,GL_STATIC_DRAW);
  89. glCompileShader(VBO);
  90.  
  91. unsigned int VAO;
  92. glGenVertexArrays(1, &VAO);
  93. glBindVertexArray(VAO);
  94. glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
  95. glEnableVertexAttribArray(0);
  96. glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
  97. glEnableVertexAttribArray(1);
  98. Shader programShader("Shaders/vShader.vs", "Shaders/fShader.fs"); // fragment/vertex shader Path
  99.  
  100. while (!glfwWindowShouldClose(window))
  101. {
  102. glfwSetKeyCallback(window, key_callback);
  103. glClear(GL_COLOR_BUFFER_BIT);
  104. glClearColor(0.1f, 0.3f, 0.6f,1.0f);
  105. programShader.use();
  106. glUniform1f(glGetUniformLocation(programShader.ID, "xPos"), xOffset / 800);
  107. glUniform1f(glGetUniformLocation(programShader.ID, "yPos"), yOffset / 600);
  108. glBindVertexArray(VAO);
  109. glDrawArrays(GL_TRIANGLES,0,3);
  110. glfwPollEvents();
  111. glfwSwapBuffers(window);
  112. }
  113. glfwTerminate();
  114. return 0;
  115. }
Add Comment
Please, Sign In to add comment