Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///// C++ Code /////
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <random>
- #include <ctime>
- #include <cstdlib>
- #include <glad/glad.h>
- #include <GLFW/glfw3.h>
- #include <glm/glm.hpp>
- #define SCREEN_WIDTH 1280
- #define SCREEN_HEIGHT 720
- #define NUM_SPITES 100
- const float sharedVertices[] = {
- -0.5f, -0.5f,
- 0.5f, -0.5f,
- -0.5f, 0.5f,
- 0.5f, 0.5f,
- };
- GLuint sharedVerticesVertexBuffer;
- GLuint positionVertexBuffer;
- GLuint colourVertexBuffer;
- GLuint spriteShaderProgram;
- char* file_to_buf(FILE* file) {
- // File size
- fseek(file, 0, SEEK_END);
- long size = ftell(file);
- fseek(file, 0, SEEK_SET);
- // Read into buffer
- char* string = (char*)malloc(size + 1);
- fread(string, size, 1, file);
- string[size] = '\0';
- return string;
- }
- GLuint shader_create(const char* path, GLenum type) {
- // Make sure the correct enumerator was passed in
- if (!(type == GL_VERTEX_SHADER || type == GL_FRAGMENT_SHADER)) {
- fprintf(stderr, "type must be GL_VERTEX_SHADER or GL_FRAGMENT_SHADER\n");
- exit(EXIT_FAILURE);
- }
- GLuint shader = glCreateShader(type);
- FILE* file = fopen(path, "r");
- if (!file) {
- fprintf(stderr, "File %s not found\n", path);
- exit(EXIT_FAILURE);
- }
- const char* source = file_to_buf(file);
- glShaderSource(shader, 1, &source, NULL);
- glCompileShader(shader);
- free((void*)source);
- // Check if the shader compiled correctly
- GLint status;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
- if (status == GL_FALSE) {
- GLint log_size;
- glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &log_size);
- char* log = (char*)malloc(log_size);
- glGetShaderInfoLog(shader, log_size, &log_size, log);
- fprintf(stderr, "%s\n", log);
- free(log);
- exit(EXIT_FAILURE);
- }
- return shader;
- }
- // vs_path: vertex shader path
- // fs_path: fragment shader path
- GLuint program_create(const char* vs_path, const char* fs_path) {
- GLuint vs = shader_create(vs_path, GL_VERTEX_SHADER);
- GLuint fs = shader_create(fs_path, GL_FRAGMENT_SHADER);
- GLuint program = glCreateProgram();
- glAttachShader(program, vs);
- glAttachShader(program, fs);
- glLinkProgram(program);
- glDetachShader(program, vs);
- glDetachShader(program, fs);
- glDeleteShader(vs);
- glDeleteShader(fs);
- return program;
- }
- typedef struct {
- float position[2];
- float colour[3];
- } Sprite;
- Sprite sprites[NUM_SPITES];
- void createSprites() {
- srand(time(NULL));
- for (int i = 0; i < sizeof(sprites) / sizeof(Sprite); i++) {
- // Set random position
- sprites[i].position[0] = rand() % SCREEN_WIDTH;
- sprites[i].position[1] = rand() % SCREEN_HEIGHT;
- // Set colour to white
- sprites[i].colour[0] = 0.5;
- sprites[i].colour[1] = 0.6;
- sprites[i].colour[2] = 0.4;
- }
- }
- void createVertexBuffers() {
- glGenBuffers(1, &sharedVerticesVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, sharedVerticesVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, sizeof(sharedVertices), sharedVertices, GL_STATIC_DRAW);
- glGenBuffers(1, &positionVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, positionVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
- glGenBuffers(1, &colourVertexBuffer);
- glBindBuffer(GL_ARRAY_BUFFER, colourVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
- }
- void updateVertexBuffers() {
- float positionData[NUM_SPITES * 2];
- for (int i = 0; i < NUM_SPITES; i += 2) {
- positionData[i] = sprites[i].position[i];
- positionData[i + 1] = sprites[i].position[i + 1];
- }
- float colourData[NUM_SPITES * 3];
- for (int i = 0; i < NUM_SPITES; i += 3) {
- colourData[i] = sprites[i].colour[i];
- colourData[i + 1] = sprites[i].colour[i + 1];
- colourData[i + 2] = sprites[i].colour[i + 2];
- }
- glBindBuffer(GL_ARRAY_BUFFER, positionVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(positionData), positionData);
- glBindBuffer(GL_ARRAY_BUFFER, colourVertexBuffer);
- glBufferData(GL_ARRAY_BUFFER, NUM_SPITES * 4 * sizeof(float), NULL, GL_STREAM_DRAW);
- glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(colourData), colourData);
- }
- void draw() {
- glEnableVertexAttribArray(0);
- glBindBuffer(GL_ARRAY_BUFFER, sharedVerticesVertexBuffer);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
- glEnableVertexAttribArray(1);
- glBindBuffer(GL_ARRAY_BUFFER, positionVertexBuffer);
- glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
- glEnableVertexAttribArray(2);
- glBindBuffer(GL_ARRAY_BUFFER, colourVertexBuffer);
- glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
- glVertexAttribDivisor(0, 0);
- glVertexAttribDivisor(1, 1);
- glVertexAttribDivisor(2, 1);
- glUseProgram(spriteShaderProgram);
- glDrawArraysInstanced(GL_TRIANGLES, 0, 4, NUM_SPITES);
- }
- void updateSprites() {
- for (int i = 0; i < NUM_SPITES; i++) {
- for (int j = 0; j < 3; j++) {
- //sprites[i].colour[j]++;
- }
- }
- }
- int main(int argc, const char * argv[]) {
- if (!glfwInit()) {
- exit(-1);
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
- glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
- glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
- GLFWwindow* window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "OpenGL Sprite Test", nullptr, nullptr);
- glfwMakeContextCurrent(window);
- gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
- createSprites();
- createVertexBuffers();
- spriteShaderProgram = program_create("spriteShader.vert", "spriteShader.frag");
- while (!glfwWindowShouldClose(window)) {
- glClearColor(0, 0, 0, 0);
- glClear(GL_COLOR_BUFFER_BIT);
- updateSprites();
- updateVertexBuffers();
- draw();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- return 0;
- }
- ///// C++ Code /////
- ///// Vertex Shader /////
- #version 330 core
- layout(location = 0)in vec2 vertex;
- layout(location = 1)in vec2 position;
- layout(location = 2)in vec3 colour;
- out vec3 vertexColour;
- void main() {
- gl_Position = vec4(vertex * position, 0., 1.);
- vertexColour = colour;
- }
- ///// Vertex Shader /////
- ///// Fragment Shader /////
- #version 330 core
- in vec3 vertexColour;
- out vec4 fragColour;
- void main() {
- fragColour = vec4(vertexColour, 1.);
- }
- ///// Fragment Shader /////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement