Simpilify skybox

This commit is contained in:
zhukaixy 2015-01-29 17:31:57 +08:00
parent ee99700ec1
commit f3536056d8
6 changed files with 62 additions and 186 deletions

View File

@ -237,10 +237,10 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClCompile Include="..\3d\CCPlane.cpp" />
<ClCompile Include="..\3d\CCRay.cpp" />
<ClCompile Include="..\3d\CCSkeleton3D.cpp" />
<ClCompile Include="..\3d\CCSkybox.cpp" />
<ClCompile Include="..\3d\CCSprite3D.cpp" />
<ClCompile Include="..\3d\CCSprite3DMaterial.cpp" />
<ClCompile Include="..\3d\CCTextureCube.cpp" />
<ClCompile Include="..\3d\CCSkybox.cpp" />
<ClCompile Include="..\audio\AudioEngine.cpp" />
<ClCompile Include="..\audio\win32\AudioCache.cpp" />
<ClCompile Include="..\audio\win32\AudioEngine-win32.cpp" />
@ -608,11 +608,11 @@ xcopy /Y /Q "$(ProjectDir)..\..\external\chipmunk\prebuilt\win32\release-lib\*.*
<ClInclude Include="..\3d\CCPlane.h" />
<ClInclude Include="..\3d\CCRay.h" />
<ClInclude Include="..\3d\CCSkeleton3D.h" />
<ClInclude Include="..\3d\CCSkybox.h" />
<ClInclude Include="..\3d\CCSprite3D.h" />
<ClInclude Include="..\3d\CCSprite3DMaterial.h" />
<ClInclude Include="..\3d\CCTextureCube.h" />
<ClInclude Include="..\3d\cocos3d.h" />
<ClInclude Include="..\3d\CCSkybox.h" />
<ClInclude Include="..\audio\include\AudioEngine.h" />
<ClInclude Include="..\audio\include\Export.h" />
<ClInclude Include="..\audio\include\SimpleAudioEngine.h" />

View File

@ -1301,12 +1301,14 @@
<ClCompile Include="..\3d\CCFrustum.cpp" />
<ClCompile Include="..\3d\CCPlane.cpp" />
<ClCompile Include="..\3d\CCAABB.cpp">
<ClCompile Include="..\3d\CCTextureCube.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="..\3d\CCSkybox.cpp">
<Filter>3d</Filter>
</ClCompile>
<ClCompile Include="..\3d\CCTextureCube.cpp">
<Filter>3d</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\physics\CCPhysicsBody.h">
@ -2551,12 +2553,13 @@
<ClInclude Include="..\3d\CCPlane.h" />
<ClInclude Include="..\physics\CCPhysicsHelper.h">
<Filter>physics</Filter>
<ClInclude Include="..\3d\CCTextureCube.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="..\3d\CCSkybox.h">
<Filter>3d</Filter>
</ClInclude>
<ClInclude Include="..\3d\CCTextureCube.h">
<Filter>3d</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="..\math\Mat4.inl">

View File

