Added textures to OpenGLSharedState

This commit is contained in:
Michaël Lemaire 2013-12-22 18:05:11 +01:00
parent 645bada91b
commit 992089f0ef
13 changed files with 219 additions and 212 deletions

View file

@ -19,7 +19,7 @@ Texture2D::~Texture2D()
delete[] data; delete[] data;
} }
void Texture2D::getSize(int* xsize, int* ysize) void Texture2D::getSize(int* xsize, int* ysize) const
{ {
*xsize = this->xsize; *xsize = this->xsize;
*ysize = this->ysize; *ysize = this->ysize;
@ -33,7 +33,7 @@ void Texture2D::setPixel(int x, int y, Color col)
data[y * xsize + x] = col; data[y * xsize + x] = col;
} }
Color Texture2D::getPixel(int x, int y) Color Texture2D::getPixel(int x, int y) const
{ {
assert(x >= 0 && x < xsize); assert(x >= 0 && x < xsize);
assert(y >= 0 && y < ysize); assert(y >= 0 && y < ysize);
@ -41,7 +41,7 @@ Color Texture2D::getPixel(int x, int y)
return data[y * xsize + x]; return data[y * xsize + x];
} }
Color Texture2D::getNearest(double dx, double dy) Color Texture2D::getNearest(double dx, double dy) const
{ {
if (dx < 0.0) dx = 0.0; if (dx < 0.0) dx = 0.0;
if (dx > 1.0) dx = 1.0; if (dx > 1.0) dx = 1.0;
@ -57,7 +57,7 @@ Color Texture2D::getNearest(double dx, double dy)
return this->data[iy * this->xsize + ix]; return this->data[iy * this->xsize + ix];
} }
Color Texture2D::getLinear(double dx, double dy) Color Texture2D::getLinear(double dx, double dy) const
{ {
if (dx < 0.0) dx = 0.0; if (dx < 0.0) dx = 0.0;
if (dx > 1.0) dx = 1.0; if (dx > 1.0) dx = 1.0;
@ -89,7 +89,7 @@ Color Texture2D::getLinear(double dx, double dy)
return c1.lerp(c2, dy); return c1.lerp(c2, dy);
} }
Color Texture2D::getCubic(double dx, double dy) Color Texture2D::getCubic(double dx, double dy) const
{ {
/* TODO */ /* TODO */
return getLinear(dx, dy); return getLinear(dx, dy);
@ -122,7 +122,7 @@ void Texture2D::add(Texture2D* source)
} }
} }
void Texture2D::save(PackStream* stream) void Texture2D::save(PackStream* stream) const
{ {
int i, n; int i, n;
stream->write(&this->xsize); stream->write(&this->xsize);
@ -151,17 +151,17 @@ void Texture2D::load(PackStream* stream)
class Texture2DWriter:public PictureWriter class Texture2DWriter:public PictureWriter
{ {
public: public:
Texture2DWriter(Texture2D *tex): tex(tex) {} Texture2DWriter(const Texture2D *tex): tex(tex) {}
virtual unsigned int getPixel(int x, int y) override virtual unsigned int getPixel(int x, int y) override
{ {
return tex->getPixel(x, y).to32BitBGRA(); return tex->getPixel(x, y).to32BitBGRA();
} }
private: private:
Texture2D *tex; const Texture2D *tex;
}; };
void Texture2D::saveToFile(const std::string &filepath) void Texture2D::saveToFile(const std::string &filepath) const
{ {
Texture2DWriter writer(this); Texture2DWriter writer(this);
writer.save(filepath, xsize, ysize); writer.save(filepath, xsize, ysize);

View file

@ -12,17 +12,17 @@ public:
Texture2D(int xsize, int ysize); Texture2D(int xsize, int ysize);
~Texture2D(); ~Texture2D();
void getSize(int* xsize, int* ysize); void getSize(int* xsize, int* ysize) const;
void setPixel(int x, int y, Color col); void setPixel(int x, int y, Color col);
Color getPixel(int x, int y); Color getPixel(int x, int y) const;
Color getNearest(double dx, double dy); Color getNearest(double dx, double dy) const;
Color getLinear(double dx, double dy); Color getLinear(double dx, double dy) const;
Color getCubic(double dx, double dy); Color getCubic(double dx, double dy) const;
void fill(Color col); void fill(Color col);
void add(Texture2D* other); void add(Texture2D* other);
void save(PackStream* stream); void save(PackStream* stream) const;
void load(PackStream* stream); void load(PackStream* stream);
void saveToFile(const std::string &filepath); void saveToFile(const std::string &filepath) const;
private: private:
int xsize; int xsize;

View file

@ -20,7 +20,7 @@ Texture3D::~Texture3D()
delete[] data; delete[] data;
} }
void Texture3D::getSize(int* xsize, int* ysize, int* zsize) void Texture3D::getSize(int* xsize, int* ysize, int* zsize) const
{ {
*xsize = this->xsize; *xsize = this->xsize;
*ysize = this->ysize; *ysize = this->ysize;
@ -36,7 +36,7 @@ void Texture3D::setPixel(int x, int y, int z, Color col)
this->data[z * this->xsize * this->ysize + y * this->xsize + x] = col; this->data[z * this->xsize * this->ysize + y * this->xsize + x] = col;
} }
Color Texture3D::getPixel(int x, int y, int z) Color Texture3D::getPixel(int x, int y, int z) const
{ {
assert(x >= 0 && x < this->xsize); assert(x >= 0 && x < this->xsize);
assert(y >= 0 && y < this->ysize); assert(y >= 0 && y < this->ysize);
@ -45,7 +45,7 @@ Color Texture3D::getPixel(int x, int y, int z)
return this->data[z * this->xsize * this->ysize + y * this->xsize + x]; return this->data[z * this->xsize * this->ysize + y * this->xsize + x];
} }
Color Texture3D::getNearest(double dx, double dy, double dz) Color Texture3D::getNearest(double dx, double dy, double dz) const
{ {
if (dx < 0.0) dx = 0.0; if (dx < 0.0) dx = 0.0;
if (dx > 1.0) dx = 1.0; if (dx > 1.0) dx = 1.0;
@ -65,7 +65,7 @@ Color Texture3D::getNearest(double dx, double dy, double dz)
return this->data[iz * this->xsize * this->ysize + iy * this->xsize + ix]; return this->data[iz * this->xsize * this->ysize + iy * this->xsize + ix];
} }
Color Texture3D::getLinear(double dx, double dy, double dz) Color Texture3D::getLinear(double dx, double dy, double dz) const
{ {
if (dx < 0.0) dx = 0.0; if (dx < 0.0) dx = 0.0;
if (dx > 1.0) dx = 1.0; if (dx > 1.0) dx = 1.0;
@ -112,7 +112,7 @@ Color Texture3D::getLinear(double dx, double dy, double dz)
return cy1.lerp(cy2, dz); return cy1.lerp(cy2, dz);
} }
Color Texture3D::getCubic(double dx, double dy, double dz) Color Texture3D::getCubic(double dx, double dy, double dz) const
{ {
/* TODO */ /* TODO */
return getLinear(dx, dy, dz); return getLinear(dx, dy, dz);
@ -146,7 +146,7 @@ void Texture3D::add(Texture3D* source)
} }
} }
void Texture3D::save(PackStream* stream) void Texture3D::save(PackStream* stream) const
{ {
int i, n; int i, n;
stream->write(&this->xsize); stream->write(&this->xsize);
@ -177,7 +177,7 @@ void Texture3D::load(PackStream* stream)
class Texture3DWriter:public PictureWriter class Texture3DWriter:public PictureWriter
{ {
public: public:
Texture3DWriter(Texture3D *tex): tex(tex) {} Texture3DWriter(const Texture3D *tex): tex(tex) {}
virtual unsigned int getPixel(int x, int y) override virtual unsigned int getPixel(int x, int y) override
{ {
@ -190,10 +190,10 @@ public:
return tex->getPixel(x, y, z).to32BitBGRA(); return tex->getPixel(x, y, z).to32BitBGRA();
} }
private: private:
Texture3D *tex; const Texture3D *tex;
}; };
void Texture3D::saveToFile(const std::string &filepath) void Texture3D::saveToFile(const std::string &filepath) const
{ {
Texture3DWriter writer(this); Texture3DWriter writer(this);
writer.save(filepath, xsize, ysize * zsize); writer.save(filepath, xsize, ysize * zsize);

View file

@ -12,17 +12,17 @@ public:
Texture3D(int xsize, int ysize, int zsize); Texture3D(int xsize, int ysize, int zsize);
~Texture3D(); ~Texture3D();
void getSize(int* xsize, int* ysize, int* zsize); void getSize(int* xsize, int* ysize, int* zsize) const;
void setPixel(int x, int y, int z, Color col); void setPixel(int x, int y, int z, Color col);
Color getPixel(int x, int y, int z); Color getPixel(int x, int y, int z) const;
Color getNearest(double dx, double dy, double dz); Color getNearest(double dx, double dy, double dz) const;
Color getLinear(double dx, double dy, double dz); Color getLinear(double dx, double dy, double dz) const;
Color getCubic(double dx, double dy, double dz); Color getCubic(double dx, double dy, double dz) const;
void fill(Color col); void fill(Color col);
void add(Texture3D* other); void add(Texture3D* other);
void save(PackStream* stream); void save(PackStream* stream) const;
void load(PackStream* stream); void load(PackStream* stream);
void saveToFile(const std::string &filepath); void saveToFile(const std::string &filepath) const;
private: private:
int xsize; int xsize;

View file

@ -21,7 +21,7 @@ Texture4D::~Texture4D()
delete[] data; delete[] data;
} }
void Texture4D::getSize(int* xsize, int* ysize, int* zsize, int* wsize) void Texture4D::getSize(int* xsize, int* ysize, int* zsize, int* wsize) const
{ {
*xsize = this->xsize; *xsize = this->xsize;
*ysize = this->ysize; *ysize = this->ysize;
@ -39,7 +39,7 @@ void Texture4D::setPixel(int x, int y, int z, int w, Color col)
this->data[w * this->xsize * this->ysize * this->zsize + z * this->xsize * this->ysize + y * this->xsize + x] = col; this->data[w * this->xsize * this->ysize * this->zsize + z * this->xsize * this->ysize + y * this->xsize + x] = col;
} }
Color Texture4D::getPixel(int x, int y, int z, int w) Color Texture4D::getPixel(int x, int y, int z, int w) const
{ {
assert(x >= 0 && x < this->xsize); assert(x >= 0 && x < this->xsize);
assert(y >= 0 && y < this->ysize); assert(y >= 0 && y < this->ysize);
@ -49,7 +49,7 @@ Color Texture4D::getPixel(int x, int y, int z, int w)
return this->data[w * this->xsize * this->ysize * this->zsize + z * this->xsize * this->ysize + y * this->xsize + x]; return this->data[w * this->xsize * this->ysize * this->zsize + z * this->xsize * this->ysize + y * this->xsize + x];
} }
Color Texture4D::getNearest(double dx, double dy, double dz, double dw) Color Texture4D::getNearest(double dx, double dy, double dz, double dw) const
{ {
if (dx < 0.0) dx = 0.0; if (dx < 0.0) dx = 0.0;
if (dx > 1.0) dx = 1.0; if (dx > 1.0) dx = 1.0;
@ -73,7 +73,7 @@ Color Texture4D::getNearest(double dx, double dy, double dz, double dw)
return this->data[iw * this->xsize * this->ysize * this->zsize + iz * this->xsize * this->ysize + iy * this->xsize + ix]; return this->data[iw * this->xsize * this->ysize * this->zsize + iz * this->xsize * this->ysize + iy * this->xsize + ix];
} }
Color Texture4D::getLinear(double dx, double dy, double dz, double dw) Color Texture4D::getLinear(double dx, double dy, double dz, double dw) const
{ {
if (dx < 0.0) dx = 0.0; if (dx < 0.0) dx = 0.0;
if (dx > 1.0) dx = 1.0; if (dx > 1.0) dx = 1.0;
@ -143,7 +143,7 @@ Color Texture4D::getLinear(double dx, double dy, double dz, double dw)
return cz1.lerp(cz2, dw); return cz1.lerp(cz2, dw);
} }
Color Texture4D::getCubic(double dx, double dy, double dz, double dw) Color Texture4D::getCubic(double dx, double dy, double dz, double dw) const
{ {
/* TODO */ /* TODO */
return getLinear(dx, dy, dz, dw); return getLinear(dx, dy, dz, dw);
@ -178,7 +178,7 @@ void Texture4D::add(Texture4D* source)
} }
} }
void Texture4D::save(PackStream* stream) void Texture4D::save(PackStream* stream) const
{ {
int i, n; int i, n;
stream->write(&this->xsize); stream->write(&this->xsize);
@ -212,7 +212,7 @@ void Texture4D::load(PackStream* stream)
class Texture4DWriter:public PictureWriter class Texture4DWriter:public PictureWriter
{ {
public: public:
Texture4DWriter(Texture4D *tex): tex(tex) {} Texture4DWriter(const Texture4D *tex): tex(tex) {}
virtual unsigned int getPixel(int x, int y) override virtual unsigned int getPixel(int x, int y) override
{ {
@ -228,10 +228,10 @@ public:
return tex->getPixel(x, y, z, w).to32BitBGRA(); return tex->getPixel(x, y, z, w).to32BitBGRA();
} }
private: private:
Texture4D *tex; const Texture4D *tex;
}; };
void Texture4D::saveToFile(const std::string &filepath) void Texture4D::saveToFile(const std::string &filepath) const
{ {
Texture4DWriter writer(this); Texture4DWriter writer(this);
writer.save(filepath, xsize * wsize, ysize * zsize); writer.save(filepath, xsize * wsize, ysize * zsize);

View file

@ -12,17 +12,17 @@ public:
Texture4D(int xsize, int ysize, int zsize, int wsize); Texture4D(int xsize, int ysize, int zsize, int wsize);
~Texture4D(); ~Texture4D();
void getSize(int* xsize, int* ysize, int* zsize, int* wsize); void getSize(int* xsize, int* ysize, int* zsize, int* wsize) const;
void setPixel(int x, int y, int z, int w, Color col); void setPixel(int x, int y, int z, int w, Color col);
Color getPixel(int x, int y, int z, int w); Color getPixel(int x, int y, int z, int w) const;
Color getNearest(double dx, double dy, double dz, double dw); Color getNearest(double dx, double dy, double dz, double dw) const;
Color getLinear(double dx, double dy, double dz, double dw); Color getLinear(double dx, double dy, double dz, double dw) const;
Color getCubic(double dx, double dy, double dz, double dw); Color getCubic(double dx, double dy, double dz, double dw) const;
void fill(Color col); void fill(Color col);
void add(Texture4D* other); void add(Texture4D* other);
void save(PackStream* stream); void save(PackStream* stream) const;
void load(PackStream* stream); void load(PackStream* stream);
void saveToFile(const std::string &filepath); void saveToFile(const std::string &filepath) const;
private: private:
int xsize; int xsize;

View file

@ -44,157 +44,12 @@ void OpenGLShaderProgram::compile()
} }
} }
void OpenGLShaderProgram::addTexture(QString sampler_name, Texture2D* texture)
{
GLuint texid;
if (textures.contains(sampler_name))
{
texid = textures[sampler_name].second;
}
else
{
functions->glGenTextures(1, &texid);
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_2D, texid);
}
functions->glBindTexture(GL_TEXTURE_2D, texid);
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int sx, sy;
texture->getSize(&sx, &sy);
float* pixels = new float[sx * sy * 4];
for (int x = 0; x < sx; x++)
{
for (int y = 0; y < sy; y++)
{
float* pixel = pixels + (y * sx + x) * 4;
Color col = texture->getPixel(x, y);
pixel[0] = (float)col.r;
pixel[1] = (float)col.g;
pixel[2] = (float)col.b;
pixel[3] = (float)col.a;
}
}
functions->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sx, sy, 0, GL_RGBA, GL_FLOAT, pixels);
delete[] pixels;
}
void OpenGLShaderProgram::addTexture(QString sampler_name, Texture3D* texture)
{
GLuint texid;
if (textures.contains(sampler_name))
{
texid = textures[sampler_name].second;
}
else
{
functions->glGenTextures(1, &texid);
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid);
}
functions->glBindTexture(GL_TEXTURE_3D, texid);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
int sx, sy, sz;
texture->getSize(&sx, &sy, &sz);
float* pixels = new float[sx * sy * sz * 4];
for (int x = 0; x < sx; x++)
{
for (int y = 0; y < sy; y++)
{
for (int z = 0; z < sz; z++)
{
float* pixel = pixels + (z * (sx * sy) + y * sx + x) * 4;
Color col = texture->getPixel(x, y, z);
pixel[0] = (float)col.r;
pixel[1] = (float)col.g;
pixel[2] = (float)col.b;
pixel[3] = (float)col.a;
}
}
}
functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz, 0, GL_RGBA, GL_FLOAT, pixels);
delete[] pixels;
}
void OpenGLShaderProgram::addTexture(QString sampler_name, Texture4D* texture)
{
GLuint texid;
if (textures.contains(sampler_name))
{
texid = textures[sampler_name].second;
}
else
{
functions->glGenTextures(1, &texid);
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid);
}
functions->glBindTexture(GL_TEXTURE_3D, texid);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
int sx, sy, sz, sw;
texture->getSize(&sx, &sy, &sz, &sw);
float* pixels = new float[sx * sy * sz * sw * 4];
for (int x = 0; x < sx; x++)
{
for (int y = 0; y < sy; y++)
{
for (int z = 0; z < sz; z++)
{
for (int w = 0; w < sw; w++)
{
float* pixel = pixels + (w * (sx * sy * sz) + z * (sx * sy) + y * sx + x) * 4;
Color col = texture->getPixel(x, y, z, w);
pixel[0] = (float)col.r;
pixel[1] = (float)col.g;
pixel[2] = (float)col.b;
pixel[3] = (float)col.a;
}
}
}
}
functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz * sw, 0, GL_RGBA, GL_FLOAT, pixels);
delete[] pixels;
}
void OpenGLShaderProgram::bind() void OpenGLShaderProgram::bind()
{ {
program->bind(); program->bind();
renderer->getSharedState()->apply(this); int texture_unit = 0;
renderer->getSharedState()->apply(this, texture_unit);
QMapIterator<QString, QPair<int, unsigned int> > iter(textures);
int i = 0;
while (iter.hasNext())
{
iter.next();
int textureSampler = program->uniformLocation(iter.key());
if (textureSampler >= 0)
{
functions->glActiveTexture(GL_TEXTURE0 + i);
functions->glBindTexture(iter.value().first, iter.value().second);
program->setUniformValue(textureSampler, i);
i++;
}
}
} }
void OpenGLShaderProgram::release() void OpenGLShaderProgram::release()

