void display()
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(1.0f);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	if(g_pPlaneMesh && g_pCylinderMesh)
	{
		glutil::MatrixStack modelMatrix;
		modelMatrix.SetMatrix(g_viewPole.CalcMatrix());

		glm::vec4 lightDirCameraSpace = modelMatrix.Top() * g_lightDirection;

		ProgramData &whiteDiffuse = g_bShowAmbient ? g_WhiteAmbDiffuseColor : g_WhiteDiffuseColor;
		ProgramData &vertexDiffuse = g_bShowAmbient ? g_VertexAmbDiffuseColor : g_VertexDiffuseColor;

		if(g_bShowAmbient)
		{
			glUseProgram(whiteDiffuse.theProgram);
			glUniform4f(whiteDiffuse.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f);
			glUniform4f(whiteDiffuse.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f);
			glUseProgram(vertexDiffuse.theProgram);
			glUniform4f(vertexDiffuse.lightIntensityUnif, 0.8f, 0.8f, 0.8f, 1.0f);
			glUniform4f(vertexDiffuse.ambientIntensityUnif, 0.2f, 0.2f, 0.2f, 1.0f);
		}
		else
		{
			glUseProgram(whiteDiffuse.theProgram);
			glUniform4f(whiteDiffuse.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f);
			glUseProgram(vertexDiffuse.theProgram);
			glUniform4f(vertexDiffuse.lightIntensityUnif, 1.0f, 1.0f, 1.0f, 1.0f);
		}

		glUseProgram(whiteDiffuse.theProgram);
		glUniform3fv(whiteDiffuse.dirToLightUnif, 1, glm::value_ptr(lightDirCameraSpace));
		glUseProgram(vertexDiffuse.theProgram);
		glUniform3fv(vertexDiffuse.dirToLightUnif, 1, glm::value_ptr(lightDirCameraSpace));
		glUseProgram(0);

		{
			glutil::PushStack push(modelMatrix);

			//Render the ground plane.
			{
				glutil::PushStack push(modelMatrix);

				glUseProgram(whiteDiffuse.theProgram);
				glUniformMatrix4fv(whiteDiffuse.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top()));
				glm::mat3 normMatrix(modelMatrix.Top());
				glUniformMatrix3fv(whiteDiffuse.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix));
				g_pPlaneMesh->Render();
				glUseProgram(0);
			}

			//Render the Cylinder
			{
				glutil::PushStack push(modelMatrix);

				modelMatrix.ApplyMatrix(g_objtPole.CalcMatrix());

				if(g_bDrawColoredCyl)
				{
					glUseProgram(vertexDiffuse.theProgram);
					glUniformMatrix4fv(vertexDiffuse.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top()));
					glm::mat3 normMatrix(modelMatrix.Top());
					glUniformMatrix3fv(vertexDiffuse.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix));
					g_pCylinderMesh->Render("lit-color");
				}
				else
				{
					glUseProgram(whiteDiffuse.theProgram);
					glUniformMatrix4fv(whiteDiffuse.modelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(modelMatrix.Top()));
					glm::mat3 normMatrix(modelMatrix.Top());
					glUniformMatrix3fv(whiteDiffuse.normalModelToCameraMatrixUnif, 1, GL_FALSE, glm::value_ptr(normMatrix));
					g_pCylinderMesh->Render("lit");
				}
				glUseProgram(0);
			}
		}
	}

	glutSwapBuffers();
}
Exemplo n.º 2
0
//--------------------------------------------------------------
void ofShader::setUniform3fv(const string & name, const float* v, int count)  const{
	if(bLoaded) {
		int loc = getUniformLocation(name);
		if (loc != -1) glUniform3fv(loc, count, v);
	}
}
Exemplo n.º 3
0
//OpenGL Loop Logic
void glLoop() {
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    /*gModelMat = glm::rotate(gModelMat, -0.01f, glm::vec3(0.f, 0.f, 1.f));
    glUniformMatrix4fv(gModelMatLoc, 1, GL_FALSE, glm::value_ptr(gModelMat));*/
    
    glBindFramebuffer(GL_FRAMEBUFFER, gShadowFbo);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glUseProgram(gShadowProg);
    
    glBindVertexArray(gShadowVao);
    
    glViewport(0, 0, SHADOW_DEPTH_TEXTURE_SIZE, SHADOW_DEPTH_TEXTURE_SIZE);
    glClearDepth(1.0f);
    glClear(GL_DEPTH_BUFFER_BIT);
    
    glUniformMatrix4fv(gShadowViewMatLoc, 1, GL_FALSE, glm::value_ptr(gShadowViewMat));
    glUniformMatrix4fv(gShadowProjMatLoc, 1, GL_FALSE, glm::value_ptr(gShadowProjMat));
    
    glEnable(GL_POLYGON_OFFSET_FILL);
    glPolygonOffset(2.f, 4.f);
    
    for (auto& e: gSceneGraph) {
        
        glUniformMatrix4fv(gShadowModelMatLoc, 1, GL_FALSE, glm::value_ptr(e.modelMat));
        
        glBindBuffer(GL_ARRAY_BUFFER, e.glPosBuf);
        glVertexAttribPointer(gShadowPosLoc, 4, GL_FLOAT, GL_FALSE, 0, NULL);

        glDrawArrays(GL_TRIANGLES, 0, e.vertCount);
    }
    
    glDisable(GL_POLYGON_OFFSET_FILL);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    
    glViewport(0, 0, gScreenWidth * 2, gScreenHeight * 2);
    
    
    // Renders Scene to gFilterSceneTexture
    
    glBindFramebuffer(GL_FRAMEBUFFER, gFilterSceneFbo);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glUseProgram(gRenderProg);
    
    glBindVertexArray(gVao);
    
    for (auto& e: gSceneGraph) {
        glUniformMatrix4fv(gModelMatLoc, 1, GL_FALSE, glm::value_ptr(e.modelMat));
        glUniform3fv(gAmbientLoc, 1, glm::value_ptr(e.ambientColor));
        glUniform3fv(gDiffuseLoc, 1, glm::value_ptr(e.diffuseColor));
        glUniform3fv(gSpecularLoc, 1, glm::value_ptr(e.specularColor));
        glUniform1f(gShininessLoc, e.shininess);
        glUniform1f(gSpecStrengthLoc, e.specStrength);
        
        glUniform1i(gIsTexturedLoc, e.isTextured);
        glUniform1i(gTexUnitLoc, e.glTexUnit);
        glUniform4fv(gColorLoc, 1, glm::value_ptr(e.objColor));
        
        glBindBuffer(GL_ARRAY_BUFFER, e.glPosBuf);
        glVertexAttribPointer(gPosLoc, 4, GL_FLOAT, GL_FALSE, 0, NULL);
        glBindBuffer(GL_ARRAY_BUFFER, e.glTexCoordBuf);
        glVertexAttribPointer(gTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 0, NULL);
        glBindBuffer(GL_ARRAY_BUFFER, e.glNormBuf);
        glVertexAttribPointer(gNormLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL);
        
        glDrawArrays(GL_TRIANGLES, 0, e.vertCount);
    }
    
    glUseProgram(gScreenProg);
    
    glBindVertexArray(gScreenVao);
    
    glDrawArrays(GL_TRIANGLES, 0, 3);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    
    // Low luminance discard pass. Results stored in gFilterLumTexture
    
    glBindFramebuffer(GL_FRAMEBUFFER, gFilterLumFbo);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glUseProgram(gFilterProg);
    
    glUniform1i(gFilterPassLoc, 1);
    
    glBindVertexArray(gFilterVao);
    
    glDrawArrays(GL_TRIANGLES, 0, 6);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    
    // Vertical pass of gaussian filter. Results stored in gFilterBlurTexture
    
    glViewport(0, 0, gScreenWidth * 2, gScreenHeight * 2);
    
    glActiveTexture(GL_TEXTURE4);
    glBindTexture(GL_TEXTURE_2D, gFilterLumTexture); // Texture obtained from scene rendering
    glActiveTexture(GL_TEXTURE0);
    
    glBindFramebuffer(GL_FRAMEBUFFER, gFilterBlurFbo);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glUseProgram(gFilterProg);
    
    glUniform1i(gFilterPassLoc, 2);
    
    glBindVertexArray(gFilterVao);
    
    glDrawArrays(GL_TRIANGLES, 0, 6);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    
    // Horizontal pass of gaussian filter. Rendered to default framebuffer (screen)
    
    glActiveTexture(GL_TEXTURE4);
    glBindTexture(GL_TEXTURE_2D, gFilterBlurTexture); // Texture obtained from pass 1 of gaussian filter
    glActiveTexture(GL_TEXTURE0);
    
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
    glUseProgram(gFilterProg);
    
    glUniform1i(gFilterPassLoc, 3);
    
    glBindVertexArray(gFilterVao);
    
    glDrawArrays(GL_TRIANGLES, 0, 6);
    
}
Exemplo n.º 4
0
/*
=============
RB_ARB2_CreateDrawInteractions

=============
*/
void RB_GLSL_CreateDrawInteractions( const drawSurf_t *surf ) {
	if ( !surf ) {
		return;
	}

	// perform setup here that will be constant for all interactions
	GL_State( GLS_SRCBLEND_ONE | GLS_DSTBLEND_ONE | GLS_DEPTHMASK | backEnd.depthFunc );

	//GL_Cull(CT_TWO_SIDED);
	qglEnableClientState(GL_VERTEX_ARRAY);
	//qglDisableClientState(GL_VERTEX_ARRAY);
	qglDisableClientState(GL_TEXTURE_COORD_ARRAY);

	glUseProgram(program);

	float	modelMatrix[16];
	
	myGlMultMatrix( surf->space->modelViewMatrix, backEnd.viewDef->projectionMatrix, modelMatrix);
	glUniformMatrix4fv(wvp, 1, GL_FALSE, &modelMatrix[0] );
	//glUniformMatrix4fv(wvp1, 1, GL_FALSE, &surf->space->modelViewMatrix[0] );
	//const float* mat = backEnd.viewDef->projectionMatrix; 
	//glUniformMatrix4fv(wvp, 1, GL_FALSE, &mat[0] );


	// enable the vertex arrays
	glEnableVertexAttribArray( 1 );
	glEnableVertexAttribArray( 2 );
	glEnableVertexAttribArray( 3 );
	glEnableVertexAttribArray( 4 );
	glEnableVertexAttribArray( 5 );
	glEnableVertexAttribArray( 6 );

	// texture 0 is the normalization cube map for the vector towards the light
	glActiveTexture(GL_TEXTURE0);
	backEnd.glState.currenttmu = 0;
	if ( backEnd.vLight->lightShader->IsAmbientLight() ) {
		globalImages->ambientNormalMap->Bind();
	} else {
		globalImages->normalCubeMapImage->Bind();
	}

	// texture 6 is the specular lookup table
	glActiveTexture(GL_TEXTURE6);
	backEnd.glState.currenttmu = 6;
	if ( r_testARBProgram.GetBool() ) {
		globalImages->specular2DTableImage->Bind();	// variable specularity in alpha channel
	} else {
		globalImages->specularTableImage->Bind();
	}

	for ( ; surf ; surf=surf->nextOnLight ) {
		// set the vertex pointers
		idDrawVert	*ac = (idDrawVert *)vertexCache.Position( surf->geo->ambientCache );

		qglVertexPointer( 3, GL_FLOAT, sizeof( idDrawVert ), ac->xyz.ToFloatPtr() );
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(idDrawVert), ac->xyz.ToFloatPtr());
		glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(idDrawVert), ac->st.ToFloatPtr());
		glVertexAttribPointer(3, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(idDrawVert), ac->color);
		glVertexAttribPointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(idDrawVert), ac->normal.ToFloatPtr());
		glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, sizeof(idDrawVert), ac->tangents[0].ToFloatPtr());
		glVertexAttribPointer(6, 3, GL_FLOAT, GL_FALSE, sizeof(idDrawVert), ac->tangents[1].ToFloatPtr());

		idVec4 localLight;

		R_GlobalPointToLocal( surf->space->modelMatrix, backEnd.vLight->globalLightOrigin, localLight.ToVec3() );
		localLight.w = 0.0f;
		glUniform3fv(lightOrgin, 1, localLight.ToFloatPtr());
		//glDrawElements(GL_TRIANGLES, surf->geo->numIndexes, GL_UNSIGNED_SHORT, surf->geo->indexes);
		// this may cause RB_ARB2_DrawInteraction to be exacuted multiple
		// times with different colors and images if the surface or light have multiple layers
		RB_CreateSingleDrawInteractions( surf, RB_GLSL_DrawInteraction );
	}

	glDisableVertexAttribArray( 1 );
	glDisableVertexAttribArray( 2 );
	glDisableVertexAttribArray( 3 );
	glDisableVertexAttribArray( 4 );
	glDisableVertexAttribArray( 5 );
	glDisableVertexAttribArray( 6 );

	// disable features
	glActiveTexture( GL_TEXTURE6 );
	backEnd.glState.currenttmu = 6;
	globalImages->BindNull();

	glActiveTexture( GL_TEXTURE5 );
	backEnd.glState.currenttmu = 5;
	globalImages->BindNull();

	glActiveTexture( GL_TEXTURE4 );
	backEnd.glState.currenttmu = 4;
	globalImages->BindNull();

	glActiveTexture( GL_TEXTURE3 );
	backEnd.glState.currenttmu = 3;
	globalImages->BindNull();

	glActiveTexture( GL_TEXTURE2 );
	backEnd.glState.currenttmu = 2;
	globalImages->BindNull();

	glActiveTexture( GL_TEXTURE1 );
	backEnd.glState.currenttmu = 1;
	globalImages->BindNull();

	glUseProgram(0);

	backEnd.glState.currenttmu = -1;
	GL_SelectTexture(0 );

}
void PezRender()
{
    #define Instances 7

    Matrix4 Model[Instances];
    Model[0] = M4MakeRotationY(Globals.Theta);
    Model[1] = M4Mul(M4Mul(
                     M4MakeTranslation((Vector3){0, 0, 0.6}),
                     M4MakeScale(V3MakeFromScalar(0.25))),
                     M4MakeRotationX(Pi/2)
    );
    Model[2] = Model[3] = Model[4] = Model[1];
    Model[1] = M4Mul(M4MakeRotationY(Globals.Theta), Model[1]);
    Model[2] = M4Mul(M4MakeRotationY(Globals.Theta + Pi/2), Model[2]);
    Model[3] = M4Mul(M4MakeRotationY(Globals.Theta - Pi/2), Model[3]);
    Model[4] = M4Mul(M4MakeRotationY(Globals.Theta + Pi), Model[4]);
    Model[5] = M4Mul(M4Mul(
                     M4MakeScale(V3MakeFromScalar(0.5)),
                     M4MakeTranslation((Vector3){0, 1.25, 0})),
                     M4MakeRotationY(-Globals.Theta)
    );
    Model[6] = M4Mul(M4Mul(
                     M4MakeScale(V3MakeFromScalar(0.5)),
                     M4MakeTranslation((Vector3){0, -1.25, 0})),
                     M4MakeRotationY(-Globals.Theta)
    );

    Vector3 LightPosition = {0.5, 0.25, 1.0}; // world space
    Vector3 EyePosition = {0, 0, 1};          // world space

    Matrix4 MVP[Instances];
    Vector3 Lhat[Instances];
    Vector3 Hhat[Instances];
    for (int i = 0; i < Instances; i++) {
        Matrix4 mv = M4Mul(Globals.View, Model[i]);
        MVP[i] = M4Mul(Globals.Projection, mv);
        Matrix3 m = M3Transpose(M4GetUpper3x3(Model[i]));
        Lhat[i] = M3MulV3(m, V3Normalize(LightPosition));    // object space
        Vector3 Eye =  M3MulV3(m, V3Normalize(EyePosition)); // object space
        Hhat[i] = V3Normalize(V3Add(Lhat[i], Eye));
    }

    int instanceCount = Instances;
    MeshPod* mesh = &Globals.Cylinder;

    glBindFramebuffer(GL_FRAMEBUFFER, Globals.FboHandle);

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glEnable(GL_DEPTH_TEST);
  
    glUseProgram(Globals.LitProgram);
    glUniform3f(u("SpecularMaterial"), 0.4, 0.4, 0.4);
    glUniform4f(u("FrontMaterial"), 0, 0, 1, 1);
    glUniform4f(u("BackMaterial"), 0.5, 0.5, 0, 1);
    glUniform3fv(u("Hhat"), Instances, &Hhat[0].x);
    glUniform3fv(u("Lhat"), Instances, &Lhat[0].x);
    glUniformMatrix4fv(u("ModelviewProjection"), Instances, 0, (float*) &MVP[0]);

    glBindVertexArray(mesh->FillVao);
    glDrawElementsInstanced(GL_TRIANGLES, mesh->FillIndexCount, GL_UNSIGNED_SHORT, 0, instanceCount);

    glUseProgram(Globals.SimpleProgram);
    glUniform4f(u("Color"), 0, 0, 0, 1);
    glUniformMatrix4fv(u("ModelviewProjection"), Instances, 0, (float*) &MVP[0]);

    glDepthMask(GL_FALSE);
    glBindVertexArray(mesh->LineVao);
    glDrawElementsInstanced(GL_LINES, mesh->LineIndexCount, GL_UNSIGNED_SHORT, 0, instanceCount);
    glDepthMask(GL_TRUE);

    glDisable(GL_DEPTH_TEST);

    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    glUseProgram(Globals.QuadProgram);
    glBindTexture(GL_TEXTURE_2D, Globals.FboTexture);
    glBindVertexArray(Globals.Grid.FillVao);
    glDrawElements(GL_TRIANGLES, Globals.Grid.FillIndexCount, GL_UNSIGNED_SHORT, 0);

    if (1) {
        glUseProgram(Globals.GridProgram);
        glBindVertexArray(Globals.Grid.LineVao);
        glDrawElements(GL_LINES, Globals.Grid.LineIndexCount, GL_UNSIGNED_SHORT, 0);
    }

    glBindTexture(GL_TEXTURE_2D, 0);
}
void UniformImplementation_Legacy::set(const Program * program, const GLint location, const std::vector<glm::vec3> & value) const
{
    program->use();
    glUniform3fv(location, static_cast<GLint>(value.size()), reinterpret_cast<const float*>(value.data()));
}
Exemplo n.º 7
0
void shader_gles3::uniform(const char* name, const float3* arg1, const size_t& count) const {
	A2E_CHECK_UNIFORM_EXISTENCE(name);
	A2E_CHECK_UNIFORM_TYPE(name, GL_FLOAT_VEC3);
	glUniform3fv(A2E_SHADER_GET_UNIFORM_POSITION(name), (GLsizei)count, (GLfloat*)arg1);
}
Exemplo n.º 8
0
void Shader::SetUniform(const std::string& name, const vec3 vector)
{
	glUniform3fv(GetUniformLocation(name), 1, Math::GetDataPtr(vector));
}
Exemplo n.º 9
0
Arquivo: main.c Projeto: Adon-m/OpenGL
GLUSboolean init(GLUSvoid)
{
	GLUStextfile vertexSource;
	GLUStextfile fragmentSource;

	GLUStgaimage image;

	GLUSshape plane;

	GLint i;

	//

	glusLookAtf(g_viewMatrix, g_camera.eye[0], g_camera.eye[1], g_camera.eye[2], g_camera.center[0], g_camera.center[1], g_camera.center[2], g_camera.up[0], g_camera.up[1], g_camera.up[2]);

	//

	if (!initWavefront(g_viewMatrix, &g_light))
	{
		return GLUS_FALSE;
	}

	//

	glusLoadTextFile("../Example28/shader/texture.vert.glsl", &vertexSource);
	glusLoadTextFile("../Example28/shader/texture.frag.glsl", &fragmentSource);

	glusBuildProgramFromSource(&g_program, (const GLUSchar**)&vertexSource.text, 0, 0, 0, (const GLUSchar**)&fragmentSource.text);

	glusDestroyTextFile(&vertexSource);
	glusDestroyTextFile(&fragmentSource);

	//

	// Retrieve the uniform locations in the program.
	g_viewProjectionMatrixLocation = glGetUniformLocation(g_program.program, "u_viewProjectionMatrix");
	g_modelMatrixLocation = glGetUniformLocation(g_program.program, "u_modelMatrix");
	g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix");
	g_lightDirectionLocation = glGetUniformLocation(g_program.program, "u_lightDirection");
	g_repeatLocation = glGetUniformLocation(g_program.program, "u_repeat");
	g_textureLocation = glGetUniformLocation(g_program.program, "u_texture");

	// Retrieve the attribute locations in the program.
	g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex");
	g_normalLocation = glGetAttribLocation(g_program.program, "a_normal");
	g_texCoordLocation = glGetAttribLocation(g_program.program, "a_texCoord");

	//
	// SSAO shader etc.
	//

	glusLoadTextFile("../Example28/shader/ssao.vert.glsl", &vertexSource);
	glusLoadTextFile("../Example28/shader/ssao.frag.glsl", &fragmentSource);

	glusBuildProgramFromSource(&g_ssaoProgram, (const GLUSchar**)&vertexSource.text, 0, 0, 0, (const GLUSchar**)&fragmentSource.text);

	glusDestroyTextFile(&vertexSource);
	glusDestroyTextFile(&fragmentSource);

	//

	// Retrieve the uniform locations in the program.
	g_ssaoTextureLocation = glGetUniformLocation(g_ssaoProgram.program, "u_texture");
	g_ssaoNormalTextureLocation = glGetUniformLocation(g_ssaoProgram.program, "u_normalTexture");
	g_ssaoDepthTextureLocation = glGetUniformLocation(g_ssaoProgram.program, "u_depthTexture");
	g_ssaoKernelLocation = glGetUniformLocation(g_ssaoProgram.program, "u_kernel");
	g_ssaoRotationNoiseTextureLocation = glGetUniformLocation(g_ssaoProgram.program, "u_rotationNoiseTexture");
	g_ssaoRotationNoiseScaleLocation = glGetUniformLocation(g_ssaoProgram.program, "u_rotationNoiseScale");
	g_ssaoInverseProjectionMatrixLocation = glGetUniformLocation(g_ssaoProgram.program, "u_inverseProjectionMatrix");
	g_ssaoProjectionMatrixLocation = glGetUniformLocation(g_ssaoProgram.program, "u_projectionMatrix");
	g_ssaoRadiusLocation = glGetUniformLocation(g_ssaoProgram.program, "u_radius");

	// Retrieve the attribute locations in the program.
	g_ssaoVertexLocation = glGetAttribLocation(g_ssaoProgram.program, "a_vertex");
	g_ssaoTexCoordLocation = glGetAttribLocation(g_ssaoProgram.program, "a_texCoord");

	//
	// Blur shader etc.
	//

	glusLoadTextFile("../Example28/shader/blur.vert.glsl", &vertexSource);
	glusLoadTextFile("../Example28/shader/blur.frag.glsl", &fragmentSource);

	glusBuildProgramFromSource(&g_blurProgram, (const GLUSchar**)&vertexSource.text, 0, 0, 0, (const GLUSchar**)&fragmentSource.text);

	glusDestroyTextFile(&vertexSource);
	glusDestroyTextFile(&fragmentSource);

	//

	// Retrieve the uniform locations in the program.
	g_blurColorTextureLocation = glGetUniformLocation(g_blurProgram.program, "u_colorTexture");

	g_blurSSAOTextureLocation = glGetUniformLocation(g_blurProgram.program, "u_ssaoTexture");

	g_blurTexelStepLocation = glGetUniformLocation(g_blurProgram.program, "u_texelStep");

	g_blurNoSSAOLocation = glGetUniformLocation(g_blurProgram.program, "u_noSSAO");

	// Retrieve the attribute locations in the program.
	g_blurVertexLocation = glGetAttribLocation(g_blurProgram.program, "a_vertex");
	g_blurTexCoordLocation = glGetAttribLocation(g_blurProgram.program, "a_texCoord");

	//
	// Texture set up for the ground plane.
	//

	glusLoadTgaImage("wood_texture.tga", &image);

	glGenTextures(1, &g_texture);
	glBindTexture(GL_TEXTURE_2D, g_texture);

	glTexImage2D(GL_TEXTURE_2D, 0, image.format, image.width, image.height, 0, image.format, GL_UNSIGNED_BYTE, image.data);

	// Mipmap generation is now included in OpenGL 3 and above
	glGenerateMipmap(GL_TEXTURE_2D);

	// Trilinear filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glBindTexture(GL_TEXTURE_2D, 0);

	//
	// Setting up the SSAO frame buffer.
	//

	glGenTextures(1, &g_ssaoTexture);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, g_ssaoTexture);

	glTexImage2D(GL_TEXTURE_2D, 0, GLUS_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GLUS_RGB, GL_UNSIGNED_BYTE, 0);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glBindTexture(GL_TEXTURE_2D, 0);

	//

	glGenTextures(1, &g_ssaoNormalTexture);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, g_ssaoNormalTexture);

	glTexImage2D(GL_TEXTURE_2D, 0, GLUS_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GLUS_RGB, GL_UNSIGNED_BYTE, 0);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glBindTexture(GL_TEXTURE_2D, 0);

	//

	glGenTextures(1, &g_ssaoDepthTexture);
	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, g_ssaoDepthTexture);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glBindTexture(GL_TEXTURE_2D, 0);

	//

	glGenFramebuffers(1, &g_ssaoFBO);
	glBindFramebuffer(GL_FRAMEBUFFER, g_ssaoFBO);

	// Attach the color buffer ...
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_ssaoTexture, 0);

	// Attach the normal buffer ...
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, g_ssaoNormalTexture, 0);

	// ... and the depth buffer,
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, g_ssaoDepthTexture, 0);

	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
	{
		printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER));

		return GLUS_FALSE;
	}

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	//
	// Setting up the blur frame buffer
	//

	glGenTextures(1, &g_blurTexture);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, g_blurTexture);

	glTexImage2D(GL_TEXTURE_2D, 0, GLUS_RGB, TEXTURE_WIDTH, TEXTURE_HEIGHT, 0, GLUS_RGB, GL_UNSIGNED_BYTE, 0);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

	glBindTexture(GL_TEXTURE_2D, 0);

	//

	glGenFramebuffers(1, &g_blurFBO);
	glBindFramebuffer(GL_FRAMEBUFFER, g_blurFBO);

	// Attach the color buffer ...
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_blurTexture, 0);

	if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
	{
		printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER));

		return GLUS_FALSE;
	}

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	//
	// Ground plane setup.
	//

	glusCreatePlanef(&plane, 20.0f);

	g_numberIndicesPlane = plane.numberIndices;

	glGenBuffers(1, &g_verticesVBO);
	glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);
	glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 4 * sizeof(GLfloat), (GLfloat*)plane.vertices, GL_STATIC_DRAW);

	glGenBuffers(1, &g_normalsVBO);
	glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO);
	glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 3 * sizeof(GLfloat), (GLfloat*)plane.normals, GL_STATIC_DRAW);

	glGenBuffers(1, &g_texCoordsVBO);
	glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsVBO);
	glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 2 * sizeof(GLfloat), (GLfloat*)plane.texCoords, GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glGenBuffers(1, &g_indicesVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane.numberIndices * sizeof(GLuint), (GLuint*)plane.indices, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	glusDestroyShapef(&plane);

	//

	glUseProgram(g_program.program);

	glGenVertexArrays(1, &g_vao);
	glBindVertexArray(g_vao);

	glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);
	glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_vertexLocation);

	glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO);
	glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_normalLocation);

	glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsVBO);
	glVertexAttribPointer(g_texCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_texCoordLocation);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesVBO);

	glBindVertexArray(0);

	//

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, g_texture);
	glUniform1i(g_textureLocation, 0);

	// How many times the surface texture is repeated.
	glUniform1f(g_repeatLocation, 6.0f);

	//
	// Post process plane setup.
	//

	glusCreatePlanef(&plane, 1.0f);

	g_numberIndicesPostprocessPlane = plane.numberIndices;

	glGenBuffers(1, &g_postprocessVerticesVBO);
	glBindBuffer(GL_ARRAY_BUFFER, g_postprocessVerticesVBO);
	glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 4 * sizeof(GLfloat), (GLfloat*)plane.vertices, GL_STATIC_DRAW);

	glGenBuffers(1, &g_postprocessTexCoordsVBO);
	glBindBuffer(GL_ARRAY_BUFFER, g_postprocessTexCoordsVBO);
	glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 2 * sizeof(GLfloat), (GLfloat*)plane.texCoords, GL_STATIC_DRAW);

	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glGenBuffers(1, &g_postprocessIndicesVBO);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_postprocessIndicesVBO);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane.numberIndices * sizeof(GLuint), (GLuint*)plane.indices, GL_STATIC_DRAW);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

	glusDestroyShapef(&plane);

	//

	glUseProgram(g_ssaoProgram.program);

	glGenVertexArrays(1, &g_ssaoVAO);
	glBindVertexArray(g_ssaoVAO);

	glBindBuffer(GL_ARRAY_BUFFER, g_postprocessVerticesVBO);
	glVertexAttribPointer(g_ssaoVertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_ssaoVertexLocation);

	glBindBuffer(GL_ARRAY_BUFFER, g_postprocessTexCoordsVBO);
	glVertexAttribPointer(g_ssaoTexCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_ssaoTexCoordLocation);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_postprocessIndicesVBO);

	glBindVertexArray(0);

	//

	glUniform1i(g_ssaoTextureLocation, 0);
	glUniform1i(g_ssaoNormalTextureLocation, 1);
	glUniform1i(g_ssaoDepthTextureLocation, 2);
	glUniform1i(g_ssaoRotationNoiseTextureLocation, 3);

	glUniform1f(g_ssaoRadiusLocation, SSAO_RADIUS);

	//
	// Create the Kernel for SSAO.
	//

	for (i = 0; i < KERNEL_SIZE; i++)
	{
		g_kernel[i * 3 + 0] = glusRandomUniformGetFloatf(-1.0f, 1.0f);
		g_kernel[i * 3 + 1] = glusRandomUniformGetFloatf(-1.0f, 1.0f);
		g_kernel[i * 3 + 2] = glusRandomUniformGetFloatf(0.0f, 1.0f);	// Kernel hemisphere points to positive Z-Axis.

		glusVector3Normalizef(&g_kernel[i * 3]);					// Normalize, so included in the hemisphere.

		GLfloat scale = (GLfloat)i / (GLfloat)KERNEL_SIZE;		// Create a scale value between [0;1[ .

		scale = glusClampf(scale * scale, 0.1f, 1.0f);			// Adjust scale, that there are more values closer to the center of the g_kernel.

		glusVector3MultiplyScalarf(&g_kernel[i * 3], &g_kernel[i * 3], scale);
	}

	// Pass g_kernel to shader
	glUniform3fv(g_ssaoKernelLocation, KERNEL_SIZE, g_kernel);

	//
	// Create the rotation noise texture
	//

	for (i = 0; i < ROTATION_NOISE_SIZE; i++)
	{
		g_rotationNoise[i * 3 + 0] = glusRandomUniformGetFloatf(-1.0f, 1.0f);
		g_rotationNoise[i * 3 + 1] = glusRandomUniformGetFloatf(-1.0f, 1.0f);
		g_rotationNoise[i * 3 + 2] = 0.0f;						// Rotate on x-y-plane, so z is zero.

		glusVector3Normalizef(&g_rotationNoise[i * 3]);			// Normalized rotation vector.
	}

	//

	glGenTextures(1, &g_ssaoRotationNoiseTexture);
	glBindTexture(GL_TEXTURE_2D, g_ssaoRotationNoiseTexture);

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, ROTATION_NOISE_SIDE_LENGTH, ROTATION_NOISE_SIDE_LENGTH, 0, GL_RGB, GL_FLOAT, g_rotationNoise);

	// No filtering
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	glBindTexture(GL_TEXTURE_2D, 0);

	//
	//

	g_rotationNoiseScale[0] = (GLfloat)TEXTURE_WIDTH / (GLfloat)ROTATION_NOISE_SIDE_LENGTH;
	g_rotationNoiseScale[1] = (GLfloat)TEXTURE_HEIGHT / (GLfloat)ROTATION_NOISE_SIDE_LENGTH;

	// Pass the scale, as the rotation noise texture is repeated over the screen x / y times.
	glUniform2fv(g_ssaoRotationNoiseScaleLocation, 1, g_rotationNoiseScale);

	//
	//

	glUseProgram(g_blurProgram.program);

	glGenVertexArrays(1, &g_blurVAO);
	glBindVertexArray(g_blurVAO);

	glBindBuffer(GL_ARRAY_BUFFER, g_postprocessVerticesVBO);
	glVertexAttribPointer(g_blurVertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_blurVertexLocation);

	glBindBuffer(GL_ARRAY_BUFFER, g_postprocessTexCoordsVBO);
	glVertexAttribPointer(g_blurTexCoordLocation, 2, GL_FLOAT, GL_FALSE, 0, 0);
	glEnableVertexAttribArray(g_blurTexCoordLocation);

	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_postprocessIndicesVBO);

	glBindVertexArray(0);

	//

	glUniform1i(g_blurColorTextureLocation, 0);

	glUniform1i(g_blurSSAOTextureLocation, 1);

	g_texelStep[0] = 1.0f / (GLfloat)TEXTURE_WIDTH;
	g_texelStep[1] = 1.0f / (GLfloat)TEXTURE_HEIGHT;

	// Pass the value to step from one to another texel.
	glUniform2fv(g_blurTexelStepLocation, 1, g_texelStep);

	// Variable to toggle between SSAO on and off
	glUniform1f(g_blurNoSSAOLocation, 0.0f);

	//
	// Basic OpenGL set up.
	//

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	glClearDepth(1.0f);

	glEnable(GL_DEPTH_TEST);

	return GLUS_TRUE;
}
Exemplo n.º 10
0
void processFn(struct fnargs* args) {
	switch (args->fn) {
	case glfnUNDEFINED:
		abort(); // bad glfn
		break;
	case glfnActiveTexture:
		glActiveTexture((GLenum)args->a0);
		break;
	case glfnAttachShader:
		glAttachShader((GLint)args->a0, (GLint)args->a1);
		break;
	case glfnBindAttribLocation:
		glBindAttribLocation((GLint)args->a0, (GLint)args->a1, (GLchar*)args->a2);
		free((void*)args->a2);
		break;
	case glfnBindBuffer:
		glBindBuffer((GLenum)args->a0, (GLuint)args->a1);
		break;
	case glfnBindFramebuffer:
		glBindFramebuffer((GLenum)args->a0, (GLint)args->a1);
		break;
	case glfnBindRenderbuffer:
		glBindRenderbuffer((GLenum)args->a0, (GLint)args->a1);
		break;
	case glfnBindTexture:
		glBindTexture((GLenum)args->a0, (GLint)args->a1);
		break;
	case glfnBlendColor:
		glBlendColor(*(GLfloat*)&args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2, *(GLfloat*)&args->a3);
		break;
	case glfnBlendEquation:
		glBlendEquation((GLenum)args->a0);
		break;
	case glfnBlendEquationSeparate:
		glBlendEquationSeparate((GLenum)args->a0, (GLenum)args->a1);
		break;
	case glfnBlendFunc:
		glBlendFunc((GLenum)args->a0, (GLenum)args->a1);
		break;
	case glfnBlendFuncSeparate:
		glBlendFuncSeparate((GLenum)args->a0, (GLenum)args->a1, (GLenum)args->a2, (GLenum)args->a3);
		break;
	case glfnBufferData:
		glBufferData((GLenum)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2, (GLenum)args->a3);
		break;
	case glfnBufferSubData:
		glBufferSubData((GLenum)args->a0, (GLint)args->a1, (GLsizeiptr)args->a2, (GLvoid*)args->a3);
		break;
	case glfnCheckFramebufferStatus:
		ret = glCheckFramebufferStatus((GLenum)args->a0);
		break;
	case glfnClear:
		glClear((GLenum)args->a0);
		break;
	case glfnClearColor:
		glClearColor(*(GLfloat*)&args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2, *(GLfloat*)&args->a3);
		break;
	case glfnClearDepthf:
		glClearDepthf(*(GLfloat*)&args->a0);
		break;
	case glfnClearStencil:
		glClearStencil((GLint)args->a0);
		break;
	case glfnColorMask:
		glColorMask((GLboolean)args->a0, (GLboolean)args->a1, (GLboolean)args->a2, (GLboolean)args->a3);
		break;
	case glfnCompileShader:
		glCompileShader((GLint)args->a0);
		break;
	case glfnCompressedTexImage2D:
		glCompressedTexImage2D((GLenum)args->a0, (GLint)args->a1, (GLenum)args->a2, (GLint)args->a3, (GLint)args->a4, (GLint)args->a5, (GLsizeiptr)args->a6, (GLvoid*)args->a7);
		break;
	case glfnCompressedTexSubImage2D:
		glCompressedTexSubImage2D((GLenum)args->a0, (GLint)args->a1, (GLint)args->a2, (GLint)args->a3, (GLint)args->a4, (GLint)args->a5, (GLenum)args->a6, (GLsizeiptr)args->a7, (GLvoid*)args->a8);
		break;
	case glfnCopyTexImage2D:
		glCopyTexImage2D((GLenum)args->a0, (GLint)args->a1, (GLenum)args->a2, (GLint)args->a3, (GLint)args->a4, (GLint)args->a5, (GLint)args->a6, (GLint)args->a7);
		break;
	case glfnCopyTexSubImage2D:
		glCopyTexSubImage2D((GLenum)args->a0, (GLint)args->a1, (GLint)args->a2, (GLint)args->a3, (GLint)args->a4, (GLint)args->a5, (GLint)args->a6, (GLint)args->a7);
		break;
	case glfnCreateProgram:
		ret = glCreateProgram();
		break;
	case glfnCreateShader:
		ret = glCreateShader((GLenum)args->a0);
		break;
	case glfnCullFace:
		glCullFace((GLenum)args->a0);
		break;
	case glfnDeleteBuffer:
		glDeleteBuffers(1, (const GLuint*)(&args->a0));
		break;
	case glfnDeleteFramebuffer:
		glDeleteFramebuffers(1, (const GLuint*)(&args->a0));
		break;
	case glfnDeleteProgram:
		glDeleteProgram((GLint)args->a0);
		break;
	case glfnDeleteRenderbuffer:
		glDeleteRenderbuffers(1, (const GLuint*)(&args->a0));
		break;
	case glfnDeleteShader:
		glDeleteShader((GLint)args->a0);
		break;
	case glfnDeleteTexture:
		glDeleteTextures(1, (const GLuint*)(&args->a0));
		break;
	case glfnDepthFunc:
		glDepthFunc((GLenum)args->a0);
		break;
	case glfnDepthMask:
		glDepthMask((GLboolean)args->a0);
		break;
	case glfnDepthRangef:
		glDepthRangef(*(GLfloat*)&args->a0, *(GLfloat*)&args->a1);
		break;
	case glfnDetachShader:
		glDetachShader((GLint)args->a0, (GLint)args->a1);
		break;
	case glfnDisable:
		glDisable((GLenum)args->a0);
		break;
	case glfnDisableVertexAttribArray:
		glDisableVertexAttribArray((GLint)args->a0);
		break;
	case glfnDrawArrays:
		glDrawArrays((GLenum)args->a0, (GLint)args->a1, (GLint)args->a2);
		break;
	case glfnDrawElements:
		glDrawElements((GLenum)args->a0, (GLint)args->a1, (GLenum)args->a2, (void*)args->a3);
		break;
	case glfnEnable:
		glEnable((GLenum)args->a0);
		break;
	case glfnEnableVertexAttribArray:
		glEnableVertexAttribArray((GLint)args->a0);
		break;
	case glfnFinish:
		glFinish();
		break;
	case glfnFlush:
		glFlush();
		break;
	case glfnFramebufferRenderbuffer:
		glFramebufferRenderbuffer((GLenum)args->a0, (GLenum)args->a1, (GLenum)args->a2, (GLint)args->a3);
		break;
	case glfnFramebufferTexture2D:
		glFramebufferTexture2D((GLenum)args->a0, (GLenum)args->a1, (GLenum)args->a2, (GLint)args->a3, (GLint)args->a4);
		break;
	case glfnFrontFace:
		glFrontFace((GLenum)args->a0);
		break;
	case glfnGenBuffer:
		glGenBuffers(1, (GLuint*)&ret);
		break;
	case glfnGenFramebuffer:
		glGenFramebuffers(1, (GLuint*)&ret);
		break;
	case glfnGenRenderbuffer:
		glGenRenderbuffers(1, (GLuint*)&ret);
		break;
	case glfnGenTexture:
		glGenTextures(1, (GLuint*)&ret);
		break;
	case glfnGenerateMipmap:
		glGenerateMipmap((GLenum)args->a0);
		break;
	case glfnGetActiveAttrib:
		glGetActiveAttrib(
			(GLuint)args->a0,
			(GLuint)args->a1,
			(GLsizei)args->a2,
			NULL,
			(GLint*)args->a4,
			(GLenum*)args->a5,
			(GLchar*)args->a6);
		break;
	case glfnGetActiveUniform:
		glGetActiveUniform(
			(GLuint)args->a0,
			(GLuint)args->a1,
			(GLsizei)args->a2,
			NULL,
			(GLint*)args->a4,
			(GLenum*)args->a5,
			(GLchar*)args->a6);
		break;
	case glfnGetAttachedShaders:
		glGetAttachedShaders((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLuint*)args->a3);
		break;
	case glfnGetAttribLocation:
		ret = glGetAttribLocation((GLint)args->a0, (GLchar*)args->a1);
		free((void*)args->a1);
		break;
	case glfnGetBooleanv:
		glGetBooleanv((GLenum)args->a0, (GLboolean*)args->a1);
		break;
	case glfnGetBufferParameteri:
		glGetBufferParameteriv((GLenum)args->a0, (GLenum)args->a1, (GLint*)&ret);
		break;
	case glfnGetFloatv:
		glGetFloatv((GLenum)args->a0, (GLfloat*)args->a1);
		break;
	case glfnGetIntegerv:
		glGetIntegerv((GLenum)args->a0, (GLint*)args->a1);
		break;
	case glfnGetError:
		ret = glGetError();
		break;
	case glfnGetFramebufferAttachmentParameteriv:
		glGetFramebufferAttachmentParameteriv((GLenum)args->a0, (GLenum)args->a1, (GLenum)args->a2, (GLint*)&ret);
		break;
	case glfnGetProgramiv:
		glGetProgramiv((GLint)args->a0, (GLenum)args->a1, (GLint*)&ret);
		break;
	case glfnGetProgramInfoLog:
		glGetProgramInfoLog((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLchar*)args->a3);
		break;
	case glfnGetRenderbufferParameteriv:
		glGetRenderbufferParameteriv((GLenum)args->a0, (GLenum)args->a1, (GLint*)&ret);
		break;
	case glfnGetShaderiv:
		glGetShaderiv((GLint)args->a0, (GLenum)args->a1, (GLint*)&ret);
		break;
	case glfnGetShaderInfoLog:
		glGetShaderInfoLog((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLchar*)args->a3);
		break;
	case glfnGetShaderPrecisionFormat:
		glGetShaderPrecisionFormat((GLenum)args->a0, (GLenum)args->a1, (GLint*)args->a2, (GLint*)args->a3);
		break;
	case glfnGetShaderSource:
		glGetShaderSource((GLuint)args->a0, (GLsizei)args->a1, (GLsizei*)args->a2, (GLchar*)args->a3);
		break;
	case glfnGetString:
		ret = (uintptr_t)glGetString((GLenum)args->a0);
		break;
	case glfnGetTexParameterfv:
		glGetTexParameterfv((GLenum)args->a0, (GLenum)args->a1, (GLfloat*)args->a2);
		break;
	case glfnGetTexParameteriv:
		glGetTexParameteriv((GLenum)args->a0, (GLenum)args->a1, (GLint*)args->a2);
		break;
	case glfnGetUniformfv:
		glGetUniformfv((GLuint)args->a0, (GLint)args->a1, (GLfloat*)args->a2);
		break;
	case glfnGetUniformiv:
		glGetUniformiv((GLuint)args->a0, (GLint)args->a1, (GLint*)args->a2);
		break;
	case glfnGetUniformLocation:
		ret = glGetUniformLocation((GLint)args->a0, (GLchar*)args->a1);
		free((void*)args->a1);
		break;
	case glfnGetVertexAttribfv:
		glGetVertexAttribfv((GLuint)args->a0, (GLenum)args->a1, (GLfloat*)args->a2);
		break;
	case glfnGetVertexAttribiv:
		glGetVertexAttribiv((GLuint)args->a0, (GLenum)args->a1, (GLint*)args->a2);
		break;
	case glfnHint:
		glHint((GLenum)args->a0, (GLenum)args->a1);
		break;
	case glfnIsBuffer:
		ret = glIsBuffer((GLint)args->a0);
		break;
	case glfnIsEnabled:
		ret = glIsEnabled((GLenum)args->a0);
		break;
	case glfnIsFramebuffer:
		ret = glIsFramebuffer((GLint)args->a0);
		break;
	case glfnIsProgram:
		ret = glIsProgram((GLint)args->a0);
		break;
	case glfnIsRenderbuffer:
		ret = glIsRenderbuffer((GLint)args->a0);
		break;
	case glfnIsShader:
		ret = glIsShader((GLint)args->a0);
		break;
	case glfnIsTexture:
		ret = glIsTexture((GLint)args->a0);
		break;
	case glfnLineWidth:
		glLineWidth(*(GLfloat*)&args->a0);
		break;
	case glfnLinkProgram:
		glLinkProgram((GLint)args->a0);
		break;
	case glfnPixelStorei:
		glPixelStorei((GLenum)args->a0, (GLint)args->a1);
		break;
	case glfnPolygonOffset:
		glPolygonOffset(*(GLfloat*)&args->a0, *(GLfloat*)&args->a1);
		break;
	case glfnReadPixels:
		glReadPixels((GLint)args->a0, (GLint)args->a1, (GLsizei)args->a2, (GLsizei)args->a3, (GLenum)args->a4, (GLenum)args->a5, (void*)args->a6);
		break;
	case glfnReleaseShaderCompiler:
		glReleaseShaderCompiler();
		break;
	case glfnRenderbufferStorage:
		glRenderbufferStorage((GLenum)args->a0, (GLenum)args->a1, (GLint)args->a2, (GLint)args->a3);
		break;
	case glfnSampleCoverage:
		glSampleCoverage(*(GLfloat*)&args->a0, (GLboolean)args->a1);
		break;
	case glfnScissor:
		glScissor((GLint)args->a0, (GLint)args->a1, (GLint)args->a2, (GLint)args->a3);
		break;
	case glfnShaderSource:
#if defined(os_ios) || defined(os_osx)
		glShaderSource((GLuint)args->a0, (GLsizei)args->a1, (const GLchar *const *)args->a2, NULL);
#else
		glShaderSource((GLuint)args->a0, (GLsizei)args->a1, (const GLchar **)args->a2, NULL);
#endif
		free(*(void**)args->a2);
		free((void*)args->a2);
		break;
	case glfnStencilFunc:
		glStencilFunc((GLenum)args->a0, (GLint)args->a1, (GLuint)args->a2);
		break;
	case glfnStencilFuncSeparate:
		glStencilFuncSeparate((GLenum)args->a0, (GLenum)args->a1, (GLint)args->a2, (GLuint)args->a3);
		break;
	case glfnStencilMask:
		glStencilMask((GLuint)args->a0);
		break;
	case glfnStencilMaskSeparate:
		glStencilMaskSeparate((GLenum)args->a0, (GLuint)args->a1);
		break;
	case glfnStencilOp:
		glStencilOp((GLenum)args->a0, (GLenum)args->a1, (GLenum)args->a2);
		break;
	case glfnStencilOpSeparate:
		glStencilOpSeparate((GLenum)args->a0, (GLenum)args->a1, (GLenum)args->a2, (GLenum)args->a3);
		break;
	case glfnTexImage2D:
		glTexImage2D(
			(GLenum)args->a0,
			(GLint)args->a1,
			(GLint)args->a2,
			(GLsizei)args->a3,
			(GLsizei)args->a4,
			0, // border
			(GLenum)args->a5,
			(GLenum)args->a6,
			(const GLvoid*)args->a7);
		break;
	case glfnTexSubImage2D:
		glTexSubImage2D(
			(GLenum)args->a0,
			(GLint)args->a1,
			(GLint)args->a2,
			(GLint)args->a3,
			(GLsizei)args->a4,
			(GLsizei)args->a5,
			(GLenum)args->a6,
			(GLenum)args->a7,
			(const GLvoid*)args->a8);
		break;
	case glfnTexParameterf:
		glTexParameterf((GLenum)args->a0, (GLenum)args->a1, *(GLfloat*)&args->a2);
		break;
	case glfnTexParameterfv:
		glTexParameterfv((GLenum)args->a0, (GLenum)args->a1, (GLfloat*)args->a2);
		break;
	case glfnTexParameteri:
		glTexParameteri((GLenum)args->a0, (GLenum)args->a1, (GLint)args->a2);
		break;
	case glfnTexParameteriv:
		glTexParameteriv((GLenum)args->a0, (GLenum)args->a1, (GLint*)args->a2);
		break;
	case glfnUniform1f:
		glUniform1f((GLint)args->a0, *(GLfloat*)&args->a1);
		break;
	case glfnUniform1fv:
		glUniform1fv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniform1i:
		glUniform1i((GLint)args->a0, (GLint)args->a1);
		break;
	case glfnUniform1iv:
		glUniform1iv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniform2f:
		glUniform2f((GLint)args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2);
		break;
	case glfnUniform2fv:
		glUniform2fv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniform2i:
		glUniform2i((GLint)args->a0, (GLint)args->a1, (GLint)args->a2);
		break;
	case glfnUniform2iv:
		glUniform2iv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniform3f:
		glUniform3f((GLint)args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2, *(GLfloat*)&args->a3);
		break;
	case glfnUniform3fv:
		glUniform3fv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniform3i:
		glUniform3i((GLint)args->a0, (GLint)args->a1, (GLint)args->a2, (GLint)args->a3);
		break;
	case glfnUniform3iv:
		glUniform3iv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniform4f:
		glUniform4f((GLint)args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2, *(GLfloat*)&args->a3, *(GLfloat*)&args->a4);
		break;
	case glfnUniform4fv:
		glUniform4fv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniform4i:
		glUniform4i((GLint)args->a0, (GLint)args->a1, (GLint)args->a2, (GLint)args->a3, (GLint)args->a4);
		break;
	case glfnUniform4iv:
		glUniform4iv((GLint)args->a0, (GLsizeiptr)args->a1, (GLvoid*)args->a2);
		break;
	case glfnUniformMatrix2fv:
		glUniformMatrix2fv((GLint)args->a0, (GLsizeiptr)args->a1, 0, (GLvoid*)args->a2);
		break;
	case glfnUniformMatrix3fv:
		glUniformMatrix3fv((GLint)args->a0, (GLsizeiptr)args->a1, 0, (GLvoid*)args->a2);
		break;
	case glfnUniformMatrix4fv:
		glUniformMatrix4fv((GLint)args->a0, (GLsizeiptr)args->a1, 0, (GLvoid*)args->a2);
		break;
	case glfnUseProgram:
		glUseProgram((GLint)args->a0);
		break;
	case glfnValidateProgram:
		glValidateProgram((GLint)args->a0);
		break;
	case glfnVertexAttrib1f:
		glVertexAttrib1f((GLint)args->a0, *(GLfloat*)&args->a1);
		break;
	case glfnVertexAttrib1fv:
		glVertexAttrib1fv((GLint)args->a0, (GLfloat*)args->a1);
		break;
	case glfnVertexAttrib2f:
		glVertexAttrib2f((GLint)args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2);
		break;
	case glfnVertexAttrib2fv:
		glVertexAttrib2fv((GLint)args->a0, (GLfloat*)args->a1);
		break;
	case glfnVertexAttrib3f:
		glVertexAttrib3f((GLint)args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2, *(GLfloat*)&args->a3);
		break;
	case glfnVertexAttrib3fv:
		glVertexAttrib3fv((GLint)args->a0, (GLfloat*)args->a1);
		break;
	case glfnVertexAttrib4f:
		glVertexAttrib4f((GLint)args->a0, *(GLfloat*)&args->a1, *(GLfloat*)&args->a2, *(GLfloat*)&args->a3, *(GLfloat*)&args->a4);
		break;
	case glfnVertexAttrib4fv:
		glVertexAttrib4fv((GLint)args->a0, (GLfloat*)args->a1);
		break;
	case glfnVertexAttribPointer:
		glVertexAttribPointer((GLuint)args->a0, (GLint)args->a1, (GLenum)args->a2, (GLboolean)args->a3, (GLsizei)args->a4, (const GLvoid*)args->a5);
		break;
	case glfnViewport:
		glViewport((GLint)args->a0, (GLint)args->a1, (GLint)args->a2, (GLint)args->a3);
		break;
	}
}
void hge::shader::SunShader::setSunDirection(const glm::vec3 &sunDirection)
{
	glUniform3fv(sunLightDirectionUniformLocation, 1, glm::value_ptr(sunDirection));
}
Exemplo n.º 12
0
	    inline void operator=(const std::tr1::array<GLfloat, 3>& val)
	    { 
	      glUseProgramObjectARB(_programHandle);
	      glUniform3fv(_uniformHandle, 1, &(val[0]));
	    }