@ -26,25 +26,21 @@
NS_CC_BEGIN
Skybox::Skybox()
: _vao(0)
, _vbo(0)
, _bufferCapacity(0)
, _bufferCount(0)
, _buffer(nullptr)
, _dirty(false)
, _vio(0)
,_texture(nullptr)
{
}
Skybox::~Skybox()
{
free(_buffer);
_buffer = nullptr;
glDeleteBuffers(1, &_vbo);
glDeleteBuffers(1, &_vio);
_vbo = 0;
_vio = 0;
if (Configuration::getInstance()->supportsShareableVAO())
{
@ -56,86 +52,57 @@ Skybox::~Skybox()
_texture->release();
}
Skybox* Skybox::create()
{
Skybox* ret = new (std::nothrow) Skybox();
if (ret && ret->init())
{
ret->autorelease();
}
else
{
CC_SAFE_DELETE(ret);
}
return ret;
}
void Skybox::ensureCapacity(int count)
{
CCASSERT(count>=0, "capacity must be >= 0");
if(_bufferCount + count > _bufferCapacity)
{
_bufferCapacity += MAX(_bufferCapacity, count);
_buffer = (V3F_C4B*)realloc(_buffer, _bufferCapacity*sizeof(V3F_C4B));
}
}
bool Skybox::init()
{
//setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
//create and set our custom shader
// create and set our custom shader
auto shader = GLProgram::createWithFilenames("Sprite3DTest/skybox.vert", "Sprite3DTest/skybox.frag");
auto state = GLProgramState::create(shader);
setGLProgramState(state);
ensureCapacity(512);
if (Configuration::getInstance()->supportsShareableVAO())
{
glGenVertexArrays(1, &_vao);
GL::bindVAO(_vao);
}
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B)* _bufferCapacity, _buffer, GL_STREAM_DRAW);
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B), (GLvoid*)offsetof(V3F_C4B, vertices));
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_COLOR);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B), (GLvoid*)offsetof(V3F_C4B, colors));
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_TEX_COORD);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD,2,GL_FLOAT,GL_FALSE,sizeof(V3F_C4B),(GLvoid*)offsetof(V3F_C4B, texcoord));
glBindBuffer(GL_ARRAY_BUFFER, 0);
initBuffers();
if (Configuration::getInstance()->supportsShareableVAO())
{
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
GL::bindVAO(0);
}
CHECK_GL_ERROR_DEBUG();
_dirty = true;
#if CC_ENABLE_CACHE_TEXTURE_DATA
// Need to listen the event only when not use batchnode, because it will use VBO
auto listener = EventListenerCustom::create(EVENT_COME_TO_FOREGROUND, [this](EventCustom* event)
{
/** listen the event that coming to foreground on Android */
this->init();
});
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
#endif
return true;
}
void Skybox::initBuffers()
{
// init vertex buffer object
Vec3 vexBuf[] =
{
Vec3(1, -1, 1), Vec3(1, 1, 1), Vec3(-1, 1, 1), Vec3(-1, -1, 1),
Vec3(1, -1, -1), Vec3(1, 1, -1), Vec3(-1, 1, -1), Vec3(-1, -1, -1)
};
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vexBuf), vexBuf, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
// init index buffer object
unsigned char idxBuf[] = { 2, 1, 0, 3, 2, 0, 1, 5, 4, 1, 4, 0, 4, 5, 6, 4, 6, 7, 7, 6, 2, 7, 2, 3 };
glGenBuffers(1, &_vio);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vio);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(idxBuf), idxBuf, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
void Skybox::draw(Renderer* renderer, const Mat4& transform, uint32_t flags)
{
_customCommand.init(_globalZOrder);
@ -153,14 +120,6 @@ void Skybox::onDraw(const Mat4& transform, uint32_t flags)
glDepthFunc(GL_LEQUAL);
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
if (_dirty)
{
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B)*_bufferCapacity, _buffer, GL_STREAM_DRAW);
_dirty = false;
}
if (Configuration::getInstance()->supportsShareableVAO())
{
@ -168,96 +127,34 @@ void Skybox::onDraw(const Mat4& transform, uint32_t flags)
}
else
{
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX);
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
// vertex
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B), (GLvoid*)offsetof(V3F_C4B, vertices));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), nullptr);
// color
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B), (GLvoid*)offsetof(V3F_C4B, colors));
//texcoord
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD,2,GL_FLOAT,GL_FALSE,sizeof(V3F_C4B),(GLvoid*)offsetof(V3F_C4B, texcoord));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vio);
}
glDrawArrays(GL_TRIANGLES, 0, _bufferCount);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawElements(GL_TRIANGLES, (GLsizei)36, GL_UNSIGNED_BYTE, nullptr);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _bufferCount);
if (Configuration::getInstance()->supportsShareableVAO())
{
GL::bindVAO(0);
}
else
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
}
glCullFace(GL_BACK);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 8);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
CHECK_GL_ERROR_DEBUG();
}
void Skybox::drawSlice(Vec3* points, const Color4F& color)
{
unsigned int vertex_count = 6;
ensureCapacity(vertex_count);
Color4B col = Color4B(color);
V3F_C4B a = {Vec3(points[0].x, points[0].y, points[0].z), col, Tex2F(0,0)};
V3F_C4B b = {Vec3(points[1].x, points[1].y, points[1].z), col, Tex2F(1,0)};
V3F_C4B c = {Vec3(points[2].x, points[2].y, points[2].z), col, Tex2F(1,1)};
V3F_C4B d = {Vec3(points[3].x, points[3].y, points[3].z), col, Tex2F(0,1)};
V3F_C4B e = {Vec3(points[0].x, points[0].y, points[0].z), col, Tex2F(0,0)};
V3F_C4B f = {Vec3(points[2].x, points[2].y, points[2].z), col, Tex2F(1,1)};
V3F_C4B* slice = (V3F_C4B*)(_buffer + _bufferCount);
slice[0] = a;
slice[1] = b;
slice[2] = c;
slice[3] = d;
slice[4] = e;
slice[5] = f;
_bufferCount += vertex_count;
_dirty = true;
}
void Skybox::drawSphere(const Vec3& origin, const float radius, const float M, const float N, const Color4F& color)
{
float step_y = M_PI/M;
float step_xz = 2*M_PI/N;
Vec3 points[4];
float angle_y = 0.0;
float angle_xz = 0.0;
int i=0, j=0;
for(i=0; i<M; i++)
{
angle_y = i * step_y;
for(j=0; j<N; j++)
{
angle_xz = j * step_xz;
points[0].x = radius * sin(angle_y) * cos(angle_xz);
points[0].z = radius * sin(angle_y) * sin(angle_xz);
points[0].y = radius * cos(angle_y);
points[1].x = radius * sin(angle_y + step_y) * cos(angle_xz);
points[1].z = radius * sin(angle_y + step_y) * sin(angle_xz);
points[1].y = radius * cos(angle_y + step_y);
points[2].x = radius*sin(angle_y + step_y)*cos(angle_xz + step_xz);
points[2].z = radius*sin(angle_y + step_y)*sin(angle_xz + step_xz);
points[2].y = radius*cos(angle_y + step_y);
points[3].x = radius * sin(angle_y) * cos(angle_xz + step_xz);
points[3].z = radius * sin(angle_y) * sin(angle_xz + step_xz);
points[3].y = radius * cos(angle_y);
points[0] += origin;
points[1] += origin;
points[2] += origin;
points[3] += origin;
drawSlice(points,color);
}
}
}
void Skybox::setTexture(Texture2D* texture)
{
CCASSERT(texture != nullptr, __FUNCTION__);
@ -269,10 +166,4 @@ void Skybox::setTexture(Texture2D* texture)
state->setUniformTexture("u_Env", _texture);
}
void Skybox::clear()
{
_bufferCount = 0;
_dirty = true;
}
NS_CC_END

