示例#1
0
void ShaderSprite::initShader()
{
    GLchar * fragSource = (GLchar*) String::createWithContentsOfFile(
                                                                     FileUtils::getInstance()->fullPathForFilename(_fragSourceFile).c_str())->getCString();
    auto program = new GLProgram();
    program->initWithByteArrays(ccPositionTextureColor_vert, fragSource);
    setShaderProgram(program);
    program->release();
    
    CHECK_GL_ERROR_DEBUG();
    
    program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_COLOR, GLProgram::VERTEX_ATTRIB_COLOR);
    program->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
    
    CHECK_GL_ERROR_DEBUG();
    
    program->link();
    
    CHECK_GL_ERROR_DEBUG();
    
    program->updateUniforms();
    
    CHECK_GL_ERROR_DEBUG();
    
    buildCustomUniforms();
    
    CHECK_GL_ERROR_DEBUG();
}
    MGLResourceManager(QGLWidget *glwidget)
        : QObject(glwidget),
          glcontext(glwidget->context()),
          currentShader(0)
    {
        sharedVertexShader = new QGLShader(QGLShader::Vertex,
                glwidget->context(), this);
        if (!sharedVertexShader->compileSourceCode(QLatin1String(TexpVertShaderSource)))
            qWarning("vertex shader failed to compile");

        MShaderProgram *normalShader = new MShaderProgram(glwidget->context(),
                                                          this);
        normalShader->addShader(sharedVertexShader);
        if (!normalShader->addShaderFromSourceCode(QGLShader::Fragment,
                QLatin1String(TexpFragShaderSource)))
            qWarning("normal fragment shader failed to compile");
        shader[NormalShader] = normalShader;

        MShaderProgram *blurShader = new MShaderProgram(glwidget->context(),
                                                        this);
        shader[BlurShader] = blurShader;
        blurShader->addShader(sharedVertexShader);
        if (!blurShader->addShaderFromSourceCode(QGLShader::Fragment,
                QLatin1String(blurshader)))
            qWarning("blur fragment shader failed to compile");

        bindAttribLocation(normalShader, "inputVertex", D_VERTEX_COORDS);
        bindAttribLocation(normalShader, "textureCoord", D_TEXTURE_COORDS);
        bindAttribLocation(blurShader, "inputVertex", D_VERTEX_COORDS);
        bindAttribLocation(blurShader, "textureCoord", D_TEXTURE_COORDS);

        normalShader->link();
        blurShader->link();
    }
    GLuint installPixelShader(const QByteArray& code)
    {
        QHash<QByteArray, MShaderProgram *>::iterator it = customShadersByCode.find(code);
        if (it != customShadersByCode.end())  {
            return it.value()->programId();
        }

        QByteArray source = code;
        source.append(TexpCustomShaderSource);
        MShaderProgram *p = new MShaderProgram(glcontext, this);
        p->addShader(sharedVertexShader);
        if (!p->addShaderFromSourceCode(QGLShader::Fragment,
                QLatin1String(source)))
            qWarning("custom fragment shader failed to compile");

        bindAttribLocation(p, "inputVertex", D_VERTEX_COORDS);
        bindAttribLocation(p, "textureCoord", D_TEXTURE_COORDS);

        if (p->link()) {
            customShadersByCode[code] = p;
            customShadersById[p->programId()] = p;
            return p->programId();
        } 
       
        qWarning() << "failed installing custom fragment shader:"
                   << p->log();
        p->deleteLater();
        
        return 0;
    }
示例#4
0
	GLProgram::GLProgram()
	{
		program = glCreateProgram();
		bindAttribLocation( "in_Vertex", GLSL_VERTEX_IDX );
		bindAttribLocation( "in_Color", GLSL_COLOR_IDX );
		bindAttribLocation( "in_Normal", GLSL_NORMAL_IDX );
		bindAttribLocation( "in_TexCoord", GLSL_TEXCOORD_IDX );
	}
