jonahshader / alife_cuda

Artificial life simulator using CUDA for compute
0 stars 0 forks source link

Sweep: Write a PolygonRenderer #5

Closed jonahshader closed 1 month ago

jonahshader commented 5 months ago

Details

Write a renderer for arbitrary polygons. Look at LineRenderer, RectRenderer, etc from src/graphics/renderers and their corresponding shaders from resources/shaders for reference. This renderer should take in a color and a vector of points that describe the polygon.

Branch

No response

sweep-ai[bot] commented 5 months ago

🚀 Here's the PR! #6

💎 Sweep Pro: You have unlimited Sweep issues

Actions

Relevant files (click to expand). Mentioned files will always appear here. https://github.com/jonahshader/alife_cuda/blob/01dba9d90972770ab22da2166fdd19073e56cd2f/src/graphics/Shader.cpp#L1-L119 https://github.com/jonahshader/alife_cuda/blob/01dba9d90972770ab22da2166fdd19073e56cd2f/src/graphics/Shader.h#L1-L29 https://github.com/jonahshader/alife_cuda/blob/01dba9d90972770ab22da2166fdd19073e56cd2f/src/graphics/renderers/RectRenderer.h#L1-L26 https://github.com/jonahshader/alife_cuda/blob/01dba9d90972770ab22da2166fdd19073e56cd2f/src/graphics/renderers/RectRenderer.cpp#L1-L109 https://github.com/jonahshader/alife_cuda/blob/01dba9d90972770ab22da2166fdd19073e56cd2f/resources/shaders/rect.vert#L1-L20 https://github.com/jonahshader/alife_cuda/blob/01dba9d90972770ab22da2166fdd19073e56cd2f/resources/shaders/rect.frag#L1-L34

Step 2: ⌨️ Coding

src/graphics/renderers/PolygonRenderer.h

Create a new file PolygonRenderer.h to declare the PolygonRenderer class.
#pragma once

#include <vector>
#include <glm/glm.hpp>
#include "graphics/Shader.h"

class PolygonRenderer {
public:
    PolygonRenderer();
    ~PolygonRenderer();
    void add_polygon(const glm::vec4& color, const std::vector<glm::vec2>& points);
    void render();

private:
    Shader shader;
    unsigned int vao{};
    unsigned int vbo{};
    std::vector<glm::vec4> colors;
    std::vector<glm::vec2> polygon_points;
};

src/graphics/renderers/PolygonRenderer.cpp

Create a new file PolygonRenderer.cpp to define the PolygonRenderer class methods.
#include "PolygonRenderer.h"
#include <glad/glad.h>

PolygonRenderer::PolygonRenderer() : shader("shaders/polygon.vert", "shaders/polygon.frag") {
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
}

void PolygonRenderer::add_polygon(const glm::vec4& color, const std::vector<glm::vec2>& points) {
    colors.push_back(color);
    polygon_points.insert(polygon_points.end(), points.begin(), points.end());
}

void PolygonRenderer::render() {
    shader.use();
    glBindVertexArray(vao);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, polygon_points.size() * sizeof(glm::vec2), polygon_points.data(), GL_DYNAMIC_DRAW);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
    glEnableVertexAttribArray(0);

    int offset = 0;
    for (const auto& color : colors) {
        shader.setVec4("color", color);
        glDrawArrays(GL_TRIANGLE_FAN, offset, polygon_points.size() / colors.size());
        offset += polygon_points.size() / colors.size();
    }

    glBindVertexArray(0);
}

PolygonRenderer::~PolygonRenderer() {
    glDeleteVertexArrays(1, &vao);
    glDeleteBuffers(1, &vbo);
}

resources/shaders/polygon.vert

Create a new file polygon.vert for the polygon vertex shader.
#version 430 core

layout (location = 0) in vec2 aPos;

void main() {
    gl_Position = vec4(aPos, 0.0, 1.0);
}

resources/shaders/polygon.frag

Create a new file polygon.frag for the polygon fragment shader.
#version 430 core

uniform vec4 color;

out vec4 FragColor;

void main() {
    FragColor = color;
}

Step 3: 🔄️ Validating

Your changes have been successfully made to the branch sweep/write_a_polygonrenderer. I have validated these changes using a syntax checker and a linter.


[!TIP] To recreate the pull request, edit the issue title or description.

This is an automated message generated by Sweep AI.