コード例 #1
0
void BeginStencilCommand::init(float depth, bool inverted, float alphaThreshold, Node* stencil, bool clearStencil)
{
    _globalOrder = depth;
    _inverted = inverted;
    _alphaThreshold = alphaThreshold;
    _stencil = stencil;
    _clearStencil = clearStencil;
    
    if (_stencil && _alphaThreshold < 1)
    {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WIN32 || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
#else
        // since glAlphaTest do not exists in OES, use a shader that writes
        // pixel only if greater than an alpha threshold
        GLProgram *program = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
        GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
        // set our alphaThreshold
        program->use();
        program->setUniformLocationWith1f(alphaValueLocation, _alphaThreshold);
        // we need to recursively apply this shader to all the nodes in the stencil node
        // FIXME: we should have a way to apply shader to all nodes without having to do this
        setProgram(_stencil, program);
#endif
    }
}
コード例 #2
0
void LayerShaderStudy00::onCustomDraw(const cocos2d::Mat4 &transform, uint32_t flags)
{
    Size winSize = Director::getInstance()->getWinSize();
    // data define
    GLfloat vertercies[] = {
        0.0f,0.0f,
        winSize.width,0.0f,
        winSize.width*0.5f,winSize.height};
    GLfloat color[] = {
        1.0f,0.0f,0.0f,1.0f,
        0.0f,1.0f,0.0f,1.0f,
        0.0f,0.0f,1.0f,1.0f
    };
    
    // gl programe use
    GLProgram* pGlPrograme = getGLProgram();
    pGlPrograme->use();
    
    // input params uniform-mvp
    pGlPrograme->setUniformsForBuiltins();
    
    // input params attribute
    GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POSITION | GL::VERTEX_ATTRIB_FLAG_COLOR);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*2, vertercies);
    glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, sizeof(GLfloat)*4, color);
    
    // draw
    glDrawArrays(GL_TRIANGLES, 0, 3);
    
    CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 3);
    CHECK_GL_ERROR_DEBUG();
}
コード例 #3
0
ファイル: main.cpp プロジェクト: firstvan/OpenGL
void mainLoop()
{
    while (!glfwWindowShouldClose(window) && !glfwGetKey(window, GLFW_KEY_ESCAPE))
    {
        glClear(GL_COLOR_BUFFER_BIT);

        program.use();

        program.setUniform("Material.Kd", 0.2f, 0.8f, 0.3f);
        program.setUniform("Material.Ka", 0.2f, 0.6f, 0.3f);
        program.setUniform("Material.Ks", 0.2f, 0.2f, 0.8f);
        program.setUniform("Material.Shininess", 100.0f);

        glm::mat4 mv = view * model;
        program.setUniform("ModelViewMatrix", mv);
        program.setUniform("NormalMatrix",
                           glm::mat3(glm::vec3(mv[0]), glm::vec3(mv[1]), glm::vec3(mv[2])));

        glm::mat4 mvp = projection * mv;
        program.setUniform("MVP", mvp);

        cube->render();

        glfwPollEvents();
        glfwSwapBuffers(window);
    }
}
コード例 #4
0
void LayerShaderStudy05::onCustomDraw(const cocos2d::Mat4& transform, uint32_t flags)
{
    // gl programe use
    GLProgram* pGlPrograme = getGLProgram();
    pGlPrograme->use();
    // input params uniform-mvp
    pGlPrograme->setUniformsForBuiltins();
    
    glBindVertexArray(m_vao);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);
    
    CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 3);
    CHECK_GL_ERROR_DEBUG();
}
コード例 #5
0
void ShaderDemoNode::onDraw(const cocos2d::Mat4& transform, uint32_t flags)
{
    // gl programe use
    GLProgram* pGlPrograme = getGLProgram();
    pGlPrograme->use();
    // 应用变换矩阵
    getGLProgramState()->apply(transform);
    
    glBindVertexArray(m_vao);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE,(GLvoid*)0);
    glBindVertexArray(0);
    
    CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, 6);
    CHECK_GL_ERROR_DEBUG();
}
コード例 #6
0
ファイル: main.cpp プロジェクト: firstvan/OpenGL
void init()
{
    glfwWindowHint(GLFW_SAMPLES, 15);
    glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);
    glClearColor(0.1f, 0.2f, 0.3f, 1.0f);

    cube = new Cube(false, 1);

    GLShader vert(GLShader::GLShaderType::VERTEX);
    vert.readShader("fog.vert");
    vert.compileShader();
    GLShader frag(GLShader::GLShaderType::FRAGMENT);
    frag.readShader("fog.frag");
    frag.compileShader();

    program.setShaders({ vert.getId(), frag.getId() });
    program.link();

    model = glm::mat4(1.0f);

    view = glm::lookAt(eye, target, up);

    projection = glm::perspective(45.0f, (float)WIN_WIDTH / WIN_HEIGHT, 0.1f, 100.0f);

    program.use();
    /*program.setUniform("Light[0].Position", view * worldLight);
    program.setUniform("Light[0].Intensity", 0.8f, 0.5f, 0.5f);
    worldLight = glm::vec4(7.0f, 5.0f, 5.0f, 1.0f);
    program.setUniform("Light[1].Position", view * worldLight);
    program.setUniform("Light[1].Intensity", -0.8f, 0.5f, 0.5f);
    */

    program.setUniform("sli.Position", eye);
    program.setUniform("sli.intensity", 0.8f, 0.5f, 0.5f);
    program.setUniform("sli.direction", target);
    program.setUniform("sli.exponent", 3);
    program.setUniform("sli.cutoff", 0.1f);

}
コード例 #7
0
void ClippingNode::visit(Renderer *renderer, const Mat4 &parentTransform, uint32_t parentFlags)
{
    if (!_visible || !hasContent())
        return;
    
    uint32_t flags = processParentFlags(parentTransform, parentFlags);

    // IMPORTANT:
    // To ease the migration to v3.0, we still support the Mat4 stack,
    // but it is deprecated and your code should not rely on it
    Director* director = Director::getInstance();
    CCASSERT(nullptr != director, "Director is null when setting matrix stack");
    director->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
    director->loadMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, _modelViewTransform);

    //Add group command
        
    _groupCommand.init(_globalZOrder);
    renderer->addCommand(&_groupCommand);

    renderer->pushGroup(_groupCommand.getRenderQueueID());

    _beforeVisitCmd.init(_globalZOrder);
    _beforeVisitCmd.func = CC_CALLBACK_0(StencilStateManager::onBeforeVisit, _stencilStateManager);
    renderer->addCommand(&_beforeVisitCmd);
    
    auto alphaThreshold = this->getAlphaThreshold();
    if (alphaThreshold < 1)
    {
#if CC_CLIPPING_NODE_OPENGLES
        // since glAlphaTest do not exists in OES, use a shader that writes
        // pixel only if greater than an alpha threshold
        GLProgram *program = GLProgramCache::getInstance()->getGLProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
        GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
        // set our alphaThreshold
        program->use();
        program->setUniformLocationWith1f(alphaValueLocation, alphaThreshold);
        // we need to recursively apply this shader to all the nodes in the stencil node
        // FIXME: we should have a way to apply shader to all nodes without having to do this
        setProgram(_stencil, program);
#endif

    }
    _stencil->visit(renderer, _modelViewTransform, flags);

    _afterDrawStencilCmd.init(_globalZOrder);
    _afterDrawStencilCmd.func = CC_CALLBACK_0(StencilStateManager::onAfterDrawStencil, _stencilStateManager);
    renderer->addCommand(&_afterDrawStencilCmd);

    int i = 0;
    bool visibleByCamera = isVisitableByVisitingCamera();
    
    if(!_children.empty())
    {
        sortAllChildren();
        // draw children zOrder < 0
        for(auto size = _children.size(); i < size; ++i)
        {
            auto node = _children.at(i);
            
            if ( node && node->getLocalZOrder() < 0 )
                node->visit(renderer, _modelViewTransform, flags);
            else
                break;
        }
        // self draw
        if (visibleByCamera)
            this->draw(renderer, _modelViewTransform, flags);

        for(auto it=_children.cbegin()+i, itCend = _children.cend(); it != itCend; ++it)
            (*it)->visit(renderer, _modelViewTransform, flags);
    }
    else if (visibleByCamera)
    {
        this->draw(renderer, _modelViewTransform, flags);
    }

    _afterVisitCmd.init(_globalZOrder);
    _afterVisitCmd.func = CC_CALLBACK_0(StencilStateManager::onAfterVisit, _stencilStateManager);
    renderer->addCommand(&_afterVisitCmd);

    renderer->popGroup();
    
    director->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}