示例#5
0
void BattleHighNode::prepareShaders()
{
    auto fileUtils = FileUtils::getInstance();
    auto vertSource = fileUtils->getStringFromFile("3d/battle_high.vsh");
    auto fragSource = fileUtils->getStringFromFile("3d/battle_high.fsh");

    auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
    glprogram->bindAttribLocation("a_positioin", MY_VERTEX_ATTRIB_POSITION);
    glprogram->bindAttribLocation("a_texcoord", MY_VERTEX_ATTRIB_TEXCOORD);
    glprogram->link();
    glprogram->updateUniforms();

    _programState = GLProgramState::getOrCreateWithGLProgram(glprogram);
    _programState->retain();
}
示例#6
0
void SimplePixelNode::prepareShaders()
{
    auto fileUtils = FileUtils::getInstance();
    auto vertSource = fileUtils->getStringFromFile("3d/simple_pixel.vsh");
    auto fragSource = fileUtils->getStringFromFile("3d/simple_pixel.fsh");

    auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
    glprogram->bindAttribLocation("a_positioin", 0);
    glprogram->bindAttribLocation("a_color", 1);

    glprogram->link();
    glprogram->updateUniforms();

    _programState = GLProgramState::getOrCreateWithGLProgram(glprogram);
    _programState->retain();
}
void ShadowCover::prepareShaders()
{
    auto fileUtils = FileUtils::getInstance();
    auto vertSource = fileUtils->getStringFromFile("3d/shadow_cover.vsh");
    auto fragSource = fileUtils->getStringFromFile("3d/shadow_cover.fsh");

    auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
    glprogram->bindAttribLocation("a_positioin", 0);
    glprogram->bindAttribLocation("a_texcoord", 1);
    glprogram->bindAttribLocation("a_radio", 2);

    glprogram->link();
    glprogram->updateUniforms();

    _programState = GLProgramState::getOrCreateWithGLProgram(glprogram);
    _programState->retain();
}
void UVEffectSprite::InitShader()
{
    auto  p = new GLProgram();
    p->initWithFilenames("Shaders/uv.vsh", "Shaders/uv.fsh");
    p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_POSITION, GLProgram::VERTEX_ATTRIB_POSITION);
    p->bindAttribLocation(GLProgram::ATTRIBUTE_NAME_TEX_COORD, GLProgram::VERTEX_ATTRIB_TEX_COORDS);
    p->link();
    p->updateUniforms();
    this->setGLProgram(p);
    auto textrue = Director::getInstance()->getTextureCache()->addImage("soroll_uv3.png");
    Texture2D::TexParams tRepeatParams;//设置纹理渲染方式
    tRepeatParams.magFilter = GL_LINEAR_MIPMAP_LINEAR;
    tRepeatParams.minFilter = GL_LINEAR;
    tRepeatParams.wrapS = GL_REPEAT;
    tRepeatParams.wrapT = GL_REPEAT;
    textrue->setTexParameters(tRepeatParams);//将贴图设置给Shader中的变量值u_texture1
    _lightani.x = _lightani.y = 0.0f;
   getGLProgramState()->setUniformTexture("u_lightTexture", textrue);
    getGLProgramState()->setUniformVec2("v_animLight", _lightani);
}
示例#9
0
void AcceEffectSpace::initShaders()
{
    auto fileUtils = FileUtils::getInstance();
    auto vertSource = fileUtils->getStringFromFile("3d/acce.vsh");
    auto fragSource = fileUtils->getStringFromFile("3d/acce.fsh");

    auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
    glprogram->bindAttribLocation("a_position", AcceEffectSpace::VERTEX_ATTRIB_POSITION);
    glprogram->bindAttribLocation("a_texcoord", AcceEffectSpace::VERTEX_ATTRIB_TEXCOORD);
    glprogram->link();
    glprogram->updateUniforms();

    _programState = GLProgramState::getOrCreateWithGLProgram(glprogram);
    _programState->retain();
}
示例#10
0
void RoadNode::initShaders()
{
    if (_roadProgram) return;
    // Road
    {
        auto fileUtils = FileUtils::getInstance();
        auto vertSource = fileUtils->getStringFromFile("3d/road.vsh");
        auto fragSource = fileUtils->getStringFromFile("3d/road.fsh");

        auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
        glprogram->bindAttribLocation("a_position", 0);
        glprogram->bindAttribLocation("a_texCoord", 1);
        glprogram->link();
        glprogram->updateUniforms();

        _roadProgram = glprogram;
        _roadProgram->retain();

        _roadTexture = Director::getInstance()->getTextureCache()->addImage("3d/road_shadow.png");
    }
    // Line
    {
        auto fileUtils = FileUtils::getInstance();
        auto vertSource = fileUtils->getStringFromFile("3d/road_line.vsh");
        auto fragSource = fileUtils->getStringFromFile("3d/road_line.fsh");

        auto glprogram = GLProgram::createWithByteArrays(vertSource.c_str(), fragSource.c_str());
        glprogram->bindAttribLocation("a_position", 0);
        glprogram->bindAttribLocation("a_texCoord", 1);
        glprogram->link();
        glprogram->updateUniforms();

        _lineProgram = glprogram;
        _lineProgram->retain();
    }
}
示例#11
0
void ShaderNode::loadShaderVertex(const std::string &vs, const std::string &fs)
{
    auto shader = new GLProgram();
    shader->initWithFilenames(vs, fs);
	this->setShaderProgram(shader);
	shader->release();

	CHECK_GL_ERROR_DEBUG();

    shader->bindAttribLocation("a_position", GLProgram::VERTEX_ATTRIB_POSITION);
	CHECK_GL_ERROR_DEBUG();

    shader->link();
	CHECK_GL_ERROR_DEBUG();

    shader->updateUniforms();
	CHECK_GL_ERROR_DEBUG();

    _uniformResolution = shader->getUniformLocation("resolution");

	this->buildCustomUniforms();
	CHECK_GL_ERROR_DEBUG();
}
示例#12
0
void GLSLShader::createVertexAttribute(GLuint location, const char *name, std::vector<Eigen::Vector4f> &data)
{
    bindAttribLocation(location, name);

    int size = data.size() * 4;
    float *dataB = new float[size];
    int temp =0;
    for(int i =0; i < size; i+=4)
    {
        dataB[i] = data[temp][0];
        dataB[i+1] = data[temp][1];
        dataB[i+2] = data[temp][2];
        dataB[i+3] = data[temp][3];
        temp++;
    }


    glGenBuffers(1, &vboHandle);
    glBindBuffer(GL_ARRAY_BUFFER, vboHandle);

    glBufferData(GL_ARRAY_BUFFER, size * sizeof(float), dataB, GL_STATIC_DRAW);

    if(vaoHandle == 0)
    {
        glGenVertexArrays(1, &vaoHandle);
    }

    glBindVertexArray(vaoHandle);
    //GLint nAttribs;
    //glGetProgramiv(programHandle, GL_ACTIVE_ATTRIBUTES, &nAttribs);
    glEnableVertexAttribArray(location);

    glBindBuffer(GL_ARRAY_BUFFER, vboHandle);
    glVertexAttribPointer(location, 4, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL);

}
void GMBasicEffect::bindAttribLocations()
{
    bindAttribLocation(ATTRIB_VERTEX, "position");
    bindAttribLocation(ATTRIB_COLOR, "color");
}
示例#14
0
bool Shader::initialiser()
{
	// Création des shaders

	if(initialiserTypeShader(m_vertexID, GL_VERTEX_SHADER, m_vertexSource) == false)
		return false;

	if(initialiserTypeShader(m_fragmentID, GL_FRAGMENT_SHADER, m_fragmentSource) == false)
		return false;


	// Création du program

	m_programID = glCreateProgram();

	glAttachShader(m_programID, m_vertexID);
	glAttachShader(m_programID, m_fragmentID);


	// Linkage du program

	bindAttribLocation();
	glLinkProgram(m_programID);


	// On vérifie que le link c'est bien passé

	GLint link(0);
	glGetProgramiv(m_programID, GL_LINK_STATUS, &link);

	if(link != GL_TRUE)
	{
		// Récupération de la taille de l'erreur

		GLint tailleErreur(0);
		char *erreur(NULL);

		glGetProgramiv(m_programID, GL_INFO_LOG_LENGTH, &tailleErreur);


		// Allocation de l'erreur

		erreur = new char[tailleErreur + 1];


		// Copie de l'erreur dans la chaine de caractères

		glGetProgramInfoLog(m_programID, tailleErreur, &tailleErreur, erreur);
		erreur[tailleErreur] = '\0';


		// Affichage de l'erreur

		std::cout << "Erreur lors du link du program : " << erreur << std::endl;


		// On retourne false

		delete[] erreur;
		return false;
	}


	// Tout s'est bien passée, on retourne true

	m_initialise = true;
	return true;
}
示例#15
0
 void GLProgram::bindAttribLocations()
 {
     for (const auto& attr : allGLAttributes())
         bindAttribLocation(attr.first, attr.second);
 }