Cubemap class

This commit is contained in:
MihailRis
2024-03-07 13:40:38 +03:00
parent 87edb1d45e
commit eefd327845
10 changed files with 105 additions and 41 deletions
+1 -1
View File
@@ -19,7 +19,7 @@ Batch2D::Batch2D(size_t capacity) : capacity(capacity), color(1.0f){
ubyte pixels[] = {
0xFF, 0xFF, 0xFF, 0xFF
};
blank = std::make_unique<Texture>(pixels, 1, 1, GL_RGBA);
blank = std::make_unique<Texture>(pixels, 1, 1, ImageFormat::rgba8888);
_texture = nullptr;
}
+2 -4
View File
@@ -19,20 +19,18 @@ Batch3D::Batch3D(size_t capacity)
};
buffer = new float[capacity * B3D_VERTEX_SIZE];
mesh = new Mesh(buffer, 0, attrs);
mesh = std::make_unique<Mesh>(buffer, 0, attrs);
index = 0;
ubyte pixels[] = {
255, 255, 255, 255,
};
blank = new Texture(pixels, 1, 1, GL_RGBA);
blank = std::make_unique<Texture>(pixels, 1, 1, ImageFormat::rgba8888);
_texture = nullptr;
}
Batch3D::~Batch3D(){
delete blank;
delete[] buffer;
delete mesh;
}
void Batch3D::begin(){
+7 -5
View File
@@ -1,21 +1,23 @@
#ifndef GRAPHICS_BATCH3D_H_
#define GRAPHICS_BATCH3D_H_
#include <stdlib.h>
#include <glm/glm.hpp>
#include "UVRegion.h"
#include "../typedefs.h"
#include <memory>
#include <stdlib.h>
#include <glm/glm.hpp>
class Mesh;
class Texture;
class Batch3D {
float* buffer;
size_t capacity;
Mesh* mesh;
std::unique_ptr<Mesh> mesh;
std::unique_ptr<Texture> blank;
size_t index;
Texture* blank;
Texture* _texture;
void vertex(float x, float y, float z,
+39
View File
@@ -0,0 +1,39 @@
#include "Cubemap.h"
#include "gl_util.h"
#include <GL/glew.h>
Cubemap::Cubemap(uint width, uint height, ImageFormat imageFormat)
: Texture(0, width, height)
{
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
uint format = gl::to_gl_format(imageFormat);
for (uint face = 0; face < 6; face++) {
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + face,
0,
format,
width,
height,
0,
format,
GL_UNSIGNED_BYTE,
NULL
);
}
}
void Cubemap::bind(){
glBindTexture(GL_TEXTURE_CUBE_MAP, id);
}
void Cubemap::unbind() {
glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
}
+14
View File
@@ -0,0 +1,14 @@
#ifndef GRAPHICS_CUBEMAP_H_
#define GRAPHICS_CUBEMAP_H_
#include "Texture.h"
class Cubemap : public Texture {
public:
Cubemap(uint width, uint height, ImageFormat format);
virtual void bind() override;
virtual void unbind() override;
};
#endif // GRAPHICS_CUBEMAP_H_
+9 -9
View File
@@ -4,16 +4,19 @@
#include <memory>
#include "ImageData.h"
#include "gl_util.h"
Texture::Texture(uint id, uint width, uint height)
: id(id), width(width), height(height) {
}
Texture::Texture(ubyte* data, uint width, uint height, uint format)
Texture::Texture(ubyte* data, uint width, uint height, ImageFormat imageFormat)
: width(width), height(height) {
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
GLenum format = gl::to_gl_format(imageFormat);
glTexImage2D(
GL_TEXTURE_2D, 0, format, width, height, 0,
format, GL_UNSIGNED_BYTE, (GLvoid *) data
@@ -33,6 +36,10 @@ void Texture::bind(){
glBindTexture(GL_TEXTURE_2D, id);
}
void Texture::unbind() {
glBindTexture(GL_TEXTURE_2D, 0);
}
void Texture::reload(ubyte* data){
glBindTexture(GL_TEXTURE_2D, id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
@@ -58,15 +65,8 @@ void Texture::setNearestFilter() {
Texture* Texture::from(const ImageData* image) {
uint width = image->getWidth();
uint height = image->getHeight();
uint format;
const void* data = image->getData();
switch (image->getFormat()) {
case ImageFormat::rgb888: format = GL_RGB; break;
case ImageFormat::rgba8888: format = GL_RGBA; break;
default:
throw std::runtime_error("unsupported image data format");
}
return new Texture((ubyte*)data, width, height, format);
return new Texture((ubyte*)data, width, height, image->getFormat());
}
uint Texture::getWidth() const {
+3 -3
View File
@@ -3,8 +3,7 @@
#include <string>
#include "../typedefs.h"
class ImageData;
#include "ImageData.h"
class Texture {
protected:
@@ -13,10 +12,11 @@ protected:
uint height;
public:
Texture(uint id, uint width, uint height);
Texture(ubyte* data, uint width, uint height, uint format);
Texture(ubyte* data, uint width, uint height, ImageFormat format);
virtual ~Texture();
virtual void bind();
virtual void unbind();
virtual void reload(ubyte* data);
void setNearestFilter();
+19
View File
@@ -0,0 +1,19 @@
#ifndef GRAPHICS_GL_UTIL_H_
#define GRAPHICS_GL_UTIL_H_
#include <GL/glew.h>
#include "ImageData.h"
namespace gl {
inline GLenum to_gl_format(ImageFormat imageFormat) {
switch (imageFormat) {
case ImageFormat::rgb888: return GL_RGB;
case ImageFormat::rgba8888: return GL_RGBA;
default:
return 0;
}
}
}
#endif // GRAPHICS_GL_UTIL_H_