Advertisement
Guest User

Instanced Rendering Help

a guest
Mar 26th, 2018
85
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ///// C++ Code /////
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <random>
  6. #include <ctime>
  7. #include <cstdlib>
  8.  
  9. #include <glad/glad.h>
  10. #include <GLFW/glfw3.h>
  11. #include <glm/glm.hpp>
  12.  
  13. #define SCREEN_WIDTH 1280
  14. #define SCREEN_HEIGHT 720
  15.  
  16. #define NUM_SPITES 100
  17.  
  18. const float sharedVertices[] = {
  19.     -0.5f, -0.5f,
  20.     0.5f, -0.5f,
  21.     -0.5f, 0.5f,
  22.     0.5f, 0.5f,
  23. };
  24. GLuint sharedVerticesVertexBuffer;
  25. GLuint positionVertexBuffer;
  26. GLuint colourVertexBuffer;
  27.  
  28. GLuint spriteShaderProgram;
  29.  
  30. char* file_to_buf(FILE* file) {
  31.     // File size
  32.     fseek(file, 0, SEEK_END);
  33.     long size = ftell(file);
  34.     fseek(file, 0, SEEK_SET);
  35.  
  36.     // Read into buffer
  37.     char* string = (char*)malloc(size + 1);
  38.     fread(string, size, 1, file);
  39.     string[size] = '\0';
  40.  
  41.     return string;
  42. }
  43.  
  44. GLuint shader_create(const char* path, GLenum type) {
  45.     // Make sure the correct enumerator was passed in
  46.     if (!(type == GL_VERTEX_SHADER || type == GL_FRAGMENT_SHADER)) {
  47.         fprintf(stderr, "type must be GL_VERTEX_SHADER or GL_FRAGMENT_SHADER\n");
  48.         exit(EXIT_FAILURE);
  49.     }
  50.     GLuint shader = glCreateShader(type);
  51.  
  52.     FILE* file = fopen(path, "r");
  53.     if (!file) {
  54.         fprintf(stderr, "File %s not found\n", path);
  55.         exit(EXIT_FAILURE);
  56.     }
  57.     const char* source = file_to_buf(file);
  58.     glShaderSource(shader, 1, &source, NULL);
  59.     glCompileShader(shader);
  60.     free((void*)source);
  61.  
  62.     // Check if the shader compiled correctly
  63.     GLint status;
  64.     glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
  65.     if (status == GL_FALSE) {
  66.         GLint log_size;
  67.         glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_size);
  68.         char* log = (char*)malloc(log_size);
  69.         glGetShaderInfoLog(shader, log_size, &log_size, log);
  70.         fprintf(stderr, "%s\n", log);
  71.         free(log);
  72.         exit(EXIT_FAILURE);
  73.     }
  74.  
  75.     return shader;
  76. }
  77.  
  78. // vs_path: vertex shader path
  79. // fs_path: fragment shader path
  80. GLuint program_create(const char* vs_path, const char* fs_path) {
  81.     GLuint vs = shader_create(vs_path, GL_VERTEX_SHADER);
  82.     GLuint fs = shader_create(fs_path, GL_FRAGMENT_SHADER);
  83.     GLuint program = glCreateProgram();
  84.     glAttachShader(program, vs);
  85.     glAttachShader(program, fs);
  86.     glLinkProgram(program);
  87.  
  88.     glDetachShader(program, vs);
  89.     glDetachShader(program, fs);
  90.     glDeleteShader(vs);
  91.     glDeleteShader(fs);
  92.  
  93.     return program;
  94. }
  95.  
  96. typedef struct {
  97.     float position[2];
  98.     float colour[3];
  99. } Sprite;
  100.  
  101. Sprite sprites[NUM_SPITES];
  102.  
  103. void createSprites() {
  104.     srand(time(NULL));
  105.  
  106.     for (int i = 0; i < sizeof(sprites) / sizeof(Sprite); i++) {
  107.         // Set random position
  108.         sprites[i].position[0] = rand() % SCREEN_WIDTH;
  109.         sprites[i].position[1] = rand() % SCREEN_HEIGHT;
  110.  
  111.         // Set colour to white
  112.         sprites[i].colour[0] = 0.5;
  113.         sprites[i].colour[1] = 0.6;
  114.         sprites[i].colour[2] = 0.4;
  115.     }
  116. }
  117.  
  118. void createVertexBuffers() {
  119.     glGenBuffers(1, &sharedVerticesVertexBuffer);
  120.     glBindBuffer(GL_ARRAY_BUFFER, sharedVerticesVertexBuffer);
  121.     glBufferData(GL_ARRAY_BUFFER, sizeof(sharedVertices), sharedVertices, GL_STATIC_DRAW);
  122.  
  123.     glGenBuffers(1, &positionVertexBuffer);
  124.     glBindBuffer(GL_ARRAY_BUFFER, positionVertexBuffer);
  125.     glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
  126.  
  127.     glGenBuffers(1, &colourVertexBuffer);
  128.     glBindBuffer(GL_ARRAY_BUFFER, colourVertexBuffer);
  129.     glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
  130. }
  131.  
  132. void updateVertexBuffers() {
  133.     float positionData[NUM_SPITES * 2];
  134.     for (int i = 0; i < NUM_SPITES; i += 2) {
  135.         positionData[i] = sprites[i].position[i];
  136.         positionData[i + 1] = sprites[i].position[i + 1];
  137.     }
  138.     float colourData[NUM_SPITES * 3];
  139.     for (int i = 0; i < NUM_SPITES; i += 3) {
  140.         colourData[i] = sprites[i].colour[i];
  141.         colourData[i + 1] = sprites[i].colour[i + 1];
  142.         colourData[i + 2] = sprites[i].colour[i + 2];
  143.     }
  144.  
  145.     glBindBuffer(GL_ARRAY_BUFFER, positionVertexBuffer);
  146.     glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
  147.     glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(positionData), positionData);
  148.  
  149.     glBindBuffer(GL_ARRAY_BUFFER, colourVertexBuffer);
  150.     glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
  151.     glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(colourData), colourData);
  152. }
  153.  
  154. void draw() {
  155.     glEnableVertexAttribArray(0);
  156.     glBindBuffer(GL_ARRAY_BUFFER, sharedVerticesVertexBuffer);
  157.     glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
  158.  
  159.     glEnableVertexAttribArray(1);
  160.     glBindBuffer(GL_ARRAY_BUFFER, positionVertexBuffer);
  161.     glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
  162.  
  163.     glEnableVertexAttribArray(2);
  164.     glBindBuffer(GL_ARRAY_BUFFER, colourVertexBuffer);
  165.     glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
  166.  
  167.     glVertexAttribDivisor(0, 0);
  168.     glVertexAttribDivisor(1, 1);
  169.     glVertexAttribDivisor(2, 1);
  170.  
  171.     glUseProgram(spriteShaderProgram);
  172.     glDrawArraysInstanced(GL_TRIANGLES, 0, 4, NUM_SPITES);
  173. }
  174.  
  175. void updateSprites() {
  176.     for (int i = 0; i < NUM_SPITES; i++) {
  177.         for (int j = 0; j < 3; j++) {
  178.             //sprites[i].colour[j]++;
  179.         }
  180.     }
  181. }
  182.  
  183. int main(int argc, const char * argv[]) {
  184.     if (!glfwInit()) {
  185.         exit(-1);
  186.     }
  187.  
  188.     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
  189.     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
  190.     glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  191.     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  192.     GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "OpenGL Sprite Test", nullptr, nullptr);
  193.     glfwMakeContextCurrent(window);
  194.  
  195.     gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
  196.  
  197.     createSprites();
  198.     createVertexBuffers();
  199.  
  200.     spriteShaderProgram = program_create("spriteShader.vert", "spriteShader.frag");
  201.  
  202.     while (!glfwWindowShouldClose(window)) {
  203.         glClearColor(0, 0, 0, 0);
  204.         glClear(GL_COLOR_BUFFER_BIT);
  205.  
  206.         updateSprites();
  207.         updateVertexBuffers();
  208.         draw();
  209.  
  210.         glfwSwapBuffers(window);
  211.         glfwPollEvents();
  212.     }
  213.  
  214.     return 0;
  215. }
  216. ///// C++ Code /////
  217.  
  218. ///// Vertex Shader /////
  219. #version 330 core
  220.  
  221. layout(location = 0)in vec2 vertex;
  222. layout(location = 1)in vec2 position;
  223. layout(location = 2)in vec3 colour;
  224.  
  225. out vec3 vertexColour;
  226.  
  227. void main() {
  228.     gl_Position = vec4(vertex * position, 0., 1.);
  229.  
  230.     vertexColour = colour;
  231. }
  232. ///// Vertex Shader /////
  233.  
  234. ///// Fragment Shader /////
  235. #version 330 core
  236.  
  237. in vec3 vertexColour;
  238.  
  239. out vec4 fragColour;
  240.  
  241. void main() {
  242.     fragColour = vec4(vertexColour, 1.);
  243. }
  244. ///// Fragment Shader /////
Advertisement
RAW Paste Data Copied
Advertisement