View File

@ -32,15 +32,10 @@ NS_CC_BEGIN
class CC_DLL Skybox : public Node
{
public:
static Skybox* create();
void drawSphere(const Vec3& origin, const float radius, const float M, const float N, const Color4F& color);
CREATE_FUNC(Skybox);
void setTexture(Texture2D*);
/** Clear the geometry in the node's buffer. */
void clear();
void onDraw(const Mat4& transform, uint32_t flags);
// Overrides
@ -52,27 +47,15 @@ CC_CONSTRUCTOR_ACCESS:
virtual bool init();
protected:
struct V3F_C4B
{
Vec3 vertices;
Color4B colors;
Tex2F texcoord;
};
void ensureCapacity(int count);
void drawSlice(Vec3* points, const Color4F& color);
void initBuffers();
GLuint _vao;
GLuint _vbo;
int _bufferCapacity;
GLsizei _bufferCount;
V3F_C4B* _buffer;
GLuint _vio;
CustomCommand _customCommand;
bool _dirty;
Texture2D* _texture;
private:
CC_DISALLOW_COPY_AND_ASSIGN(Skybox);

View File

@ -2374,7 +2374,6 @@ void Sprite3DCubeMapTest::addNewSpriteWithCoords(Vec2 p)
// config skybox
Skybox* box = Skybox::create();
box->setTexture(_textureCube);
box->drawSphere(Vec3::ZERO, 1.6f, 20, 20, Color4F(0, 0, 0, 0));
addChild(box);
//auto rotate_action = RotateBy::create(1.5, Vec3(0, -30, 0));

View File

@ -4,9 +4,9 @@ varying vec3 v_reflect;
void main(void)
{
mat4 MVMatrixLimited = CC_MVMatrix;
MVMatrixLimited[0][3] = 0;
MVMatrixLimited[1][3] = 0;
MVMatrixLimited[2][3] = 0;
MVMatrixLimited[0][3] = 0.0;
MVMatrixLimited[1][3] = 0.0;
MVMatrixLimited[2][3] = 0.0;
vec4 position = MVMatrixLimited* a_position;