Factorise cubevbo/vao in shader.cpp

This commit is contained in:
vlj 2014-03-22 23:09:33 +01:00
parent 6538d16eb3
commit c0b2b23692
3 changed files with 75 additions and 71 deletions

View File

@ -870,74 +870,6 @@ void IrrDriver::renderLights(const core::aabbox3df& cambox,
m_video_driver->setRenderTarget(m_rtts->getRTT(RTT_COLOR), false, false);
}
static GLuint cubevao = 0;
static GLuint cubevbo;
static GLuint cubeidx;
static void createcubevao()
{
// From CSkyBoxSceneNode
float corners[] =
{
// top side
1., 1., -1.,
1., 1., 1.,
-1., 1., 1.,
-1., 1., -1.,
// Bottom side
1., -1., 1.,
1., -1., -1.,
-1., -1., -1.,
-1., -1., 1.,
// right side
1., -1, -1,
1., -1, 1,
1., 1., 1.,
1., 1., -1.,
// left side
-1., -1., 1.,
-1., -1., -1.,
-1., 1., -1.,
-1., 1., 1.,
// back side
-1., -1., -1.,
1., -1, -1.,
1, 1, -1.,
-1, 1, -1.,
// front side
1., -1., 1.,
-1., -1., 1.,
-1, 1., 1.,
1., 1., 1.,
};
int indices[] = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
8, 9, 10, 10, 11, 8,
12, 13, 14, 14, 15, 12,
16, 17, 18, 18, 19, 16,
20, 21, 22, 22, 23, 20
};
glGenBuffers(1, &cubevbo);
glGenBuffers(1, &cubeidx);
glGenVertexArrays(1, &cubevao);
glBindVertexArray(cubevao);
glBindBuffer(GL_ARRAY_BUFFER, cubevbo);
glBufferData(GL_ARRAY_BUFFER, 6 * 4 * 3 * sizeof(float), corners, GL_STATIC_DRAW);
glEnableVertexAttribArray(MeshShader::SkyboxShader::attrib_position);
glVertexAttribPointer(MeshShader::SkyboxShader::attrib_position, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, cubeidx);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * 6 * sizeof(int), indices, GL_STATIC_DRAW);
}
static void getXYZ(GLenum face, float i, float j, float &x, float &y, float &z)
{
switch (face)
@ -1330,11 +1262,9 @@ void IrrDriver::renderSkybox()
if (SkyboxTextures.empty()) return;
scene::ICameraSceneNode *camera = m_scene_manager->getActiveCamera();
if (!cubevao)
createcubevao();
if (!SkyboxCubeMap)
generateSkyboxCubemap();
glBindVertexArray(cubevao);
glBindVertexArray(MeshShader::SkyboxShader::cubevao);
glDisable(GL_CULL_FACE);
assert(SkyboxTextures.size() == 6);
core::matrix4 transform = irr_driver->getProjViewMatrix();

View File

@ -105,6 +105,68 @@ static void initBillboardVBO()
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), quad, GL_STATIC_DRAW);
}
GLuint SharedObject::cubevbo = 0;
GLuint SharedObject::cubeindexes = 0;
static void initCubeVBO()
{
// From CSkyBoxSceneNode
float corners[] =
{
// top side
1., 1., -1.,
1., 1., 1.,
-1., 1., 1.,
-1., 1., -1.,
// Bottom side
1., -1., 1.,
1., -1., -1.,
-1., -1., -1.,
-1., -1., 1.,
// right side
1., -1, -1,
1., -1, 1,
1., 1., 1.,
1., 1., -1.,
// left side
-1., -1., 1.,
-1., -1., -1.,
-1., 1., -1.,
-1., 1., 1.,
// back side
-1., -1., -1.,
1., -1, -1.,
1, 1, -1.,
-1, 1, -1.,
// front side
1., -1., 1.,
-1., -1., 1.,
-1, 1., 1.,
1., 1., 1.,
};
int indices[] = {
0, 1, 2, 2, 3, 0,
4, 5, 6, 6, 7, 4,
8, 9, 10, 10, 11, 8,
12, 13, 14, 14, 15, 12,
16, 17, 18, 18, 19, 16,
20, 21, 22, 22, 23, 20
};
glGenBuffers(1, &SharedObject::cubevbo);
glBindBuffer(GL_ARRAY_BUFFER, SharedObject::cubevbo);
glBufferData(GL_ARRAY_BUFFER, 6 * 4 * 3 * sizeof(float), corners, GL_STATIC_DRAW);
glGenBuffers(1, &SharedObject::cubeindexes);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedObject::cubeindexes);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6 * 6 * sizeof(int), indices, GL_STATIC_DRAW);
}
void Shaders::loadShaders()
{
const std::string &dir = file_manager->getAsset(FileManager::SHADER, "");
@ -232,6 +294,7 @@ void Shaders::loadShaders()
initQuadVBO();
initQuadBuffer();
initBillboardVBO();
initCubeVBO();
FullScreenShader::BloomBlendShader::init();
FullScreenShader::BloomShader::init();
FullScreenShader::ColorLevelShader::init();
@ -1206,6 +1269,7 @@ namespace MeshShader
GLuint SkyboxShader::uniform_tex;
GLuint SkyboxShader::uniform_screen;
GLuint SkyboxShader::uniform_InvProjView;
GLuint SkyboxShader::cubevao;
void SkyboxShader::init()
{
@ -1217,6 +1281,14 @@ namespace MeshShader
uniform_InvProjView = glGetUniformLocation(Program, "InvProjView");
uniform_tex = glGetUniformLocation(Program, "tex");
uniform_screen = glGetUniformLocation(Program, "screen");
glGenVertexArrays(1, &cubevao);
glBindVertexArray(cubevao);
glBindBuffer(GL_ARRAY_BUFFER, SharedObject::cubevbo);
glEnableVertexAttribArray(attrib_position);
glVertexAttribPointer(attrib_position, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, SharedObject::cubeindexes);
glBindVertexArray(0);
}
void SkyboxShader::setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &InvProjView, const core::vector2df &screen, unsigned TU_tex)

View File

@ -29,6 +29,7 @@ class SharedObject
{
public:
static GLuint billboardvbo;
static GLuint cubevbo, cubeindexes;
};
namespace MeshShader
{
@ -312,6 +313,7 @@ public:
static GLuint Program;
static GLuint attrib_position;
static GLuint uniform_MVP, uniform_InvProjView, uniform_tex, uniform_screen;
static GLuint cubevao;
static void init();
static void setUniforms(const core::matrix4 &ModelViewProjectionMatrix, const core::matrix4 &InvProjView, const core::vector2df &screen, unsigned TU_tex);