コード例 #8
0
void ClippingNode::visit()
{
    if(!_visible)
        return;
    
    kmGLPushMatrix();
    transform();
    //Add group command
    
    Renderer* renderer = Director::getInstance()->getRenderer();
    
    _groupCommand.init(0,_vertexZ);
    renderer->addCommand(&_groupCommand);

    renderer->pushGroup(_groupCommand.getRenderQueueID());

    _beforeVisitCmd.init(0,_vertexZ);
    _beforeVisitCmd.func = CC_CALLBACK_0(ClippingNode::onBeforeVisit, this);
    renderer->addCommand(&_beforeVisitCmd);
    if (_alphaThreshold < 1)
    {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
#else
        // since glAlphaTest do not exists in OES, use a shader that writes
        // pixel only if greater than an alpha threshold
        GLProgram *program = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST);
        GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
        // set our alphaThreshold
        program->use();
        program->setUniformLocationWith1f(alphaValueLocation, _alphaThreshold);
        // we need to recursively apply this shader to all the nodes in the stencil node
        // XXX: we should have a way to apply shader to all nodes without having to do this
        setProgram(_stencil, program);
        
#endif

    }
    _stencil->visit();

    _afterDrawStencilCmd.init(0,_vertexZ);
    _afterDrawStencilCmd.func = CC_CALLBACK_0(ClippingNode::onAfterDrawStencil, this);
    renderer->addCommand(&_afterDrawStencilCmd);

    int i = 0;
    
    if(!_children.empty())
    {
        sortAllChildren();
        // draw children zOrder < 0
        for( ; i < _children.size(); i++ )
        {
            auto node = _children.at(i);
            
            if ( node && node->getZOrder() < 0 )
                node->visit();
            else
                break;
        }
        // self draw
        this->draw();
        
        for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
            (*it)->visit();
    }
    else
    {
        this->draw();
    }

    _afterVisitCmd.init(0,_vertexZ);
    _afterVisitCmd.func = CC_CALLBACK_0(ClippingNode::onAfterVisit, this);
    renderer->addCommand(&_afterVisitCmd);

    renderer->popGroup();
    
    kmGLPopMatrix();
}
コード例 #9
0
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

    Size visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();



    auto center = Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);

    auto bg = Sprite::create("bg.png");
    bg->setPosition(center);
    auto bgsize = bg->getContentSize();
    float scaleX = visibleSize.width / bgsize.width;
    float scaleY = visibleSize.height / bgsize.height;
    bg->setScaleX(scaleX);
    bg->setScaleY(scaleY);
    addChild(bg);

    auto grass = Sprite::create("grass.png");
    grass->setAnchorPoint(ccp(0.5, 0.5));
    grass->setPosition(center);
    addChild(grass);

    Texture2D::TexParams p;
    p.minFilter = GL_LINEAR;
    p.magFilter = GL_LINEAR;
    p.wrapS = GL_REPEAT;
    p.wrapT = GL_REPEAT;

    glActiveTexture(GL_TEXTURE0);

    auto textureCache = Director::getInstance()->getTextureCache();
    auto tex1 = textureCache->addImage("grass.png");
    //tex1->setTexParameters(p);

    GLProgram* prog = new GLProgram();
    prog->initWithFilenames("perlin_wind.vert", "perlin_wind.frag");
    prog->retain();
    prog->link();
    prog->use();
    prog->updateUniforms();


    float gtime = Director::getInstance()->getDeltaTime();
    float ctime = Director::getInstance()->getTotalFrames() * Director::getInstance()->getAnimationInterval();

    //CCLog("Wind is %f", pnoise);

    ShaderCache::getInstance()->addGLProgram(prog, "perlin_wind");

    grass->setGLProgram(prog);

    GLProgramState* state = GLProgramState::getOrCreateWithGLProgram(prog);

    //state->setUniformFloat("u_gtime", 1.0f);
    //state->setUniformFloat("u_ctime",0.5f);
    //state->setUniformFloat("u_color", m_cloudAmount);
    state->setUniformFloat("u_wind", 0);
    schedule(schedule_selector(HelloWorld::setWind));
    prog->release();

    schedule(schedule_selector(HelloWorld::setWind));
    return true;


}
コード例 #10
0
ファイル: CCClippingNode.cpp プロジェクト: 253627764/WagonWar
void ClippingNode::visit(Renderer *renderer, const kmMat4 &parentTransform, bool parentTransformUpdated)
{
    if(!_visible)
        return;
    
    bool dirty = parentTransformUpdated || _transformUpdated;
    if(dirty)
        _modelViewTransform = transform(parentTransform);
    _transformUpdated = false;

    // IMPORTANT:
    // To ease the migration to v3.0, we still support the kmGL stack,
    // but it is deprecated and your code should not rely on it
    kmGLPushMatrix();
    kmGLLoadMatrix(&_modelViewTransform);

    //Add group command
        
    _groupCommand.init(_globalZOrder);
    renderer->addCommand(&_groupCommand);

    renderer->pushGroup(_groupCommand.getRenderQueueID());

    _beforeVisitCmd.init(_globalZOrder);
    _beforeVisitCmd.func = CC_CALLBACK_0(ClippingNode::onBeforeVisit, this);
    renderer->addCommand(&_beforeVisitCmd);
    if (_alphaThreshold < 1)
    {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_MAC || CC_TARGET_PLATFORM == CC_PLATFORM_WINDOWS || CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
#else
        // since glAlphaTest do not exists in OES, use a shader that writes
        // pixel only if greater than an alpha threshold
        GLProgram *program = ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_ALPHA_TEST_NO_MV);
        GLint alphaValueLocation = glGetUniformLocation(program->getProgram(), GLProgram::UNIFORM_NAME_ALPHA_TEST_VALUE);
        // set our alphaThreshold
        program->use();
        program->setUniformLocationWith1f(alphaValueLocation, _alphaThreshold);
        // we need to recursively apply this shader to all the nodes in the stencil node
        // XXX: we should have a way to apply shader to all nodes without having to do this
        setProgram(_stencil, program);
        
#endif

    }
    _stencil->visit(renderer, _modelViewTransform, dirty);

    _afterDrawStencilCmd.init(_globalZOrder);
    _afterDrawStencilCmd.func = CC_CALLBACK_0(ClippingNode::onAfterDrawStencil, this);
    renderer->addCommand(&_afterDrawStencilCmd);

    int i = 0;
    
    if(!_children.empty())
    {
        sortAllChildren();
        // draw children zOrder < 0
        for( ; i < _children.size(); i++ )
        {
            auto node = _children.at(i);
            
            if ( node && node->getLocalZOrder() < 0 )
                node->visit(renderer, _modelViewTransform, dirty);
            else
                break;
        }
        // self draw
        this->draw(renderer, _modelViewTransform, dirty);
        
        for(auto it=_children.cbegin()+i; it != _children.cend(); ++it)
            (*it)->visit(renderer, _modelViewTransform, dirty);
    }
    else
    {
        this->draw(renderer, _modelViewTransform, dirty);
    }

    _afterVisitCmd.init(_globalZOrder);
    _afterVisitCmd.func = CC_CALLBACK_0(ClippingNode::onAfterVisit, this);
    renderer->addCommand(&_afterVisitCmd);

    renderer->popGroup();
    
    kmGLPopMatrix();
}
コード例 #11
0
void CubeTextureVAO::onDraw(const Mat4 &transform, bool transformUpdated, Ref* ss)
{
	Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
	Director::getInstance()->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
	Director::getInstance()->pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
	Director::getInstance()->loadIdentityMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);

	Mat4 modelViewMatrix;
	Mat4::createLookAt(Vec3(0, 0, 5), Vec3(0, 0, 0), Vec3(0, -1, 0), &modelViewMatrix);
	//modelViewMatrix.translate(0, 0, 0);

	static float rotation = -20;
	modelViewMatrix.rotate(Vec3(0, 1, 0), CC_DEGREES_TO_RADIANS(rotation));
	modelViewMatrix.translate(-1, 2, 0);

	Mat4 projectionMatrix;
	Mat4::createPerspective(60, 480 / 320, 1.0, 42, &projectionMatrix);
	Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION, projectionMatrix);
	Director::getInstance()->multiplyMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW, modelViewMatrix);

