Example #1
0
int SHOW_MODULE_INFO(const char *filename, const char *fmtstr)
{
	int fp;
	struct obj_file *f;

	/* Locate the file to be loaded.  */
	if (!strchr(filename, '/') && !strchr(filename, '.')) {
		char *tmp = search_module_path(filename);
		if (tmp == NULL) {
			error("%s: no module by that name found", filename);
			return 1;
		}
		filename = tmp;
	}
	error_file = filename;

	/* Attempt to open and parse the module file. */
	if ((fp = gzf_open(filename, O_RDONLY)) < 0) {
		error("%s: %m", filename);
		return -1;
	} else if ((f = obj_load(fp, ET_REL, filename)) == NULL)
		return -1;
	gzf_close(fp);

	format_query_string(f, fmtstr);
	return 0;
}
Example #2
0
Model *obj_load_model(char *filename)
{
    Model *model;
    Obj *objFile;
    model = model_get_by_filename(filename);
    if (model)
    {
        return model;
    }
    objFile = obj_load(filename);
    if (!objFile)
    {
        return NULL;
    }

    model = model_new();
    if (!model)
    {
        return NULL;
    }

    if (obj_file_convert_to_model(objFile,model) != 0)
    {
        model_free(model);
        return NULL;
    }

    return model;
}
Example #3
0
Entity *newCube(Vec3D position,const char *name)
{
    Entity * ent;
    char buffer[255];
    int i;
    ent = entity_new();
    if (!ent)
    {
        return NULL;
    }
    /*for (i = 0; i < 24;i++)
    {
        //sprintf(buffer,"models/robot/walk_bot_%06i.obj",i + 1);
        ent->objAnimation[i] = obj_load(buffer);
    }*/
    ent->objModel = obj_load("models/cube.obj");//ent->objAnimation[0];
	if( !ent->objModel )
		slog( "test" );
    ent->texture = LoadSprite("models/cube_text.png",1024,1024); //LoadSprite("models/robot/robot.png",1024,1024);
    vec3d_cpy(ent->body.position,position);
	vec3d_set(ent->scale,1,1,1);
	vec3d_set(ent->rotation,0,0,0);
	vec4d_set(ent->color,1,1,1,1);
    cube_set(ent->body.bounds,-1,-1,-1,2,2,2);
    ent->rotation.x = 90;
    sprintf(ent->name,"%s",name);
    ent->think = think;
    ent->state = 0;
    mgl_callback_set(&ent->body.touch,touch_callback,ent);
    return ent;
}
Example #4
0
Entity *newCube(Vec3D position,const char *name)
{
    Entity * ent;
    ent = entity_new();
    if (!ent)
    {
        return NULL;
    }
    ent->objModel = obj_load("models/cube.obj");
    ent->texture = LoadSprite("models/cube_text.png",1024,1024);
    vec3d_cpy(ent->body.position,position);
    cube_set(ent->body.bounds,-1,-1,-1,2,2,2);
    sprintf(ent->name,"%s",name);
    mgl_callback_set(&ent->body.touch,touch_callback,ent);
	ent->body.id = -3;
    return ent;
}
Example #5
0
int convertfile(const char *infile, const char *outfile)
{
	FILE *infp;
	Geometry *new_geometry;

	infp = fopen(infile, "rb");
	if (!infp) {
		fprintf(stderr, "error opening input file '%s'\n", infile);
		return -1;
	}

	// try to guess the file type based on input name
	const char *c = strrchr(infile, '.');
	if (c) {
		if (!strcmp(c, ".ac")) {
			std::cout << "loading meshes from AC3d file '" << infile << "'" << std::endl;
			ac3d_load(infp);
		} else if (!strcmp(c, ".obj")) {
			std::cout << "loading meshes from object file '" << infile << "'" << std::endl;
			obj_load(infp, &new_geometry);
		} else {
			std::cerr << "unrecognized input mesh" << std::endl;
			fclose(infp);
			return -1;
		}
	} else {
		std::cerr << "unrecognized input mesh" << std::endl;
		fclose(infp);
		return -1;
	}

	fclose(infp);

	// do some processing

	// dump the output
	dump_geometry_tree(new_geometry, outfile);

	// dump an xml file describing it
	dump_geometry_xml(new_geometry, outfile);

	return 0;
}
Example #6
0
static mesh_s *make_player_mesh()
{
    mesh_s *mesh = mesh_new();

    // shader
    GLuint shaders[2];
    shaders[0] = graphics_create_shader_from_file(
                     GL_VERTEX_SHADER, "ship.v.glsl");
    shaders[1] = graphics_create_shader_from_file(
                     GL_FRAGMENT_SHADER, "ship.f.glsl");
    mesh_set_program(mesh, graphics_create_program(2, shaders));

    // geometry
    obj_s *obj = obj_load("ship.obj");
    mesh_set_attribute(mesh, "position", 3,
                       obj_get_vertex_count(obj) * 3 * sizeof(float),
                       obj_get_vertices(obj));
    mesh_set_elements(mesh,
                      obj_get_triangle_count(obj) * 3 * sizeof(unsigned),
                      obj_get_triangles(obj));
    obj_release(obj);

    return mesh;
}
Example #7
0
void createLevel(int sc){

	int i, j, c;
	//int iSect[5][5];
	int randomNum;
	Entity *testEn;
	char enText[] = "models/red_piece.png";
	char shipText[] = "models/yell_piece.png";
	char powText[] = "models/green_piece.png";
	char blText[] = "models/orange_piece.png";
	char enObj[] = "models/cube.obj";
	
	//memcpy(iSect, *lvlSect, sizeof(lvlSect));
	testEn = newScorer(vec3d(100, cameraPosition.y + 60, 100),sc);
	for (i = 0; i < 5; i++){ //Y-Axis
		for (j = 0; j < 5; j++){ //X-Axis
			c = lvlSect[i][j];
			switch (c){
				case 0:
					//nothing
					//testEn = newship(vec3d((worldWidth / 2) * ((float)j - 2), cameraPosition.y + 60, (worldHeight / 2) * ((float)i - 2)), "ship", obj_load("models/cube.obj"), LoadSprite(shipText, 1024, 1024));
					break;
				case 1:
					//ships
					testEn = newship(vec3d(worldWidth/2 * (j - 2), cameraPosition.y + 60, worldHeight/2 * (i - 2)), "ship", obj_load(enObj), LoadSprite(shipText, 1024, 1024));
					break;
				case 2:
					//powerups
					switch (rand() % 3){
						case 1:
							testEn = newPower(vec3d(worldWidth / 2 * (j - 2), cameraPosition.y + 60, worldHeight / 2 * (i - 2)), "power", obj_load(enObj), LoadSprite(powText, 1024, 1024), P_MINI);
							break;
						case 2:
							testEn = newPower(vec3d(worldWidth / 2 * (j - 2), cameraPosition.y + 60, worldHeight / 2 * (i - 2)), "power", obj_load(enObj), LoadSprite(powText, 1024, 1024), P_BOMB);
							break;
						default:
							testEn = newPower(vec3d(worldWidth / 2 * (j - 2), cameraPosition.y + 60, worldHeight / 2 * (i - 2)), "power", obj_load(enObj), LoadSprite(powText, 1024, 1024), P_INVERT);
							break;
					}
					break;
				case 3:
					//walls
					switch (rand() % 4){
						case 0:
							testEn = newWall(vec3d(worldWidth / 2 * (j - 2), cameraPosition.y + 60, worldHeight / 2 * (i - 2)), "wall", obj_load(enObj), LoadSprite(enText, 1024, 1024), W_STRAIGHT, 0);
							break;
						case 1:
							testEn = newWall(vec3d(worldWidth / 2 * (j - 2), cameraPosition.y + 60, worldHeight / 2 * (i - 2)), "wall", obj_load(enObj), LoadSprite(enText, 1024, 1024), W_HORZ, rand() % 2);
							break;
						case 2:
							testEn = newWall(vec3d(worldWidth / 2 * (j - 2), cameraPosition.y + 60, worldHeight / 2 * (i - 2)), "wall", obj_load(enObj), LoadSprite(enText, 1024, 1024), W_ZIG, rand() % 4);
							break;
						default:
							testEn = newWall(vec3d(worldWidth / 2 * (j - 2), cameraPosition.y + 60, worldHeight / 2 * (i - 2)), "wall", obj_load(enObj), LoadSprite(enText, 1024, 1024), W_VERT, rand() % 2);
							break;
					}
					break;
			}
		}
	}

}
Example #8
0
/**
 * 2. Determinación de superficies ocultas por medio de Z-Buffer y optimización por Backface culling.
 */
