Skip to content

Commit

Permalink
vertex colors on opengl
Browse files Browse the repository at this point in the history
  • Loading branch information
WinteryFox committed Nov 7, 2023
1 parent ee9d373 commit ddc587f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 24 deletions.
4 changes: 2 additions & 2 deletions engine/gl/GlBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ namespace Vixen::Gl {
buffer,
static_cast<GLsizeiptr>(size),
nullptr,
this->flags
flags
);
spdlog::trace("Created new GL commandBuffer {} ({}B) and flags {}", buffer, size, this->flags);
spdlog::trace("Created new GL commandBuffer {} ({}B) and flags {}", buffer, size, flags);
}

GlBuffer::~GlBuffer() {
Expand Down
3 changes: 1 addition & 2 deletions engine/gl/GlVertexArrayObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace Vixen::Gl {
glVertexArrayAttribFormat(vao, location.index, location.size, location.type, location.normalized,
location.offset);
glEnableVertexArrayAttrib(vao, location.index);
// TODO: The binding index doesn't necessarily need to be the same as the attribute index
glVertexArrayAttribBinding(vao, location.index, location.index);
glVertexArrayAttribBinding(vao, location.index, binding.index);
}

if (binding.buffer->getBufferUsage() & Buffer::Usage::INDEX)
Expand Down
6 changes: 4 additions & 2 deletions engine/gl/GlVertexArrayObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ namespace Vixen::Gl {
GLsizei stride;
};

VertexBinding(const std::shared_ptr<GlBuffer> &buffer, const std::vector<Location> &locations)
: buffer(buffer), locations(locations) {}
VertexBinding(uint32_t index, const std::shared_ptr<GlBuffer> &buffer, const std::vector<Location> &locations)
: index(index), buffer(buffer), locations(locations) {}

uint32_t index;

std::shared_ptr<GlBuffer> buffer;

Expand Down
76 changes: 58 additions & 18 deletions engine/gl/test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,18 @@

#endif

struct Vertex {
glm::vec3 position;
glm::vec3 color;
};

int main() {
#ifdef _WIN32
system(("chcp " + std::to_string(CP_UTF8)).c_str());
#endif
spdlog::set_level(spdlog::level::trace);

std::vector<glm::vec3> vertices = {
{0.5f, 0.5f, 0.0f}, // top right
{0.5f, -0.5f, 0.0f}, // bottom right
{-0.5f, -0.5f, 0.0f}, // bottom left
{-0.5f, 0.5f, 0.0f} // top left
};
std::vector<std::uint32_t> indices = { // note that we start from 0!
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};

auto window = Vixen::Gl::GlWindow("Vixen OpenGL Test", 720, 480, true);
auto window = Vixen::Gl::GlWindow("Vixen OpenGL Test", 1920, 1080, true);
window.center();
window.setVisible(true);

Expand All @@ -44,26 +38,64 @@ int main() {

Vixen::Gl::GlShaderProgram program(vertexModule, fragmentModule);

std::vector<Vertex> vertices{
{{-0.5f, -0.5f, 0.0f}, {1.0f, 0.0f, 0.0f}},
{{0.5f, -0.5f, 0.0f}, {0.0f, 1.0f, 0.0f}},
{{0.5f, 0.5f, 0.0f}, {0.0f, 0.0f, 1.0f}},
{{-0.5f, 0.5f, 0.0f}, {1.0f, 1.0f, 1.0f}}
};

std::vector<uint32_t> indices{
0, 1, 2,
2, 3, 0
};

auto vbo = std::make_shared<Vixen::Gl::GlBuffer>(
Vixen::Buffer::Usage::VERTEX | Vixen::Buffer::Usage::INDEX,
vertices.size() * sizeof(glm::vec3) +
indices.size() * sizeof(std::uint32_t)
vertices.size() * sizeof(Vertex) +
indices.size() * sizeof(uint32_t)
);
vbo->write(
reinterpret_cast<const char *>(vertices.data()),
sizeof(Vertex) * vertices.size(),
0
);
vbo->write(
reinterpret_cast<const char *>(indices.data()),
sizeof(uint32_t) * indices.size(),
sizeof(Vertex) * vertices.size()
);
vbo->write(reinterpret_cast<const char *>(vertices.data()), vertices.size() * sizeof(glm::vec3), 0);
vbo->write(reinterpret_cast<const char *>(indices.data()), vertices.size() * sizeof(glm::vec3), 0);

auto vao = Vixen::Gl::GlVertexArrayObject(
{
Vixen::Gl::VertexBinding(
0,
vbo,
{
Vixen::Gl::VertexBinding::Location(0, 3, GL_FLOAT, GL_FALSE, 0, sizeof(glm::vec3))
Vixen::Gl::VertexBinding::Location(
0,
3,
GL_FLOAT,
GL_FALSE,
offsetof(Vertex, position),
sizeof(Vertex)
),
Vixen::Gl::VertexBinding::Location(
1,
3,
GL_FLOAT,
GL_FALSE,
offsetof(Vertex, color),
sizeof(Vertex)
)
}
)
},
vertices.size() * sizeof(glm::vec3)
vertices.size() * sizeof(Vertex)
);

double old = glfwGetTime();
uint32_t fps;
while (!window.shouldClose()) {
window.clear();

Expand All @@ -73,6 +105,14 @@ int main() {

window.update();
window.swap();

fps++;
double now = glfwGetTime();
if (now - old >= 1) {
spdlog::info("FPS: {}", fps);
old = now;
fps = 0;
}
}
return EXIT_SUCCESS;
}

0 comments on commit ddc587f

Please sign in to comment.