示例#1
0
void Skybox::render(const Camera &camera) const{
	shader->bind();

	glPushAttrib(GL_ENABLE_BIT|GL_DEPTH_BUFFER_BIT);
	glDisable(GL_DEPTH_TEST);
	glDisable(GL_CULL_FACE);

	Shader::upload_projection_view_matrices(
			camera.projection_matrix(),
			glm::lookAt(glm::vec3(0.0), camera.look_at()-camera.position(), camera.up())
	);

	/* Disable most attribs from Shader::vertex_x */
	Shader::push_vertex_attribs(2);

	glBindBuffer(GL_ARRAY_BUFFER, vbo);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(float)*3*36) );
	texture->texture_bind(Shader::TEXTURE_CUBEMAP_0);

	glDrawArrays(GL_TRIANGLES, 0, 36);

	checkForGLErrors("Skybox::render(): render");

	Shader::pop_vertex_attribs();
	glPopAttrib();

	checkForGLErrors("Skybox::render(): post");
}
示例#2
0
文件: main.cpp 项目: torandi/basejump
static void render(){
	checkForGLErrors("Frame begin");

	Engine::render();

	SDL_GL_SwapBuffers();
	checkForGLErrors("Frame end");
}
示例#3
0
void RenderObject::recursive_render(const aiNode* node,
		const glm::mat4 &parent_matrix) const {


	aiMatrix4x4 m = node->mTransformation;
	m.Transpose();

	glm::mat4 matrix(parent_matrix);

	matrix *= glm::make_mat4((float*)&m);

	Shader::upload_model_matrix(matrix);

	for(unsigned int i=0; i<node->mNumMeshes; ++i) {
		const aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];

		if(mesh->mNumFaces > 0) {
			auto it = mesh_data.find(mesh);
			const mesh_data_t *md;

			if(it != mesh_data.end()) {
				md = &(it->second);
			} else {
				continue;
			}

			if(md->num_indices > 0) {
				glBindBuffer(GL_ARRAY_BUFFER, md->vb);
				glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, md->ib);

				glVertexAttribPointer(Shader::ATTR_POSITION,  3, GL_FLOAT, GL_FALSE, sizeof(Shader::vertex_t), (const GLvoid*)offsetof(Shader::vertex_t, pos));
				glVertexAttribPointer(Shader::ATTR_TEXCOORD,  2, GL_FLOAT, GL_FALSE, sizeof(Shader::vertex_t), (const GLvoid*)offsetof(Shader::vertex_t, uv));
				glVertexAttribPointer(Shader::ATTR_NORMAL,    3, GL_FLOAT, GL_FALSE, sizeof(Shader::vertex_t), (const GLvoid*)offsetof(Shader::vertex_t, normal));
				glVertexAttribPointer(Shader::ATTR_TANGENT,   3, GL_FLOAT, GL_FALSE, sizeof(Shader::vertex_t), (const GLvoid*)offsetof(Shader::vertex_t, tangent));
				glVertexAttribPointer(Shader::ATTR_BITANGENT, 3, GL_FLOAT, GL_FALSE, sizeof(Shader::vertex_t), (const GLvoid*)offsetof(Shader::vertex_t, bitangent));
				glVertexAttribPointer(Shader::ATTR_COLOR,     4, GL_FLOAT, GL_FALSE, sizeof(Shader::vertex_t), (const GLvoid*)offsetof(Shader::vertex_t, color));

				checkForGLErrors("set attrib pointers");

				materials[md->mtl_index].bind();
				checkForGLErrors("Activte material");

				glDrawElements(GL_TRIANGLES, md->num_indices, GL_UNSIGNED_INT,0 );
				checkForGLErrors("Draw material");
			}
		}
	}

	for(unsigned int i=0; i<node->mNumChildren; ++i) {
		recursive_render(node->mChildren[i], matrix);
	}

}
示例#4
0
void GLWidget::resizeGL(int width, int height)
{
    // Respond to the window being resized by updating the viewport and
    // projection matrices.

    checkForGLErrors();

    // Setup projection matrix for new window
    m_gl_state.setOrthographicProjectionFromWidthAndHeight(width, height);

    // Update OpenGL viewport and internal variables
    glViewport(0, 0, width, height);
    checkForGLErrors();
}
示例#5
0
TextureArray::TextureArray(std::vector<std::string> path, bool mipmap)
	: TextureBase()
	, _num(path.size())
	, _texture(0) {

	if ( path.size() == 0 ){
		fprintf(stderr, "TextureArray must have at least one image, got 0.\n");
		path.push_back("default.jpg");
		_num++;
	}

	/* hack to get size */
	{
		SDL_Surface* surface = load_image(path[0], &size);
		SDL_FreeSurface(surface);
	}

	fprintf(verbose, "Creating TextureArray with %zd images at %dx%d\n", path.size(), size.x, size.y);

	glGenTextures(1, &_texture);
	glBindTexture(GL_TEXTURE_2D_ARRAY, _texture);
	glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

	if ( mipmap ){
		glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_GENERATE_MIPMAP, GL_TRUE);
	} else {
		glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_GENERATE_MIPMAP, GL_FALSE);
	}

	glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, size.x, size.y, num_textures(), 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);

	int n = 0;
	for ( const std::string& filename: path) {
		glm::ivec2 cur_size;
		SDL_Surface* surface = load_image(filename, &cur_size);
		glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, n++, cur_size.x, cur_size.y, 1, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
		checkForGLErrors(("Texture2DArray: glTexSubImage3D ( " + filename + ")").c_str());
		SDL_FreeSurface(surface);
	}

	glBindTexture(GL_TEXTURE_2D_ARRAY, 0);
	checkForGLErrors("Texture2DArray: Create");
}
示例#6
0
void Text::realloc( size_t num_chars ) {
	// Free fbo
	delete fbo_;
	fbo_ = nullptr;

	if(size_ != num_chars) {
	//Resize vertex buffer
		vertices_.resize(num_chars * NUM_VERTICES);
		glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
		glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_t) * vertices_.size(), 0, GL_DYNAMIC_DRAW);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
		checkForGLErrors("Text::realloc(): create vertex buffer");
		//Fill index buffer
		std::vector<unsigned int> indices;
		unsigned int first_index = 0;
		for(unsigned int c = 0 ; c < num_chars; ++c) {
			for(int i=0; i< NUM_INDICES; ++i) {
				indices.push_back(first_index + index_offset[i]);
			}
			first_index += NUM_VERTICES;
		}

		//Upload indices
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indices.size(), indices.data(), GL_STATIC_DRAW);
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

		size_ = num_chars;
	}
}
示例#7
0
static void render_loading_scene() {
	checkForGLErrors("Frame begin");
	glClearColor(1, 0, 1, 1);
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	Shader::upload_state(resolution);
	Shader::upload_projection_view_matrices(screen_ortho, glm::mat4());
	glViewport(0, 0, resolution.x, resolution.y);

	Quad loadingscreen;
	loadingscreen.set_scale(glm::vec3(resolution.x,resolution.y,0));
	Texture2D* loadingtexture = Texture2D::from_filename(PATH_BASE "/data/textures/loading.png");
	loadingtexture->texture_bind(Shader::TEXTURE_2D_0);


	shaders[SHADER_PASSTHRU]->bind();

	loadingscreen.render();
	SDL_GL_SwapBuffers();
	checkForGLErrors("Frame end");
}
示例#8
0
文件: main.cpp 项目: torandi/basejump
static void init(const char* title){
	Logging::init();
	Logging::add_destination(verbose_flag ? Logging::VERBOSE : Logging::WARNING, stderr);
	Logging::add_destination(Logging::VERBOSE, "frob.log");
	Logging::info("FFS: Frobnicator Fubar System - Engine starting\n");
	init_window(title);
	Data::add_search_path(srcdir);
	Engine::setup_opengl();
	Shader::initialize();

	//Set default fog
	Shader::fog_t fog = { glm::vec4(0.584f, 0.698f, 0.698f, 1.f), 0.005f };
	Shader::upload_fog(fog);

	Config config = Config::parse("/graphics.cfg");

	MovableLight::shadowmap_resolution = glm::ivec2(config["/shadowmap/resolution"]->as_vec2());
	MovableLight::shadowmap_far_factor = config["/shadowmap/far_factor"]->as_float();

	Loading::init(resolution);

	static const char* resources[] = {
		"texture:/textures/default.jpg",
		"texture:/textures/default_normalmap.jpg",
		"texture:/textures/white.jpg",
		"shader:/shaders/normal",
		"shader:/shaders/passthru",
	};
	Engine::preload(std::vector<std::string>(resources, resources + sizeof(resources)/sizeof(char*)), Loading::progress);
	CL::init();
	srand((unsigned int)time(0));

	Engine::init();
	Loading::cleanup();
	Engine::start(seek);
	checkForGLErrors("post init()");
}
示例#9
0
void myInit(void)
{	
	glClearColor(0.0,0.0,0.0,0.0);
	glClearDepth(1.0);								// Enables Clearing Of The Depth Buffer

	//------------------------------------------------------------Object & Texture

	//	readFile("/Users/jiharu/svn/594CM/gl_obj/obj/triangular/Shatter1.obj");
	loadTextures();
	#pragma mark blend
	//DEFINE NIGTHS in some INIT function..
	glEnable(GL_LIGHTING); //enable lighting
	glShadeModel (GL_SMOOTH); //GL_SMOOTH, GL_FLAT
	//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);				// Really Nice Perspective Calculations

	#pragma mark Morph obj loader 2/4
	//------------------------------------------------------------Morph obj loader
	maxver=0;									// Sets Max Vertices To 0 By Default
	objload("/Users/jiharu/svn/594CM/gl_obj/obj/morph/obj3.obj",&morph1);
	objload("/Users/jiharu/svn/594CM/gl_obj/obj/morph/obj2.obj",&morph2);
	objload("/Users/jiharu/svn/594CM/gl_obj/obj/morph/obj4.obj",&morph3);
	objallocate(&morph4,382);							// Manually Reserver Ram For A 4th 468 Vertice Object (morph4) New object has 382 vertices
	for(int i=0;i<382;i++)								// Loop Through All 468 Vertices
	{
		morph4.points[i].x=((float)(rand()%300)/100)-1.5;			// morph4 x Point Becomes A Random Float Value From -7 to 7
		morph4.points[i].y=((float)(rand()%300)/100)-1.5;			// morph4 y Point Becomes A Random Float Value From -7 to 7
		morph4.points[i].z=((float)(rand()%300)/100)-1.5;			// morph4 z Point Becomes A Random Float Value From -7 to 7
	}
	objload("/Users/jiharu/svn/594CM/gl_obj/obj/morph/obj3.obj",&helper);						// Load sphere.txt Object Into Helper (Used As Starting Point)
	sour=dest=&morph1;								// Source & Destination Are Set To Equal First Object (morph1)

	#pragma mark -
	#pragma mark light
	//------------------------------------------------------------Light
	//define the lighting model
	float ambient[] = {0.08, 0.08, 0.01, 1.0};
	float diffuse[] = {0.7, 0.7, 0.0, 1.0};
	float specular[] = {0.3, 0.3, 0.3, 1.0};
	
	glEnable(GL_LIGHT0); //turn on one of the lights
	//	glDisable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);	
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
	
	//------------------------------------------------------------Spot Light 1, 2, & 3
	float spot_direction1[] = { 0.0, -1.0, 0.0 }; 
	float ambient1[] = {0.01, .2, 0.5, 1.0};
	float diffuse1[] = {0.01, 0.5, 0.5, 1.0};
	float specular1[] = {0.0, 0.8, 0.8, 1.0};
	
	glEnable(GL_LIGHT1); //turn on one of the lights
	//	glDisable(GL_LIGHT1);	
	glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);	
	glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
	
	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction1);
	glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5); 
	glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5); 
	glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);
	glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);	// degrees
	glLightf (GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
	
	float spot_direction2[] = { 0.0, -1.0, 0.0 }; 	
	float ambient2[] = {0.2, 0.1, 0.5, 1.0};
	float diffuse2[] = {0.2, 0.5, 0.8, 1.0};
	float specular2[] = {0.0, 0.8, 0.8, 1.0};
	
	glEnable(GL_LIGHT2); //turn on one of the lights
	//	glDisable(GL_LIGHT2);	
	glLightfv(GL_LIGHT2, GL_AMBIENT, ambient2);
	glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse2);	
	glLightfv(GL_LIGHT2, GL_SPECULAR, specular2);
	
	glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, spot_direction2);
	glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 1.5); 
	glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.8); 
	glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.2);
	glLightf (GL_LIGHT2, GL_SPOT_CUTOFF, 45.0);	// degrees
	glLightf (GL_LIGHT2, GL_SPOT_EXPONENT, 1.0);
		
	float spot_direction3[] = { .5, -1.0, 0.0 }; 
	float ambient3[] = {0.5, 0.5, 0.5, 1.0};
	float diffuse3[] = {1.0, 1.0, 1.0, 1.0};
	float specular3[] = {1.0, 1.0, 1.0, 1.0};
	
	glEnable(GL_LIGHT3); //turn on one of the lights
	//	glDisable(GL_LIGHT2);	
	glLightfv(GL_LIGHT3, GL_AMBIENT, ambient3);
	glLightfv(GL_LIGHT3, GL_DIFFUSE, diffuse3);	
	glLightfv(GL_LIGHT3, GL_SPECULAR, specular3);
	
	glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, spot_direction3);
	glLightf(GL_LIGHT3, GL_CONSTANT_ATTENUATION, 0.5); 
	glLightf(GL_LIGHT3, GL_LINEAR_ATTENUATION, 0.8); 
	glLightf(GL_LIGHT3, GL_QUADRATIC_ATTENUATION, 0.2);
	glLightf (GL_LIGHT3, GL_SPOT_CUTOFF, 45.0);	// degrees
	glLightf (GL_LIGHT3, GL_SPOT_EXPONENT, 2.0);
	
	#pragma mark material
	//------------------------------------------------------------material
	//define the material of the object
	float mat_shininess[] = { 50. };
	float mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
	float mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
	float mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	float mat_emission[] = {1.0, 0.1, 0.1, 1.0};
	
	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); 
	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); 
	
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClearAccum(0.0, 0.0, 0.0, 0.0);
	
	checkForGLErrors("clearInit");
}
示例#10
0
void GLWidget::paintGL()
{
    // This method gets called by the event handler to draw the scene, so
    // this is where you need to build your scene -- make your changes and
    // additions here.  All rendering happens in this function.

    checkForGLErrors();

    // Clear the screen with the background colour.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    // Setup the model-view transformation matrix stack.
    transformStack().loadIdentity();
    checkForGLErrors();

    const float GLOBAL_X_SCALE = 0.8f;
    const float GLOBAL_Y_SCALE = 0.8f;
    const float BODY_X_SCALE = 120.0f;
    const float BODY_X_INV_SCALE = (float)(1/BODY_X_SCALE);
    const float BODY_Y_SCALE = 150.0f;
    const float BODY_Y_INV_SCALE = (float)(1/BODY_Y_SCALE);
    const float NECK_X_SCALE = 65.0f;
    const float NECK_Y_SCALE = 50.0f;
    const float BEAK_UP_X_SCALE = 28.0f;
    const float BEAK_UP_Y_SCALE = 5.0f;
    const float BEAK_DOWN_X_SCALE = 28.0f;
    const float BEAK_DOWN_Y_SCALE = 5.0f;
    const float ARM_X_SCALE = 20.0f;
    const float ARM_Y_SCALE = 70.0f;
    const float LEG_X_SCALE = 10.0f;
    const float LEG_X_INV_SCALE = (float)(1/LEG_X_SCALE);
    const float LEG_Y_SCALE = 60.0f;
    const float LEG_Y_INV_SCALE = (float)(1/LEG_Y_SCALE);
    const float FOOT_X_SCALE = 10.0f;
    const float FOOT_Y_SCALE = 40.0f;

    // Note that successive transformations are applied *before* the previous
    // ones.

    // Push the current transformation matrix on the stack
    transformStack().pushMatrix();
    transformStack().scale(GLOBAL_X_SCALE,GLOBAL_Y_SCALE);

    // Draw body
    transformStack().pushMatrix(); // Draw body push

    transformStack().translate(body_move_horizontal_trans,body_move_vertical_trans);
    transformStack().scale(BODY_X_SCALE,BODY_Y_SCALE);
    m_gl_state.setColor(1.0, 1.0, 0.0);
    m_unit_body.draw();
    transformStack().scale(BODY_X_INV_SCALE,BODY_Y_INV_SCALE);

    // Draw left leg and foot
    transformStack().pushMatrix();
    drawLeftLegFoot(LEG_X_SCALE,LEG_Y_SCALE,
                    LEG_X_INV_SCALE,LEG_Y_INV_SCALE,
                    FOOT_X_SCALE,FOOT_Y_SCALE);
    transformStack().popMatrix();

    // Draw right leg and foot
    transformStack().pushMatrix();
    drawRightLegFoot(LEG_X_SCALE,LEG_Y_SCALE,
                        LEG_X_INV_SCALE,LEG_Y_INV_SCALE,
                        FOOT_X_SCALE,FOOT_Y_SCALE);
    transformStack().popMatrix();

    // Draw left arm
    transformStack().pushMatrix();
    drawLeftArm(ARM_X_SCALE,ARM_Y_SCALE);
    transformStack().popMatrix();

    // Draw head, upper beak and lower beak
    transformStack().pushMatrix();
    drawNeck(NECK_X_SCALE,NECK_Y_SCALE,
             BEAK_UP_X_SCALE,BEAK_UP_Y_SCALE,
             BEAK_DOWN_X_SCALE,BEAK_DOWN_Y_SCALE,
             BODY_Y_SCALE);
    transformStack().popMatrix();

    transformStack().popMatrix(); // Draw body pop

    // Retrieve the previous state of the transformation stack
    transformStack().popMatrix();


    // Execute any GL functions that are in the queue just to be safe
    glFlush();
    checkForGLErrors();
}
示例#11
0
void myInit(void)
{	
	glClearColor(0.0,0.0,0.0,0.0);
	//------------------------------------------------------------Object & Texture
	readFile("/Users/jiharu/svn/594CM/gl_obj/obj/triangular/Shatter1.obj");
	loadTextures();
	
	//DEFINE NIGTHS in some INIT function..
	glEnable(GL_LIGHTING); //enable lighting
	glShadeModel (GL_SMOOTH); //GL_SMOOTH, GL_FLAT
#pragma mark -
#pragma mark light
	//------------------------------------------------------------Light
	//define the lighting model
	float ambient[] = {0.08, 0.08, 0.01, 1.0};
	float diffuse[] = {0.7, 0.7, 0.0, 1.0};
	float specular[] = {0.3, 0.3, 0.3, 1.0};
	
	glEnable(GL_LIGHT0); //turn on one of the lights
	//	glDisable(GL_LIGHT0);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);	
	glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
	
	//------------------------------------------------------------Spot Light 1, 2, & 3
	float spot_direction1[] = { 0.0, -1.0, 0.0 }; 
	float ambient1[] = {0.01, .2, 0.5, 1.0};
	float diffuse1[] = {0.01, 0.5, 0.5, 1.0};
	float specular1[] = {0.0, 0.3, 1.0, 1.0};
	
	glEnable(GL_LIGHT1); //turn on one of the lights
	//	glDisable(GL_LIGHT1);	
	glLightfv(GL_LIGHT1, GL_AMBIENT, ambient1);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse1);	
	glLightfv(GL_LIGHT1, GL_SPECULAR, specular1);
	
	glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction1);
	glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5); 
	glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5); 
	glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2);
	glLightf (GL_LIGHT1, GL_SPOT_CUTOFF, 30.0);	// degrees
	glLightf (GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);
	
	float spot_direction2[] = { 0.0, -1.0, 0.0 }; 	
	float ambient2[] = {0.9, 0.1, 0.01, 1.0};
	float diffuse2[] = {0.9, 0.5, 0.0, 1.0};
	float specular2[] = {1.0, 0.1, 0.1, 1.0};
	
	glEnable(GL_LIGHT2); //turn on one of the lights
	//	glDisable(GL_LIGHT2);	
	glLightfv(GL_LIGHT2, GL_AMBIENT, ambient2);
	glLightfv(GL_LIGHT2, GL_DIFFUSE, diffuse2);	
	glLightfv(GL_LIGHT2, GL_SPECULAR, specular2);
	
	glLightfv(GL_LIGHT2, GL_SPOT_DIRECTION, spot_direction2);
	glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 1.5); 
	glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.8); 
	glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 0.2);
	glLightf (GL_LIGHT2, GL_SPOT_CUTOFF, 45.0);	// degrees
	glLightf (GL_LIGHT2, GL_SPOT_EXPONENT, 1.0);
		
	float spot_direction3[] = { .5, -1.0, 0.0 }; 
	float ambient3[] = {0.5, 0.5, 0.5, 1.0};
	float diffuse3[] = {1.0, 1.0, 1.0, 1.0};
	float specular3[] = {1.0, 1.0, 1.0, 1.0};
	
	glEnable(GL_LIGHT3); //turn on one of the lights
	//	glDisable(GL_LIGHT2);	
	glLightfv(GL_LIGHT3, GL_AMBIENT, ambient3);
	glLightfv(GL_LIGHT3, GL_DIFFUSE, diffuse3);	
	glLightfv(GL_LIGHT3, GL_SPECULAR, specular3);
	
	glLightfv(GL_LIGHT3, GL_SPOT_DIRECTION, spot_direction3);
	glLightf(GL_LIGHT3, GL_CONSTANT_ATTENUATION, 0.5); 
	glLightf(GL_LIGHT3, GL_LINEAR_ATTENUATION, 0.8); 
	glLightf(GL_LIGHT3, GL_QUADRATIC_ATTENUATION, 0.2);
	glLightf (GL_LIGHT3, GL_SPOT_CUTOFF, 45.0);	// degrees
	glLightf (GL_LIGHT3, GL_SPOT_EXPONENT, 2.0);
	
	#pragma mark material
	//------------------------------------------------------------material
	//define the material of the object
	float mat_shininess[] = { 50. };
	float mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
	float mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
	float mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
	float mat_emission[] = {1.0, 0.1, 0.1, 1.0};
	
	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); 
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); 
	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission); 
	
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClearAccum(0.0, 0.0, 0.0, 0.0);
	
	checkForGLErrors("clearInit");
}
void GLWidget::paintGL()
{
    // This method gets called by the event handler to draw the scene, so
    // this is where you need to build your scene -- make your changes and
    // additions here.  All rendering happens in this function.

    checkForGLErrors();

    // Clear the screen with the background colour.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  

    // Setup the model-view transformation matrix stack.
    transformStack().loadIdentity();
    checkForGLErrors();

    //////////////////////////////////////////////////////////////////////////
    // TODO:
    //   Modify this function draw the scene.  This should include function
    //   calls to pieces that apply the appropriate transformation matrices
    //   and render the individual body parts.
    //////////////////////////////////////////////////////////////////////////

    // Draw our hinged object
    
    //const float BODY_WIDTH = 60.0f;
    //const float BODY_LENGTH = 100.0f;
    //const float ARM_LENGTH = 100.0f;
    //const float ARM_WIDTH = 20.0f;

	//============================ADDED============================
	const float BODY_WIDTH = 100.0f;
    const float BODY_LENGTH = 200.0f;
    const float ARM_LENGTH = 120.0f;
    const float ARM_WIDTH = 60.0f;
	const float HEAD_LENGTH = 60.0f;
    const float HEAD_WIDTH = 50.0f;
    const float LEG_WIDTH = 20.0f;
    const float LEG_LENGTH = 80.0f;
    const float BEAK_WIDTH = 5.0f;
    const float BEAK_LENGTH = 40.0f;
    
    // Note that successive transformations are applied *before* the previous
    // ones.

    // Push the current transformation matrix on the stack
    transformStack().pushMatrix();
    
    	//move penguin left to right
        transformStack().translate(m_x_move, 0.0);
        
        //move penguin up and down
		transformStack().translate(0.0, m_y_move);
        
        // Draw the 'body'
        transformStack().pushMatrix();
            // Scale square to size of body
            transformStack().scale(BODY_WIDTH, BODY_LENGTH);

            // Set the colour to white
            m_gl_state.setColor(1.0, 1.0,1.0);

            // Draw the body
	    	m_hexagon.draw();
        transformStack().popMatrix();

        //Head objects 
        transformStack().pushMatrix();
    		
        	transformStack().translate(0.0, BODY_LENGTH + HEAD_LENGTH/4);

        	// Rotate along the hinge
        	transformStack().rotateInDegrees(m_head_angle);
        	transformStack().translate(0.0, 2*HEAD_LENGTH/4);
        		
        	//draw the head shape
			transformStack().pushMatrix();
        		// Scale the size of the head
        		transformStack().scale(HEAD_WIDTH, HEAD_LENGTH);
        		
        		m_gl_state.setColor(0.0, 0.0, 0.3);
	    		m_pentagon.draw();
	
       		// Retrieve the previous state of the transformation stack
        	transformStack().popMatrix();
                
        	//Lower beak (moves up and down)
        	transformStack().pushMatrix();
				transformStack().translate(-HEAD_LENGTH, -HEAD_LENGTH/2);
                        
                //move beak up and down
                transformStack().translate(0.0, m_beak);
                        
                //draw "beak"
				transformStack().pushMatrix();
                    // Scale the size of the beak
        			transformStack().scale(BEAK_LENGTH, BEAK_WIDTH);
        			
        			// Draw the square for the beak
        			m_gl_state.setColor(1.0, 1.0, 1.0);
	    			m_unit_square.draw();
	    			
				transformStack().popMatrix();
			
			transformStack().popMatrix();
			
			//Upper beak (doeasn't move)
			transformStack().pushMatrix();
				transformStack().translate(-HEAD_LENGTH, -HEAD_LENGTH/2 + 2*BEAK_WIDTH);
        		//transformStack().translate(-HEAD_LENGTH, -HEAD_LENGTH/2 + 2*BEAK_WIDTH);
        		// Scale the size of the beak
        		transformStack().scale(BEAK_LENGTH, 2*BEAK_WIDTH);
        			
        		// Draw the square for the beak
        		m_gl_state.setColor(1.0, 1.0, 1.0);
	    		m_unit_square.draw();
			
			transformStack().popMatrix();
			
			//Eye
			transformStack().pushMatrix();
			
            	transformStack().translate(-HEAD_WIDTH/2, 0.0);
				transformStack().pushMatrix();
					transformStack().scale(6.0f, 6.0f);
        			m_gl_state.setColor(1.0, 1.0, 1.0);	
        			m_unit_circle.draw();
        		transformStack().popMatrix();
        		
        		transformStack().scale(3.0f, 3.0f);
        		m_gl_state.setColor(0.0, 0.0, 0.0);	
        		m_unit_circle.draw();
        		
			transformStack().popMatrix();
			
       	// Retrieve the previous state of the transformation stack
        transformStack().popMatrix();
    	
    	//draw the joint of a head and body
    	transformStack().pushMatrix();
   
   			transformStack().translate(0.0, BODY_LENGTH);
			transformStack().scale(5.0f, 5.0f);
        	m_gl_state.setColor(0.0, 1.0, 1.0);	
        	m_unit_circle.draw();
   
    	transformStack().popMatrix();
    	
    	
    	
    	//Draw the right leg
    	transformStack().pushMatrix();
    		// Move the leg to the joint hinge
        	transformStack().translate(BODY_WIDTH/2, -BODY_LENGTH/2 + LEG_WIDTH/2);
        	
        	// Rotate along the hinge
        	transformStack().rotateInDegrees(m_leg_angle);
        	
        	// Move to center location of the leg
			transformStack().translate(0.0, -LEG_LENGTH/2 + LEG_WIDTH/2);
			
			transformStack().pushMatrix();
				transformStack().pushMatrix();
			
					transformStack().scale(LEG_WIDTH, LEG_LENGTH);
                	//transformStack().translate(0.0, -0.5);
                	         
        			// Draw the square for the leg
        			m_gl_state.setColor(0.0, 0.0, 0.3);
	    			m_unit_square.draw();
        	
                // Retrieve the previous state of the transformation stack
            	transformStack().popMatrix();
            	
            	transformStack().translate(0.0, -LEG_LENGTH/2 + LEG_WIDTH/2);
			
        		// Rotate along the hinge
        		transformStack().rotateInDegrees(m_foot_angle);
        		
				transformStack().translate(-LEG_LENGTH/2 + LEG_WIDTH/2, 0.0);
                
                transformStack().scale(LEG_LENGTH, LEG_WIDTH);
        			 
        		// Draw the square for the foot
        		m_gl_state.setColor(0.0, 0.0, 0.3);
	    		m_unit_square.draw();
            
             // Retrieve the previous state of the transformation stack	
        	transformStack().popMatrix();	
        	
        	//draw the ankle joint 
        	transformStack().translate(0.0, -LEG_LENGTH/2 + 10.0f);
			transformStack().scale(5.0f, 5.0f);
        	m_gl_state.setColor(0.0, 1.0, 1.0);	
        	m_unit_circle.draw();
            	
        // Retrieve the previous state of the transformation stack    	 
		transformStack().popMatrix();
         
        //draw the joint of a body and a right leg          
   		transformStack().pushMatrix();
   
   			transformStack().translate(BODY_WIDTH/2, -BODY_LENGTH/2 - LEG_LENGTH/4);
   			transformStack().translate(0.0, LEG_LENGTH/2 - LEG_WIDTH/2);
			transformStack().scale(5.0f, 5.0f);
        	m_gl_state.setColor(0.0, 1.0, 1.0);	
        	m_unit_circle.draw();
   
    	transformStack().popMatrix();
   
    	
    	
    	//Draw left leg
    	transformStack().pushMatrix();
    		// Move the leg to the joint hinge
        	transformStack().translate(-BODY_WIDTH/2, -BODY_LENGTH/2 + LEG_WIDTH/2);
        	
        	// Rotate along the hinge
        	transformStack().rotateInDegrees(m_leg_angle);
			
			// Move to center location of the leg
			transformStack().translate(0.0, -LEG_LENGTH/2 + LEG_WIDTH/2);
			
			transformStack().pushMatrix();
				transformStack().pushMatrix();
			
					transformStack().scale(LEG_WIDTH, LEG_LENGTH);
                	//transformStack().translate(0.0, -0.5);
                	         
        			// Draw the square for the leg
        			m_gl_state.setColor(0.0, 0.0, 0.3);
	    			m_unit_square.draw();
        	
                // Retrieve the previous state of the transformation stack
            	transformStack().popMatrix();
            	
            	transformStack().translate(0.0, -LEG_LENGTH/2 + LEG_WIDTH/2);
			
        		// Rotate along the hinge
        		transformStack().rotateInDegrees(m_foot_angle);
        		
				transformStack().translate(-LEG_LENGTH/2 + LEG_WIDTH/2, 0.0);
                
                transformStack().scale(LEG_LENGTH, LEG_WIDTH);
        			 
        		// Draw the square for the foot
        		m_gl_state.setColor(0.0, 0.0, 0.3);
	    		m_unit_square.draw();
            
             // Retrieve the previous state of the transformation stack	
        	transformStack().popMatrix();	
        	
        	//draw the ankle joint 
        	transformStack().translate(0.0, -LEG_LENGTH/2 + 10.0f);
			transformStack().scale(5.0f, 5.0f);
        	m_gl_state.setColor(0.0, 1.0, 1.0);	
        	m_unit_circle.draw();
            	
        // Retrieve the previous state of the transformation stack    	 
		transformStack().popMatrix();
        
        //draw the joint ob a body and a left leg         
   		transformStack().pushMatrix();
   
   			transformStack().translate(-BODY_WIDTH/2, -BODY_LENGTH/2 - LEG_LENGTH/4);
   			transformStack().translate(0.0, LEG_LENGTH/2 - LEG_WIDTH/2);
			transformStack().scale(5.0f, 5.0f);
        	m_gl_state.setColor(0.0, 1.0, 1.0);	
        	m_unit_circle.draw();
   
    	transformStack().popMatrix();
    	
    	
    	
        // Draw the 'arm'
      	transformStack().pushMatrix();
        	// Move the arm to the joint hinge
        	transformStack().translate(0.0, BODY_LENGTH/2  - 10.0f);

       		 // Rotate along the hinge
        	transformStack().rotateInDegrees(m_joint_angle);
			
			// Move to center location of arm, under previous rotation
			transformStack().translate(0.0, -ARM_LENGTH/2 + 10.0f);
			
			transformStack().pushMatrix();
        		// Scale the size of the arm
        		transformStack().scale(ARM_WIDTH, ARM_LENGTH);

        		// Move to center location of arm, under previous rotation
        		//transformStack().translate(0.0, -0.5);

        		// Draw the square for the arm
        		m_gl_state.setColor(0.0, 0.0, 0.3);
        		m_tetragon.draw();

		 	transformStack().popMatrix();
		transformStack().popMatrix(); 
		
		//draw the arm joint
		transformStack().translate(0.0, BODY_LENGTH/2 - 10.0f);
		transformStack().scale(5.0f, 5.0f);
        m_gl_state.setColor(0.0, 1.0, 1.0);	
        m_unit_circle.draw();
        //transformStack().popMatrix();

    // Retrieve the previous state of the transformation stack
    transformStack().popMatrix();
	 
	//============================ADDED============================ 
	 
    // Execute any GL functions that are in the queue just to be safe
    glFlush();
    checkForGLErrors();
}
示例#13
0
RenderTarget::RenderTarget(const glm::ivec2& size, GLenum format, int flags, GLenum filter) throw()
	: TextureBase()
	, id(0)
	, front(0)
	, back(0)
	, max(1) {

	checkForGLErrors("RenderTarget() pre");
	this->size = size;

	/* init_vbo is a no-op if it already is initialized */
	init_vbo();

	/* generate projection matrix for this target */
	projection = glm::ortho(0.0f, (float)size.x, 0.0f, (float)size.y, -1.0f, 1.0f);
	projection = glm::scale(projection, glm::vec3(1.0f, -1.0f, 1.0f));
	projection = glm::translate(projection, glm::vec3(0.0f, -(float)size.y, 0.0f));

	glGenFramebuffers(1, &id);
	glGenTextures(2, color);
	glGenTextures(1, &depth);

	glBindFramebuffer(GL_FRAMEBUFFER, id);
	Engine::setup_opengl();

	/* bind color buffers */
	for ( int i = 0; i < 2; i++ ){
		glBindTexture(GL_TEXTURE_2D, color[i]);
		glTexImage2D(GL_TEXTURE_2D, 0, format, size.x, size.y, 0, format == GL_RGB8 ? GL_RGB : GL_RGBA, GL_UNSIGNED_INT, NULL);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
	}
	glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color[front], 0);
	checkForGLErrors("glFramebufferTexture2D::color");

	/* bind depth buffer */
	if ( flags & DEPTH_BUFFER ){
		glBindTexture(GL_TEXTURE_2D, depth);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, size.x, size.y, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

		//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE );
		//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);

		glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0);
		checkForGLErrors("glFramebufferTexture2D::depth");
	}

	/* enable doublebuffering */
	if ( flags & DOUBLE_BUFFER ){
		max = 2;
	}

	GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if(status != GL_FRAMEBUFFER_COMPLETE){
		switch( status ) {
		case GL_FRAMEBUFFER_UNSUPPORTED_EXT:
			fprintf(stderr, "Framebuffer object format is unsupported by the video hardware. (GL_FRAMEBUFFER_UNSUPPORTED_EXT)\n");
			break;
		case GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT:
			fprintf(stderr, "Framebuffer incomplete attachment. (GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT_EXT)\n");
			break;
		case GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT:
			fprintf(stderr, "Framebuffer incomplete missing attachment. (GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT_EXT)\n");
			break;
		case GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT:
			fprintf(stderr, "Framebuffer incomplete dimensions. (GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS_EXT)\n");
			break;
		case GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT:
			fprintf(stderr, "Framebuffer incomplete formats. (GL_FRAMEBUFFER_INCOMPLETE_FORMATS_EXT)\n");
			break;
		case GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT:
			fprintf(stderr, "Framebuffer incomplete draw buffer. (GL_FRAMEBUFFER_INCOMPLETE_DRAW_BUFFER_EXT)\n");
			break;
		case GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT:
			fprintf(stderr, "Framebuffer incomplete read buffer. (GL_FRAMEBUFFER_INCOMPLETE_READ_BUFFER_EXT)\n");
			break;
		case GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT:
			fprintf(stderr, "Framebuffer incomplete multisample buffer. (GL_FRAMEBUFFER_INCOMPLETE_MULTISAMPLE_EXT)\n");
			break;
		default:
			fprintf(stderr, "Framebuffer incomplete: %s\n", gluErrorString(status));
		}

		util_abort();
	}

	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	checkForGLErrors("RenderTarget() fin");

	with([this](){
		RenderTarget::clear(Color::black);
	} );
}