int main(int argc, char* argv[])
{
	// Crear una ventana de 750x750 pixels.
	int cw = 750;
	int ch = 750;

	cg_init(cw, ch, NULL);

	// Actualizar la pantalla:
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glMatrixMode(GL_PROJECTION);
	glViewport(0,0,cw, ch);
	glFrustum(-1,1,-1,1,1,1000);

	Obj* obj = obj_load("../Models/knight.obj");

	float ang = 0.0f;
	float pitch = 0.0f;
	float ang_vel = 1.0f;

	char done = 0;
	char wireframe = 0;
	char bfc = 0;
	glEnable(GL_DEPTH_TEST);
	char zbuff = 1;
	unsigned char key_pressed[1024];
	memset(key_pressed, 0, 1024);

	while (!done)
	{
		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
			switch (event.type)
			{
				case SDL_KEYDOWN:
					key_pressed[event.key.keysym.sym] = 1;

					if (event.key.keysym.sym == SDLK_z)
					{
						zbuff = !zbuff;
						if(zbuff)
							glEnable(GL_DEPTH_TEST);
						else
							glDisable(GL_DEPTH_TEST);
						break;
					}
					else if (event.key.keysym.sym == SDLK_b)
					{
						bfc = !bfc;
						if(bfc)
						{
							glEnable(GL_CULL_FACE);
							glCullFace(GL_BACK);
							glFrontFace(GL_CW);
						}
						else
							glDisable(GL_CULL_FACE);
						break;
					}
					else if (event.key.keysym.sym == SDLK_m)
					{
						wireframe = !wireframe;
						if(wireframe)
							glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
						else
							glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
						break;
					}
					else if (event.key.keysym.sym != SDLK_ESCAPE)
						break;
				case SDL_QUIT :
					done = 1;
					break;
				case SDL_KEYUP:
					key_pressed[event.key.keysym.sym] = 0;

			}
		}

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0.0f, 0.0f, -50.0f);
		glRotatef(pitch, 1.0f, 0.0f, 0.0f);
		glRotatef(ang, 0.0f, 1.0f, 0.0f);
		glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);

		if(key_pressed[SDLK_RIGHT]) ang += ang_vel;
		if(key_pressed[SDLK_LEFT]) ang -= ang_vel;
		if(key_pressed[SDLK_UP]) pitch += ang_vel;
		if(key_pressed[SDLK_DOWN]) pitch -= ang_vel;

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		obj_render(obj);
		cg_repaint();
	}

	// Liberar recursos:
	obj_free(obj);
	cg_close();

	return 0;
}
Example #9
0
int main(int argc, char *argv[])
{
    int i;
    float r = 0;
    Space *space;
    Entity *cube1,*cube2;
    char bGameLoopRunning = 1;
    Vec3D cameraPosition = {-10,3,0};
    Vec3D cameraRotation = {180,0,180};
    SDL_Event e;
    Obj *bgobj;
    Sprite *bgtext;

	//my variables
	Ship *playerShip;
	int specMode;
    
    init_logger("gametest3d.log");
    if (graphics3d_init(1024,768,1,"gametest3d",33) != 0)
    {
        return -1;
    }
    model_init();
    obj_init();
    entity_init(255);
	initShips();
    
    bgobj = obj_load("models/mountainvillage.obj");
    bgtext = LoadSprite("models/mountain_text.png",1024,1024);
    
    //cube1 = newCube(vec3d(0,0,0),"Cubert");
    //cube2 = newCube(vec3d(10,0,0),"Hobbes");
    
    //cube1->body.velocity.x = 0.1;
    
    space = space_new();
    space_set_steps(space,100);
    
    space_add_body(space,&cube1->body);
    space_add_body(space,&cube2->body);

	//my variables
	specMode = 0;
	numShips = 0;
	shipVel = 0;
	shipRot = 0;
	turretRot = 0;
	gunElev = 0;
	realTurrRot = 0;

	playerShip = spawnShip(space, vec3d(-10,0,0), 1);

    while (bGameLoopRunning)
    {
		updateAllShipPos();
		for (i = 0; i < 100;i++)
        {
			space_do_step(space);
        }
		updateAllShipComp();
		applyGrav();
		if(specMode == 0)
		{
			cameraPosition.x = playerShip->hull->body.position.x;
			cameraPosition.y = (playerShip->hull->body.position.y + 5);
			cameraPosition.z = playerShip->hull->body.position.z;
		}

        while ( SDL_PollEvent(&e) ) 
        {
            if (e.type == SDL_QUIT)
            {
                bGameLoopRunning = 0;
            }
            else if (e.type == SDL_KEYDOWN)
            {
                if (e.key.keysym.sym == SDLK_ESCAPE)
                {
                    bGameLoopRunning = 0;
                }
                else if (e.key.keysym.sym == SDLK_SPACE)
                {
                    cameraPosition.z++;
                }
                else if (e.key.keysym.sym == SDLK_z)
                {
                    cameraPosition.z--;
                }
                else if (e.key.keysym.sym == SDLK_w)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            -sin(cameraRotation.z * DEGTORAD),
                            cos(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
                else if (e.key.keysym.sym == SDLK_s)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            sin(cameraRotation.z * DEGTORAD),
                            -cos(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
                else if (e.key.keysym.sym == SDLK_d)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            cos(cameraRotation.z * DEGTORAD),
                            sin(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
                else if (e.key.keysym.sym == SDLK_a)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            -cos(cameraRotation.z * DEGTORAD),
                            -sin(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
                else if (e.key.keysym.sym == SDLK_LEFT)
                {
                    cameraRotation.y -= 1;
                }
                else if (e.key.keysym.sym == SDLK_RIGHT)
                {
                    cameraRotation.y += 1;
                }
                else if (e.key.keysym.sym == SDLK_UP)
                {
                    cameraRotation.x += 1;
                }
                else if (e.key.keysym.sym == SDLK_DOWN)
                {
                    cameraRotation.x -= 1;
                }
				

				
				else if (e.key.keysym.sym == SDLK_y && shipVel < 0.4)
                {
                    shipVel += 0.1;
                }
				else if (e.key.keysym.sym == SDLK_h && shipVel > 0.05)
                {
                    shipVel -= 0.1;
                }
				else if (e.key.keysym.sym == SDLK_j)
                {
                    shipRot += 0.5;
					if(shipRot >= 360){shipRot -= 360;}
                }
				else if (e.key.keysym.sym == SDLK_g)
                {
                    shipRot -= 0.5;
					if(shipRot < 0){shipRot += 360;}
                }
				else if (e.key.keysym.sym == SDLK_m && turretRot < 135)
                {
                    turretRot += 1;
                }
				else if (e.key.keysym.sym == SDLK_b && turretRot > -135)
                {
                    turretRot -= 1;
                }
				else if (e.key.keysym.sym == SDLK_o && gunElev < 50)
                {
                    gunElev += 0.5;
                }
				else if (e.key.keysym.sym == SDLK_l && gunElev > -5)
                {
                    gunElev -= 0.5;
                }
				else if (e.key.keysym.sym == SDLK_q)
                {
					if(specMode == 0){specMode = 1;}
					else {specMode = 0;}
                }
				else if (e.key.keysym.sym == SDLK_p)
                {
					fireBullet(space, playerShip->gun->body.position, realTurrRot, gunElev, 0.5, -1);
				}
				else if (e.key.keysym.sym == SDLK_x)
                {
					fireBullet(space, playerShip->hull->body.position, shipRot, 0, 0, -2);
				}
				else if (e.key.keysym.sym == SDLK_1)
                {
					startLevel(space, 1);
				}
				else if (e.key.keysym.sym == SDLK_2)
                {
					startLevel(space, 2);
				}
				else if (e.key.keysym.sym == SDLK_3)
                {
					startLevel(space, 3);
				}
            }
        }
		 
        graphics3d_frame_begin();
        
        glPushMatrix();
        set_camera(
            cameraPosition,
            cameraRotation);
        
        entity_draw_all();
		//updateAllShipModels();
        obj_draw(
            bgobj,
            vec3d(0,0,2),
            vec3d(90,90,0),
            vec3d(5,5,5),
            vec4d(1,1,1,1),
            bgtext
        );
        
        if (r > 360)r -= 360;
        glPopMatrix();
        /* drawing code above here! */
        graphics3d_next_frame();
    } 
    return 0;
}
Example #10
0
int main(int argc, char* argv[])
{
    int cw = 750;
    int ch = 750;

    cg_init(cw, ch, NULL);

    glClearColor(0,0,1,1);
    // Actualizar la pantalla:
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glMatrixMode(GL_PROJECTION);
    glViewport(0,0,cw, ch);
    glFrustum(-1,1,-1,1,1,1000);

    glEnable(GL_LIGHTING);

    // glEnable(GL_NORMALIZE);
    float l0[] = {1.0f,1.0f,1.0f,1.0f};
    float la[] = {0.10f,0.10f,0.10f,1.0f};
    float l0p[]= {1.0f,1.0f,1.0f,1.0f};
    float ls[] = {1.0f,1.0f,1.0f,1.0f};
    glLightfv(GL_LIGHT0, GL_AMBIENT, la);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, l0);
    glLightfv(GL_LIGHT0, GL_POSITION, l0p);
    glLightfv(GL_LIGHT0, GL_SPECULAR, ls);
    glEnable(GL_LIGHT0);

    float cyan[] = {1.0f, 1.0f, 1.0f, 1.f};
    glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
    glMaterialfv(GL_FRONT, GL_SPECULAR, ls);
    glMateriali(GL_FRONT, GL_SHININESS, 16);

    float ang = 0.0f;
    float pitch = 0.0f;
    float ang_vel = 1.0f;


    char done = 0;
    char wireframe = 0;
    char bfc = 0;
    glEnable(GL_DEPTH_TEST);
    char zbuff = 1;
    unsigned char key_pressed[1024];
    memset(key_pressed, 0, 1024);

    char use_shader = 0;
    char specular = 0;
    Shader gouraud = shader_new("shaders/gouraud_vp.glsl", "shaders/gouraud_fp.glsl");
    GLint uniform_especular = shader_get_unif_loc(gouraud, "especular");
    GLint uniform_tex = shader_get_unif_loc(gouraud, "tex");

    Obj* objPtr ;
    int example = atoi(argv[1]);

    if (example==1 || example==3)
        objPtr = obj_load("../Models/knight_texturas.obj");
    else if (example==2)
        objPtr = obj_load("../Models/box_texturas.obj");

    //Cargo la imagen de disco usando SDL_image
    SDL_Surface* surface; // = IMG_Load("../Models/knight.png");

    if (example==1 )
        surface = IMG_Load("../Models/knight.png");
    else if (example==2)
        surface = IMG_Load("../Models/box.jpg");
    else if (example==3)
        surface = IMG_Load("../Models/knight_good.png");


//    Obj* objPtr = obj_load("../Models/knight_texturas.obj");
//    Obj* objPtr = obj_load("../Models/box_texturas.obj");

    //Cargo la imagen de disco usando SDL_image
//    SDL_Surface* surface = IMG_Load("../Models/knight.png");
//    SDL_Surface* surface = IMG_Load("../Models/knight_good.png");
//    SDL_Surface* surface = IMG_Load("../Models/box.jpg");

    if (surface==NULL) { //Si falla la carga de la imagen, despliego el mensaje de error correspondiente y termino el programa.
        printf("Error: \"%s\"\n", SDL_GetError());
        return 1;
    }

    GLuint texture;
    //Creo un espacio para alojar una textura en memoria de video
    glGenTextures(1,&texture);
    //Activo la textura nro 0
    glActiveTexture(GL_TEXTURE0);
    //Habilito la carga para la textura recien creada
    glBindTexture(GL_TEXTURE_2D,texture);

    //Cargo los datos de la imagen en la textura.
    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGBA,
                 surface->w,
                 surface->h,
                 0,
                 GL_RGB,GL_UNSIGNED_BYTE,
                 surface->pixels);
    //Luego de copiada la imagen a memoria de video, puedo liberarla sin problemas
    SDL_FreeSurface(surface);

    //Seteo el filtro a usar cuando se mapea la textura a una superficie mas chica (GL_LINEAR = filtro bilineal)
    glTexParameteri(GL_TEXTURE_2D,
                    GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR);
    //Seteo el filtro a usar cuando se mapea la textura a una superficie mas grande (GL_LINEAR = filtro bilineal)
    glTexParameteri(GL_TEXTURE_2D,
                    GL_TEXTURE_MAG_FILTER,
                    GL_LINEAR);

    //Seteo el comportamiento cuando encuentro coordenadas de textura fuera del rango [0,1]
    //GL_REPEAT es el comportamiento por defecto.
    glTexParameteri(GL_TEXTURE_2D,
                    GL_TEXTURE_WRAP_S,
                    GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D,
                    GL_TEXTURE_WRAP_T,
                    GL_REPEAT);

    while (!done)
    {

        SDL_Event event;
        while(SDL_PollEvent(&event))
        {
            switch (event.type)
            {
            case SDL_KEYDOWN:
                key_pressed[event.key.keysym.sym] = 1;
                if (event.key.keysym.sym == SDLK_p)
                {
                    use_shader = !use_shader;
                    break;
                }
                else if (event.key.keysym.sym == SDLK_s)
                {
                    specular = !specular;
                    break;
                }
                else if (event.key.keysym.sym == SDLK_z)
                {
                    zbuff = !zbuff;
                    if(zbuff)
                        glEnable(GL_DEPTH_TEST);
                    else
                        glDisable(GL_DEPTH_TEST);
                    break;
                }
                else if (event.key.keysym.sym == SDLK_m)
                {
                    wireframe = !wireframe;
                    if(wireframe)
                        glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
                    else
                        glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
                    break;
                }
                else if (event.key.keysym.sym == SDLK_b)
                {
                    bfc = !bfc;
                    if(bfc)
                    {
                        glEnable(GL_CULL_FACE);
                        glCullFace(GL_BACK);
                        glFrontFace(GL_CW);
                    }
                    else
                        glDisable(GL_CULL_FACE);
                    break;
                }
                else if (event.key.keysym.sym != SDLK_ESCAPE)
                    break;
            case SDL_QUIT :
                done = 1;
                break;
            case SDL_KEYUP:
                key_pressed[event.key.keysym.sym] = 0;

            }
        }

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        if (example==1 || example==3)
            glTranslatef(0.0f, 0.0f, -50.0f);
        else if (example==2)
            glTranslatef(0.0f, 0.0f, -2.0f);

//        glTranslatef(0.0f, 0.0f, -50.0f);
//        glTranslatef(0.0f, 0.0f, -2.0f);

        glRotatef(pitch, 1.0f, 0.0f, 0.0f);
        glRotatef(ang, 0.0f, 1.0f, 0.0f);

        glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);

        if(key_pressed[SDLK_RIGHT]) ang += ang_vel;
        if(key_pressed[SDLK_LEFT]) ang -= ang_vel;
        if(key_pressed[SDLK_UP]) pitch += ang_vel;
        if(key_pressed[SDLK_DOWN]) pitch -= ang_vel;

        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

        //Informo a OpenGL que para todas las operaciones a continuaci�n utilice las texturas 2D cargadas
        glEnable(GL_TEXTURE_2D);

        if(use_shader)
        {
            shader_use(gouraud);
            glUniform1i(uniform_especular, specular);
            //Le digo al shader que el sampler2D de nombre "tex" se corresponde con GL_TEXTURE0
            //que es donde cargu� mi textura.
            glUniform1i(uniform_tex, 0);
            //Luego asocio la textura con el id "texture"
            glBindTexture(GL_TEXTURE_2D,texture);
            obj_render2(objPtr);
            shader_stop(gouraud);
        }
        else
        {
            glBindTexture(GL_TEXTURE_2D,texture);
            obj_render2(objPtr);
        }

        cg_repaint();
    } // while

    // Liberar recursos:
    obj_free(objPtr);
    shader_free(gouraud);
    glDeleteTextures(1,&texture);
    cg_close();

    return 0;
}
Example #11
0
int KALLSYMS_MAIN (int argc, char **argv)
{
    struct option long_opts[] = {
	    {"version", 0, 0, 'V'},
	    {"help", 0, 0, 'h'},
	    {0, 0, 0, 0}
    };
    char *filename = NULL;
    int fp;
    struct obj_file *fin, *fout;
    int i, c;

    error_file = "kallsyms";

    /* To handle repeated calls from combined modprobe */
    errors = optind = 0;

    /* Process the command line.  */
    while ((c = getopt_long(argc, argv, "Vh",
			    &long_opts[0], NULL)) != EOF)
	switch (c) {
	case 'V':
	    fputs("kallsyms version " MODUTILS_VERSION "\n", stderr);
	    return(0);
	case 'h':       /* Print the usage message. */
	    kallsyms_usage();
	    return(0);
	default:
	    kallsyms_usage();
	    return(1);
	}

    if (optind != argc-1) {
	kallsyms_usage();
	return(1);
    }

    filename = argv[optind++];
    error_file = filename;
    if ((fp = gzf_open(filename, O_RDONLY)) < 0) {
	error("%s: %m", filename);
	return 1;
    }

    if ((fin = obj_load(fp, ET_EXEC, filename)) == NULL) {
	gzf_close(fp);
	return 1;
    }
    gzf_close(fp);

    error_file = "kallsyms";
    if (obj_kallsyms(fin, &fout))
	return 1;

    /* Write the extracted data */
    fwrite(&fout->header, sizeof(fout->header), 1, stdout);
    for (i = 0; i < fout->header.e_shnum; ++i)
	fwrite(&fout->sections[i]->header, fout->header.e_shentsize, 1, stdout);
    for (i = 0; i < fout->header.e_shnum; ++i) {
	if (fout->sections[i]->header.sh_size) {
	    fseek(stdout, fout->sections[i]->header.sh_offset, SEEK_SET);
	    fwrite(fout->sections[i]->contents,
	    	fout->sections[i]->header.sh_size, 1, stdout);
	}
    }

    return 0;
}
Example #12
0
int main(int argc, char *argv[])
{
    int i;
    float r = 0;
	int f = -15;
	int g = 0;
	int l = 0;
	int y = 0;
	int u = 15;
	int q = 0;
	int gamestate=0;
	int PlayerHP=15, Bosshealth=30, enemy1health=5, enemy2health=5, Bigtimer=600, CD;
	int SpawnLemon1=0, SpawnLemon2=0, SpawnLemon3=0, SpawnBigLemon=0, enemybullet=0;
	float Lemonx1=0, Lemonx2=0, Lemonx3=0, LemonxBig=0, enemybulletx=0;
	int Lemony1=0, Lemony2=0, Lemony3=0, LemonyBig=0, enemybullety=0;
	int a=0, b=0, c=0, d=0, count, Goomba_attack1=0, Goomba_attack2=0, Goomba_move1, Goomba_move2;
    Space *space;
    Entity *cube1,*cube2, *Actor;
    char bGameLoopRunning = 1;
	Vec3D Bosspos = {u, y, 2};
	Vec3D enemy1pos = {5, 10, 2};
	Vec3D enemy2pos = {5, -10, 2};
    Vec3D cameraPosition = {0,0,70.3};
    Vec3D cameraRotation = {0,0,360};
    SDL_Event e;
    Obj *megaman,*boss, *mapp, *mape, *lemon, *BossHPBar, *PlayerHPBar, *Goomba;
    Sprite *megamantexture, *bosstexture, *maptexture1, *maptexture2, *lemontexture, *BosstextureHP, *PlayertextureHP, *Goombatexture;
    
    init_logger("gametest3d.log");
    if (graphics3d_init(1024,768,1,"gametest3d",33) != 0)
    {
        return -1;
    }
    model_init();
    obj_init();
    entity_init(255);
    
   //load objects, models here, replace cube with megaman
    megaman = obj_load("models/MPU.obj");
	//load his uv map "texture file"
    megamantexture = LoadSprite("models/MegaManBody1.png",1024,1024);

	PlayerHPBar = obj_load("models/cube.obj");
	PlayertextureHP = NULL;


	boss = obj_load("models/Fireman.obj");
	bosstexture = LoadSprite("models/FireManBody1.png",1024,1024);

	BossHPBar = obj_load("models/cube.obj");
	BosstextureHP = NULL;

	Goomba = obj_load("models/goomba.obj");
	Goombatexture = LoadSprite("models/goomba_tex.png",1024,1024);

    mapp = obj_load("models/MidtermMapSingle.obj");
	maptexture1 = LoadSprite("models/PlayerTile(Red).png",1024,1024);

	mape = obj_load("models/MidtermMapSingle.obj");
	maptexture2 = LoadSprite("models/BossTile(Blue).png",1024,1024);
    
    lemon = obj_load("models/cube.obj");
	lemontexture = NULL;//LoadSprite("models/cube_text.png",1024,1024);
    
	    
    while (bGameLoopRunning)
    {
        entity_think_all();

        while ( SDL_PollEvent(&e) ) 
        {
            if (e.type == SDL_QUIT)
            {
                bGameLoopRunning = 0;
            }
            else if (e.type == SDL_KEYDOWN)
            {
                if (e.key.keysym.sym == SDLK_ESCAPE)
                {
                    bGameLoopRunning = 0;
                }
                else if (e.key.keysym.sym == SDLK_x)
                {
                    cameraPosition.z++;
                }
                else if (e.key.keysym.sym == SDLK_z)
                {
                    cameraPosition.z--;
                }
                else if (e.key.keysym.sym == SDLK_h)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            -sin(cameraRotation.z * DEGTORAD),
                            cos(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
                else if (e.key.keysym.sym == SDLK_y)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            sin(cameraRotation.z * DEGTORAD),
                            -cos(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
                else if (e.key.keysym.sym == SDLK_j)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            cos(cameraRotation.z * DEGTORAD),
                            sin(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
                else if (e.key.keysym.sym == SDLK_g)
                {
                    vec3d_add(
                        cameraPosition,
                        cameraPosition,
                        vec3d(
                            -cos(cameraRotation.z * DEGTORAD),
                            -sin(cameraRotation.z * DEGTORAD),
                            0
                        ));
                }
				else if (e.key.keysym.sym == SDLK_d)
                {
                   
				   
				   if(f >= -5)
				   {
					   f = -5;
				   }
				   else
				   {
					   f += 10;
				   }
                }
				else if (e.key.keysym.sym == SDLK_a)
                {
                   
				   
				   if(f <= -20)
				   {
					   f = -25;
				   }
				   else
				   {
					   f -= 10;
				   }
				  			
                }
				else if (e.key.keysym.sym == SDLK_w)
                {
                   
				   if(g >= 5)
				   {
					   g = 10;
				   }
				   else
				   {
					   g += 10;
				   }
			
                }
				else if (e.key.keysym.sym == SDLK_s)
                {
                   
				   g -= 10;
				    if(g <= -5)
				   {
					   g = -10;
				   }
			
                }
                else if (e.key.keysym.sym == SDLK_LEFT)
                {
                    cameraRotation.z += 1;
                }
                else if (e.key.keysym.sym == SDLK_RIGHT)
                {
                    cameraRotation.z -= 1;
                }
                else if (e.key.keysym.sym == SDLK_UP)
                {
                    cameraRotation.x += 1;
                }
                else if (e.key.keysym.sym == SDLK_DOWN)
                {
                    cameraRotation.x -= 1;
                }
                else if (e.key.keysym.sym == SDLK_SPACE)
                {
                    
					CD = TIME + Bigtimer;					
					
                }
				else if (e.key.keysym.sym == SDLK_n)
                {
                   
				   if (enemy1health <= 0 && enemy2health <= 0)
				   {
					   gamestate=1;
				   }
			
                }
				else if (e.key.keysym.sym == SDLK_n)
                {
                   
				   if (enemy1health <= 0 && enemy2health <= 0)
				   {
					   gamestate=1;
				   }
			
                }
            }
			else if (e.type == SDL_KEYUP)
			{
				if (e.key.keysym.sym == SDLK_SPACE)
				{
					
					if (TIME >= CD && SpawnBigLemon !=1)
					{
						SpawnBigLemon = 1;
						LemonxBig = f;
						LemonyBig = g;
					}
					else if (SpawnLemon1 != 1)
					{
						SpawnLemon1 = 1;
						Lemonx1 = f;
						Lemony1 = g;
					}
					else if (SpawnLemon2 != 1)
					{
						SpawnLemon2 = 1;
						Lemonx2 = f;
						Lemony2 = g;
					}
					else if (SpawnLemon3 != 1)
					{
						SpawnLemon3 = 1;
						Lemonx3 = f;
						Lemony3 = g;
					}
					
				}
			}
				
        }

                
        graphics3d_frame_begin();
        
        glPushMatrix();
        set_camera(
            cameraPosition,
            cameraRotation);
        
        //entity_draw_all();
      
		if (PlayerHP > 0 )
		{
		//Megaman
        obj_draw(
            megaman,
            vec3d(f, g, 2),
            vec3d(90,90,0),
            vec3d(0.5,0.5,0.5),
            vec4d(1,1,1,1),
            megamantexture
        );

		//Megaman HP BAR
		obj_draw(
            PlayerHPBar,
            vec3d(-30+PlayerHP/(2),20,2),
            vec3d(90,-90,0),
            vec3d(.9,.9,PlayerHP/(2)),
            vec4d(0,1,0,1),
			PlayertextureHP
        );
		}

		//Megaman Projectiles
		//Lemon1
		if (SpawnLemon1 != 0)
		{
			obj_draw(
			lemon,
            vec3d(Lemonx1 + a*(.4), Lemony1, 4),
            vec3d(90,90,0),
            vec3d(1,1,2),
            vec4d(.95,.89,0,1),
		    lemontexture
		);
			a++;
				if (Lemonx1 + a*(.4) >=30)
				{
					SpawnLemon1 = 0;
					a=0;
				}
				else if (Lemonx1 + a*(.4) == enemy1pos.x && Lemony1 == enemy1pos.y)
				{
					SpawnLemon1 = 0;
					a=0;
					enemy1health -= 1;
					//slog("Enemy Health %d",enemy1health);
				}
				else if (Lemonx1 + a*(.4) == enemy2pos.x && Lemony1 == enemy2pos.y)
				{
					SpawnLemon1 = 0;
					a=0;
					enemy2health -= 1;
					//slog("Enemy Health %d",enemy2health);
				}
				else if (gamestate == 1 && Lemonx1 + a*(.4) == Bosspos.x && Lemony1 == Bosspos.y)
				{
				SpawnLemon1 = 0;
				a=0;
				Bosshealth -= 1;
				//slog("Boss Health %d",Bosshealth);
				}
		}
		//Lemon2
		if (SpawnLemon2 != 0)
		{
			obj_draw(
			lemon,
            vec3d(Lemonx2 + b*(.4), Lemony2, 4),
            vec3d(90,90,0),
            vec3d(1,1,2),
            vec4d(.95,.89,0,1),
		    lemontexture
		);
			b++;
				if (Lemonx2 + b*(.4) >=30)
				{
					SpawnLemon2 = 0;
					b=0;
				}
				else if (Lemonx2 + b*(.4) == enemy1pos.x && Lemony2 == enemy1pos.y)
				{
					SpawnLemon2 = 0;
					b=0;
					enemy1health -= 1;
					//slog("Enemy Health %d",enemy1health);
				}
				else if (Lemonx2 + b*(.4) == enemy2pos.x && Lemony2 == enemy2pos.y)
				{
					SpawnLemon2 = 0;
					b=0;
					enemy2health -= 1;
					//slog("Enemy Health %d",enemy2health);
				}
				else if (gamestate == 1 && Lemonx2 + b*(.4) == Bosspos.x && Lemony2 == Bosspos.y)
				{
					SpawnLemon2 = 0;
					b=0;
					Bosshealth -= 1;
					//slog("Boss Health %d",Bosshealth);
				}
		}
		if (SpawnLemon3 != 0)
		{
			obj_draw(
			lemon,
            vec3d(Lemonx3 + c*(.4), Lemony3, 4),
            vec3d(90,90,0),
            vec3d(1,1,2),
            vec4d(.95,.89,0,1),
		    lemontexture
		);
			c++;
				//lemons fly off staege
				if (Lemonx3 + c*(.4) >=30)
				{
					SpawnLemon3 = 0;
					c=0;
				}
				//lemons collide with enemies
				else if (Lemonx3 + c*(.4) == enemy1pos.x && Lemony3 == enemy1pos.y)
				{
					SpawnLemon3 = 0;
					c=0;
					enemy1health -= 1;
					slog("Enemy Health %d",enemy1health);
				}
				else if (Lemonx3 + c*(.4) == enemy2pos.x && Lemony3 == enemy2pos.y)
				{
					SpawnLemon3 = 0;
					c=0;
					enemy2health -= 1;
					slog("Enemy Health %d",enemy2health);
				}
				else if (gamestate == 1 && Lemonx3 + c*(.4) == Bosspos.x && Lemony3 == Bosspos.y)
				{
					SpawnLemon3 = 0;
					c=0;
					Bosshealth -= 1;
					//slog("Boss Health %d",Bosshealth);
				}
		}
		if (SpawnBigLemon != 0)
		{
			obj_draw(
			lemon,
            vec3d(LemonxBig + d*(.4), LemonyBig, 4),
            vec3d(90,90,0),
            vec3d(2,2,4),
            vec4d(.9,.3,.1,1),
		    lemontexture
		);
			d++;
				if (LemonxBig + d*(.4) >=30)
				{
					SpawnBigLemon = 0;
					d=0;
				}
				else if (LemonxBig + d*(.4) == enemy1pos.x && LemonyBig == enemy1pos.y)
				{
					SpawnBigLemon = 0;
					d=0;
					enemy1health -= 5;
					//slog("Enemy Health %d",enemy1health);
				}
				else if (LemonxBig + d*(.4) == enemy2pos.x && LemonyBig == enemy2pos.y)
				{
					SpawnBigLemon = 0;
					d=0;
					enemy2health -= 5;
					//slog("Enemy Health %d",enemy2health);
				}
				else if (gamestate == 1 && LemonxBig + d*(.4) == Bosspos.x && LemonyBig == Bosspos.y)
				{
					SpawnBigLemon = 0;
					d=0;
					Bosshealth -= 5;
					slog("Boss Health %d",Bosshealth);
				}
		}

		
		//MAP
		obj_draw(
            mapp,
            vec3d(-15,0,2),
            vec3d(90,90,0),
            vec3d(5,5,5),
            vec4d(1,1,1,1),
		    maptexture1
		);
		obj_draw(
            mape,
            vec3d(15,0,2),
            vec3d(90,90,0),
            vec3d(5,5,5),
            vec4d(1,1,1,1),
		    maptexture2
		);
		

		//Enemies
		if (gamestate == 0)
		{

			if (enemy1health > 0)
			{
			//enemy_move(enemypos.x,enemypos.y, &enemypos);
			//enemy_attack1(Goomba_attack1, &enemy1pos);

			obj_draw(
            Goomba,
            vec3d(enemy1pos.x,enemy1pos.y,2),
            vec3d(90,-90,0),
            vec3d(0.1,0.1,0.1),
            vec4d(1,1,1,1),
            Goombatexture
			);
						
			if (Goomba_attack1 == 0)
			{
				Goomba_attack1 = rand_ranged( 1, 5); //start to (end - 1)
				if (Goomba_attack1 == 2)
				{
					Goomba_attack1 = 1;
				}
		
			};

			if (Goomba_attack1 == 1)
			{
				enemy1pos.x -=.1;
				if (enemy1pos.x <= -30)
				{
					Goomba_attack1 = 0;
					enemy1pos.x = 5;
				}
			}

			}
			else
			{
				enemy1pos.x = 50;
				enemy1pos.y = 50;
			}

			if (enemy2health > 0)
			{
			//enemy_move(enemypos.x,enemypos.y, &enemypos);
			//enemy_attack2(Goomba_attack2, &enemy2pos);
			obj_draw(
            Goomba,
            vec3d(enemy2pos.x,enemy2pos.y,2),
            vec3d(90,-90,0),
            vec3d(0.1,0.1,0.1),
            vec4d(1,1,1,1),
            Goombatexture
			);

			//bottom Goomba won't attack

			if (Goomba_attack2 == 0)
			{
				Goomba_attack2 = rand_ranged( 1, 5); //start to (end - 1)
				if (Goomba_attack2 == 2)
				{
					Goomba_attack2 = 1;
				}
		
			};

			if (Goomba_attack2 == 1)
			{
				enemy2pos.x -=.1;
				if (enemy2pos.x <= -30)
				{
					Goomba_attack2 = 0;
					enemy2pos.x = 5;
				}
			}

			
			}
			else
			{
				enemy2pos.x = 50;
				enemy2pos.y = 50;
			}
			
		}
		if (gamestate != 0)
		{
		if (Bosshealth > 0)
		{
			Boss_move(Bosspos.x,Bosspos.y, &Bosspos);      	    
			//Fire man
			obj_draw(
				boss,
				vec3d(Bosspos.x,Bosspos.y,2),
				vec3d(90,-90,0),
				vec3d(0.5,0.5,0.5),
				vec4d(1,1,1,1),
				bosstexture
				);

				obj_draw(
				BossHPBar,
				vec3d(30-Bosshealth/(2.5),20,2),
				vec3d(90,-90,0),
				vec3d(.9,.9,Bosshealth/(2.5)),
				vec4d(1,0,0,1),
				BosstextureHP
			);

		}
		else
		{
			obj_free(boss);

			Bosspos.x = 50;
			Bosspos.y = 50;
			slog("You WIN!");
		}
		

		}
        
        if (r > 360)r -= 360;
        glPopMatrix();
        /* drawing code above here! */
        graphics3d_next_frame();
	} 
	return 0;
	}
Example #13
0
/**
 * Dibuje el modelo definido por el archivo knight.obj 1 disponible en webasignatura. El
 * formato del archivo OBJ se encuentra definido en el anexo I de este documento. Cada
 * triángulo del modelo debe ser dibujado usando un tono de gris randómico en sus vértices.
 * Nota: Para parsear el archivo obj, puede utilizar las funciones fgets (definida en stdio.h),
 * strcmp, strtok (definidas en string.h), atof y atoi (definidas en stdlib.h).
 */
int main(int argc, char* argv[])
{
// Crear una ventana de 500x500 pixels:
	int cw = 900;
	int ch = 900;

	cg_init(cw, ch, NULL);

#ifdef WIN32
    freopen( "CON", "w", stdout );
    freopen( "CON", "w", stderr );
#endif

	printf("GL Version: %s\n", glGetString(GL_VERSION));
	// Actualizar la pantalla:
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glMatrixMode(GL_PROJECTION);
	glViewport(0,0,cw, ch);
	glFrustum(-1,1,-1,1,1,1000);

	//Habilito la iluminación del pipeline estático de OpenGL.
	glEnable(GL_LIGHTING);

	//Creo un Vec4 para representar el color (RGBA) y la Intensidad de la luz.
	//P.e.: (1,1,1,1) = Luz Blanca intensa, (0.5,0.5,0.5,1) = Luz blanca tenue, (0.5,0,0,1) = Luz roja tenue.
	float l0[] = {1.0f,1.0f,1.0f,1.0f};
	//Creo un Vec4 para representar el color (RGBA) y la intensidad de la iluminación ambiente de la luz
	float la[] = {0.10f,0.10f,0.10f,1.0f};
	//Creo un Vec4 para representar la posición de la luz. El cuarto elemento representa el tipo de luz: 1=puntual, 0=direccional
	float l0p[]= {1.0f,1.0f,1.0f,1.0f};
	//Creo un Vec4 para representar el color (RGBA) y la intensidad especular de la luz
	float ls[] = {1.0f,1.0f,1.0f,1.0f};
	//Cargo la intesidad ambiente de la Luz Nro 0 del pipline estático.
	glLightfv(GL_LIGHT0, GL_AMBIENT, la);
	//Cargo la intesidad difusa de la Luz Nro 0 del pipline estático.
	glLightfv(GL_LIGHT0, GL_DIFFUSE, l0);
	//Cargo la posición de la Luz Nro 0 del pipline estático.
	glLightfv(GL_LIGHT0, GL_POSITION, l0p);
	//Cargo la intesidad especular de la Luz Nro 0 del pipline estático.
	glLightfv(GL_LIGHT0, GL_SPECULAR, ls);
	//Prendo la Luz nro 0 del pipline estático. Hay 8 luces, representadas por las constantes GL_LIGHT0 a GL_LIGHT7
	//Por defecto está todas apagadas al inicio.
	glEnable(GL_LIGHT0);

	//Creo un Vec4 para representar el color difuso(RGBA) del material del objeto a dibujar.
	float cyan[] = {1.0f, 0.0f, 1.0f, 1.f};
	//Cargo el color difuso del la cara de adelante del objeto a dibujar.
	glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan);
	//Cargo el color especular del la cara de adelante del objeto a dibujar.
	glMaterialfv(GL_FRONT, GL_SPECULAR, ls);
	//Cargo el coeficiente especular de la cara de adelante del objeto a dibujar.
	glMateriali(GL_FRONT, GL_SHININESS, 32);

	float ang = 0.0f;
	float pitch = 0.0f;
	float ang_vel = 1.0f;

	Obj* obj = obj_load("Models/knight.obj");

	printf("num of faces %d\n", obj->numfaces);    // delete
	printf("num of vertices %d\n", obj->numverts);  // delete


	char done = 0;
	char wireframe = 0;
	char bfc = 0;
	glEnable(GL_DEPTH_TEST);
	char zbuff = 1;
	unsigned char key_pressed[1024];
	memset(key_pressed, 0, 1024);

	while (!done)
	{

		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
			switch (event.type)
			{
				case SDL_KEYDOWN:
					key_pressed[event.key.keysym.sym] = 1;
					if (event.key.keysym.sym == SDLK_z)
					{
						zbuff = !zbuff;
						if(zbuff)
							glEnable(GL_DEPTH_TEST);
						else
							glDisable(GL_DEPTH_TEST);
						break;
					}
					else if (event.key.keysym.sym == SDLK_m)
					{
						wireframe = !wireframe;
						if(wireframe)
							glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
						else
							glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
						break;
					}
					else if (event.key.keysym.sym == SDLK_b)
					{
						bfc = !bfc;
						if(bfc)
						{
							glEnable(GL_CULL_FACE);
							glCullFace(GL_BACK);
							glFrontFace(GL_CW);
						}
						else
							glDisable(GL_CULL_FACE);
						break;
					}
					else if (event.key.keysym.sym != SDLK_ESCAPE)
						break;
				case SDL_QUIT : done = 1;break;
				case SDL_KEYUP: key_pressed[event.key.keysym.sym] = 0;

			}
		}

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0.0f, 0.0f, -50.0f);
		glRotatef(pitch, 1.0f, 0.0f, 0.0f);
		glRotatef(ang, 0.0f, 1.0f, 0.0f);

		glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);

		if(key_pressed[SDLK_RIGHT]) ang += ang_vel;
		if(key_pressed[SDLK_LEFT]) ang -= ang_vel;
		if(key_pressed[SDLK_UP]) pitch += ang_vel;
		if(key_pressed[SDLK_DOWN]) pitch -= ang_vel;

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		obj_render(obj);

		cg_repaint();
	}
	obj_free(obj);
	// Liberar recursos:
	cg_close();

	// Ejemplo del modulo de Manejo de Memoria (MM):
	int* pint = (int *)cg_malloc(10*sizeof(int));
	printf("pint is a pointer: %p\n", pint);
	cg_free(pint); // olvidarse de liberar este objeto produce un mensaje


	return 0;
}
/*
 *	Read the symbols in an object and register them in the symbol table.
 *	Return -1 if there is an error.
 */
static int loadobj(const char *objname, int ignore_suffix)
{
	static char *currdir;
	static int currdir_len;
	int fp;
	MODULE *mod;
	SYMBOL *undefs[5000];
	SYMBOL *defsym[5000];
	struct obj_file *f;
	struct obj_section *sect;
	struct obj_symbol *objsym;
	int i;
	int is_2_2;
	int ksymtab;
	int len;
	int n_undefs = 0;
	int n_defsym = 0;
	char *p;
	void *image;
	unsigned long m_size;

	p = strrchr(objname, '/');
	len = 1 + (int)(p - objname);

	if ((fp = open(objname, O_RDONLY)) < 0)
		return 1;

	if (!(f = obj_load(fp, ET_REL, objname))) {
		close(fp);
		return -1;
	}
	close(fp);

	/*
	 * Allow arch code to define _GLOBAL_OFFSET_TABLE_
	 */
	arch_create_got(f);

	/*
	 * If we have changed base directory
	 * then use the defined symbols from modules
	 * in the _same_ directory to resolve whatever
	 * undefined symbols there are.
	 *
	 * This strategy ensures that we will have
	 * as correct dependencies as possible,
	 * even if the same symbol is defined by
	 * other modules in other directories.
	 */
	if (currdir_len != len || currdir == NULL ||
	    strncmp(currdir, objname, len) != 0) {
		if (currdir)
			resolve();
		currdir = bb_xstrdup(objname);
		currdir_len = len;
	}

	mod = modules + n_modules++;
	mod->name = bb_xstrdup(objname);

	if ((sect = obj_find_section(f, "__ksymtab")) != NULL)
		ksymtab = sect->idx; /* Only in 2.2 (or at least not 2.0) */
	else
		ksymtab = -1;

	if (sect ||
	    obj_find_section(f, ".modinfo") ||
	    obj_find_symbol(f, "__this_module"))
		is_2_2 = 1;
	else
		is_2_2 = 0;

	for (i = 0; i < HASH_BUCKETS; ++i) {
		for (objsym = f->symtab[i]; objsym; objsym = objsym->next) {
			if (objsym->secidx == SHN_UNDEF) {
				if (ELFW(ST_BIND)(objsym->info) != STB_WEAK &&
				    objsym->r_type /* assumes that R_arch_NONE is always 0 on all arch */) {
					undefs[n_undefs++] = addsym(objsym->name,
								    mod,
								    SYM_UNDEF,
								    ignore_suffix);
				}
				continue;
			}
			/* else */

			if (is_2_2 && ksymtab != -1) {
				/* A 2.2 module using EXPORT_SYMBOL */
				if (objsym->secidx == ksymtab &&
				    ELFW(ST_BIND)(objsym->info) == STB_GLOBAL) {
					/* Ignore leading "__ksymtab_" */
					defsym[n_defsym++] = addsym(objsym->name + 10,
								    mod,
								    SYM_DEFINED,
								    ignore_suffix);
				}
				continue;
			}
			/* else */

			if (is_2_2) {
				/*
				 * A 2.2 module _not_ using EXPORT_SYMBOL
				 * It might still want to export symbols,
				 * although strictly speaking it shouldn't...
				 * (Seen in pcmcia)
				 */
				if (ELFW(ST_BIND)(objsym->info) == STB_GLOBAL) {
					defsym[n_defsym++] = addsym(objsym->name,
								    mod,
								    SYM_DEFINED,
								    ignore_suffix);
				}
				continue;
			}
			/* else */

			/* Not undefined or 2.2 ksymtab entry */
			if (objsym->secidx < SHN_LORESERVE
			/*
			 * The test below is removed for 2.0 compatibility
			 * since some 2.0-modules (correctly) hide the
			 * symbols it exports via register_symtab()
			 */
			/* && ELFW(ST_BIND)(objsym->info) == STB_GLOBAL */
			     ) {
				defsym[n_defsym++] = addsym(objsym->name,
							    mod,
							    SYM_DEFINED,
							    ignore_suffix);
			}
		}
	}

	/*
	 *	Finalize the information about a module.
	 */
	mod->defsym.n_syms = n_defsym;
	if (n_defsym > 0) {
		int size = n_defsym * sizeof(SYMBOL *);

		mod->defsym.symtab = (SYMBOL **)xmalloc(size);
		memcpy(mod->defsym.symtab, defsym, size);
	} else
		mod->defsym.symtab = NULL;

	mod->undefs.n_syms = n_undefs;
	if (n_undefs > 0) {
		int size = n_undefs * sizeof(SYMBOL *);

		mod->undefs.symtab = (SYMBOL **) xmalloc(size);
		memcpy(mod->undefs.symtab, undefs, size);
		mod->resolved = 0;
	} else {
		mod->undefs.symtab = NULL;
		mod->resolved = 1;
	}

	/* Do a pseudo relocation to base address 0x1000 (arbitrary).
	 * All undefined symbols are treated as absolute 0.  This builds
	 * enough of a module to allow extraction of internal data such
	 * as device tables.
	 */
	obj_clear_undefineds(f);
	obj_allocate_commons(f);
	m_size = obj_load_size(f);
	if (!obj_relocate(f, 0x1000)) {
		bb_error_msg("depmod obj_relocate failed\n");
		return(-1);
	}
	extract_generic_string(f, mod);
	image = xmalloc(m_size);
	obj_create_image(f, image);
	free(image);

	obj_free(f);
	return 0;
}
Example #15
0
/**
 * http://cse.csusb.edu/tongyu/courses/cs420/notes/lighting.php
 * Sombreado por medio de la técnica Gouraud Shading.
 */
