2021-11-27 19:16:41 +00:00
|
|
|
#pragma once
|
2021-11-25 21:15:24 +00:00
|
|
|
|
2022-08-19 02:40:34 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <iostream>
|
2021-12-02 21:58:15 +00:00
|
|
|
#ifdef __linux__
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#include <GL/gl.h>
|
2021-12-13 03:17:59 +00:00
|
|
|
#else
|
2021-11-25 21:15:24 +00:00
|
|
|
#include <gl/glew.h>
|
2021-12-02 21:58:15 +00:00
|
|
|
#include <gl/gl.h>
|
|
|
|
#endif
|
2021-11-25 21:15:24 +00:00
|
|
|
|
|
|
|
#include "vbo.h"
|
|
|
|
|
|
|
|
namespace simpleengine::gfx {
|
|
|
|
class VAO {
|
|
|
|
public:
|
|
|
|
GLuint handle;
|
|
|
|
|
2022-08-19 02:40:34 +00:00
|
|
|
VAO(GLuint handle) : handle(handle) {
|
|
|
|
//glCreateVertexArrays(1, &handle);
|
2021-11-25 21:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~VAO() {
|
2022-08-19 02:40:34 +00:00
|
|
|
std::cout << "TODO, drop VAO (" << handle << ")" << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
VAO& operator=(const VAO& other) {
|
|
|
|
if (this != &other) {
|
|
|
|
handle = other.handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::cout << "Copied " << handle << std::endl;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VAO init() {
|
|
|
|
GLuint handle;
|
|
|
|
glCreateVertexArrays(1, &handle);
|
|
|
|
|
|
|
|
return VAO(handle);
|
2021-11-25 21:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void destroy() {
|
|
|
|
glDeleteVertexArrays(1, &handle);
|
|
|
|
}
|
|
|
|
|
2021-11-27 18:52:48 +00:00
|
|
|
void bind() const {
|
2021-11-25 21:15:24 +00:00
|
|
|
glBindVertexArray(handle);
|
2022-08-19 02:40:34 +00:00
|
|
|
|
2022-08-19 04:20:53 +00:00
|
|
|
// TODO: Handle opengl errors EVERYWHERE
|
2022-08-19 02:40:34 +00:00
|
|
|
GLenum err = glGetError();
|
|
|
|
if (err != GL_NO_ERROR) {
|
2022-08-19 04:20:53 +00:00
|
|
|
fprintf(stderr, "Ran into opengl error: 0x%x\n", err);
|
2022-08-19 02:40:34 +00:00
|
|
|
}
|
2021-11-25 21:15:24 +00:00
|
|
|
}
|
|
|
|
|
2021-11-26 03:28:47 +00:00
|
|
|
// TODO: Fix this.
|
2021-11-27 18:52:48 +00:00
|
|
|
void enable_attrib(const VBO& vbo, GLuint index, GLint size, GLenum type, GLsizei stride, size_t offset) const {
|
2021-11-25 21:15:24 +00:00
|
|
|
bind();
|
|
|
|
vbo.bind();
|
|
|
|
|
|
|
|
// NOTE: glVertexAttribPointer will AUTO-CONVERT integer values to floating point.
|
|
|
|
// Integer vertex attributes must be specified with glVertexAttribIPointer.
|
|
|
|
// THIS IS EVIL. OpenGL is bad. Who designed this to fail silently?
|
|
|
|
switch (type) {
|
|
|
|
case GL_BYTE:
|
|
|
|
case GL_UNSIGNED_BYTE:
|
|
|
|
case GL_SHORT:
|
|
|
|
case GL_UNSIGNED_SHORT:
|
|
|
|
case GL_INT:
|
|
|
|
case GL_UNSIGNED_INT:
|
|
|
|
case GL_INT_2_10_10_10_REV:
|
|
|
|
case GL_UNSIGNED_INT_2_10_10_10_REV:
|
|
|
|
glVertexAttribIPointer(index, size, type, stride, (void *) offset);
|
|
|
|
break;
|
|
|
|
default:
|
2021-11-27 18:52:48 +00:00
|
|
|
glVertexAttribPointer(index, size, type, GL_FALSE, stride, (void *) offset);
|
2021-11-25 21:15:24 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
glEnableVertexAttribArray(index);
|
|
|
|
|
|
|
|
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's
|
|
|
|
// bound vertex buffer object so afterwards we can safely unbind.
|
2021-11-26 03:28:47 +00:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
2021-11-25 21:15:24 +00:00
|
|
|
|
|
|
|
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this
|
|
|
|
// rarely happens. Modifying other VAOs requires a call to glBindVertexArray anyways so we generally
|
|
|
|
// don't unbind VAOs (nor VBOs) when it's not directly necessary.
|
|
|
|
glBindVertexArray(0);
|
2022-08-30 23:39:24 +00:00
|
|
|
|
|
|
|
// TODO: Handle opengl errors EVERYWHERE
|
|
|
|
GLenum err = glGetError();
|
|
|
|
if (err != GL_NO_ERROR) {
|
|
|
|
fprintf(stderr, "Ran into opengl error: 0x%x\n", err);
|
|
|
|
}
|
2021-11-25 21:15:24 +00:00
|
|
|
}
|
2022-08-18 20:03:51 +00:00
|
|
|
|
|
|
|
void disable_attrib(const VBO& vbo, GLuint index) const {
|
|
|
|
bind();
|
|
|
|
vbo.bind();
|
|
|
|
|
|
|
|
glDisableVertexAttribArray(index);
|
|
|
|
//glDisableVertexArrayAttrib(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_attrib_value(const VBO& vbo, GLuint index, float f) const {
|
|
|
|
bind();
|
|
|
|
vbo.bind();
|
|
|
|
|
|
|
|
glVertexAttrib1f(index, f);
|
|
|
|
}
|
2021-11-25 21:15:24 +00:00
|
|
|
};
|
2021-11-27 19:16:41 +00:00
|
|
|
}
|