View file

@ -4,10 +4,6 @@
#include "opengl_global.h" #include "opengl_global.h"
#include <QString> #include <QString>
#include <QMatrix4x4>
#include <QColor>
#include <QMap>
#include <QPair>
class QOpenGLShaderProgram; class QOpenGLShaderProgram;
class QOpenGLFunctions_3_2_Core; class QOpenGLFunctions_3_2_Core;
@ -25,15 +21,12 @@ public:
void addFragmentSource(QString path); void addFragmentSource(QString path);
void compile(); void compile();
void addTexture(QString sampler_name, Texture2D* texture);
void addTexture(QString sampler_name, Texture3D* texture);
void addTexture(QString sampler_name, Texture4D* texture);
void drawTriangles(float* vertices, int triangle_count); void drawTriangles(float* vertices, int triangle_count);
void drawTriangleStrip(float* vertices, int vertex_count); void drawTriangleStrip(float* vertices, int vertex_count);
protected: protected:
inline QOpenGLShaderProgram* getProgram() const {return program;} inline QOpenGLShaderProgram* getProgram() const {return program;}
inline OpenGLRenderer* getRenderer() const {return renderer;}
friend class OpenGLVariable; friend class OpenGLVariable;
private: private:
@ -45,8 +38,6 @@ private:
QString name; QString name;
QOpenGLShaderProgram* program; QOpenGLShaderProgram* program;
QOpenGLFunctions_3_2_Core* functions; QOpenGLFunctions_3_2_Core* functions;
QMap<QString, QPair<int, unsigned int> > textures;
}; };
} }