int main(int argc, char* argv[])
{
	// Crear una ventana de 750x750 pixels.
	int cw = 750;
	int ch = 750;

	cg_init(cw, ch, NULL);

	// Actualizar la pantalla:
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	glMatrixMode(GL_PROJECTION);
	glViewport(0,0,cw, ch);
	glFrustum(-1,1,-1,1,1,1000);

	//Habilito la iluminación del pipeline estático de OpenGL.
	glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);

    // GL_LIGHT0
    Vec4 *vec4La0 = vec4Contructor(0.10f, 0.10f, 0.10f, 1.0f);
    Vec4 *vec4Ld0 = vec4Contructor(1.0f, 1.0f, 1.0f, 1.0f);
    Vec4 *vec4Ls0 = vec4Contructor(1.0f, 1.0f, 1.0f, 1.0f);
    Vec4 *vec4Lp0 = vec4Contructor(1.0f, 1.0f, 1.0f, 0.0f);
    loadLight(GL_LIGHT0, vec4La0, vec4Ld0, vec4Lp0, vec4Ls0);

    // GL_LIGHT1
    Vec4 *vec4La1 = vec4Contructor(0.5f,0.5f,0.5f,1.0f);
    Vec4 *vec4Ld1 = vec4Contructor(0.5f, 0.3f, 0.2f, 1.0f);
    Vec4 *vec4Ls1 = vec4Contructor(1.0f,1.0f,1.0f,1.0f);
    Vec4 *vec4Lp1 = vec4Contructor(1.0f, 1.0f, 1.0f, 0.0f);
    loadLight(GL_LIGHT1, vec4La1, vec4Ld1, vec4Lp1, vec4Ls1);
