Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.41 KB | None | 0 0
  1. // Vertex.vert
  2. #version 150 core
  3.  
  4. in vec3 in_position;
  5. in vec2 in_texture;
  6.  
  7. out vec2 Texture;
  8.  
  9. uniform mat4 in_model;
  10. uniform mat4 in_view;
  11. uniform mat4 in_projection;
  12.  
  13. void main()
  14. {
  15. gl_Position = in_projection * in_view * in_model * vec4(in_position, 1.0);
  16. Texture = in_texture;
  17. }
  18.  
  19. // Fragment.frag
  20. #version 150 core
  21.  
  22. in vec2 Texture;
  23.  
  24. out vec4 Colour;
  25.  
  26. uniform sampler2D Sampler2D;
  27.  
  28. void main()
  29. {
  30. Colour = texture(Sampler2D, Texture);
  31. }
  32.  
  33. // Source.cpp
  34. #include <cfloat>
  35. #include <iostream>
  36. #include <string>
  37. #include <vector>
  38.  
  39. #include <GL/glew.h>
  40.  
  41. #include <glm/glm.hpp>
  42. #include <glm/gtc/matrix_transform.hpp>
  43. #include <glm/gtc/type_ptr.hpp>
  44. #include <glm/gtx/transform.hpp>
  45.  
  46. #include <SFML/Graphics.hpp>
  47. #include <SFML/Window.hpp>
  48.  
  49. #include "Archive.h"
  50.  
  51. using namespace glm;
  52. using namespace sf;
  53. using namespace std;
  54.  
  55. struct Camera
  56. {
  57. vec3 Position = { 0.0f, 0.0f, 5.0f };
  58. vec3 Target = { 0.0f, 0.0f, 0.0f };
  59. vec3 Up = { 0.0f, 1.0f, 0.0f };
  60.  
  61. float Fovy = 74.0f;
  62. float Aspect = 16.0f / 9.0f;
  63. float ZNear = FLT_MIN;
  64. float ZFar = FLT_MAX;
  65.  
  66. mat4 View;
  67. mat4 Projection;
  68. };
  69.  
  70. struct Actor
  71. {
  72. vec3 Scale = { 1.0f, 1.0f, 1.0f };
  73. vec3 Rotation = { 0.0f, 0.0f, 0.0f };
  74. vec3 Position = { 0.0f, 0.0f, 0.0f };
  75.  
  76. vector<GLfloat> Vertices;
  77. vector<vec2> Uvs;
  78. vector<vec3> Normals;
  79. vector<GLuint> Elements;
  80.  
  81. GLuint Texture;
  82.  
  83. Actor(string fileName)
  84. {
  85. Image image;
  86.  
  87. if (!image.loadFromFile(fileName + ".png"))
  88. {
  89. cerr << "ERROR: Unable to load texture" << endl;
  90. }
  91.  
  92. glGenTextures(1, &Texture);
  93. glBindTexture(GL_TEXTURE_2D, Texture);
  94.  
  95. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.getSize().x, image.getSize().y, 0, GL_RGBA, GL_UNSIGNED_BYTE, image.getPixelsPtr());
  96. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
  97. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
  98. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  99. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  100. glGenerateMipmap(GL_TEXTURE_2D);
  101.  
  102. Vertices.push_back(-1.0f); Vertices.push_back(-1.0f); Vertices.push_back(0.0f); Vertices.push_back(0.0f); Vertices.push_back(0.0f);
  103. Vertices.push_back(1.0f); Vertices.push_back(-1.0f); Vertices.push_back(0.0f); Vertices.push_back(1.0f); Vertices.push_back(0.0f);
  104. Vertices.push_back(1.0f); Vertices.push_back(1.0f); Vertices.push_back(0.0f); Vertices.push_back(1.0f); Vertices.push_back(1.0f);
  105. Vertices.push_back(-1.0f); Vertices.push_back(1.0f); Vertices.push_back(0.0f); Vertices.push_back(0.0f); Vertices.push_back(1.0f);
  106.  
  107. Elements.push_back(0); Elements.push_back(1); Elements.push_back(2);
  108. Elements.push_back(2); Elements.push_back(3); Elements.push_back(0);
  109. }
  110. };
  111.  
  112. GLuint CreateShader(GLenum shaderType, string fileName, Archive& archive)
  113. {
  114. string source;
  115.  
  116. archive.open(fileName);
  117. source.resize(archive.getSize());
  118. archive.read(&source[0], archive.getSize());
  119.  
  120. GLuint shader = glCreateShader(shaderType);
  121. const char* pointer = source.c_str();
  122.  
  123. glShaderSource(shader, 1, &pointer, nullptr);
  124. glCompileShader(shader);
  125.  
  126. GLsizei length;
  127.  
  128. glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length);
  129.  
  130. if (length > 1)
  131. {
  132. GLchar* infoLog = new GLchar[length];
  133.  
  134. glGetShaderInfoLog(shader, length, &length, infoLog);
  135.  
  136. cerr << infoLog << endl;
  137.  
  138. delete[] infoLog;
  139. }
  140.  
  141. return shader;
  142. }
  143.  
  144. GLuint CreateProgram(GLuint vertex, GLuint fragment)
  145. {
  146. GLuint program = glCreateProgram();
  147.  
  148. glAttachShader(program, vertex);
  149. glAttachShader(program, fragment);
  150.  
  151. glLinkProgram(program);
  152.  
  153. GLsizei length;
  154.  
  155. glGetProgramiv(program, GL_INFO_LOG_LENGTH, &length);
  156.  
  157. if (length > 1)
  158. {
  159. GLchar* infoLog = new GLchar[length];
  160.  
  161. glGetProgramInfoLog(program, length, &length, infoLog);
  162.  
  163. cerr << infoLog << endl;
  164.  
  165. delete[] infoLog;
  166. }
  167.  
  168. return program;
  169. }
  170.  
  171. int main(int argc, char* argv[])
  172. {
  173. Window window(VideoMode(1920, 1080), "");
  174.  
  175. window.setVerticalSyncEnabled(true);
  176.  
  177. if (!window.setActive(true))
  178. {
  179. cerr << "ERROR: Unable to set the window as the current target for OpenGL rendering" << endl;
  180.  
  181. return 1;
  182. }
  183.  
  184. glewExperimental = GL_TRUE;
  185.  
  186. if (glewInit() != GLEW_OK)
  187. {
  188. cerr << "ERROR: Unable to initialise GLEW" << endl;
  189.  
  190. return 1;
  191. }
  192.  
  193. Archive shaders("Shaders.lea");
  194. Archive models("Models.lea");
  195.  
  196. Actor actor("tree01");
  197.  
  198. GLuint vertex = CreateShader(GL_VERTEX_SHADER, "Vertex.vert", shaders);
  199. GLuint fragment = CreateShader(GL_FRAGMENT_SHADER, "Fragment.frag", shaders);
  200. GLuint program = CreateProgram(vertex, fragment);
  201.  
  202. GLuint vertexArray;
  203. GLuint vertexBuffer;
  204. GLuint elementBuffer;
  205.  
  206. glGenVertexArrays(1, &vertexArray);
  207. glBindVertexArray(vertexArray);
  208.  
  209. glGenBuffers(1, &vertexBuffer);
  210. glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
  211.  
  212. glGenBuffers(1, &elementBuffer);
  213. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer);
  214.  
  215. // glUseProgram(program);
  216.  
  217. GLint position = glGetAttribLocation(program, "in_position");
  218.  
  219. glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, 0);
  220. glEnableVertexAttribArray(position);
  221.  
  222. GLint texture = glGetAttribLocation(program, "in_texture");
  223.  
  224. glVertexAttribPointer(texture, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 5, (void*)(sizeof(GLfloat) * 3));
  225. glEnableVertexAttribArray(texture);
  226.  
  227. GLint projection = glGetUniformLocation(program, "in_projection");
  228. GLint view = glGetUniformLocation(program, "in_view");
  229. GLint model = glGetUniformLocation(program, "in_model");
  230.  
  231. glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  232.  
  233. Camera camera;
  234.  
  235. while (window.isOpen())
  236. {
  237. // Input handling code omitted
  238.  
  239. camera.View = lookAt(camera.Position, camera.Target, camera.Up);
  240. camera.Projection = perspective(radians(camera.Fovy), camera.Aspect, camera.ZNear, camera.ZFar);
  241.  
  242. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  243.  
  244. glUniformMatrix4fv(projection, 1, GL_FALSE, value_ptr(camera.Projection));
  245. glUniformMatrix4fv(view, 1, GL_FALSE, value_ptr(camera.View));
  246.  
  247. glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * actor.Vertices.size(), &actor.Vertices[0], GL_STATIC_DRAW);
  248. glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint) * actor.Elements.size(), &actor.Elements[0], GL_STATIC_DRAW);
  249.  
  250. mat4 transform = translate(mat4(), actor.Position);
  251. transform *= rotate(transform, actor.Rotation.z, vec3(0.0f, 0.0f, 1.0f));
  252. transform *= rotate(transform, actor.Rotation.y, vec3(0.0f, 1.0f, 0.0f));
  253. transform *= rotate(transform, actor.Rotation.x, vec3(1.0f, 0.0f, 0.0f));
  254. transform *= scale(transform, actor.Scale);
  255.  
  256. glUniformMatrix4fv(model, 1, GL_FALSE, value_ptr(transform));
  257.  
  258. glBindTexture(GL_TEXTURE_2D, actor.Texture);
  259.  
  260. glDrawElements(GL_TRIANGLES, actor.Elements.size(), GL_UNSIGNED_INT, 0);
  261.  
  262. window.display();
  263. }
  264.  
  265. glUseProgram(0);
  266.  
  267. glDisableVertexAttribArray(texture);
  268. glDisableVertexAttribArray(position);
  269.  
  270. glDeleteBuffers(1, &elementBuffer);
  271. glDeleteBuffers(1, &vertexBuffer);
  272.  
  273. glDeleteVertexArrays(1, &vertexArray);
  274.  
  275. glDetachShader(program, fragment);
  276. glDetachShader(program, vertex);
  277.  
  278. glDeleteShader(fragment);
  279. glDeleteShader(vertex);
  280.  
  281. glDeleteProgram(program);
  282.  
  283. return 0;
  284. }```
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement