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();
}
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();
}
Ejemplo n.º 3
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);
}