Skip to content

Commit

Permalink
types+porting preps
Browse files Browse the repository at this point in the history
- added types
- started preparation to port CoGL across platforms
- started moving from OpenGL types and macros to to CoGL types
  • Loading branch information
CardboardDog committed Jul 13, 2024
1 parent 1592135 commit 8cd9db1
Show file tree
Hide file tree
Showing 11 changed files with 95 additions and 40 deletions.
11 changes: 6 additions & 5 deletions cogl/include/buffers.hxx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#include<iostream>
#include<glad/glad.h>
#include<types.hxx>
namespace cogl{
class VertexAttributeArray{
public:
VertexAttributeArray(int length);
~VertexAttributeArray();
void use();
void addAttribute(GLuint index, GLint length, GLenum type, bool normalized);
void addAttribute(GLuint index, GLint length, cogl::dataType type, bool normalized);
void disableAttribute(GLuint index);
void enableAttribute(GLuint index);
protected:
Expand All @@ -16,13 +17,13 @@ namespace cogl{
};
class ArrayBuffer{
public:
ArrayBuffer(GLenum type);
ArrayBuffer(cogl::bufferType type);
~ArrayBuffer();
void draw(GLenum geometry, GLsizei amount);
void draw(cogl::geometryType geometry, cogl::dataSize amount);
void use();
void setData(GLsizei size, const void* data);
void setData(cogl::dataSize size, cogl::bufferData data);
protected:
unsigned int VBO;
GLenum bufferType;
cogl::bufferType bufferType;
};
};
3 changes: 2 additions & 1 deletion cogl/include/cogl.hxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include<graphics.hxx>
#include<shaders.hxx>
#include<buffers.hxx>
#include<textures.hxx>
#include<textures.hxx>
#include<types.hxx>
7 changes: 4 additions & 3 deletions cogl/include/graphics.hxx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include<glad/glad.h>
#include<types.hxx>
#include<GLFW/glfw3.h>
#include<string>
#define COGL_SCREEN_WIDTH 400
Expand All @@ -10,11 +11,11 @@ namespace cogl{
GraphicsSystem(std::string name);
~GraphicsSystem();
void clear(float r, float g, float b, float a);
void clear(GLbitfield buffer);
void clear(cogl::clearBuffer buffer);
void swapBuffers();
void setDisplay(int display);
void setViewport(GLint x, GLint y, GLsizei width, GLsizei height);
void set(GLenum setting, bool value);
void setViewport(GLint x, GLint y, GLint width, GLint height);
void set(cogl::setting setting, bool value);
void update();
bool isAlive();
int width;
Expand Down
5 changes: 3 additions & 2 deletions cogl/include/shaders.hxx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include<glad/glad.h>
#include<string>
#include<types.hxx>
namespace cogl{
class Shader{
public:
Shader(GLenum type, std::string code);
Shader(cogl::shaderType type, std::string code);
~Shader();
unsigned int shader;
protected:
GLenum type;
cogl::shaderType type;
};
class ShaderProgram{
public:
Expand Down
7 changes: 4 additions & 3 deletions cogl/include/textures.hxx
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#include <glad/glad.h>
#include<types.hxx>
namespace cogl{
class Texture{
public:
Texture(bool mipmaps);
~Texture();
void use(int id);
void use();
void set(GLenum setting, GLint value);
void set(GLenum setting, bool value);
void load(unsigned char** data, GLenum color, int width, int height);
void set(cogl::setting setting, GLint value);
void set(cogl::setting setting, bool value);
void load(unsigned char** data, cogl::colorType color, int width, int height);
protected:
unsigned int texture;
bool mipmaps;
Expand Down
53 changes: 53 additions & 0 deletions cogl/include/types.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<glad/glad.h>
namespace cogl{
typedef GLsizei dataSize;
typedef GLbitfield clearBuffer;
typedef unsigned char* textureData;
typedef const void* bufferData;
typedef GLenum setting;
typedef GLenum colorType;
typedef GLenum shaderType;
typedef GLenum bufferType;
typedef GLenum geometryType;
typedef GLenum dataType;
}
#define coglTriangles GL_TRIANGLES
#define coglQuads GL_QUADS
#define coglVertexShader GL_VERTEX_SHADER
#define coglFragmentShader GL_FRAGMENT_SHADER
#define coglTextureRepeatX GL_TEXTURE_WRAP_S
#define coglTextureRepeatY GL_TEXTURE_WRAP_T
#define coglTextureUpscale GL_TEXTURE_MAG_FILTER
#define coglTextureDownscale GL_TEXTURE_MIN_FILTER
#define coglTextureAutoAlgin GL_UNPACK_ALIGNMENT
#define coglRepeat GL_REPEAT
#define coglNearest GL_NEAREST
#define coglLinear GL_LINEAR
#define coglTrue GL_TRUE
#define coglFalse GL_FALSE
#define coglRGB GL_RGB
#define coglRGBA GL_RGBA
#define coglR GL_R
#define coglElementArray GL_ELEMENT_ARRAY_BUFFER
#define coglVertexArray GL_ARRAY_BUFFER
#define coglFloat GL_FLOAT
#define coglInt GL_INT
#define coglDouble GL_DOUBLE
#define coglUnsignedInt GL_UNSIGNED_INT
#define coglGeometryMode GL_FRONT_AND_BACK
#define coglWireframe GL_LINE
#define coglFull GL_FILL
#define coglDepthTesting GL_DEPTH_TEST
#define coglAlways GL_ALWAYS
#define coglNever GL_NEVER
#define coglLess GL_LESS
#define coglGreater GL_GREATER
#define coglCulling GL_CULL_FACE
#define coglBack GL_BACK
#define coglFront GL_FRONT
#define coglDither GL_DITHER // dithering will be ignored on platforms with 32bit color
#define coglModelView GL_MODELVIEW
#define coglProjection GL_PROJECTION
#define coglDepthBuffer GL_DEPTH_BUFFER_BIT
#define coglColorBuffer GL_COLOR_BUFFER_BIT
#define coglStencilBuffer GL_STENCIL_BUFFER_BIT
6 changes: 3 additions & 3 deletions cogl/source/buffers.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ VertexAttributeArray::~VertexAttributeArray(){
void VertexAttributeArray::use(){
glBindVertexArray(this->VAO);
}
void VertexAttributeArray::addAttribute(GLuint index, GLint length, GLenum type, bool normalized){
void VertexAttributeArray::addAttribute(GLuint index, GLint length, cogl::dataType type, bool normalized){
GLsizei size;
switch(type){
case GL_FLOAT:
Expand Down Expand Up @@ -43,14 +43,14 @@ ArrayBuffer::ArrayBuffer(GLenum type){
glGenBuffers(1,&this->VBO);
this->bufferType = type;
}
void ArrayBuffer::setData(GLsizei size, const GLvoid* data){
void ArrayBuffer::setData(cogl::dataSize size, cogl::bufferData data){
glBindBuffer(this->bufferType,this->VBO);
glBufferData(this->bufferType,size,data,GL_STATIC_DRAW);
}
void ArrayBuffer::use(){
glBindBuffer(this->bufferType,this->VBO);
}
void ArrayBuffer::draw(GLenum geometry, GLsizei amount){
void ArrayBuffer::draw(cogl::geometryType geometry, cogl::dataSize amount){
glBindBuffer(this->bufferType,this->VBO);
switch(this->bufferType){
case GL_ELEMENT_ARRAY_BUFFER:
Expand Down
6 changes: 3 additions & 3 deletions cogl/source/graphics.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ void GraphicsSystem::clear(float r, float g, float b, float a){
glClearColor(r,g,b,a);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
}
void GraphicsSystem::clear(GLbitfield buffer){
void GraphicsSystem::clear(cogl::clearBuffer buffer){
glClear(buffer);
}
void GraphicsSystem::swapBuffers(){
Expand All @@ -32,10 +32,10 @@ void GraphicsSystem::swapBuffers(){
void GraphicsSystem::update(){
glfwPollEvents();
}
void GraphicsSystem::setViewport(GLint x, GLint y, GLsizei width, GLsizei height){
void GraphicsSystem::setViewport(GLint x, GLint y, GLint width, GLint height){
glViewport(x,y,width,height);
}
void GraphicsSystem::set(GLenum setting, bool value){
void GraphicsSystem::set(cogl::setting setting, bool value){
if(value){
glEnable(setting);
}else{
Expand Down
6 changes: 3 additions & 3 deletions cogl/source/textures.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ Texture::Texture(bool mipmaps){
glBindTexture(GL_TEXTURE_2D,this->texture);
this->mipmaps = mipmaps;
}
void Texture::load(unsigned char** data, GLenum color, int width, int height){
void Texture::load(cogl::textureData* data, cogl::colorType color, int width, int height){
glBindTexture(GL_TEXTURE_2D,this->texture);
glTexImage2D(GL_TEXTURE_2D,0,color,width,height,0,color,GL_UNSIGNED_BYTE,*data);
if(this->mipmaps)glGenerateMipmap(GL_TEXTURE_2D);
}
void Texture::set(GLenum setting, GLint value){
void Texture::set(cogl::setting setting, GLint value){
if( setting == GL_UNPACK_ALIGNMENT ||
setting == GL_UNPACK_ROW_LENGTH ||
setting == GL_UNPACK_SKIP_PIXELS||
Expand All @@ -20,7 +20,7 @@ void Texture::set(GLenum setting, GLint value){
glTexParameteri(GL_TEXTURE_2D,setting,value);
}
}
void Texture::set(GLenum setting, bool value){
void Texture::set(cogl::setting setting, bool value){
if( setting == GL_UNPACK_ALIGNMENT ||
setting == GL_UNPACK_ROW_LENGTH ||
setting == GL_UNPACK_SKIP_PIXELS||
Expand Down
28 changes: 14 additions & 14 deletions sample/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ int main(){
std::string fragmentCode = "#version 330 core\nout vec4 FragColor;\nin vec3 ourColor;\nin vec2 TexCoord;\nuniform sampler2D texture1;\nvoid main()\n{\n\tFragColor = texture(texture1, TexCoord);\n}";
std::string vertexCode = "#version 330 core\nlayout (location = 0) in vec3 aPos;\nlayout (location = 1) in vec3 aColor;\nlayout (location = 2) in vec2 aTexCoord;\nout vec3 ourColor;\nout vec2 TexCoord;\nvoid main()\n{\n\tgl_Position = vec4(aPos, 1.0);\n\tourColor = aColor;\n\tTexCoord = vec2(aTexCoord.x, aTexCoord.y);\n}";
cogl::GraphicsSystem* GSYS = new cogl::GraphicsSystem("Hello COGL");
cogl::Shader* vertexShader = new cogl::Shader(GL_VERTEX_SHADER, vertexCode);
cogl::Shader* fragmentShader = new cogl::Shader(GL_FRAGMENT_SHADER, fragmentCode);
cogl::Shader* vertexShader = new cogl::Shader(coglVertexShader, vertexCode);
cogl::Shader* fragmentShader = new cogl::Shader(coglFragmentShader, fragmentCode);
cogl::ShaderProgram* shaderProgram = new cogl::ShaderProgram();
shaderProgram->link(vertexShader);
shaderProgram->link(fragmentShader);
Expand All @@ -25,24 +25,24 @@ int main(){
};
unsigned int indices[] = {0,1,3,1,2,3};
cogl::VertexAttributeArray* VAO = new cogl::VertexAttributeArray(8);
cogl::ArrayBuffer* VBO = new cogl::ArrayBuffer(GL_ARRAY_BUFFER);
cogl::ArrayBuffer* EBO = new cogl::ArrayBuffer(GL_ELEMENT_ARRAY_BUFFER);
cogl::ArrayBuffer* VBO = new cogl::ArrayBuffer(coglVertexArray);
cogl::ArrayBuffer* EBO = new cogl::ArrayBuffer(coglElementArray);
VBO->setData(sizeof(vertices),&vertices);
EBO->setData(sizeof(indices),&indices);
VAO->addAttribute(0,3,GL_FLOAT,false); // vertices
VAO->addAttribute(1,3,GL_FLOAT,false); // colors
VAO->addAttribute(2,2,GL_FLOAT,false); // UVs
VAO->addAttribute(0,3,coglFloat,false); // vertices
VAO->addAttribute(1,3,coglFloat,false); // colors
VAO->addAttribute(2,2,coglFloat,false); // UVs
cogl::Texture* texture = new cogl::Texture(true);
texture->set(GL_TEXTURE_WRAP_S,GL_REPEAT); // some settings
texture->set(GL_TEXTURE_WRAP_T,GL_REPEAT);
texture->set(GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
texture->set(GL_TEXTURE_MAG_FILTER,GL_LINEAR);
texture->set(GL_UNPACK_ALIGNMENT,true);
texture->set(coglTextureRepeatX,coglRepeat); // some settings
texture->set(coglTextureRepeatY,coglRepeat);
texture->set(coglTextureUpscale,coglLinear);
texture->set(coglTextureDownscale,coglNearest);
texture->set(coglTextureAutoAlgin,true);
stbi_set_flip_vertically_on_load(1);
int width, height, channels;
unsigned char* data = stbi_load(IMAGE_PATH,&width,&height,&channels,0);
if(data){
texture->load(&data,GL_RGB,width,height);
texture->load(&data,coglRGB,width,height);
}else std::cout << "could not load image at: "<<IMAGE_PATH<<" - "<<stbi_failure_reason()<<std::endl;
stbi_image_free(data); // cleanup stb's data
while(GSYS->isAlive()){ // check if the user has closed the app
Expand All @@ -51,7 +51,7 @@ int main(){
texture->use();
shaderProgram->use();
VAO->use();
EBO->draw(GL_TRIANGLES,6); // draws the triangles
EBO->draw(coglTriangles,6); // draws the triangles
GSYS->swapBuffers();
GSYS->update();
}
Expand Down
3 changes: 0 additions & 3 deletions vendor.yml

This file was deleted.

0 comments on commit 8cd9db1

Please sign in to comment.