#if Use_Custom_Shader
	////-------- 使用自定义的shader渲染顶点数据 begin ----
	mShaderProgram->use();
	mShaderProgram->setUniformsForBuiltins();
#else
	//-------- 使用cocos的shader渲染顶点数据 begin ----
	GLProgram* glProgram = getGLProgram();
	glProgram->use();
	glProgram->setUniformsForBuiltins();
	//-------- 使用cocos的shader渲染顶点数据 end ----
#endif

	// 0 ~ 1 之间波动
	float tmp22 = sinf(counter*0.01);
	counter++;


	//先激活VAO, 在激活VBO,才能获取到缓冲区的映射
	//再进行数据修改,然后取消映射
	glBindVertexArray(_quadVAO);
	glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);

	//GLfloat* data2 = NULL;
	//data2 = (GLfloat*)glMapBufferRange(GL_ARRAY_BUFFER, (GLintptr)0, (GLsizeiptr)50, GL_MAP_WRITE_BIT);
	//if (data2 != (GLfloat*)NULL) {
	//	data2[9 * 0 + 0] = tmp22; /* Modify X values */
	//	//data2[9 * 0 + 1] += tmp22; /* Modify Y values */
	//	data2[9 * 1 + 1] = tmp22; /* Modify Y values */
	//	glUnmapBuffer(GL_ARRAY_BUFFER);
	//}
	//else
	//{
	//	int a = 1;
	//}

	////set sampler
	GL::bindTexture2DN(0, _textureID);
	//glActiveTexture( GL_TEXTURE0 );
	//glBindTexture(GL_TEXTURE_2D, _textureID);
	GL::blendFunc(mBlendFunc.src, mBlendFunc.dst);

	//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
	glEnable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);


	glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_BYTE, 0);

	//解除激活
	glBindVertexArray(0);

	//glUniform1i(_textureUniform, 0); // unnecc in practice


	// 0 ~ 1 之间波动
	float tmp = sinf(tmpCount*0.01);
	//float tmp = sinf(tmpCount*0.005);
	tmp = tmp < 0 ? -tmp : tmp;
	tmpCount++;