View file

@ -4,11 +4,11 @@ OpenGLSharedState::OpenGLSharedState()
{ {
} }
void OpenGLSharedState::apply(OpenGLShaderProgram *program) void OpenGLSharedState::apply(OpenGLShaderProgram *program, int &texture_unit)
{ {
for (const auto &pair : variables) for (const auto &pair : variables)
{ {
pair.second->apply(program); pair.second->apply(program, texture_unit);
} }
} }

View file

@ -20,7 +20,7 @@ public:
/*! /*!
* \brief Apply the stored variables to the bound program. * \brief Apply the stored variables to the bound program.
*/ */
void apply(OpenGLShaderProgram* program); void apply(OpenGLShaderProgram* program, int &texture_unit);
/*! /*!
* \brief Get or create a variable in the state. * \brief Get or create a variable in the state.

View file

@ -59,8 +59,8 @@ void OpenGLSkybox::update()
renderer->getSharedState()->set("sunColor", sun_color); renderer->getSharedState()->set("sunColor", sun_color);
SoftwareBrunetonAtmosphereRenderer* bruneton = (SoftwareBrunetonAtmosphereRenderer*)renderer->getAtmosphereRenderer(); SoftwareBrunetonAtmosphereRenderer* bruneton = (SoftwareBrunetonAtmosphereRenderer*)renderer->getAtmosphereRenderer();
program->addTexture("transmittanceTexture", bruneton->getModel()->getTextureTransmittance()); renderer->getSharedState()->set("transmittanceTexture", bruneton->getModel()->getTextureTransmittance());
program->addTexture("inscatterTexture", bruneton->getModel()->getTextureInscatter()); renderer->getSharedState()->set("inscatterTexture", bruneton->getModel()->getTextureInscatter());
} }
void OpenGLSkybox::render() void OpenGLSkybox::render()

View file

@ -2,20 +2,33 @@
#include <cassert> #include <cassert>
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QOpenGLFunctions_3_2_Core>
#include "OpenGLRenderer.h"
#include "OpenGLShaderProgram.h" #include "OpenGLShaderProgram.h"
#include "Vector3.h" #include "Vector3.h"
#include "Matrix4.h" #include "Matrix4.h"
#include "Color.h" #include "Color.h"
#include "Texture2D.h"
#include "Texture3D.h"
#include "Texture4D.h"
OpenGLVariable::OpenGLVariable(const std::string &name): OpenGLVariable::OpenGLVariable(const std::string &name):
name(name) name(name)
{ {
type = TYPE_NONE; type = TYPE_NONE;
texture_toupload = false;
} }
void OpenGLVariable::apply(OpenGLShaderProgram *program) void OpenGLVariable::apply(OpenGLShaderProgram *program, int &texture_unit)
{ {
QOpenGLShaderProgram* pr = program->getProgram(); QOpenGLShaderProgram* pr = program->getProgram();
QOpenGLFunctions_3_2_Core* functions = program->getRenderer()->getOpenGlFunctions();
if (texture_toupload)
{
uploadTexture(program->getRenderer());
texture_toupload = false;
}
switch (type) switch (type)
{ {
@ -31,11 +44,51 @@ void OpenGLVariable::apply(OpenGLShaderProgram *program)
case TYPE_MATRIX4: case TYPE_MATRIX4:
pr->setUniformValue(name.c_str(), value_matrix4); pr->setUniformValue(name.c_str(), value_matrix4);
break; break;
case TYPE_TEXTURE_2D:
functions->glActiveTexture(GL_TEXTURE0 + texture_unit);
functions->glBindTexture(GL_TEXTURE_2D, texture_id);
pr->setUniformValue(name.c_str(), texture_unit);
texture_unit++;
break;
case TYPE_TEXTURE_3D:
case TYPE_TEXTURE_4D:
functions->glActiveTexture(GL_TEXTURE0 + texture_unit);
functions->glBindTexture(GL_TEXTURE_3D, texture_id);
pr->setUniformValue(name.c_str(), texture_unit);
texture_unit++;
break;
case TYPE_NONE: case TYPE_NONE:
break; break;
} }
} }
void OpenGLVariable::set(const Texture2D *texture)
{
assert(type == TYPE_NONE or type == TYPE_TEXTURE_2D);
type = TYPE_TEXTURE_2D;
value_tex2d = texture;
texture_toupload = true;
}
void OpenGLVariable::set(const Texture3D *texture)
{
assert(type == TYPE_NONE or type == TYPE_TEXTURE_3D);
type = TYPE_TEXTURE_3D;
value_tex3d = texture;
texture_toupload = true;
}
void OpenGLVariable::set(const Texture4D *texture)
{
assert(type == TYPE_NONE or type == TYPE_TEXTURE_4D);
type = TYPE_TEXTURE_4D;
value_tex4d = texture;
texture_toupload = true;
}
void OpenGLVariable::set(float value) void OpenGLVariable::set(float value)
{ {
assert(type == TYPE_NONE or type == TYPE_FLOAT); assert(type == TYPE_NONE or type == TYPE_FLOAT);
@ -77,3 +130,102 @@ void OpenGLVariable::set(const Color &color)
type = TYPE_COLOR; type = TYPE_COLOR;
value_color = QColor(color.r, color.g, color.b); value_color = QColor(color.r, color.g, color.b);
} }
void OpenGLVariable::uploadTexture(OpenGLRenderer* renderer)
{
QOpenGLFunctions_3_2_Core* functions = renderer->getOpenGlFunctions();
assert(type == TYPE_TEXTURE_2D or type == TYPE_TEXTURE_3D or type == TYPE_TEXTURE_4D);
if (texture_id == 0)
{
GLuint texid;
functions->glGenTextures(1, &texid);
texture_id = texid;
}
GLenum textype = (type == TYPE_TEXTURE_2D) ? GL_TEXTURE_2D : GL_TEXTURE_3D;
functions->glBindTexture(textype, texture_id);
functions->glTexParameteri(textype, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
functions->glTexParameteri(textype, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
functions->glTexParameteri(textype, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
functions->glTexParameteri(textype, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
if (textype == GL_TEXTURE_3D)
{
functions->glTexParameteri(textype, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
if (type == TYPE_TEXTURE_2D)
{
int sx, sy;
value_tex2d->getSize(&sx, &sy);
float* pixels = new float[sx * sy * 4];
for (int x = 0; x < sx; x++)
{
for (int y = 0; y < sy; y++)
{
float* pixel = pixels + (y * sx + x) * 4;
Color col = value_tex2d->getPixel(x, y);
pixel[0] = (float)col.r;
pixel[1] = (float)col.g;
pixel[2] = (float)col.b;
pixel[3] = (float)col.a;
}
}
functions->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sx, sy, 0, GL_RGBA, GL_FLOAT, pixels);
delete[] pixels;
}
else if (type == TYPE_TEXTURE_3D)
{
int sx, sy, sz;
value_tex3d->getSize(&sx, &sy, &sz);
float* pixels = new float[sx * sy * sz * 4];
for (int x = 0; x < sx; x++)
{
for (int y = 0; y < sy; y++)
{
for (int z = 0; z < sz; z++)
{
float* pixel = pixels + (z * (sx * sy) + y * sx + x) * 4;
Color col = value_tex3d->getPixel(x, y, z);
pixel[0] = (float)col.r;
pixel[1] = (float)col.g;
pixel[2] = (float)col.b;
pixel[3] = (float)col.a;
}
}
}
functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz, 0, GL_RGBA, GL_FLOAT, pixels);
delete[] pixels;
}
else
{
int sx, sy, sz, sw;
value_tex4d->getSize(&sx, &sy, &sz, &sw);
float* pixels = new float[sx * sy * sz * sw * 4];
for (int x = 0; x < sx; x++)
{
for (int y = 0; y < sy; y++)
{
for (int z = 0; z < sz; z++)
{
for (int w = 0; w < sw; w++)
{
float* pixel = pixels + (w * (sx * sy * sz) + z * (sx * sy) + y * sx + x) * 4;
Color col = value_tex4d->getPixel(x, y, z, w);
pixel[0] = (float)col.r;
pixel[1] = (float)col.g;
pixel[2] = (float)col.b;
pixel[3] = (float)col.a;
}
}
}
}
functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz * sw, 0, GL_RGBA, GL_FLOAT, pixels);
delete[] pixels;
}
}

View file

@ -30,7 +30,7 @@ public:
public: public:
OpenGLVariable(const std::string &name); OpenGLVariable(const std::string &name);
void apply(OpenGLShaderProgram *program); void apply(OpenGLShaderProgram *program, int &texture_unit);
void set(const Texture2D *texture); void set(const Texture2D *texture);
void set(const Texture3D *texture); void set(const Texture3D *texture);
@ -42,6 +42,9 @@ public:
void set(const QMatrix4x4 &matrix); void set(const QMatrix4x4 &matrix);
void set(const Color &color); void set(const Color &color);
protected:
void uploadTexture(OpenGLRenderer* renderer);
private: private:
std::string name; std::string name;
OpenGLVariableType type; OpenGLVariableType type;
@ -50,6 +53,12 @@ private:
QColor value_color; QColor value_color;
QVector3D value_vector3; QVector3D value_vector3;
QMatrix4x4 value_matrix4; QMatrix4x4 value_matrix4;
const Texture2D* value_tex2d;
const Texture3D* value_tex3d;
const Texture4D* value_tex4d;
bool texture_toupload;
unsigned int texture_id;
}; };
} }