void opengl_display()
{
	static double start = get_time();
	float t = get_time()-start;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	

	
	t *= 25.f;


	
//	background_world.rotate_y(t);
	background_world = statue_world;

	vec3 eye = vec3(0,0,distance_z);
	view.look_at(eye, vec3(0,0,0), vec3(0,1,0));

	
	//render background
	wvp = projection * background_world;

	if(background_mesh.program != NULL)
	{
		glUseProgram(background_mesh.program->get_program());
		glUniformMatrix4fv(background_mesh.wvp_loc, 1, GL_FALSE, &wvp[0]);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_CUBE_MAP, background_mesh.texs_idxs[0]);
		glUniform1i(background_mesh.texs_locs[0], 0);

		glUniformMatrix4fv(background_mesh.wvp_loc, 1, GL_FALSE, &wvp[0]);

		background_mesh.draw();
	}

	//render statue

	env_inv_world = background_world.inverse();
	wvp = projection * view;
	wvp *= statue_world;
	eye = env_inv_world * eye;


	if(statue_mesh.program != NULL)
	{

		glUseProgram(statue_mesh.program->get_program());
		glUniformMatrix4fv(statue_mesh.wvp_loc, 1, GL_FALSE, &wvp[0]);
		glUniformMatrix4fv(statue_mesh.world_loc, 1, GL_FALSE, &statue_world[0]);
		glUniformMatrix4fv(statue_mesh.env_inv_world_loc, 1,GL_FALSE, &env_inv_world[0]);
		glUniform3fv(statue_mesh.eye_loc, 1, &eye.x);

		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, statue_mesh.texs_idxs[0]);
		glUniform1i(statue_mesh.texs_locs[0], 0);

		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_CUBE_MAP, statue_mesh.texs_idxs[1]);
		glUniform1i(statue_mesh.texs_locs[1], 1);

		statue_mesh.draw();

	}
}
Exemplo n.º 14
0
/*
 Inits the shader program for this object
 */
void GLSphereBlue::initShader(void)
{

#ifdef _WIN32
    // This loads the shader program from a file
    _program = LoadAndCreateShaderProgram("../data/shaders/bluesphere.vs", "../data/shaders/bluesphere.vs");
#else
    // This loads the shader program from a file
    _program = LoadAndCreateShaderProgram("/Users/geethanjalijeevanatham/Desktop/Helloworld/myopenGL/model/testing/assignment2/Problem2/Problem2/data/shaders/bluesphere.vs", "/Users/geethanjalijeevanatham/Desktop/Helloworld/myopenGL/model/testing/assignment2/Problem2/Problem2/data/shaders/bluesphere.fs");
#endif
    glUseProgram(_program);



    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Vertex information / names

    glBindAttribLocation(_program, 0, "in_Position");
    glBindAttribLocation(_program, 1, "in_Normal");
    glBindAttribLocation(_program, 2, "in_Color");


    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Define the model view matrix.


    _modelMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); // Create our model matrix which will halve the size of our model


    _projectionMatrixLocation = glGetUniformLocation(_program, "projectionMatrixBox"); // Get the location of our projection matrix in the shader
    _viewMatrixLocation = glGetUniformLocation(_program, "viewMatrixBox"); // Get the location of our view matrix in the shader
    _modelMatrixLocation = glGetUniformLocation(_program, "modelMatrixBox"); // Get the location of our model matrix in the shader
    _inverseViewMatrixLocation = glGetUniformLocation(_program, "inverseViewMatrix");


    glUniformMatrix4fv(_projectionMatrixLocation, 1, GL_FALSE, &projectionMatrix()[0][0] ); // Send our projection matrix to the shader
    glUniformMatrix4fv(_viewMatrixLocation, 1, GL_FALSE, &viewMatrix()[0][0]); // Send our view matrix to the shader
    glUniformMatrix4fv(_modelMatrixLocation, 1, GL_FALSE, &_modelMatrix[0][0]); // Send our model matrix to the shader
    glUniformMatrix4fv(_inverseViewMatrixLocation, 1, GL_FALSE, &invRotatedViewMatrix()[0][0]);

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Material
    _material._diffuse_material = glm::vec3(0.0, 0.0, 1.0);
    _material._ambient_material = glm::vec3(0.0, 0.0, 1.0);
    _material._specular_material = glm::vec3(1.0, 1.0, 1.0);
    _material._shininess = 150.0;


    _material._ambientColorPos = glGetUniformLocation(_program, "ambient_color");
    _material._diffuseColorPos = glGetUniformLocation(_program, "diffuse_color");
    _material._specularColorPos = glGetUniformLocation(_program, "specular_color");
    _material._shininessIdx = glGetUniformLocation(_program, "shininess");


    // Send the material to your shader program
    glUniform3fv(_material._ambientColorPos, 1, &_material._ambient_material[0] );
    glUniform3fv(_material._diffuseColorPos, 1, &_material._diffuse_material[0]);
    glUniform3fv(_material._specularColorPos, 1, &_material._specular_material[0]);
    glUniform1f(_material._shininessIdx, _material._shininess);

    ///////////////////////////////////////////////////////////////////////////////////////////////
    // Light

    // define the position of the light and send the light position to your shader program
    _light_source4._lightPos = glm::vec4(1.5,0.0,2.0,0.0);
    _light_source4._ambient_intensity = 0.0;
    _light_source4._specular_intensity = 0.0;
    _light_source4._diffuse_intensity = 1.0;
    _light_source4._attenuation_coeff = 0.02;



    // Read all the index values from the shader program
    _light_source4._ambientIdx = glGetUniformLocation(_program, "ambient_intensity");
    _light_source4._diffuseIdx = glGetUniformLocation(_program, "diffuse_intensity");
    _light_source4._specularIdx = glGetUniformLocation(_program, "specular_intensity");
    _light_source4._attenuation_coeffIdx = glGetUniformLocation(_program, "attenuationCoefficient");
    _light_source4._lightPosIdx = glGetUniformLocation(_program, "light_position");


    // Send the light information to your shader program
    glUniform1f(_light_source4._ambientIdx, _light_source4._ambient_intensity );
    glUniform1f(_light_source4._diffuseIdx, _light_source4._diffuse_intensity);
    glUniform1f(_light_source4._specularIdx, _light_source4._specular_intensity);
    glUniform1f(_light_source4._attenuation_coeffIdx, _light_source4._attenuation_coeff);
    glUniform4fv(_light_source4._lightPosIdx, 1, &_light_source4._lightPos[0]);




    glUseProgram(0);


}
Exemplo n.º 15
0
void SCE_RSetProgramParam3fv (SCEint idx, size_t size, const float *val)
{
    glUniform3fv (idx, size, val);
}
Exemplo n.º 16
0
	void ShaderGL::SetFloat3(UINT uniform, float* data, UINT count)
	{
		glUniform3fv(mUniformsGL[uniform], count, data);
	}
Exemplo n.º 17
0
/**
 * \brief drawText,		render 2D text on the screen (Orthogonal Projection)
 * @param str,			text string.
 * @param pos,			2D position on screen, TODO support Z as priority for future reference, but for now not used
 * @param rgba,			text color
 * @param tick			time elapsed
 * @return float width 	of text string.
 */
float RenderText::drawText(const char*str, glm::vec3 &pos, glm::vec4 &rgba, float tick)
{
//	checkGLError("RenderText::DrawText   Should be ok!");
	float width = 0;
	m_pos = pos;
	int num = strlen(str);
	glm::vec4 *vertices = 0;

	glDisable(GL_DEPTH_TEST);
	glm::mat4 m = glm::ortho(0.0f, static_cast<float>(m_width),static_cast<float>(m_height),0.0f,-200.0f,1000.0f);

	// Use the font class to build the vertex array from the sentence text and sentence draw location.
	// Create the vertex array.

	m_scaleZ = -m_scaleZ;
	vertices = getVertices(str, false, &width);		// note uses m_pos creates or retrieves existing vertex buffer from a cache table
	m_scaleZ = -m_scaleZ;

	TextShader &shader= m_textShader;
	glEnable (GL_BLEND);
	m_blendType = BL_ADDITIVE;
	switch (m_blendType)
	{
	case BL_ADDITIVE:					// Additive
		glBlendFunc(GL_ONE, GL_ONE);
//		glBlendFunc( GL_DST_ALPHA, GL_ONE_MINUS_SRC_COLOR );	// very good additive
		break;
	case BL_TRANSLUCENT:				// Requires alpha
		glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
//		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);	// Texture uses alpha
		break;
	case BL_SUBTRACTIVE:
		//glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_COLOR);
		glBlendFunc( GL_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA ); //subtractive black => max
//		glBlendFunc(GL_ZERO, GL_SRC_ALPHA);		// needs alpha
//		glBlendFunc(GL_ZERO, GL_SRC_COLOR);
		break;
	case BL_TRANSPARENT:
		glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
		break;
	}

	// Use the program object
	glUseProgram(shader.mShader);
	checkGLError("RenderText::DrawText   glUseProgram (Text)");

	// 1. create buffers tri array for simplicity? so that every tri can be handled separately or with a new index buffer could be expensive though???
	// Load the vertex data
	glVertexAttribPointer(shader.mAttribVtxLoc, 4, GL_FLOAT, GL_FALSE, sizeof(float)*4, &vertices[0].x);
	checkGLError("RenderText::DrawText   glVertexAttribPointer (V)");
	glEnableVertexAttribArray(shader.mAttribVtxLoc);
	checkGLError("RenderText::DrawText   glEnableVertexAttribArray (V)");
	glActiveTexture(GL_TEXTURE0 + 0);
	checkGLError("RenderText::DrawText   glActiveTexture");
	glBindTexture(GL_TEXTURE_2D, m_font->GetTexture(0));
	checkGLError("glBindTexture");
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	checkGLError("glTexParameteri");
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	checkGLError("glTexParameteri");
	glUniform1i(shader.mTexture, 0 ); // Texture Unit 0
	checkGLError("RenderText::DrawText   glUniform1i");

	// Once per frame
	glUniform1f(shader.mTimeLoc, tick);
	checkGLError("RenderText::DrawText   glUniform1f");
	glUniform2f(shader.mResolutionLoc, 1.0f/(float)m_width, 1.0f/(float)m_height);
	checkGLError("RenderText::DrawText   glUniform2f");
	glUniformMatrix4fv(shader.mMatrixPVW, 1, GL_FALSE, &m[0][0]);
	checkGLError("RenderText::DrawText   glUniformMatrix4fv");
	glm::vec3 sv(1.0f, 1.0f, 1.0f);
	glUniform3fv(shader.mScaleV,1, (float *)&sv.x);				// mScale location => variable "ScaleV" in vertex shader
	checkGLError("RenderText::DrawText   glUniformMatrix3fv");
	glm::vec4 color = rgba;
	glUniform4fv(shader.mColor,1, (float *)&color.x);
	checkGLError("RenderText::DrawText   glUniformMatrix4fv");
	glm::vec4 TPos(0.0f,0.0f,m_pos.z, 1.0f);
	glUniform4fv(shader.mTPos, 1, (float *)&TPos.x);
	checkGLError("RenderText::DrawText   glUniformMatrix4fv");
	glDrawArrays(GL_TRIANGLES, 0, 6*num);
	checkGLError("RenderText::DrawText   glDrawArrays");

	// Clean-up
	glDisableVertexAttribArray(shader.mAttribVtxLoc);
	checkGLError("RenderText::DrawText   glDisableVertexAttribArray (Vtx)");
	glBindTexture(GL_TEXTURE_2D, 0);
	glUseProgram(0);
	checkGLError("RenderText::DrawText   glUseProgram(0)");

	releaseVertices(vertices, false);
	glDisable(GL_BLEND);

	return width;			// Generate a buffer for the vertices
}
Exemplo n.º 18
0
Arquivo: main.c Projeto: AJ92/OpenGL
GLUSboolean init(GLUSvoid)
{
    // This is a white light.
    struct LightProperties light = { { 1.0f, 1.0f, 1.0f }, { 0.3f, 0.3f, 0.3f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f } };

    // Blue color material with white specular color.
    struct MaterialProperties material = { { 0.0f, 1.0f, 0.0f, 1.0f }, { 0.0f, 1.0f, 0.0f, 1.0f }, { 1.0f, 1.0f, 1.0f, 1.0f }, 20.0f };

    GLUStextfile vertexSource;
    GLUStextfile fragmentSource;

    GLUSshape wavefrontObj;

    glusFileLoadText("../Example16/shader/phong.vert.glsl", &vertexSource);
    glusFileLoadText("../Example16/shader/phong.frag.glsl", &fragmentSource);

    glusProgramBuildFromSource(&g_program, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text);

    glusFileDestroyText(&vertexSource);
    glusFileDestroyText(&fragmentSource);

    //

    g_projectionMatrixLocation = glGetUniformLocation(g_program.program, "u_projectionMatrix");
    g_modelViewMatrixLocation = glGetUniformLocation(g_program.program, "u_modelViewMatrix");
    g_normalMatrixLocation = glGetUniformLocation(g_program.program, "u_normalMatrix");

    g_light.directionLocation = glGetUniformLocation(g_program.program, "u_light.direction");
    g_light.ambientColorLocation = glGetUniformLocation(g_program.program, "u_light.ambientColor");
    g_light.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_light.diffuseColor");
    g_light.specularColorLocation = glGetUniformLocation(g_program.program, "u_light.specularColor");

    g_material.ambientColorLocation = glGetUniformLocation(g_program.program, "u_material.ambientColor");
    g_material.diffuseColorLocation = glGetUniformLocation(g_program.program, "u_material.diffuseColor");
    g_material.specularColorLocation = glGetUniformLocation(g_program.program, "u_material.specularColor");
    g_material.specularExponentLocation = glGetUniformLocation(g_program.program, "u_material.specularExponent");

    g_vertexLocation = glGetAttribLocation(g_program.program, "a_vertex");
    g_normalLocation = glGetAttribLocation(g_program.program, "a_normal");

    //

    // Use a helper function to load an wavefront object file.
    glusShapeLoadWavefront("monkey.obj", &wavefrontObj);

    g_numberVertices = wavefrontObj.numberVertices;

    glGenBuffers(1, &g_verticesVBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);
    glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) wavefrontObj.vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &g_normalsVBO);
    glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO);
    glBufferData(GL_ARRAY_BUFFER, wavefrontObj.numberVertices * 3 * sizeof(GLfloat), (GLfloat*) wavefrontObj.normals, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, 0);

    glusShapeDestroyf(&wavefrontObj);

    //

    glUseProgram(g_program.program);

    glGenVertexArrays(1, &g_vao);
    glBindVertexArray(g_vao);

    glBindBuffer(GL_ARRAY_BUFFER, g_verticesVBO);
    glVertexAttribPointer(g_vertexLocation, 4, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(g_vertexLocation);

    glBindBuffer(GL_ARRAY_BUFFER, g_normalsVBO);
    glVertexAttribPointer(g_normalLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(g_normalLocation);

    //

    glusMatrix4x4LookAtf(g_viewMatrix, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

    //

    glusVector3Normalizef(light.direction);

    // Transform light to camera space, as it is currently in world space.
    glusMatrix4x4MultiplyVector3f(light.direction, g_viewMatrix, light.direction);

    // Set up light ...
    glUniform3fv(g_light.directionLocation, 1, light.direction);
    glUniform4fv(g_light.ambientColorLocation, 1, light.ambientColor);
    glUniform4fv(g_light.diffuseColorLocation, 1, light.diffuseColor);
    glUniform4fv(g_light.specularColorLocation, 1, light.specularColor);

    // ... and material values.
    glUniform4fv(g_material.ambientColorLocation, 1, material.ambientColor);
    glUniform4fv(g_material.diffuseColorLocation, 1, material.diffuseColor);
    glUniform4fv(g_material.specularColorLocation, 1, material.specularColor);
    glUniform1f(g_material.specularExponentLocation, material.specularExponent);

    //

    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    glClearDepth(1.0f);

    glEnable(GL_DEPTH_TEST);

    glEnable(GL_CULL_FACE);

    return GLUS_TRUE;
}
void UniformImplementation_Legacy::set(const Program * program, const GLint location, const glm::vec3 & value) const
{
    program->use();
    glUniform3fv(location, 1, glm::value_ptr(value));
}
Exemplo n.º 20
0
void Shader::setUniform3fv( int iUniformLocation, int iCount, float * pValue)
{
	/// Need to Activate the shader before, but we are not doing it
	/// in here to save computation time (if we are setting several variable at the same time)
	glUniform3fv( iUniformLocation, iCount, pValue);
}
Exemplo n.º 21
0
/*!****************************************************************************
 @Function		RenderScene
 @Return		bool		true if no error occurred
 @Description	Main rendering loop function of the program. The shell will
				call this function every frame.
				eglSwapBuffers() will be performed by PVRShell automatically.
				PVRShell will also manage important OS events.
				Will also manage relevant OS events. The user has access to
				these events through an abstraction layer provided by PVRShell.
******************************************************************************/
bool OGLES2MagicLantern::RenderScene()
{
	PVRTMat4 mLightWorld;

	// Clear the color and depth buffer.
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Time based animation and locks the app to 60 FPS.
	// Uses the shell function PVRShellGetTime() to get the time in milliseconds.
	unsigned long ulTime = PVRShellGetTime();
	unsigned long ulDeltaTime = ulTime - m_ulTimePrev;
	m_ulTimePrev = ulTime;
	m_fFrame += (float)ulDeltaTime * (60.0f/1000.0f);

	// Create an animation for the the position and rotation of the light-ball.
	m_LightPosition.x = cos(m_fFrame/140.0f)*60.0f;
	m_LightPosition.z = sin(m_fFrame/70.0f)*60.0f - 40.0f;
	m_LightPosition.y = sin(m_fFrame/100.0f)*20.0f;

	mLightWorld  = PVRTMat4::Translation(m_LightPosition.x, m_LightPosition.y-10, m_LightPosition.z);
	mLightWorld *= PVRTMat4::RotationX(m_fFrame/540.0f);
	mLightWorld *= PVRTMat4::RotationZ(m_fFrame/370.0f);
	
	// To draw a scene, you must go through all the MeshNodes and draw the referenced meshes.
	for (unsigned int i = 0; i < m_Scene.nNumMeshNode; ++i)
	{
		SPODNode& Node = m_Scene.pNode[i]; 

		// Get the current effect from the material ID. The list of IDs was built in LoadPFX()
		GLuint uiFXID = m_puiMaterialEffectID[Node.nIdxMaterial];

		// Use the loaded effect (Vertex and Fragment shader)
		// And also bind all textures in the effect.
		m_pFX[uiFXID]->Activate();
		
		// Set the blend mode
		// Based in the info stored in the material by PVRShaman.
		// We check whether the blend mode is 'opaque' (ONE,ZERO).
		// Otherwise we enable blending and set the corresponding operations.
		if (m_Scene.pMaterial[Node.nIdxMaterial].eBlendSrcRGB == ePODBlendFunc_ONE && m_Scene.pMaterial[Node.nIdxMaterial].eBlendDstRGB == ePODBlendFunc_ZERO)
		{
			glDisable(GL_BLEND);
		}
		else
		{
			glEnable(GL_BLEND);
			glBlendFunc(m_Scene.pMaterial[Node.nIdxMaterial].eBlendSrcRGB, m_Scene.pMaterial[Node.nIdxMaterial].eBlendDstRGB);
		}

		// Now process PVRShaman semantics and set-up the associated uniforms.
		const CPVRTArray<SPVRTPFXUniform>& Uniforms = m_pFX[uiFXID]->GetUniformArray();
		for(unsigned int j = 0; j < Uniforms.GetSize() ; ++j)
		{
			switch(Uniforms[j].nSemantic)
			{
			case ePVRTPFX_UsWORLDVIEWPROJECTION:
				{
					PVRTMat4 mWVP;

					// The whole scene (except the ball) is static.
					// The POD file was saved with 'word space' coordinates so there is no need to apply the
					// transformation matrices for the meshes, just the identity.
					// If you are exporting animation you will need to set the corresponding mesh
					// transformation matrix here (see IntroducingPOD training course).
					PVRTMat4 mWorld = PVRTMat4::Identity();

					// Check whether the current effect is the sphere and set up the word matrix with the sphere animation.
					if (m_ppEffectParser->GetEffect(uiFXID).Name == c_SphereEffectName)
					{
						mWorld = mLightWorld;
					}

					// Pass the model-view-projection matrix (MVP) to the shader to transform the vertices.
					PVRTMat4 mModelView;
					mModelView = m_mView * mWorld;
					mWVP = m_mProjection * mModelView;

					glUniformMatrix4fv(Uniforms[j].nLocation, 1, GL_FALSE, mWVP.f);
				} 
				break;
			case ePVRTPFX_UsWORLDIT:
				{
					// Passes the inverse transpose of the light rotation matrix.
					// This is needed to transform the light direction (from the light to each vertex) 
					// and it will give us a three component texture vector to map the cubemap texture.
					PVRTMat3 mLightModelIT = PVRTMat3(mLightWorld).inverse().transpose();
					glUniformMatrix3fv(Uniforms[j].nLocation, 1, GL_FALSE, mLightModelIT.f);
				}
				break;
			case ePVRTPFX_UsLIGHTPOSWORLD:
				{
					glUniform3f(Uniforms[j].nLocation, m_LightPosition.x, m_LightPosition.y, m_LightPosition.z);
				}
				break;
			case ePVRTPFX_UsLIGHTCOLOR:
				{
					// Some color variation here to make it more interesting.
					float afLightColor[] = { 1.0f, sin(m_fFrame/300.0f)*0.3f+0.7f, cos(m_fFrame/400.0f)*0.3f+0.7f};
					glUniform3fv(Uniforms[j].nLocation, 1, afLightColor ); 
				}
				break;
			}
		}

		// Now that all transformation matrices and the materials are ready,
		// call a function to actually draw the mesh.
		// We need to pass the current effect to process 'attributes' properly.
		DrawMesh(i, m_pFX[uiFXID]);
	}

	// Displays the demo name using the tools. For a detailed explanation, see the training course IntroducingPVRTools.
	m_Print3D.DisplayDefaultTitle("Magic Lantern", "", ePVRTPrint3DSDKLogo);
	m_Print3D.Flush();

	return true;
}
Exemplo n.º 22
0
// The MAIN function, from here we start our application and run our Game loop
int main()
{
    // Init GLFW
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", nullptr, nullptr); // Windowed
    glfwMakeContextCurrent(window);

    // Set the required callback functions
    glfwSetKeyCallback(window, key_callback);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetScrollCallback(window, scroll_callback);

    // Options
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    // Initialize GLEW to setup the OpenGL Function pointers
    glewExperimental = GL_TRUE;
    glewInit();

    // Define the viewport dimensions
    glViewport(0, 0, SCR_WIDTH, SCR_HEIGHT);

    // Setup some OpenGL options
    glEnable(GL_DEPTH_TEST);

    // Setup and compile our shaders
    Shader shader("parallax_mapping.vs", "parallax_mapping.frag");

    // Load textures
    GLuint diffuseMap = loadTexture("../../../resources/textures/bricks2.jpg");
	GLuint normalMap = loadTexture("../../../resources/textures/bricks2_normal.jpg");
	GLuint heightMap = loadTexture("../../../resources/textures/bricks2_disp.jpg");
    //GLuint diffuseMap = loadTexture("../../../resources/textures/wood.png");
    //GLuint normalMap = loadTexture("../../../resources/textures/toy_box_normal.png");
    //GLuint heightMap = loadTexture("../../../resources/textures/toy_box_disp.png");

    // Set texture units 
    shader.Use();
    glUniform1i(glGetUniformLocation(shader.Program, "diffuseMap"), 0);
	glUniform1i(glGetUniformLocation(shader.Program, "normalMap"), 1);
	glUniform1i(glGetUniformLocation(shader.Program, "depthMap"), 2);

    // Light position
    glm::vec3 lightPos(0.5f, 1.0f, 0.3f);

    // Game loop
    while (!glfwWindowShouldClose(window))
    {
        // Set frame time
        GLfloat currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;

        // Check and call events
        glfwPollEvents();
        Do_Movement();

        // Clear the colorbuffer
        glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Configure view/projection matrices
        shader.Use();
        glm::mat4 view = camera.GetViewMatrix();
        glm::mat4 projection = glm::perspective(camera.Zoom, (GLfloat)SCR_WIDTH / (GLfloat)SCR_HEIGHT, 0.1f, 100.0f);
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "view"), 1, GL_FALSE, glm::value_ptr(view));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
        // Render normal-mapped quad
        glm::mat4 model; 
        //model = glm::rotate(model, (GLfloat)glfwGetTime() * -10, glm::normalize(glm::vec3(1.0, 0.0, 1.0))); // Rotates the quad to show parallax mapping works in all directions
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        glUniform3fv(glGetUniformLocation(shader.Program, "lightPos"), 1, &lightPos[0]);
		glUniform3fv(glGetUniformLocation(shader.Program, "viewPos"), 1, &camera.Position[0]);
		glUniform1f(glGetUniformLocation(shader.Program, "height_scale"), height_scale);
		glUniform1i(glGetUniformLocation(shader.Program, "parallax"), parallax_mapping);
        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, diffuseMap);
        glActiveTexture(GL_TEXTURE1);
        glBindTexture(GL_TEXTURE_2D, normalMap);
		glActiveTexture(GL_TEXTURE2);
		glBindTexture(GL_TEXTURE_2D, heightMap);
        RenderQuad();

        // render light source (simply renders a smaller plane at the light's position for debugging/visualization)
        model = glm::mat4();
        model = glm::translate(model, lightPos);
        model = glm::scale(model, glm::vec3(0.1f));
        glUniformMatrix4fv(glGetUniformLocation(shader.Program, "model"), 1, GL_FALSE, glm::value_ptr(model));
        //RenderQuad();

        // Swap the buffers
        glfwSwapBuffers(window);
    }

    glfwTerminate();
    return 0;
}
Exemplo n.º 23
0
/*
==================
RB_ARB2_DrawInteraction
==================
*/
void	RB_GLSL_DrawInteraction( const drawInteraction_t *din ) {
	// load all the vertex program parameters

	glUniform3fv(viewOrgin, 1, din->localViewOrigin.ToFloatPtr());
	glUniform4fv(lightProjS, 1, din->lightProjection[0].ToFloatPtr());
	glUniform4fv(lightProjT, 1, din->lightProjection[1].ToFloatPtr());
	glUniform4fv(lightProjQ, 1, din->lightProjection[2].ToFloatPtr());
	glUniform4fv(lightFallOffS, 1, din->lightProjection[3].ToFloatPtr());
	glUniform4fv(bumpMatrixS, 1, din->bumpMatrix[0].ToFloatPtr());
	glUniform4fv(bumpMatrixT, 1, din->bumpMatrix[1].ToFloatPtr());
	glUniform4fv(diffuseMatrixS, 1, din->diffuseMatrix[0].ToFloatPtr());
	glUniform4fv(diffuseMatrixT, 1, din->diffuseMatrix[1].ToFloatPtr());
	glUniform4fv(specularMatrixS, 1, din->specularMatrix[0].ToFloatPtr());
	glUniform4fv(specularMatrixT, 1, din->specularMatrix[1].ToFloatPtr());

	static const float zero[4] = { 0, 0, 0, 0 };
	static const float one[4] = { 1, 1, 1, 1 };
	static const float negOne[4] = { -1, -1, -1, -1 };

	switch ( din->vertexColor ) {
	case SVC_IGNORE:
		glUniform4fv(colorModulate, 1, zero);
		glUniform4fv(colorAdd, 1, one);
		break;
	case SVC_MODULATE:
		glUniform4fv(colorModulate, 1, one);
		glUniform4fv(colorAdd, 1, zero);
		break;
	case SVC_INVERSE_MODULATE:
		glUniform4fv(colorModulate, 1, negOne);
		glUniform4fv(colorAdd, 1, one);
		break;
	}

	glUniform4fv(diffuseColor, 1, din->diffuseColor.ToFloatPtr());
	glUniform4fv(specularColor, 1, din->specularColor.ToFloatPtr());

	// texture 1 will be the per-surface bump map
	glActiveTexture(GL_TEXTURE1);
	backEnd.glState.currenttmu = 1;
	din->bumpImage->Bind();

	// texture 2 will be the light falloff texture
	glActiveTexture(GL_TEXTURE2);
	backEnd.glState.currenttmu = 2;
	din->lightFalloffImage->Bind();

	// texture 3 will be the light projection texture
	glActiveTexture(GL_TEXTURE3);
	backEnd.glState.currenttmu = 3;
	din->lightImage->Bind();

	// texture 4 is the per-surface diffuse map
	glActiveTexture(GL_TEXTURE4);
	backEnd.glState.currenttmu = 4;
	din->diffuseImage->Bind();

	// texture 5 is the per-surface specular map
	glActiveTexture(GL_TEXTURE5);
	backEnd.glState.currenttmu = 5;
	din->specularImage->Bind();

	RB_DrawElementsWithCounters( din->surf->geo );
}
Exemplo n.º 24
0
void display(void)
{
	printError("pre display");
	static float time = 0;

	// clear the screen
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	// Upload camera matrix
	mat4 cameraMatrix = lookAt(cameraPos.x, cameraPos.y, cameraPos.z, targetPos.x, targetPos.y, targetPos.z, upVector.x, upVector.y, upVector.z);
	
	// Skybox
	glDisable(GL_DEPTH_TEST);
	mat4 scale = S(100, 100, 100);
	mat4 skybox_matrix = cameraMatrix;
	skybox_matrix.m[3] = 0;
	skybox_matrix.m[7] = 0;
	skybox_matrix.m[11] = 0;
	skybox_matrix.m[15] = 1;

	// Upload time
	glUniform1f(glGetUniformLocation(program, "gTime"), time);

	// Default camera for all other renders except the skybox
	glUniformMatrix4fv(glGetUniformLocation(program, "gView"), 1, GL_TRUE, skybox_matrix.m);

	glUniformMatrix4fv(glGetUniformLocation(program, "gWorld"), 1, GL_TRUE, scale.m);
	glUniform1i(glGetUniformLocation(program, "skybox_active"), 1);
	
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, skybox_texture);
	DrawModel(skybox, program, "InPosL", "InNormalL", "InTex");
	glUniform1i(glGetUniformLocation(program, "skybox_active"), 0);
	glEnable(GL_DEPTH_TEST);

	// Default camera for all other renders except the skybox
	glUniformMatrix4fv(glGetUniformLocation(program, "gView"), 1, GL_TRUE, cameraMatrix.m);

	// Eye position
	GLfloat eyePos[] = { cameraPos.x, cameraPos.y, cameraPos.z };
	glUniform3fv(glGetUniformLocation(program, "gEyePosW"), 1, eyePos);

	// Ground (multitextured)
	scale = S(10, 10, 10);
	glUniformMatrix4fv(glGetUniformLocation(program, "gWorld"), 1, GL_TRUE, scale.m);
	
	glUniform1i(glGetUniformLocation(program, "multitexturing"), 1);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, grass_texture);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, conc_texture);
	DrawModel(ground, program, "InPosL", "InNormalL", "InTex");
	glUniform1i(glGetUniformLocation(program, "multitexturing"), 0);

	// Castle
	scale = S(1, 1, 1);
	mat4 trans = T(0, 0, 50);
	
	glBindTexture(GL_TEXTURE_2D, conc_texture);
	glUniformMatrix4fv(glGetUniformLocation(program, "gWorld"), 1, GL_TRUE, Mult(trans, scale).m);
	DrawModel(castle, program, "InPosL", "InNormalL", "InTex");

	// Windmill
	render_windmill(&windmill, program, time);
	
	glutSwapBuffers();

	time += 0.01;
}
Exemplo n.º 25
0
void ParticleRenderer2D::drawParticles(
        uint32_t count,
        const glm::vec2* positionArray,
        const float* massArray,
        const glm::vec3* colorArray,
        unsigned int * instrumentArray,
        int * lifetimeArray,
        float size,
        float volume) {
    // Active la gestion de la transparence
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

    glDisable(GL_DEPTH_TEST);

    glUseProgram(m_ProgramID);

    glBindVertexArray(m_VAOID);
    
    glm::vec3 bassColor = m_band[Instrument::bass].giveColor(soundManager->scaleVolume(volume));
    glm::vec3 drumsColor = m_band[Instrument::drums].giveColor(soundManager->scaleVolume(volume));
    glm::vec3 guitarAColor = m_band[Instrument::guitarA].giveColor(soundManager->scaleVolume(volume));
    glm::vec3 guitarBColor = m_band[Instrument::guitarB].giveColor(soundManager->scaleVolume(volume));

    // Dessine chacune des particules
    for(uint32_t i = 0; i < count; ++i) {
		if(soundManager->getMaxVolume() > 0) {
			// renvoyer la bonne couleur en fonction de l'instrument de la particule
			//~ glm::vec3 color = Instrument::giveColor(glm::vec3(0.), soundManager->scaleVolume(volume));
			glm::vec3 color;
			
			float coef = float(lifetimeArray[i]) / 100.f;
			//~ std::cout << "coef : " << coef << std::endl;
			
			switch(instrumentArray[i]){
				case Instrument::bass:
					color = bassColor*coef;
					break;
					
				case Instrument::drums:
					color = drumsColor*coef;
					break;
					
				case Instrument::guitarA:
					color = guitarAColor*coef;
					break;
					
				case Instrument::guitarB:
					color = guitarBColor*coef;
					break;
					
				default:
					color = glm::vec3(1.)*coef;
					break;
			};
			
			glUniform3fv(m_uParticleColor, 1, glm::value_ptr(color));
		} else {
			glUniform3fv(m_uParticleColor, 1, glm::value_ptr(colorArray[i]));
		}
        glUniform2fv(m_uParticlePosition, 1, glm::value_ptr(positionArray[i]));
        glUniform1f(m_uParticleScale, m_fMassScale * size);
        glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
    }

    glBindVertexArray(0);

    glDisable(GL_BLEND);
}
Exemplo n.º 26
0
JNIEXPORT void JNICALL Java_org_lwjgl_opengl_GL20_nglUniform3fv(JNIEnv *env, jclass clazz, jint location, jint count, jlong values, jlong function_pointer) {
	const GLfloat *values_address = (const GLfloat *)(intptr_t)values;
	glUniform3fvPROC glUniform3fv = (glUniform3fvPROC)((intptr_t)function_pointer);
	glUniform3fv(location, count, values_address);
}
Exemplo n.º 27
0
//OpenGL Initialization Logic
void glInit() {
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_LINE_SMOOTH);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable(GL_POLYGON_OFFSET_LINE);
    glPolygonOffset(-1.f, -1.0f);
    glEnable(GL_MULTISAMPLE);
    
    glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
    
    int samples, bufs;
    glGetIntegerv(GL_SAMPLE_BUFFERS, &bufs);
    glGetIntegerv(GL_SAMPLES, &samples);
    printf("Multisampling buffers: %d, samples: %d\n", bufs, samples);
    
    glm::vec3 coneDir = {0.f, -1.f, -1.f};
    glm::vec4 lightPos = {0.f, 30.f, 30.f, 1.f};
    
    glm::mat4 scaleBiasMat = {{0.5f, 0.f, 0.f, 0.f}, {0.f, 0.5f, 0.f, 0.f}, {0.f, 0.f, 0.5f, 0.f}, {0.5f, 0.5f, 0.5f, 1.f}};
    
    
    GLuint TeapotTexture;
    
    int imgWidth, imgHeight, imgComp;
    gTexImage = stbi_load("texture.jpg", &imgWidth, &imgHeight, &imgComp, STBI_rgb);
    glGenTextures(1, &TeapotTexture);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, TeapotTexture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imgWidth, imgHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, gTexImage);
    glActiveTexture(GL_TEXTURE0);
    
    
    //Screen Shader
    
    gScreenProg = glCreateProgram();
    gScreenVS = CreateShader(GL_VERTEX_SHADER, "screen.vert");
    gScreenFS = CreateShader(GL_FRAGMENT_SHADER, "screen.frag");
    glAttachShader(gScreenProg, gScreenVS);
    glAttachShader(gScreenProg, gScreenFS);
    glLinkProgram(gScreenProg);
    glUseProgram(gScreenProg);
    
    glm::vec3 screenColor = {1.f, 1.f, 1.f};
    GLint screenColorLoc = glGetUniformLocation(gScreenProg, "uColor");
    glUniform3fv(screenColorLoc, 1, glm::value_ptr(screenColor));
    
    glGenVertexArrays(1, &gScreenVao);
    glBindVertexArray(gScreenVao);
    
    glGenBuffers(1, &gScreenPosBuf);
    glBindBuffer(GL_ARRAY_BUFFER, gScreenPosBuf);
    glm::vec4 screenPosData[] = {{-0.8f, -0.5f, 0.f, 1.f}, {-0.8f, -0.8f, 0.f, 1.f}, {-0.5f, -0.5f, 0.f, 1.f}, {-0.5f, -0.5f, 0.f, 1.f}, {-0.8f, -0.8f, 0.f, 1.f}, {-0.5f, -0.8f, 0.f, 1.f}};
    glBufferData(GL_ARRAY_BUFFER, 6 * 4 * sizeof(float), &screenPosData[0], GL_STATIC_DRAW);
    
    GLint screenPosLoc = glGetAttribLocation(gScreenProg, "vPosition");
    glEnableVertexAttribArray(screenPosLoc);
    glBindBuffer(GL_ARRAY_BUFFER, gScreenPosBuf);
    glVertexAttribPointer(screenPosLoc, 4, GL_FLOAT, GL_FALSE, 0, NULL);
    
    
    //Shadow Map Creation
    
    gShadowProg = glCreateProgram();
    gShadowVS = CreateShader(GL_VERTEX_SHADER, "shadow.vert");
    gShadowFS = CreateShader(GL_FRAGMENT_SHADER, "shadow.frag");
    glAttachShader(gShadowProg, gShadowVS);
    glAttachShader(gShadowProg, gShadowFS);
    glLinkProgram(gShadowProg);
    glUseProgram(gShadowProg);
    
    glGenFramebuffers(1, &gShadowFbo);
    glBindFramebuffer(GL_FRAMEBUFFER, gShadowFbo);
    
    glDrawBuffer(GL_NONE);
    
    glGenTextures(1, &gShadowTexture);
    glActiveTexture(GL_TEXTURE2);
    glBindTexture(GL_TEXTURE_2D, gShadowTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, SHADOW_DEPTH_TEXTURE_SIZE, SHADOW_DEPTH_TEXTURE_SIZE, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glActiveTexture(GL_TEXTURE0);
    
    glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, gShadowTexture, 0);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    gShadowViewMat = glm::lookAt(glm::vec3(lightPos), glm::vec3(lightPos) + coneDir, glm::vec3(0, 1, 0));
    gShadowProjMat = glm::frustum(-1.f, 1.f, -1.f, 1.f, 1.f, SHADOW_FRUSTRUM_DEPTH);
    
    gShadowModelMatLoc = glGetUniformLocation(gShadowProg, "uModelMat");
    
    gShadowViewMatLoc = glGetUniformLocation(gShadowProg, "uViewMat");
    glUniformMatrix4fv(gShadowViewMatLoc, 1, GL_FALSE, glm::value_ptr(gShadowViewMat));
    
    gShadowProjMatLoc = glGetUniformLocation(gShadowProg, "uProjMat");
    glUniformMatrix4fv(gShadowProjMatLoc, 1, GL_FALSE, glm::value_ptr(gShadowProjMat));
    
    glGenVertexArrays(1, &gShadowVao);
    glBindVertexArray(gShadowVao);
    
    gShadowPosLoc = glGetAttribLocation(gShadowProg, "vPosition");
    glEnableVertexAttribArray(gShadowPosLoc);
    
    //Program Creation
    
    gRenderProg = glCreateProgram();
    gRenderVS = CreateShader(GL_VERTEX_SHADER, "shader.vert");
    gRenderFS = CreateShader(GL_FRAGMENT_SHADER, "shader.frag");
    glAttachShader(gRenderProg, gRenderVS);
    glAttachShader(gRenderProg, gRenderFS);
    glLinkProgram(gRenderProg);
    glUseProgram(gRenderProg);
    
    
    //Texture
  
    //Uniforms
    
    gModelMatLoc = glGetUniformLocation(gRenderProg, "uModelMat");
    gModelMat = glm::rotate(glm::mat4(1.f), -1.57f, glm::vec3(1.f, 0.f, 0.f));
    glUniformMatrix4fv(gModelMatLoc, 1, GL_FALSE, glm::value_ptr(gModelMat));
    
    gViewMatLoc = glGetUniformLocation(gRenderProg, "uViewMat");
    glm::mat4 viewMat = glm::mat4(1.f);
    viewMat = glm::rotate(glm::mat4(1.f), 0.7f, glm::vec3(1.f, 0.f, 0.f)) * viewMat;
    viewMat = glm::translate(glm::mat4(1.f), glm::vec3(0.f, 0.f, -50.f)) * viewMat;
    glUniformMatrix4fv(gViewMatLoc, 1, GL_FALSE, glm::value_ptr(viewMat));
    
    gProjMatLoc = glGetUniformLocation(gRenderProg, "uProjMat");
    glm::mat4 projMat = glm::perspective(45.f, (float)gScreenWidth / (float)gScreenHeight, PROJ_NEAR, PROJ_FAR);
    glUniformMatrix4fv(gProjMatLoc, 1, GL_FALSE, glm::value_ptr(projMat));
    
    gScaleBiasMatLoc = glGetUniformLocation(gRenderProg, "uScaleBiasMat");
    glUniformMatrix4fv(gScaleBiasMatLoc, 1, GL_FALSE, glm::value_ptr(scaleBiasMat));
    
    gShadowMatLoc = glGetUniformLocation(gRenderProg, "uShadowMat");
    glm::mat4 shadowMat = gShadowProjMat * gShadowViewMat;
    glUniformMatrix4fv(gShadowMatLoc, 1, GL_FALSE, glm::value_ptr(shadowMat));
    
    gTexUnitLoc = glGetUniformLocation(gRenderProg, "uTexUnit");
    glUniform1i(gTexUnitLoc, 0);
    
    gIsTexturedLoc = glGetUniformLocation(gRenderProg, "uIsTextured");
    glUniform1i(gIsTexturedLoc, 1);
    
    gColorLoc = glGetUniformLocation(gRenderProg, "uColor");
    glm::vec4 color = {1.0f, 0.f, 0.f, 1.f};
    glUniform4fv(gColorLoc, 1, glm::value_ptr(color));
    
    gAmbientLoc = glGetUniformLocation(gRenderProg, "uAmbient");
    glm::vec3 ambVec = {0.3f, 0.3f, 0.3f};
    glUniform3fv(gAmbientLoc, 1, glm::value_ptr(ambVec));
    
    gDiffuseLoc = glGetUniformLocation(gRenderProg, "uDiffuse");
    glm::vec3 difVec = {0.5f, 0.f, 0.f};
    glUniform3fv(gDiffuseLoc, 1, glm::value_ptr(difVec));
    
    gSpecularLoc = glGetUniformLocation(gRenderProg, "uSpecular");
    glm::vec3 specVec = {1.f, 1.f, 1.f};
    glUniform3fv(gSpecularLoc, 1, glm::value_ptr(specVec));
    
    gShininessLoc = glGetUniformLocation(gRenderProg, "uShininess");
    float shininess = 20.0f;
    glUniform1f(gShininessLoc, shininess);
    
    gLightPosLoc = glGetUniformLocation(gRenderProg, "uLightPos");
    glUniform4fv(gLightPosLoc, 1, glm::value_ptr(lightPos));
    
    gSpecStrengthLoc = glGetUniformLocation(gRenderProg, "uSpecStrength");
    float strength = 3.f;
    glUniform1f(gSpecStrengthLoc, strength);
    
    
    gConstAttenuationLoc = glGetUniformLocation(gRenderProg, "uConstAttenuation");
    float constAttenuation = 1.f;
    glUniform1f(gConstAttenuationLoc, constAttenuation);
    
    gLinearAttenuationLoc = glGetUniformLocation(gRenderProg, "uLinearAttenuation");
    float linearAttenuation = 0.05f;
    glUniform1f(gLinearAttenuationLoc, linearAttenuation);
    
    gConeDirectionLoc = glGetUniformLocation(gRenderProg, "uConeDirection");
    glUniform3fv(gConeDirectionLoc, 1, glm::value_ptr(coneDir));
    
    gConeAngleLoc = glGetUniformLocation(gRenderProg, "uConeAngle");
    float coneAngle = 0.8f;
    glUniform1f(gConeAngleLoc, coneAngle);
    
    gSpotExponentLoc = glGetUniformLocation(gRenderProg, "uSpotExponent");
    float spotExponent = 1.0f;
    glUniform1f(gSpotExponentLoc, spotExponent);
    
    gAmbientSkyLoc = glGetUniformLocation(gRenderProg, "uAmbientSky");
    glm::vec3 ambientSky = {0.3f, 0.3f, 0.3f};
    glUniform3fv(gAmbientSkyLoc, 1, glm::value_ptr(ambientSky));
    
    gAmbientGroundLoc = glGetUniformLocation(gRenderProg, "uAmbientGround");
    glm::vec3 ambientGround = {0.1f, 0.1f, 0.1f};
    glUniform3fv(gAmbientGroundLoc, 1, glm::value_ptr(ambientGround));
    
    gDepthTextureLoc = glGetUniformLocation(gRenderProg, "uDepthTexture");
    glUniform1i(gDepthTextureLoc, 2);
    
    gProjectorDepthTextureLoc = glGetUniformLocation(gRenderProg, "uProjectorDepthTexture");
    glUniform1i(gProjectorDepthTextureLoc, 3);

    //Vertex Array Object
    
    glGenVertexArrays(1, &gVao);
    glBindVertexArray(gVao);
    

    //Vertex Attribute Specification
    
    gPosLoc = glGetAttribLocation(gRenderProg, "vPosition");
    glEnableVertexAttribArray(gPosLoc);
    
    gTexCoordLoc = glGetAttribLocation(gRenderProg, "vTexCoord");
    glEnableVertexAttribArray(gTexCoordLoc);

    gNormLoc = glGetAttribLocation(gRenderProg, "vNormal");
    glEnableVertexAttribArray(gNormLoc);
    
    //Floor
    
    gFloorObj.modelMat = glm::mat4(1.f);
    
    GLuint floorBuffers[3];
    glGenBuffers(3, &floorBuffers[0]);
    
    glBindBuffer(GL_ARRAY_BUFFER, floorBuffers[0]);
    glm::vec4 floorPosData[] = {{-60.f, 0.f, -30.f, 1.f}, {-60, 0.f, 30.f, 1.f}, {60.f, 0.f, -30.f, 1.f}, {60.f, 0.f, -30.f, 1.f}, {-60.f, 0.f, 30.f, 1.f}, {60.f, 0.f, 30.f, 1.f}};
    glBufferData(GL_ARRAY_BUFFER, 4 * 6 * sizeof(float), &floorPosData[0], GL_STATIC_DRAW);
    
    glBindBuffer(GL_ARRAY_BUFFER, floorBuffers[1]);
    glm::vec2 floorTexCoordData[] = {{0.f, 0.f}, {0.f, 1.f}, {1.f, 0.f}, {1.f, 0.f}, {0.f, 1.f}, {1.f, 1.f}};
    glBufferData(GL_ARRAY_BUFFER, 2 * 6 * sizeof(float), &floorTexCoordData[0], GL_STATIC_DRAW);
    
    glBindBuffer(GL_ARRAY_BUFFER, floorBuffers[2]);
    glm::vec3 floorNormData[] = {{0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}, {0.f, 1.f, 0.f}};
    glBufferData(GL_ARRAY_BUFFER, 3 * 6 * sizeof(float), &floorNormData[0], GL_STATIC_DRAW);
    
    gFloorObj.glPosBuf = floorBuffers[0];
    gFloorObj.glTexCoordBuf = floorBuffers[1];
    gFloorObj.glNormBuf = floorBuffers[2];
    
    gFloorObj.vertCount = 6;
    
    gFloorObj.ambientColor = {0.3f, 0.3f, 0.3f};
    //gFloorObj.ambientColor = {1.f, 1.f, 1.f};
    gFloorObj.diffuseColor = {0.5f, 0.5f, 0.5f};
    gFloorObj.specularColor = {1.f, 1.f, 1.f};

    gFloorObj.shininess = 20.f;
    gFloorObj.specStrength = 0.f;
    
    gFloorObj.isTextured = false;
    gFloorObj.objColor = {0.5f, 0.5f, 0.5f, 1.f};
    //gFloorObj.objColor = {1.f, 1.f, 1.f, 1.f};
    
    gFloorObj.glTexture = TeapotTexture;
    gFloorObj.glTexUnit = 0;
    
    //Teapot
    
    gTeapotObj.modelMat = glm::rotate(glm::mat4(1.f), -1.57f, glm::vec3(1.f, 0.f, 0.f));
    
    GLuint TeapotBuffers[3];
    glGenBuffers(3, &TeapotBuffers[0]);
    
    glBindBuffer(GL_ARRAY_BUFFER, TeapotBuffers[0]);
    glBufferData(GL_ARRAY_BUFFER, teapot.m_Info.vertCount * 4 * sizeof(float), &teapot.m_Info.v[0], GL_STREAM_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, TeapotBuffers[1]);
    glBufferData(GL_ARRAY_BUFFER, teapot.m_Info.vertCount * 2 * sizeof(float), &teapot.m_Info.vt[0], GL_STREAM_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, TeapotBuffers[2]);
    glBufferData(GL_ARRAY_BUFFER, teapot.m_Info.vertCount * 3 * sizeof(float), &teapot.m_Info.vn[0], GL_STREAM_DRAW);
    
    gTeapotObj.glPosBuf = TeapotBuffers[0];
    gTeapotObj.glTexCoordBuf = TeapotBuffers[1];
    gTeapotObj.glNormBuf = TeapotBuffers[2];
    
    gTeapotObj.vertCount = teapot.m_Info.vertCount;
    
    gTeapotObj.ambientColor = {0.3f, 0.3f, 0.3f};
    gTeapotObj.diffuseColor = {0.8f, 0.f, 0.f};
    gTeapotObj.specularColor = {1.f, 1.f, 1.f};
    
    gTeapotObj.shininess = 20.f;
    gTeapotObj.specStrength = 5.f;
    
    gTeapotObj.isTextured = true;
    gTeapotObj.objColor = {0.1f, 0.f, 0.f, 1.f};
    
    gTeapotObj.glTexture = TeapotTexture;
    gTeapotObj.glTexUnit = 1;
    
    gSceneGraph.push_back(gTeapotObj);
    gSceneGraph.push_back(gFloorObj);
    
    gFilterProg = glCreateProgram();
    gFilterVS = CreateShader(GL_VERTEX_SHADER, "bloom.vert");
    gFilterFS = CreateShader(GL_FRAGMENT_SHADER, "bloom.frag");
    glAttachShader(gFilterProg, gFilterVS);
    glAttachShader(gFilterProg, gFilterFS);
    glLinkProgram(gFilterProg);
    glUseProgram(gFilterProg);
    
    gFilterRenderTextureLoc = glGetUniformLocation(gFilterProg, "uRenderTexture");
    glUniform1i(gFilterRenderTextureLoc, 3);
    
    // Swap textures in shader by binding texture to tex unit 4 (GL_TEXTURE3)
    
    gFilterTextureLoc = glGetUniformLocation(gFilterProg, "uTexture");
    glUniform1i(gFilterTextureLoc, 4);
    
    
    gFilterWidthLoc = glGetUniformLocation(gFilterProg, "uScreenWidth");
    glUniform1i(gFilterWidthLoc, gScreenWidth * 2);
    
    gFilterHeightLoc = glGetUniformLocation(gFilterProg, "uScreenHeight");
    glUniform1i(gFilterHeightLoc, gScreenHeight * 2);
    
    gFilterPassLoc = glGetUniformLocation(gFilterProg, "uPass");
    glUniform1i(gFilterPassLoc, 1);
    
    gFilterLumThresholdLoc = glGetUniformLocation(gFilterProg, "uLumThreshold");
    glUniform1f(gFilterLumThresholdLoc, 0.75f);
    
    // Calculates the weights of the gaussian function
    
    float weights[20];
    float sum = 0.f;
    float sigma = 5.f;
    
    weights[0] = gauss(0, sigma);
    sum += weights[0];
    
    for (int i = 1; i < 20; i++) {
        weights[i] = gauss(i, sigma);
        sum += 2 * weights[i];
    }
    
    for (int i = 0; i < 20; i++) {
        weights[i] = weights[i] / sum; // Normalizes weights: sum of weights equals 1
    }
    
    gFilterWeightsLoc = glGetUniformLocation(gFilterProg, "uWeights");
    glUniform1fv(gFilterWeightsLoc, 20, weights);
    
    glGenBuffers(1, &gFilterPosBuf);
    glBindBuffer(GL_ARRAY_BUFFER, gFilterPosBuf);
    glm::vec4 filterPosData[] = {{-1.f, 1.f, 0.f, 1.f}, {-1.f, -1.f, 0.f, 1.f}, {1.f, 1.f, 0.f, 1.f}, {1.f, 1.f, 0.f, 1.f}, {-1.f, -1.f, 0.f, 1.f}, {1.f, -1.f, 0.f, 1.f}};
    glBufferData(GL_ARRAY_BUFFER, 6 * 4 * sizeof(float), &filterPosData[0], GL_STATIC_DRAW);
    
    glGenBuffers(1, &gFilterTexCoordBuf);
    glBindBuffer(GL_ARRAY_BUFFER, gFilterTexCoordBuf);
    glm::vec2 filterTexCoordData[] = {{0.f, 1.f}, {0.f, 0.f}, {1.f, 1.f}, {1.f, 1.f}, {0.f, 0.f}, {1.f, 0.f}};
    glBufferData(GL_ARRAY_BUFFER, 6 * 2 * sizeof(float), &filterTexCoordData[0], GL_STATIC_DRAW);
    
    glGenVertexArrays(1, &gFilterVao);
    glBindVertexArray(gFilterVao);
    
    gFilterPosBufLoc = glGetAttribLocation(gFilterProg, "vPosition");
    glEnableVertexAttribArray(gFilterPosBufLoc);
    glBindBuffer(GL_ARRAY_BUFFER, gFilterPosBuf);
    glVertexAttribPointer(gFilterPosBufLoc, 4, GL_FLOAT, GL_FALSE, 0, NULL);
    
    gFilterTexCoordBufLoc = glGetAttribLocation(gFilterProg, "vTexCoord");
    glEnableVertexAttribArray(gFilterTexCoordBufLoc);
    glBindBuffer(GL_ARRAY_BUFFER, gFilterTexCoordBuf);
    glVertexAttribPointer(gFilterTexCoordBufLoc, 2, GL_FLOAT, GL_FALSE, 0, NULL);
    
    
    // Scene rendered to gFilterSceneTexture
    
    glGenFramebuffers(1, &gFilterSceneFbo);
    glBindFramebuffer(GL_FRAMEBUFFER, gFilterSceneFbo);
    
    glGenTextures(1, &gFilterSceneTexture);
    glActiveTexture(GL_TEXTURE3);
    glBindTexture(GL_TEXTURE_2D, gFilterSceneTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gScreenWidth * 2, gScreenHeight * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glActiveTexture(GL_TEXTURE0);
    
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gFilterSceneTexture, 0);
    
    glGenRenderbuffers(1, &gFilterSceneRbo);
    glBindRenderbuffer(GL_RENDERBUFFER, gFilterSceneRbo);
    
    glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, gScreenWidth * 2, gScreenHeight * 2);
    
    glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, gFilterSceneRbo);
    
    GLenum drawModes[1] = {GL_COLOR_ATTACHMENT0};
    
    glDrawBuffers(1, drawModes);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    
    // Results of low luminance discard stored in gFilterLumTexture
    
    glGenFramebuffers(1, &gFilterLumFbo);
    glBindFramebuffer(GL_FRAMEBUFFER, gFilterLumFbo);
    
    glGenTextures(1, &gFilterLumTexture);
    glBindTexture(GL_TEXTURE_2D, gFilterLumTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gScreenWidth * 2, gScreenHeight * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gFilterLumTexture, 0);
    
    GLenum lumDrawModes[1] = {GL_COLOR_ATTACHMENT0};
    
    glDrawBuffers(1, lumDrawModes);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
    
    
    // Results of gaussian filter pass1 stored in gFilterPassTexture (depth buffer not needed)
    
    glGenFramebuffers(1, &gFilterBlurFbo);
    glBindFramebuffer(GL_FRAMEBUFFER, gFilterBlurFbo);
    
    glGenTextures(1, &gFilterBlurTexture);
    glBindTexture(GL_TEXTURE_2D, gFilterBlurTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gScreenWidth * 2, gScreenHeight * 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    
    glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, gFilterBlurTexture, 0);
    
    GLenum passDrawModes[1] = {GL_COLOR_ATTACHMENT0};
    
    glDrawBuffers(1, passDrawModes);
    
    glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