//    GLfloat spot_direction[] = { -1.0, -1.0, 0.0 };
//    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, 45.0);
//    glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction);
//    glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0);

    // GL_LIGHT2
    Vec4 *vec4La2 = vec4Contructor( 0.2f, 0.2f, 0.2f, 1.0f);
    Vec4 *vec4Ld2 = vec4Contructor(0.8f, 0.8f, 0.8, 1.0f );
    Vec4 *vec4Lp2 = vec4Contructor(-1.5f, 1.0f, -4.0f, 1.0f );
    Vec4 *vec4Ls2 = vec4Contructor(0.5f, 0.5f, 0.5f, 1.0f );
    loadLight(GL_LIGHT2, vec4La2, vec4Ld2, vec4Lp2, vec4Ls2);


    // TODO, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7

    Obj* obj = obj_load("../Models/knight.obj");

	float ang = 0.0f;
	float pitch = 0.0f;
	float ang_vel = 1.0f;

	char done = 0;
	char wireframe = 0;
	char gourandShading = 0;
	char bfc = 0;
	char zbuff = 1;
    char enableLight0 = 0;
    char enableLight1 = 0;
    char enableLight2 = 0;
    char enableLight3 = 0;
    char enableLight4 = 0;
    char enableLight5 = 0;
    char enableLight6 = 0;
    char enableLight7 = 0;
	unsigned char key_pressed[1024];
	memset(key_pressed, 0, 1024);

	while (!done)
	{
		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
			switch (event.type)
			{
				case SDL_KEYDOWN:
					key_pressed[event.key.keysym.sym] = 1;

					if (event.key.keysym.sym == SDLK_z)
					{
						zbuff = !zbuff;
						if(zbuff)
							glEnable(GL_DEPTH_TEST);
						else
							glDisable(GL_DEPTH_TEST);
						break;
					}
					else if (event.key.keysym.sym == SDLK_b)
					{
						bfc = !bfc;
						if(bfc)
						{
							glEnable(GL_CULL_FACE);
							glCullFace(GL_BACK);
							glFrontFace(GL_CW);
						}
						else
							glDisable(GL_CULL_FACE);
						break;
					}
					else if (event.key.keysym.sym == SDLK_m)
					{
						wireframe = !wireframe;
						if(wireframe)
							glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
						else
							glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
						break;
					}
                    else if (event.key.keysym.sym == SDLK_g)
                    {
                        gourandShading = !gourandShading;
                        if(gourandShading)
                            glShadeModel(GL_FLAT);
                        else
                            glShadeModel(GL_SMOOTH);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_0)
                    {
                        enableLight0 = !enableLight0;
                        if(enableLight0) glEnable(GL_LIGHT0);
                        else glDisable(GL_LIGHT0);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_1)
                    {
                        enableLight1 = !enableLight1;
                        if(enableLight1) glEnable(GL_LIGHT1);
                        else glDisable(GL_LIGHT1);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_2)
                    {
                        enableLight2 = !enableLight2;
                        if(enableLight2) glEnable(GL_LIGHT2);
                        else glDisable(GL_LIGHT2);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_3)
                    {
                        enableLight3 = !enableLight3;
                        if(enableLight3) glEnable(GL_LIGHT3);
                        else glDisable(GL_LIGHT3);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_4)
                    {
                        enableLight4 = !enableLight4;
                        if(enableLight4) glEnable(GL_LIGHT4);
                        else glDisable(GL_LIGHT4);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_5)
                    {
                        enableLight5 = !enableLight5;
                        if(enableLight5) glEnable(GL_LIGHT5);
                        else glDisable(GL_LIGHT5);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_6)
                    {
                        enableLight6 = !enableLight6;
                        if(enableLight6) glEnable(GL_LIGHT6);
                        else glDisable(GL_LIGHT6);
                        break;
                    }
                    else if (event.key.keysym.sym == SDLK_7)
                    {
                        enableLight7 = !enableLight7;
                        if(enableLight7) glEnable(GL_LIGHT7);
                        else glDisable(GL_LIGHT7);
                        break;
                    }
					else if (event.key.keysym.sym != SDLK_ESCAPE)
						break;
				case SDL_QUIT :
					done = 1;
					break;
				case SDL_KEYUP:
					key_pressed[event.key.keysym.sym] = 0;

			}
		}

		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
		glTranslatef(0.0f, 0.0f, -50.0f);
		glRotatef(pitch, 1.0f, 0.0f, 0.0f);
		glRotatef(ang, 0.0f, 1.0f, 0.0f);
		glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);

		if(key_pressed[SDLK_RIGHT]) ang += ang_vel;
		if(key_pressed[SDLK_LEFT]) ang -= ang_vel;
		if(key_pressed[SDLK_UP]) pitch += ang_vel;
		if(key_pressed[SDLK_DOWN]) pitch -= ang_vel;

		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		obj_render(obj);
		cg_repaint();
	}

	// Liberar recursos:

    obj_free(obj);
	cg_close();

	return 0;
}