Exemplo n.º 1
0
void setLighting(GLuint program)
{
	//Initialize shader lighting parameters
	glm::vec4 light_position(0.0, 1.0, 0.0, 1.0);
	glm::vec4 light_ambient(0.2, 0.2, 0.2, 1.0);
	glm::vec4 light_diffuse(1.0, 1.0, 1.0, 1.0);
	glm::vec4 light_specular(1.0, 1.0, 1.0, 1.0);
	glm::vec4 material_ambient(1.0, 1.0, 1.0, 1.0);
	glm::vec4 material_diffuse(1.0, 1.0, 1.0, 1.0);
	glm::vec4 material_specular(1.0, 1.0, 1.0, 1.0);
	float  material_shininess = 50.0;
	glm::vec4 ambient_product = light_ambient * material_ambient;
	glm::vec4 diffuse_product = light_diffuse * material_diffuse;
	glm::vec4 specular_product = light_specular * material_specular;
	glUniform4fv(glGetUniformLocation(program, "AmbientProduct"), 1, (GLfloat*)&ambient_product);
	glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, (GLfloat*)&diffuse_product);
	glUniform4fv(glGetUniformLocation(program, "SpecularProduct"), 1, (GLfloat*)&specular_product);
	glUniform4fv(glGetUniformLocation(program, "LightPosition"), 1, (GLfloat*)&light_position);
	glUniform1f(glGetUniformLocation(program, "Shininess"), material_shininess);
	//End lighting
}
Exemplo n.º 2
0
void CChildView::InitGL()
{
	// Create a checkerboard pattern
	for (int i = 0; i < 64; i++) {
		for (int j = 0; j < 64; j++) {
			GLubyte c;
			c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255;
			image[i][j][0]  = c;
			image[i][j][1]  = c;
			image[i][j][2]  = c;
		}
	}
	for (int i = 0; i < 64; i++) {
		for (int j = 0; j < 64; j++) {
			GLubyte c;
			c = (((i & 0x16) == 0) | ((j & 0x16) == 0)) * 255;
			image2[i][j][0] = c;
			image2[i][j][1] = 0;
			image2[i][j][2] = c;
		}
	}

	m_program = LoadShaders( "ShaderWnd/vertex.glsl", "ShaderWnd/fragment.glsl" );


	//GLuint vertexbuffer;
	glUseProgram(m_program);

	glGenTextures(2, textures);

	glBindTexture(GL_TEXTURE_2D, textures[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	glBindTexture(GL_TEXTURE_2D, textures[1]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	glActiveTexture( GL_TEXTURE0 );
	glBindTexture( GL_TEXTURE_2D, textures[0]);
	glActiveTexture( GL_TEXTURE1 );
	glBindTexture( GL_TEXTURE_2D, textures[1]);
	glActiveTexture( GL_TEXTURE2 );
	glEnable(GL_TEXTURE_2D);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	glBindTexture(GL_TEXTURE_2D, m_brick.TexName());


	m_cube->InitGL(m_program);
	m_wall->InitGL(m_program);

	point4 light_position (-5.f, 5.f, -5.f, 0.f);
	color4 light_ambient (0.2f, 0.2f, 0.2f, 1.f);
	color4 light_diffuse (1.f, 1.f, 1.f, 1.f);
	color4 light_specular (1.f, 1.f, 1.f, 1.f);

	color4 material_ambient(.3f, .3f, .3f, 1.f);
	color4 material_diffuse (0.3f, .3f, 0.3f, 1.f);
	color4 material_specular (1.f, 1.f, 1.f, 1.f);
	float material_shininess = 100.0f;

	color4 ambient_product = light_ambient*material_ambient;
	color4 diffuse_product = light_diffuse*material_diffuse;
	color4 specular_product = light_specular*material_specular;

	glUniform4fv(glGetUniformLocation(m_program, "AmbientProduct"), 1, value_ptr(ambient_product));
	glUniform4fv(glGetUniformLocation(m_program, "DiffuseProduct"), 1, value_ptr(diffuse_product));
	glUniform4fv(glGetUniformLocation(m_program, "SpecularProduct"), 1, value_ptr(specular_product));
	glUniform4fv(glGetUniformLocation(m_program, "LightPosition"), 1, value_ptr(light_position));
	glUniform1f(glGetUniformLocation(m_program, "Shininess"), material_shininess);

	//Set the value of the fragment shader texture sampler variable
	//	("texture") to the the appropriate texture unit. In this case,
	//	zero, for GL_TEXTURE0 which was previously set by calling
	//	glActiveTexture().
	glUniform1i( glGetUniformLocation(m_program, "diffuse_mat"), 0);


	m_nPVM = glGetUniformLocation(m_program, "mPVM");
	m_nVM = glGetUniformLocation(m_program, "mVM");

	glClearColor(1.f,1.f,1.f,1.f);
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
	
	// Enable blending
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
Exemplo n.º 3
0
// OpenGL initialization
void
init()
{
    // Subdivide a tetrahedron into a sphere
    tetrahedron( NumTimesToSubdivide );

    // Create a vertex array object
    GLuint vao;
    glGenVertexArrays( 1, &vao );
    glBindVertexArray( vao );

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals),
		  NULL, GL_STATIC_DRAW );
    glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points );
    glBufferSubData( GL_ARRAY_BUFFER, sizeof(points),
		     sizeof(normals), normals );

    // Load shaders and use the resulting shader program
    GLuint program = InitShader( "vshader56.glsl", "fshader56.glsl" );
    glUseProgram( program );
	
    // set up vertex arrays
    GLuint vPosition = glGetAttribLocation( program, "vPosition" );
    glEnableVertexAttribArray( vPosition );
    glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
			   BUFFER_OFFSET(0) );

    GLuint vNormal = glGetAttribLocation( program, "vNormal" ); 
    glEnableVertexAttribArray( vNormal );
    glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
			   BUFFER_OFFSET(sizeof(points)) );

    // Initialize shader lighting parameters
    point4 light_position( 0.0, 0.0, 2.0, 0.0 );
    color4 light_ambient( 0.2, 0.2, 0.2, 1.0 );
    color4 light_diffuse( 1.0, 1.0, 1.0, 1.0 );
    color4 light_specular( 1.0, 1.0, 1.0, 1.0 );

    color4 material_ambient( 1.0, 0.0, 1.0, 1.0 );
    color4 material_diffuse( 1.0, 0.8, 0.0, 1.0 );
    color4 material_specular( 1.0, 0.0, 1.0, 1.0 );
    float  material_shininess = 5.0;

    color4 ambient_product = light_ambient * material_ambient;
    color4 diffuse_product = light_diffuse * material_diffuse;
    color4 specular_product = light_specular * material_specular;

    glUniform4fv( glGetUniformLocation(program, "AmbientProduct"),
		  1, ambient_product );
    glUniform4fv( glGetUniformLocation(program, "DiffuseProduct"),
		  1, diffuse_product );
    glUniform4fv( glGetUniformLocation(program, "SpecularProduct"),
		  1, specular_product );
	
    glUniform4fv( glGetUniformLocation(program, "LightPosition"),
		  1, light_position );

    glUniform1f( glGetUniformLocation(program, "Shininess"),
		 material_shininess );
		 
    // Retrieve transformation uniform variable locations
    ModelView = glGetUniformLocation( program, "ModelView" );
    Projection = glGetUniformLocation( program, "Projection" );
    
    glEnable( GL_DEPTH_TEST );
    
    glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* white background */
}
Exemplo n.º 4
0
void CChildView::InitGL()
{
	// Create a checkerboard pattern
	for (int i = 0; i < 64; i++) {
		for (int j = 0; j < 64; j++) {
			GLubyte c;
			c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255;
			image[i][j][0]  = c;
			image[i][j][1]  = c;
			image[i][j][2]  = c;
			image2[i][j][0] = c;
			image2[i][j][1] = 0;
			image2[i][j][2] = c;
		}
	}

	colorcube();

	m_program = LoadShaders( "ShaderWnd/vertex.glsl", "ShaderWnd/fragment.glsl" );


	static const vec4 g_vertex_buffer_data[] = { 
		vec4(-1.0f, -1.0f, 0.0f, 1.0f),
		vec4(1.0f, -1.0f, 0.0f, 1.0f),
		vec4(0.0f,  0.5f, 0.0f, 1.0f)
	};

	static const vec4 g_color_buffer_data[] = { 
		vec4(1.0f, 0.0f, 0.0f, 1.0f),
		vec4(0.0f, 1.0f, 0.0f, 1.0f),
		vec4(0.0f, 0.0f, 1.0f, 1.0f)
	};

	//GLuint vertexbuffer;
	glUseProgram(m_program);

	GLuint vao;
	glGenVertexArrays( 1, &vao);
	glBindVertexArray( vao);

	/*glGenBuffers(1, &vertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);*/


	glGenTextures(2, textures);

	glBindTexture(GL_TEXTURE_2D, textures[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	glBindTexture(GL_TEXTURE_2D, textures[1]);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	glActiveTexture( GL_TEXTURE0 );
	glBindTexture( GL_TEXTURE_2D, textures[0]);
	glActiveTexture( GL_TEXTURE1 );
	glBindTexture( GL_TEXTURE_2D, textures[1]);

	// Create and initialize a buffer object
	GLuint buffer;
	glGenBuffers(1, &buffer);
	glBindBuffer(GL_ARRAY_BUFFER, buffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(points) +  sizeof(tex_coords) +        sizeof(normals), NULL, GL_STATIC_DRAW);
	glBufferSubData(GL_ARRAY_BUFFER, 0,                sizeof(points), value_ptr(points[0]));
	glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(normals), value_ptr(normals[0]));
	glBufferSubData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals), sizeof(tex_coords), value_ptr(tex_coords[0]));


	point4 light_position (-5.f, 5.f, -5.f, 0.f);
	color4 light_ambient (0.2f, 0.2f, 0.2f, 1.f);
	color4 light_diffuse (1.f, 1.f, 1.f, 1.f);
	color4 light_specular (1.f, 1.f, 1.f, 1.f);

	color4 material_ambient(.3f, .6f, .3f, 1.f);
	color4 material_diffuse (0.3f, .6f, 0.3f, 1.f);
	color4 material_specular (1.f, 1.f, 1.f, 1.f);
	float material_shininess = 100.0f;

	color4 ambient_product = light_ambient*material_ambient;
	color4 diffuse_product = light_diffuse*material_diffuse;
	color4 specular_product = light_specular*material_specular;

	glUniform4fv(glGetUniformLocation(m_program, "AmbientProduct"), 1, value_ptr(ambient_product));
	glUniform4fv(glGetUniformLocation(m_program, "DiffuseProduct"), 1, value_ptr(diffuse_product));
	glUniform4fv(glGetUniformLocation(m_program, "SpecularProduct"), 1, value_ptr(specular_product));
	glUniform4fv(glGetUniformLocation(m_program, "LightPosition"), 1, value_ptr(light_position));
	glUniform1f(glGetUniformLocation(m_program, "Shininess"), material_shininess);

	// set up vertex arrays (after shaders are loaded)
	GLuint vPosition = glGetAttribLocation(m_program, "vPosition");
		glEnableVertexAttribArray(vPosition);
		glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
	GLuint vNormal = glGetAttribLocation(m_program, "vNormal");
		glEnableVertexAttribArray(vNormal);
		glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)));
	GLuint vTex = glGetAttribLocation(m_program, "vTexCoord");
		glEnableVertexAttribArray(vTex);
		glVertexAttribPointer(vTex, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)+sizeof(normals)));

	// Set the value of the fragment shader texture sampler variable
	//   ("texture") to the the appropriate texture unit. In this case,
	//   zero, for GL_TEXTURE0 which was previously set by calling
	//   glActiveTexture().
	glUniform1i( glGetUniformLocation(m_program, "diffuse_mat"), 0);


	m_nPVM = glGetUniformLocation(m_program, "mPVM");
	m_nVM = glGetUniformLocation(m_program, "mVM");

	glClearColor(1.f,1.f,1.f,1.f);
	glEnable(GL_DEPTH_TEST);
	glShadeModel(GL_SMOOTH);
}
Exemplo n.º 5
0
// OpenGL initialization
void
init()
{
    // Create a vertex array object
    GLuint vao, vbo, ebo;
    glGenVertexArrays( 1,&vao);
    glBindVertexArray( vao );

	// Create Vertex and Normal buffer
	glGenBuffers(1,&vbo);
	glBindBuffer(GL_ARRAY_BUFFER,vbo);
	glBufferData(GL_ARRAY_BUFFER,sizeof(vertexarray) + sizeof(normalarray),NULL,GL_STATIC_DRAW);
	glBufferSubData(GL_ARRAY_BUFFER,0,sizeof(vertexarray),vertexarray);
	glBufferSubData(GL_ARRAY_BUFFER,sizeof(vertexarray),sizeof(normalarray),normalarray);

    // Load shaders and use the resulting shader program
    GLuint program = InitShader( "vshader56.glsl", "fshader56.glsl" );
    glUseProgram( program );
	
    // set up vertex arrays
	GLuint vPosition = glGetAttribLocation(program, "vPosition" );
	glVertexAttribPointer(vPosition,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(0));
    glEnableVertexAttribArray( vPosition );

	GLuint vNormal = glGetAttribLocation(program, "vNormal" );
	glVertexAttribPointer(vNormal,3,GL_FLOAT,GL_FALSE,0,BUFFER_OFFSET(sizeof(vertexarray)));
    glEnableVertexAttribArray( vNormal );

	// Create Element Array Buffer
	glGenBuffers(1,&ebo);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,ebo);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(elems),elems,GL_STATIC_DRAW);

    // Initialize shader lighting parameters for light 1
    point4 light_position1( -0.5, -1.0, 2.0, 0.0 );
    color4 light_ambient1( 0.3, 0.2, 0.2, 1.0 );
    color4 light_diffuse1( 0.9, 0.6, 0.6, 1.0 );
    color4 light_specular1( 1.0, 0.6, 0.6, 1.0 );

    // Initialize shader lighting parameters for light 2
    point4 light_position2( 0.5, 1.0, -1.0, 0.0 );
    color4 light_ambient2( 0.2, 0.2, 0.3, 1.0 );
    color4 light_diffuse2( 0.6, 0.6, 0.9, 1.0 );
    color4 light_specular2( 0.6, 0.6, 1.0, 1.0 );

    // Initialize shader material parameters for cube
    color4 material_ambient( 0.4, 0.25, 0.1, 1.0 );
    color4 material_diffuse( 0.8, 0.5, 0.2, 1.0 );
    color4 material_specular( 0.8, 0.8, 0.8, 1.0 );
    float  material_shininess = 16.0;

    color4 ambient_product1 = (light_ambient1) * material_ambient;
    color4 diffuse_product1 = (light_diffuse1) * material_diffuse;
    color4 specular_product1 = (light_specular1) * material_specular;

    color4 ambient_product2 = (light_ambient2) * material_ambient;
    color4 diffuse_product2 = (light_diffuse2) * material_diffuse;
    color4 specular_product2 = (light_specular2) * material_specular;

    glUniform4fv( glGetUniformLocation(program, "AmbientProduct1"),
		  1, ambient_product1 );
    glUniform4fv( glGetUniformLocation(program, "AmbientProduct2"),
		  1, ambient_product2 );

    glUniform4fv( glGetUniformLocation(program, "DiffuseProduct1"),
		  1, diffuse_product1 );
    glUniform4fv( glGetUniformLocation(program, "DiffuseProduct2"),
		  1, diffuse_product2 );

    glUniform4fv( glGetUniformLocation(program, "SpecularProduct1"),
		  1, specular_product1 );
    glUniform4fv( glGetUniformLocation(program, "SpecularProduct2"),
		  1, specular_product2 );
	
    glUniform4fv( glGetUniformLocation(program, "LightPosition1"),
		  1, light_position1 );
    glUniform4fv( glGetUniformLocation(program, "LightPosition2"),
		  1, light_position2 );

    glUniform1f( glGetUniformLocation(program, "Shininess"),
		 material_shininess );
		 
    // Retrieve transformation uniform variable locations
    ModelView = glGetUniformLocation( program, "ModelView" );
    Projection = glGetUniformLocation( program, "Projection" );
    
    glEnable( GL_DEPTH_TEST );
    
    glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* white background */
}