void SimpleRender::Render(ICamera* camera, const Vector4f& lightPosition) {

    ShaderProgram* shader = isAo ? m_aoShader : m_simpleShader;

    // bind shader of the batch.
    shader->Bind();

    m_vertexBuffer->EnableVertexAttribInterleavedWithBind();

    shader->SetUniform("textureArray", 0);
    Texture::SetActiveTextureUnit(0);
    m_arrayTexture->Bind();

    shader->SetUniform("aoOnly", 1.0f);

    Matrix4f modelMatrix = Matrix4f::CreateIdentity(); //geoObj->GetModelMatrix();

    shader->SetPhongUniforms(
	modelMatrix
	, camera, lightPosition);

    for(size_t i = 0; i < m_chunks.size(); ++i) {

	Chunk* chunk = m_chunks[i];

	chunk->m_indexBuffer->Bind();
	chunk->m_indexBuffer->DrawIndices(GL_TRIANGLES, (chunk->m_numTriangles)*3);
	chunk->m_indexBuffer->Unbind();
    }

    m_vertexBuffer->DisableVertexAttribInterleavedWithBind();

    m_arrayTexture->Unbind();

}
Пример #2
0
void MaterialManager::Init(unsigned int reserveObjectsCount)
{
	LOG_INFO("Init material manager");
	mCurrentMaterial = nullptr;

	mMaterials.reserve(reserveObjectsCount);

	ShaderProgram * program = new ShaderProgram();
	Shader * vertexShader = new Shader(File::GetRelative("Shaders/default.vert"), ShaderType::VertexShader);
	Shader * fragmentShader = new Shader(File::GetRelative("Shaders/default.frag"), ShaderType::FragmentShader);
	program->AddShader(*vertexShader);
	program->AddShader(*fragmentShader);
	program->Compile();
	program->Bind(); // To make sure nothing goes wrong before the first update loop
	
	mDefaultMaterial = new Material(program);
	//mDefaultMaterial->SetShaderProgram(*program);
	//mDefaultMaterial->BindShadersAndTextures();
}
Пример #3
0
	void GLText::Draw(ShaderProgram &program, GLCamera &camera) const {
		program.Bind();
		glUniformMatrix4fv(program.getUniformLocation("camera"), 1, GL_FALSE, glm::value_ptr(camera.PVMatrix()));
		glm::mat4 model;
		model = glm::translate(model, position);
		model = glm::rotate(model, glm::radians(rotation.x), { 1,0,0 });
		model = glm::rotate(model, glm::radians(rotation.y+180), { 0,1,0 });
		model = glm::rotate(model, glm::radians(rotation.z), { 0,0,1 });
		model = glm::scale(model, scale);
		glUniformMatrix4fv(program.getUniformLocation("model"), 1, GL_FALSE, glm::value_ptr(model));

		glDisable(GL_CULL_FACE);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, textureid);

		glBindVertexArray(vao);
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr); // Draw plane with 36 vertices
		glBindVertexArray(0);

		glBindTexture(GL_TEXTURE_2D, 0);
		glActiveTexture(0);
		glEnable(GL_CULL_FACE);

	}
Пример #4
0
	void ModelRenderer::RenderInitialPose( Model & p_Model )
	{
		// Get const free graphic device
		GraphicDevice & graphicDevice = const_cast<GraphicDevice &>( m_GraphicDevice );

		// Get the default shader program.
		ShaderProgram * pShaderProgram = m_GraphicDevice.GetDefaultShaderProgram( GraphicDevice::InitialPoseShader );

		// Error check the shader program.
		if( pShaderProgram == NULL )
		{
			return;
		}

		// Error check the vertex data.
		if( p_Model.GetVertexGroup( ).GetVertexDataCount( ) == 0 )
		{
			return;
		}

		// Bind the shader program.
		pShaderProgram->Bind( );

		// Set uniform data.
		pShaderProgram->SetUniformMatrix4x4f( "uProjectionMatrix", MatrixManager::GetProjectionMatrix( ) );
		pShaderProgram->SetUniformMatrix4x4f( "uModelViewMatrix", MatrixManager::GetModelViewMatrix( ) );

		// Set light uniforms
		GraphicDevice::DefaultModelSettings & modelSettings =  graphicDevice.GetDefaultModelSettings( );

		// Set light count
		pShaderProgram->SetUniform1i( "uLightCount", modelSettings.GetActiveLightCount( ) );

		// Set ambient color
		const Vector3f32 & ambColor = modelSettings.GetAmbientLight( );
		pShaderProgram->SetUniform3f("uAmbientColor", ambColor.x, ambColor.y, ambColor.z);

		// Go throguh all the lights
		for( SizeType i = 0; i < modelSettings.GetActiveLightCount( ); i++ )
		{
			std::stringstream positionStream;
			positionStream << "uLightPositions[" << i << "]";
			std::stringstream colorStream;
			colorStream << "uLightColors[" << i << "]";

			const Vector4f32 & pos = modelSettings.GetLight( i ).GetPosition( );
			const Vector3f32 & color = modelSettings.GetLight( i ).GetColor( );
			pShaderProgram->SetUniform4f( positionStream.str( ).c_str( ), pos.x, pos.y, pos.z, pos.w );
			pShaderProgram->SetUniform3f( colorStream.str( ).c_str( ), color.x, color.y, color.z );
		}

		// Get the vertex group
		ModelVertexGroup & vertexGroup = p_Model.GetVertexGroup( );

		// Go throguh all the vertex data elements.
		for( SizeType i = 0; i < vertexGroup.GetVertexDataCount( ); i++ )
		{
			// Get the current vertex data.
			ModelVertexData * vertexData = vertexGroup.GetVertexData( i );

			// Error check the vertex data pointer.
			if( vertexData == NULL || vertexData->GetVertexArray( ) == NULL )
			{
				continue;
			}

			// Get the model material
			const ModelMaterial & material = vertexData->GetMaterial();

			// Bind the color texture if any
			Texture * pTexture = material.GetColorTexture();

			//pShaderProgram->SetUniform4f( "uColor", 1.0f, 1.0f, 1.0f, 1.0f );
			if (vertexData->GetBitmask() & 0x02 && pTexture)
			{
				pShaderProgram->SetUniform1i( "uUseTexture", 1 );
			}
			else
			{
				pShaderProgram->SetUniform1i( "uUseTexture", 0 );
			}

			// Set use normals flag.
			if( vertexData->GetBitmask( ) & 0x04 )
			{
				pShaderProgram->SetUniform1i( "uUseNormals", 1 );
			}
			else
			{
				pShaderProgram->SetUniform1i( "uUseNormals", 0 );
			}

			// Set diffuse color
			Vector4f32 diffuseColor = material.GetDiffuseColor();
			pShaderProgram->SetUniform4f("uDiffuseColor", diffuseColor.x, diffuseColor.y, diffuseColor.z, diffuseColor.w);

			// Bind the texture.
			if( pTexture )
			{
				pTexture->Bind( 0 );
			}

			// Render the model.
			vertexData->GetVertexArray( )->Render( PrimitiveMode::Triangles );

			// Unbind the texture.
			if (pTexture)
			{
				pTexture->Unbind();
			}

		}

		// Unbind the shader program.
		pShaderProgram->Unbind( );
	}