Closed jonahshader closed 1 month ago
Here are the code search results. I'm now analyzing these search results to write the PR.
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;
}
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.
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