#define g_iWeightNumber 17


	//////设置blur权重
	//float weightArr[g_iWeightNumber] = { 0.9 };
	//GLuint weightLoc = glGetUniformLocation(mShaderProgram->getProgram(), "g_aryWeight");
	//glUniform1fv(weightLoc, g_iWeightNumber, weightArr);


	////设置横向blur偏移数组
	//float offsetArr[g_iWeightNumber * 2] = { 0.1 };
	//GLuint offsetLoc = glGetUniformLocation(mShaderProgram->getProgram(), "g_aryVerticalOffset");
	//glUniform2fv(offsetLoc, g_iWeightNumber, offsetArr);

#if Use_Custom_Shader
	//过滤向量
	float hor[2] = { 0.00, 0.00 };
	if (touching)
	{
		hor[0] = tmp;
		hor[1] = tmp;
	}
	GLuint horLoc = glGetUniformLocation(mShaderProgram->getProgram(), "g_vec2HorizontalDir");
	glUniform2fv(horLoc, 1, hor);

	//过滤偏移
	float offset =  0.0 ;
	GLuint offsetLoc = glGetUniformLocation(mShaderProgram->getProgram(), "g_fFilterOffset");
	glUniform1f(offsetLoc, offset);

	//亮度
	float glowGen =  1.0 ;
	GLuint glowGenLoc = glGetUniformLocation(mShaderProgram->getProgram(), "g_fGlowGene");
	glUniform1f(glowGenLoc, glowGen);
#endif

	CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, vertexCount);


	CHECK_GL_ERROR_DEBUG();
	glDisable(GL_DEPTH_TEST);

	Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_PROJECTION);
	Director::getInstance()->popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);
}