void HelloWorld::onDraw() { kmMat4 oldProj; kmMat4 oldMv; // //先获得旧的matrix,然后设置新的matrix kmGLGetMatrix(KM_GL_MODELVIEW, &oldMv); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLLoadMatrix(&_modelViewMV); kmGLGetMatrix(KM_GL_PROJECTION, &oldProj); kmGLMatrixMode(KM_GL_PROJECTION); //想显示3d cubic的时候,不能够把projection matrix设置为对等矩阵 // kmGLLoadIdentity(); kmGLMatrixMode(KM_GL_MODELVIEW); //mShaderProgram->use(); //we don't need any builtin Uniforms, since we will add our mvp matrix all by hand typedef struct { float Position[3]; float Color[4]; float tex[2]; } Vertex; #define TEX_COORD_MAX 1 Vertex Vertices[] = { // Front {{1, -1, 0}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, 1, 0}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, 1, 0}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, -1, 0}, {0, 0, 0, 1}, {0, 0}}, // Back {{1, 1, -2}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{-1, -1, -2}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{1, -1, -2}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, 1, -2}, {0, 0, 0, 1}, {0, 0}}, // Left {{-1, -1, 0}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{-1, 1, 0}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, 1, -2}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, -1, -2}, {0, 0, 0, 1}, {0, 0}}, // Right {{1, -1, -2}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, 1, -2}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{1, 1, 0}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{1, -1, 0}, {0, 0, 0, 1}, {0, 0}}, // Top {{1, 1, 0}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, 1, -2}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, 1, -2}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, 1, 0}, {0, 0, 0, 1}, {0, 0}}, // Bottom {{1, -1, -2}, {1, 0, 0, 1}, {TEX_COORD_MAX, 0}}, {{1, -1, 0}, {0, 1, 0, 1}, {TEX_COORD_MAX, TEX_COORD_MAX}}, {{-1, -1, 0}, {0, 0, 1, 1}, {0, TEX_COORD_MAX}}, {{-1, -1, -2}, {0, 0, 0, 1}, {0, 0}} }; int vertexCount = sizeof(Vertices) / sizeof(Vertices[0]); GLubyte Indices[] = { // Front 0, 1, 2, 2, 3, 0, // Back 4, 5, 6, 4, 5, 7, // Left 8, 9, 10, 10, 11, 8, // Right 12, 13, 14, 14, 15, 12, // Top 16, 17, 18, 18, 19, 16, // Bottom 20, 21, 22, 22, 23, 20 }; //set color for each vertex note:the color value is between 0-1 // mShaderProgram->setUniformLocationWith4f(mColorLocation, 0.5, 0.5, 0.5, 1); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(Indices), Indices, GL_STATIC_DRAW); _positionLocation = glGetAttribLocation(mShaderProgram->getProgram(), "Position"); _colorLocation = glGetAttribLocation(mShaderProgram->getProgram(), "SourceColor"); _projectionUniform = glGetUniformLocation(mShaderProgram->getProgram(), "Projection"); _modelViewUniform = glGetUniformLocation(mShaderProgram->getProgram(), "Modelview"); _textureLocation = glGetAttribLocation(mShaderProgram->getProgram(), "TextureCoord"); _textureUniform = glGetUniformLocation(mShaderProgram->getProgram(), "CC_Texture0"); kmGLPushMatrix(); kmGLLoadIdentity(); auto winSize = Director::getInstance()->getWinSizeInPixels(); // float zeye = Director::getInstance()->getZEye(); // float zfarPlane = zeye + winSize.height/2; static float rotation = 0; //kmGLRotatef(20, 0, 0, 1); //the order to call translate/rotate/scale matters //in kzMath, the order should be translate -> rotate -> scale //in generate mv = t * r * s * m; kmGLTranslatef(winSize.width/2, winSize.height/2, 0 ); kmGLRotatef(rotation++, 1, 1, 1); kmGLScalef(150, 150, 150 ); //we must scale the model coordinate such that we can see the model with the zNear and far plane if (rotation >= 360 ) { rotation = 0; } //kmGLTranslatef(0, 0, -0.1); kmMat4 modelView; kmGLGetMatrix(KM_GL_MODELVIEW, &modelView); mShaderProgram->use(); mShaderProgram->setUniformsForBuiltins(); mShaderProgram->setUniformLocationWithMatrix4fv(_modelViewUniform, modelView.mat , 1); kmGLPopMatrix(); // GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); //this two calls are replacement for the above call glEnableVertexAttribArray(_positionLocation); glEnableVertexAttribArray(_colorLocation); glEnableVertexAttribArray(_textureLocation); glVertexAttribPointer(_positionLocation, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0); glVertexAttribPointer(_colorLocation, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex),(GLvoid*)(sizeof(float) * 3)); glVertexAttribPointer(_textureLocation, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(float) * 7)); // //set sampler GL::bindTexture2D(_textureID); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, 0); mShaderProgram->setUniformLocationWith1i(_textureUniform, 0); GL::bindTexture2D(_textureID2); glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_BYTE, (void*)(18 * sizeof(GLubyte))); mShaderProgram->setUniformLocationWith1i(_textureUniform, 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1,vertexCount); CHECK_GL_ERROR_DEBUG(); kmGLMatrixMode(KM_GL_PROJECTION); kmGLLoadMatrix(&oldProj); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLLoadMatrix(&oldMv); }
void Layout::stencilClippingVisit() { if (!_clippingStencil || !_clippingStencil->isVisible()) { CCNode::visit(); return; } if (g_sStencilBits < 1) { CCNode::visit(); return; } static GLint layer = -1; if (layer + 1 == g_sStencilBits) { static bool once = true; if (once) { char warning[200] = {0}; snprintf(warning, sizeof(warning), "Nesting more than %d stencils is not supported. Everything will be drawn without stencil for this node and its childs.", g_sStencilBits); CCLOG("%s", warning); once = false; } CCNode::visit(); return; } layer++; GLint mask_layer = 0x1 << layer; GLint mask_layer_l = mask_layer - 1; GLint mask_layer_le = mask_layer | mask_layer_l; GLboolean currentStencilEnabled = GL_FALSE; GLuint currentStencilWriteMask = ~0; GLenum currentStencilFunc = GL_ALWAYS; GLint currentStencilRef = 0; GLuint currentStencilValueMask = ~0; GLenum currentStencilFail = GL_KEEP; GLenum currentStencilPassDepthFail = GL_KEEP; GLenum currentStencilPassDepthPass = GL_KEEP; currentStencilEnabled = glIsEnabled(GL_STENCIL_TEST); glGetIntegerv(GL_STENCIL_WRITEMASK, (GLint *)¤tStencilWriteMask); glGetIntegerv(GL_STENCIL_FUNC, (GLint *)¤tStencilFunc); glGetIntegerv(GL_STENCIL_REF, ¤tStencilRef); glGetIntegerv(GL_STENCIL_VALUE_MASK, (GLint *)¤tStencilValueMask); glGetIntegerv(GL_STENCIL_FAIL, (GLint *)¤tStencilFail); glGetIntegerv(GL_STENCIL_PASS_DEPTH_FAIL, (GLint *)¤tStencilPassDepthFail); glGetIntegerv(GL_STENCIL_PASS_DEPTH_PASS, (GLint *)¤tStencilPassDepthPass); glEnable(GL_STENCIL_TEST); CHECK_GL_ERROR_DEBUG(); glStencilMask(mask_layer); GLboolean currentDepthWriteMask = GL_TRUE; glGetBooleanv(GL_DEPTH_WRITEMASK, ¤tDepthWriteMask); glDepthMask(GL_FALSE); glStencilFunc(GL_NEVER, mask_layer, mask_layer); glStencilOp(GL_ZERO, GL_KEEP, GL_KEEP); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLPushMatrix(); kmGLLoadIdentity(); kmGLMatrixMode(KM_GL_PROJECTION); kmGLPushMatrix(); kmGLLoadIdentity(); ccDrawSolidRect(CCPoint(-1,-1), CCPoint(1,1), ccc4f(1, 1, 1, 1)); kmGLMatrixMode(KM_GL_PROJECTION); kmGLPopMatrix(); kmGLMatrixMode(KM_GL_MODELVIEW); kmGLPopMatrix(); glStencilFunc(GL_NEVER, mask_layer, mask_layer); glStencilOp(GL_REPLACE, GL_KEEP, GL_KEEP); kmGLPushMatrix(); transform(); _clippingStencil->visit(); kmGLPopMatrix(); glDepthMask(currentDepthWriteMask); glStencilFunc(GL_EQUAL, mask_layer_le, mask_layer_le); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); CCNode::visit(); glStencilFunc(currentStencilFunc, currentStencilRef, currentStencilValueMask); glStencilOp(currentStencilFail, currentStencilPassDepthFail, currentStencilPassDepthPass); glStencilMask(currentStencilWriteMask); if (!currentStencilEnabled) { glDisable(GL_STENCIL_TEST); } layer--; }