Exemplo n.º 28
0
int main()
{
    int width = 640;
    int height = 480;
    
    if(glfwInit() == GL_FALSE)
    {
        std::cerr << "failed to init GLFW" << std::endl;
        return 1;
    } 
    glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
    glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 3);
 
    // create a window
    if(glfwOpenWindow(width, height, 0, 0, 0, 8, 24, 8, GLFW_WINDOW) == GL_FALSE)
    {
        std::cerr << "failed to open window" << std::endl;
        glfwTerminate();
        return 1;
    }
    
    // setup windows close callback
    glfwSetWindowCloseCallback(closedWindow);
    
    if (gl3wInit())
    {
        std::cerr << "failed to init GL3W" << std::endl;
        glfwCloseWindow();
        glfwTerminate();
        return 1;
    }

    // shader source code
    
    // the vertex shader simply passes through data
    std::string vertex_source =
        "#version 430\n"
        "layout(location = 0) in vec4 vposition;\n"
        "void main() {\n"
        "   gl_Position = vposition;\n"
        "}\n";
    
    // the geometry shader creates the billboard quads
    std::string geometry_source =
        "#version 430\n"
        "uniform mat4 View;\n"
        "uniform mat4 Projection;\n"
        "layout (points) in;\n"
        "layout (triangle_strip, max_vertices = 4) out;\n"
        "out vec2 txcoord;\n"
        "void main() {\n"
        "   vec4 pos = View*gl_in[0].gl_Position;\n"
        "   txcoord = vec2(-1,-1);\n"
        "   gl_Position = Projection*(pos+0.2*vec4(txcoord,0,0));\n"
        "   EmitVertex();\n"
        "   txcoord = vec2( 1,-1);\n"
        "   gl_Position = Projection*(pos+0.2*vec4(txcoord,0,0));\n"
        "   EmitVertex();\n"
        "   txcoord = vec2(-1, 1);\n"
        "   gl_Position = Projection*(pos+0.2*vec4(txcoord,0,0));\n"
        "   EmitVertex();\n"
        "   txcoord = vec2( 1, 1);\n"
        "   gl_Position = Projection*(pos+0.2*vec4(txcoord,0,0));\n"
        "   EmitVertex();\n"
        "}\n";    
    
    // the fragment shader creates a bell like radial color distribution    
    std::string fragment_source =
        "#version 330\n"
        "in vec2 txcoord;\n"
        "layout(location = 0) out vec4 FragColor;\n"
        "void main() {\n"
        "   float s = 0.2*(1/(1+15.*dot(txcoord, txcoord))-1/16.);\n"
        "   FragColor = s*vec4(0.3,0.3,1.0,1);\n"
        "}\n";
   
    // program and shader handles
    GLuint shader_program, vertex_shader, geometry_shader, fragment_shader;
    
    // we need these to properly pass the strings
    const char *source;
    int length;

    // create and compiler vertex shader
    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    source = vertex_source.c_str();
    length = vertex_source.size();
    glShaderSource(vertex_shader, 1, &source, &length); 
    glCompileShader(vertex_shader);
    if(!check_shader_compile_status(vertex_shader))
    {
        return 1;
    }
    
    // create and compiler geometry shader
    geometry_shader = glCreateShader(GL_GEOMETRY_SHADER);
    source = geometry_source.c_str();
    length = geometry_source.size();
    glShaderSource(geometry_shader, 1, &source, &length); 
    glCompileShader(geometry_shader);
    if(!check_shader_compile_status(geometry_shader))
    {
        return 1;
    }
 
    // create and compiler fragment shader
    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    source = fragment_source.c_str();
    length = fragment_source.size();
    glShaderSource(fragment_shader, 1, &source, &length);   
    glCompileShader(fragment_shader);
    if(!check_shader_compile_status(fragment_shader))
    {
        return 1;
    }
    
    // create program
    shader_program = glCreateProgram();
    
    // attach shaders
    glAttachShader(shader_program, vertex_shader);
    glAttachShader(shader_program, geometry_shader);
    glAttachShader(shader_program, fragment_shader);
    
    // link the program and check for errors
    glLinkProgram(shader_program);
    check_program_link_status(shader_program);
    
    // obtain location of projection uniform
    GLint View_location = glGetUniformLocation(shader_program, "View");
    GLint Projection_location = glGetUniformLocation(shader_program, "Projection");

    std::string compute_source =
        "#version 430\n"
        "layout(local_size_x=256) in;\n"
        
        "uniform vec3 center[3];\n"
        "uniform float radius[3];\n"
        "uniform vec3 g;\n"
        "uniform float dt;\n"
        "uniform float bounce;\n"
        "uniform int seed;\n"
        "uniform layout(rgba32f) imageBuffer particles;\n"
                
        "float hash(int x) {\n"
        "   x = x*1235167 + int(gl_GlobalInvocationID)*948737 + seed*9284365;\n"
        "   x = (x >> 13) ^ x;\n"
        "   return ((x * (x * x * 60493 + 19990303) + 1376312589) & 0x7fffffff)/float(0x7fffffff-1);\n"
        "}\n"
        
        "void main() {\n"
        "   int index = int(gl_GlobalInvocationID);\n"
        "   vec3 inposition = imageLoad(particles, 2*index).xyz;\n"
        "   vec3 invelocity = imageLoad(particles, 2*index+1).xyz;\n"
        "   vec3 outvelocity = invelocity;\n"
        "   for(int j = 0;j<3;++j) {\n"
        "       vec3 diff = inposition-center[j];\n"
        "       float dist = length(diff);\n"
        "       float vdot = dot(diff, invelocity);\n"
        "       if(dist<radius[j] && vdot<0.0)\n"
        "           outvelocity -= bounce*diff*vdot/(dist*dist);\n"
        "   }\n"
        "   outvelocity += dt*g;\n"
        "   vec3 outposition = inposition + dt*outvelocity;\n"
        "   if(outposition.y < -30.0)\n"
        "   {\n"
        "       outvelocity = vec3(0,0,0);\n"
        "       outposition = 0.5-vec3(hash(3*index+0),hash(3*index+1),hash(3*index+2));\n"
        "       outposition = vec3(0,20,0) + 5.0*outposition;\n"
        "   }\n"
        "   imageStore(particles, 2*index, vec4(outposition,1));\n"
        "   imageStore(particles, 2*index+1, vec4(outvelocity,1));\n"
        "}\n";
   
    // program and shader handles
    GLuint compute_program, compute_shader;

    // create and compiler vertex shader
    compute_shader = glCreateShader(GL_COMPUTE_SHADER);
    source = compute_source.c_str();
    length = compute_source.size();
    glShaderSource(compute_shader, 1, &source, &length); 
    glCompileShader(compute_shader);
    if(!check_shader_compile_status(compute_shader))
    {
        return 1;
    }

    // create program
    compute_program = glCreateProgram();
    
    // attach shaders
    glAttachShader(compute_program, compute_shader);
   
    // link the program and check for errors
    glLinkProgram(compute_program);
    check_program_link_status(compute_program);

    GLint center_location = glGetUniformLocation(compute_program, "center");
    GLint radius_location = glGetUniformLocation(compute_program, "radius");
    GLint g_location = glGetUniformLocation(compute_program, "g");
    GLint dt_location = glGetUniformLocation(compute_program, "dt");
    GLint bounce_location = glGetUniformLocation(compute_program, "bounce");
    GLint seed_location = glGetUniformLocation(compute_program, "seed");
    GLint particles_location = glGetUniformLocation(compute_program, "particles");
   
    const int particles = 128*1024;

    // randomly place particles in a cube
    std::vector<glm::vec4> vertexData(2*particles);
    for(int i = 0;i<particles;++i)
    {
        // initial position
        vertexData[2*i+0] = glm::vec4(
                                0.5f-float(std::rand())/RAND_MAX,
                                0.5f-float(std::rand())/RAND_MAX,
                                0.5f-float(std::rand())/RAND_MAX,
                                0
                            );
        vertexData[2*i+0] = glm::vec4(0.0f,20.0f,0.0f,1) + 5.0f*vertexData[2*i+0];
        
        // initial velocity
        vertexData[2*i+1] = glm::vec4(0,0,0,0);
    }
    
    // generate vbos and vaos
    GLuint vao, vbo;
    glGenVertexArrays(1, &vao);
    glGenBuffers(1, &vbo);
    

    glBindVertexArray(vao);
        
    glBindBuffer(GL_ARRAY_BUFFER, vbo);

    // fill with initial data
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec4)*vertexData.size(), &vertexData[0], GL_STATIC_DRAW);
                        
    // set up generic attrib pointers
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (char*)0 + 0*sizeof(GLfloat));
    // set up generic attrib pointers
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 8*sizeof(GLfloat), (char*)0 + 4*sizeof(GLfloat));


    // "unbind" vao
    glBindVertexArray(0);
    
    // texture handle
    GLuint buffer_texture;
    
    // generate and bind the buffer texture
    glGenTextures(1, &buffer_texture);
    glBindTexture(GL_TEXTURE_BUFFER, buffer_texture);
    
    // tell the buffer texture to use 
    glTexBuffer(GL_TEXTURE_BUFFER, GL_RGBA32F, vbo);
    
    
    
    // we are blending so no depth testing
    glDisable(GL_DEPTH_TEST);
    
    // enable blending
    glEnable(GL_BLEND);
    //  and set the blend function to result = 1*source + 1*destination
    glBlendFunc(GL_ONE, GL_ONE);

    // define spheres for the particles to bounce off
    const int spheres = 3;
    glm::vec3 center[spheres];
    float radius[spheres];
    center[0] = glm::vec3(0,12,1);
    radius[0] = 3;
    center[1] = glm::vec3(-3,0,0);
    radius[1] = 7;
    center[2] = glm::vec3(5,-10,0);
    radius[2] = 12;

    // physical parameters
    float dt = 1.0f/60.0f;
    glm::vec3 g(0.0f, -9.81f, 0.0f);
    float bounce = 1.2f; // inelastic: 1.0f, elastic: 2.0f

    int current_buffer=0;
    running = true;
    while(running)
    {   
        // get the time in seconds
        float t = glfwGetTime();
        
        // terminate on excape 
        if(glfwGetKey(GLFW_KEY_ESC))
        {
            running = false;
        }



        // use the transform shader program
        glUseProgram(compute_program);

        // set the uniforms
        glUniform3fv(center_location, 3, reinterpret_cast<GLfloat*>(center)); 
        glUniform1fv(radius_location, 3, reinterpret_cast<GLfloat*>(radius));
        glUniform3fv(g_location, 1, glm::value_ptr(g));
        glUniform1f(dt_location, dt);
        glUniform1f(bounce_location, bounce);
        glUniform1i(seed_location, std::rand());
        
        glBindImageTexture(0, buffer_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_RGBA32F);
        glUniform1i(particles_location, 0);

        glDispatchCompute(particles/256, 1, 1);

        
        // clear first
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        
        // use the shader program
        glUseProgram(shader_program);
        
        // calculate ViewProjection matrix
        glm::mat4 Projection = glm::perspective(90.0f, 4.0f / 3.0f, 0.1f, 100.f);
        
        // translate the world/view position
        glm::mat4 View = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -30.0f));
        
        // make the camera rotate around the origin
        View = glm::rotate(View, 30.0f, glm::vec3(1.0f, 0.0f, 0.0f)); 
        View = glm::rotate(View, -22.5f*t, glm::vec3(0.0f, 1.0f, 0.0f)); 
        
        // set the uniform
        glUniformMatrix4fv(View_location, 1, GL_FALSE, glm::value_ptr(View)); 
        glUniformMatrix4fv(Projection_location, 1, GL_FALSE, glm::value_ptr(Projection)); 
        
        // bind the current vao
        glBindVertexArray(vao);

        // draw
        glDrawArrays(GL_POINTS, 0, particles);
       
        // check for errors
        GLenum error = glGetError();
        if(error != GL_NO_ERROR)
        {
            std::cerr << gluErrorString(error);
            running = false;       
        }
        
        // finally swap buffers
        glfwSwapBuffers();
    }
    
    // delete the created objects
        
    glDeleteVertexArrays(1, &vao);
    glDeleteBuffers(1, &vbo);
    glDeleteTextures(1, &buffer_texture);
    
    glDetachShader(shader_program, vertex_shader);	
    glDetachShader(shader_program, geometry_shader);	
    glDetachShader(shader_program, fragment_shader);
    glDeleteShader(vertex_shader);
    glDeleteShader(geometry_shader);
    glDeleteShader(fragment_shader);
    glDeleteProgram(shader_program);

    glDetachShader(compute_program, compute_shader);	
    glDeleteShader(compute_shader);
    glDeleteProgram(compute_program);
    
    glfwCloseWindow();
    glfwTerminate();
    return 0;
}
Exemplo n.º 29
0
void TheMaze2::render() {
    
    glEnableVertexAttribArray(position_attr_);
    glEnableVertexAttribArray(normal_attr_);
    glEnableVertexAttribArray(text_coord_attr_);
    glEnableVertexAttribArray(color_attr_);
    
    player_.updateModelMatrix();
    //player_.model_matrix_ = glm::inverse(player_.model_matrix_);
    glUniformMatrix4fv(view_uni_, 1, GL_FALSE, glm::value_ptr(player_.model_matrix_));
    
    int light_count = light_positions_ws_.size();
    glUniform1i(light_count_uni_, light_count);
    
    // convert each light position to camera space
    glm::vec3 light_pos_cs[16];
    for (int i = 0; i < light_count; i++) {
        glm::vec3 light_pos_ws = light_positions_ws_[i];
        light_pos_cs[i] = (glm::vec3)(player_.model_matrix_ * glm::vec4(light_pos_ws, 1.0));
    }
    glUniform3fv(light_pos_cs_uni_, light_count, glm::value_ptr(light_pos_cs[0]));
    
    for (Surface *sf : surfaces_) {
        sf->updateModelMatrix();
        glUniformMatrix4fv(vert_model_uni_, 1, GL_FALSE, glm::value_ptr(sf->model_matrix_));
        glUniformMatrix4fv(norm_model_uni_, 1, GL_FALSE, glm::value_ptr(sf->model_matrix_));
        glUniform1i(text_samp_uni_, 0);
        
        glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, wall_text_);
        
        glDrawElements(GL_TRIANGLES, sf->ebo_count_, GL_UNSIGNED_SHORT, sf->ebo_pos_);
    }
    
    for (Model *ws : walls_) {
        ws->updateModelMatrix();
        glUniformMatrix4fv(vert_model_uni_, 1, GL_FALSE, glm::value_ptr(ws->model_matrix_));
        glUniformMatrix4fv(norm_model_uni_, 1, GL_FALSE, glm::value_ptr(ws->model_matrix_));
        glUniform1i(text_samp_uni_, 0);
        
        glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, wall_text_);
        
        glDrawElements(GL_TRIANGLES, ws->ebo_count_, GL_UNSIGNED_SHORT, ws->ebo_pos_);
    }
    
    for (Projectile *proj : projectiles_) {
        proj->updateModelMatrix();
        glUniformMatrix4fv(vert_model_uni_, 1, GL_FALSE, glm::value_ptr(proj->model_matrix_));
        glUniformMatrix4fv(norm_model_uni_, 1, GL_FALSE, glm::value_ptr(proj->model_matrix_));
        glUniform1i(text_samp_uni_, 0);
        
        glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, wall_text_);
        
        glDrawElements(GL_TRIANGLES, proj->ebo_count_, GL_UNSIGNED_SHORT, proj->ebo_pos_);
    }
    
    glDisableVertexAttribArray(position_attr_);
    glDisableVertexAttribArray(normal_attr_);
    glDisableVertexAttribArray(text_coord_attr_);
    glDisableVertexAttribArray(color_attr_);
}
Exemplo n.º 30
0
void PVScene::draw(int x, int y, int w, int h) {
    static float time = 0.0f;
    time += 0.01f;
    viewMatrix = lookAtLH(float3(cosf(time) * 5, 2, sinf(time) * 5), float3(0, 0, 0), float3(0, 1, 0));
    float4x4 viewProjectionMatrix = projectionMatrix * viewMatrix;

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClearDepth(1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBindVertexArray(vertexArray);
    shader->bind();

    GLuint viewProjectionLocation = shader->getUniformLocation("viewProjection");
    glUniformMatrix4fv(viewProjectionLocation, 1, true, &viewProjectionMatrix.m[0][0]);

    GLuint lightColorLocation = shader->getUniformLocation("lightColor");
    GLuint lightPositionLocation = shader->getUniformLocation("lightPosition");
    GLuint numLightsLocation = shader->getUniformLocation("numLights");

    float lightPositions[3 * 4];
    float lightColors[3 * 4];

    int numLights = lights.size();

    if (numLights > 4)
        numLights = 4;

    for (int i = 0; i < numLights; i++) {
        PVLight *light = lights[i];

        float3 lightPosition = light->getPosition();
        float3 lightColor = light->getColor();

        lightPositions[i * 3 + 0] = lightPosition.x;
        lightPositions[i * 3 + 1] = lightPosition.y;
        lightPositions[i * 3 + 2] = lightPosition.z;

        lightColors[i * 3 + 0] = lightColor.x;
        lightColors[i * 3 + 1] = lightColor.y;
        lightColors[i * 3 + 2] = lightColor.z;
    }

    glUniform3fv(lightPositionLocation, numLights, lightPositions);
    glUniform3fv(lightColorLocation, numLights, lightColors);
    glUniform1i(numLightsLocation, numLights);

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    glCullFace(GL_BACK);
    glFrontFace(GL_CW);

    glViewport(x, y, w, h);

    for (PVMesh *mesh : meshes)
        mesh->draw(shader);

    glDisable(GL_CULL_FACE);
    glActiveTexture(GL_TEXTURE0);
    glDisable(GL_DEPTH_TEST);
    glUseProgram(0);
    glBindVertexArray(0);

    GLCHECK();
}