Skip to content

Commit

Permalink
Lower required OpenGL version from 4.6 to 4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sunverwerth committed Sep 27, 2020
1 parent cd3a263 commit 2d6baad
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/Gfx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Gfx::Gfx(const char* title, int width, int height, bool fullscreen) {
SDL_GetWindowSize(window, &width_, &height_);

SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 6);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, SDL_TRUE);
unsigned int flags = SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG;
Expand All @@ -66,6 +66,7 @@ Gfx::Gfx(const char* title, int width, int height, bool fullscreen) {
#ifdef _DEBUG
if (glDebugMessageCallback) {
glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(debugCallback, nullptr);
}
#endif
Expand Down
42 changes: 21 additions & 21 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,24 @@ SOFTWARE.
Mesh::Mesh() {
int numQuads = 65536 / 4;

glCreateBuffers(1, &vbo);
glNamedBufferStorage(vbo, sizeof(SpriteVertex) * numQuads * 4, nullptr, GL_DYNAMIC_STORAGE_BIT);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(SpriteVertex) * numQuads * 4, nullptr, GL_DYNAMIC_DRAW);

glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(SpriteVertex), (void*)offsetof(SpriteVertex, position));

glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(SpriteVertex), (void*)offsetof(SpriteVertex, uv));

glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 4, GL_FLOAT, GL_FALSE, sizeof(SpriteVertex), (void*)offsetof(SpriteVertex, color));

glCreateBuffers(1, &ebo);
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
int numIndices = numQuads * 6;
std::vector<unsigned short> indices(numIndices);
for (int i = 0; i < numQuads; i++) {
Expand All @@ -44,23 +58,7 @@ Mesh::Mesh() {
indices[i * 6 + 4] = i * 4 + 3;
indices[i * 6 + 5] = i * 4 + 0;
}
glNamedBufferStorage(ebo, sizeof(unsigned short) * numIndices, indices.data(), 0);

glCreateVertexArrays(1, &vao);
glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(SpriteVertex));
glVertexArrayElementBuffer(vao, ebo);

glEnableVertexArrayAttrib(vao, 0);
glVertexArrayAttribBinding(vao, 0, 0);
glVertexArrayAttribFormat(vao, 0, 2, GL_FLOAT, GL_FALSE, offsetof(SpriteVertex, position));

glEnableVertexArrayAttrib(vao, 1);
glVertexArrayAttribBinding(vao, 1, 0);
glVertexArrayAttribFormat(vao, 1, 2, GL_FLOAT, GL_FALSE, offsetof(SpriteVertex, uv));

glEnableVertexArrayAttrib(vao, 2);
glVertexArrayAttribBinding(vao, 2, 0);
glVertexArrayAttribFormat(vao, 2, 4, GL_FLOAT, GL_FALSE, offsetof(SpriteVertex, color));
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short) * numIndices, indices.data(), GL_DYNAMIC_DRAW);
}

Mesh::~Mesh() {
Expand All @@ -70,10 +68,12 @@ Mesh::~Mesh() {
}

void Mesh::bind() const {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBindVertexArray(vao);
}

void Mesh::setVertices(const void* data, size_t elementSize, int num) {
glNamedBufferSubData(vbo, 0, elementSize * num, data);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferSubData(GL_ARRAY_BUFFER, 0, elementSize * num, data);
numVertices_ = num;
}
18 changes: 11 additions & 7 deletions src/Texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ SOFTWARE.
#include "sys.h"

Texture::Texture(const Image& image): width_(image.width()), height_(image.height()) {
glCreateTextures(GL_TEXTURE_2D, 1, &texture);
glTextureStorage2D(texture, 1, GL_RGBA8, width_, height_);
glTextureParameteri(texture, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTextureParameteri(texture, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, image.format() == Image::Format::RGB8 ? GL_RGB8 : GL_RGBA8, width_, height_, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
load(image);
}

Expand All @@ -45,17 +46,20 @@ void Texture::load(const Image& image) {
return;
}

glTextureSubImage2D(texture, 0, 0, 0, width_, height_, image.format() == Image::Format::RGB8 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, image.data());
glBindTexture(GL_TEXTURE_2D, texture);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width_, height_, image.format() == Image::Format::RGB8 ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, image.data());
}

void Texture::bind(int unit) {
if (unit_ >= 0) unbind();
unit_ = unit;
glBindTextureUnit(unit_, texture);
glActiveTexture(GL_TEXTURE0 + unit_);
glBindTexture(GL_TEXTURE_2D, texture);
}

void Texture::unbind() {
if (unit_ < 0) return;
glBindTextureUnit(unit_, 0);
glActiveTexture(GL_TEXTURE0 + unit_);
glBindTexture(GL_TEXTURE_2D, 0);
unit_ = -1;
}

0 comments on commit 2d6baad

Please sign in to comment.