Пример #1
0
void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black
    glShadeModel(GL_SMOOTH); // Type of shading for the polygons

    // Viewport transformation
    glViewport(0,0,screen_width,screen_height);

    // Projection transformation
    glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations
    glLoadIdentity(); // We initialize the projection matrix as identity
    gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,10000.0f); // We define the "viewing volume"

    glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

    glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping

    Load3DS (&object,"spaceship.3ds");

    object.id_texture=LoadBitmap("spaceshiptexture.bmp"); // The Function LoadBitmap() return the current texture ID

    // If the last function returns -1 it means the file was not found so we exit from the program
    if (object.id_texture==-1)
    {
        printf("Image file: spaceshiptexture.bmp not found\n");
        //MessageBox(NULL,"Image file: spaceshiptexture.bmp not found", "Zetadeck",MB_OK | MB_ICONERROR);
        exit (0);
    }
}
Пример #2
0
void init(void) {
	unsigned char *data;
	int width,height;
	
	glFogCoordfEXT = (PFNGLFOGCOORDFEXTPROC) wglGetProcAddress("glFogCoordfEXT");

	glClearDepth(1);
	glClearColor(fogcolor[0],fogcolor[1],fogcolor[2],fogcolor[3]);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);

	glFogi(GL_FOG_MODE,GL_LINEAR);
	glFogfv(GL_FOG_COLOR,fogcolor);
	glFogf(GL_FOG_START,0);
	glFogf(GL_FOG_END,50);
	glFogi(GL_FOG_COORDINATE_SOURCE_EXT,GL_FOG_COORDINATE_EXT);
	
	glEnable(GL_LIGHT0);

	glGenTextures(1,&texture);
	glBindTexture(GL_TEXTURE_2D,texture);
	if((data = LoadJPEG("./data/ground.jpg",&width,&height))) {
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
		gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_RGBA,
			GL_UNSIGNED_BYTE,data);
		free(data);
	}
	
	vertex = Load3DS("./data/ground.3ds",&num_vertex);
}
Пример #3
0
int Object3DS::loadModel()
{
	if (fileName[0] != '-')
	{
		Load3DS(&object, fileName);
	}
	return 0;
}
Пример #4
0
void loadModels(ApplicationInfo *application){
  application->num_objects=getnumfiles("data/Models",".3ds");
  application->objects = (model_3ds*)malloc(sizeof(model_3ds)*application->num_objects);
  for(int x=0;x< application->num_objects;x++){
    Load3DS(&(application->objects[x]),getfilename("data/Models",".3ds",x));
    Buffer3DS(&(application->objects[x]));
  }
  printf("Buffering done\n");
}
Пример #5
0
char ObjLoad(char *p_object_name, char *p_texture_name, float p_pos_x, float p_pos_y, float p_pos_z, int p_rot_x, int p_rot_y, int p_rot_z)
{
    if (Load3DS (&object[obj_qty],p_object_name)==0) return(0); //Object loading
	object[obj_qty].id_texture=LoadBMP(p_texture_name); // The Function LoadBitmap() returns the current texture ID
	ObjCalcNormals(&object[obj_qty]); //Once we have all the object data we need to calculate all the normals of the object's vertices
	MatrIdentity_4x4(object[obj_qty].matrix); //Object matrix init
	ObjPosition(&object[obj_qty], p_pos_x, p_pos_y, p_pos_z); // Object initial position
	ObjRotate(&object[obj_qty], p_rot_x, p_rot_y, p_rot_z); // Object initial rotation
	obj_qty++; // Let's increase the object number and get ready to load another object!
	return (1); // If all is ok then return 1
}
Пример #6
0
//---------------------------------------------------------------------------//
// Load
//
//---------------------------------------------------------------------------//
bool CSceneLoader::Load(CEscena3D *pEscena3D, const char *pData, unsigned uDataSize)
{
  ushort uVersion = *(ushort *)pData;
  switch (uVersion)
  {
    case N2_3DSFILE:   return Load3DS  (pEscena3D, pData, uDataSize);
    case N2_N3DFILEV2: return LoadN3Dv2(pEscena3D, pData, uDataSize);
    default:           return LoadN3Dv1(pEscena3D, pData, uDataSize);
  }
  GLOG(("ERR: File format not supported for 3D file\n"));
  return false;
}
Пример #7
0
void initModels()
{
	printf("initModels\n");

    glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black

    glShadeModel(GL_SMOOTH); // Type of shading for the polygons

    // Viewport transformation

    glViewport(0,0,screen_width,screen_height);

    // Projection transformation

    glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations

    glLoadIdentity(); // We initialize the projection matrix as identity

    gluPerspective(45.0f,(GLfloat)screen_width/(GLfloat)screen_height,10.0f,10000.0f); // We define the "viewing volume"

    glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)

    //glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping

    Load3DS (&modelo,"Resource/modelo_actor.3DS");

    //Aqui vai ser para carregar texturas, mais tarde

    modelo.id_texture=LoadTextura("Resource/spaceshiptexture.bmp"); // The Function LoadBitmap() return the current texture ID

    // If the last function returns -1 it means the file was not found so we exit from the program

    if (modelo.id_texture==-1)
    {
        //MessageBox(NULL,"Image file: spaceshiptexture.bmp not found", "Zetadeck",MB_OK | MB_ICONERROR);

    	printf("textura da nave nao encontrada\n");

        exit (0);
    }

    
    // glDisable(GL_TEXTURE_2D);
}
Пример #8
0
void init(void) {
	unsigned char *data;
	float *model;
	int i,width,height,num_face;
	
	glClearDepth(1);
	glClearColor(0,0,0,0);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	glEnable(GL_LIGHT0);

	glEnable(GL_TEXTURE_GEN_S);
	glEnable(GL_TEXTURE_GEN_T);
	glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
	glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_SPHERE_MAP);
	
	glGenTextures(1,&texture);
	glBindTexture(GL_TEXTURE_2D,texture);
	if((data = LoadJPEG("./data/base.jpg",&width,&height))) {
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_RGBA,
			GL_UNSIGNED_BYTE,data);
		free(data);
	}
	
	model = Load3DS("./data/model.3ds",&num_face);
	list = glGenLists(1);
	glNewList(list,GL_COMPILE);
	glBegin(GL_TRIANGLES);
	for(i = 0; i < num_face; i++) {
		glNormal3fv((float*)&model[i << 3] + 3);
		glVertex3fv((float*)&model[i << 3]);
	}
	glEnd();
	glEndList();
	free(model);
}
Пример #9
0
/*
** void _3DS_LoadPolysets
*/
void _3DS_LoadPolysets( const char *filename, polyset_t **ppPSET, int *numpsets, qboolean verbose ){
	_3DS_t _3ds;
	int numPolysets;
	polyset_t *pPSET;
	triangle_t *ptri, *triangles;
	int i;

	// load the 3DS
	memset( &_3ds, 0, sizeof( _3ds ) );
	Load3DS( filename, &_3ds, verbose );

	// compute information
	numPolysets = _3ds.editChunk.numNamedObjects;

	// allocate memory
	pPSET = calloc( 1, numPolysets * sizeof( polyset_t ) );
	triangles = ptri = calloc( 1, POLYSET_MAXTRIANGLES * sizeof( triangle_t ) );

	// copy the data over
	for ( i = 0; i < numPolysets; i++ )
	{
		char matnamebuf[1024];
		int j;
		triangle_t *tri;
		_3DSTriObject_t *pTO = &_3ds.editChunk.pNamedObjects[i].pTriObjects[0];

		pPSET[i].triangles = ptri;
		pPSET[i].numtriangles = pTO->numFaces;
		strcpy( pPSET[i].name, _3ds.editChunk.pNamedObjects[i].name );

		strcpy( matnamebuf, filename );
		if ( strrchr( matnamebuf, '/' ) ) {
			*( strrchr( matnamebuf, '/' ) + 1 ) = 0;
		}
		strcat( matnamebuf, pTO->pMeshMaterialGroups[0].name );

		if ( strstr( matnamebuf, gamedir ) ) {
			strcpy( pPSET[i].materialname, strstr( matnamebuf, gamedir ) + strlen( gamedir ) );
		}
		else{
			strcpy( pPSET[i].materialname, pTO->pMeshMaterialGroups[0].name );
		}

		assert( pPSET[i].numtriangles < POLYSET_MAXTRIANGLES );

		for ( tri = ptri, j = 0; j < pPSET[i].numtriangles; j++ )
		{
			int i0 = pTO->pFaces[j].c;
			int i1 = pTO->pFaces[j].b;
			int i2 = pTO->pFaces[j].a;

			tri->verts[0][0] = pTO->pPoints[i0].x;
			tri->verts[0][1] = pTO->pPoints[i0].y;
			tri->verts[0][2] = pTO->pPoints[i0].z;

			tri->verts[1][0] = pTO->pPoints[i1].x;
			tri->verts[1][1] = pTO->pPoints[i1].y;
			tri->verts[1][2] = pTO->pPoints[i1].z;

			tri->verts[2][0] = pTO->pPoints[i2].x;
			tri->verts[2][1] = pTO->pPoints[i2].y;
			tri->verts[2][2] = pTO->pPoints[i2].z;
/*
            for ( k = 0; k < 3; k++ )
            {
                tri->colors[0][k] = 1;
                tri->colors[1][k] = 1;
                tri->colors[2][k] = 1;
            }
 */

			if ( pTO->pTexVerts ) {
				tri->texcoords[0][0] = pTO->pTexVerts[i0].s;
				tri->texcoords[0][1] = 1.0f - pTO->pTexVerts[i0].t;
				tri->texcoords[1][0] = pTO->pTexVerts[i1].s;
				tri->texcoords[1][1] = 1.0f - pTO->pTexVerts[i1].t;
				tri->texcoords[2][0] = pTO->pTexVerts[i2].s;
				tri->texcoords[2][1] = 1.0f - pTO->pTexVerts[i2].t;
			}

			tri++;
		}

		ptri += pPSET[i].numtriangles;
		assert( ptri - triangles < POLYSET_MAXTRIANGLES );
	}

	// compute normal data
#if 0
	for ( i = 0; i < numPolysets; i++ )
	{
		// unique vertices based solely on vertex position
		ComputeNormals( &_3ds.editChunk.pNamedObjects[i].pTriObjects[0],
						pPSET[i].triangles );
	}
#endif

	free( _3ds.editChunk.pMaterials );
	free( _3ds.editChunk.pNamedObjects );

	*ppPSET = pPSET;
	*numpsets = numPolysets;
}
Пример #10
0
//------------------------------------------------------------------------------
// SDL and OpenGL setup in the onInit() method
//------------------------------------------------------------------------------
bool Client::onInit() {
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
		printf("problem initializing SDL\n");
        return false;
    }

	if (   SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,    1) != 0

		|| SDL_GL_SetAttribute(SDL_GL_RED_SIZE,        8) != 0
		|| SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE,      8) != 0
		|| SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,       8) != 0
		|| SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE,      8) != 0
		|| SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE,    32) != 0
		|| SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE,     16) != 0
/*
		// Anti-aliasing -- why won't this work under linux???
		|| SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS,  1) != 0
		|| SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES,  2) != 0
*/
		|| SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE,  8) != 0
		|| SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE,8) != 0
		|| SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8) != 0
		|| SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE,8) != 0 )
	{
		printf("SDL OpenGL setup problem\n");
		return false;
	}

	// create window with given width and height
	onResize(wView, hView);

	// hide system cursor
	SDL_ShowCursor(SDL_DISABLE);

	// set white as the default clear / background color
    glClearColor(1.0, 1.0, 1.0, 0.0);

	// Enable depth testing
	glEnable(GL_DEPTH_TEST);

	
	glEnable(GL_CULL_FACE); // do not calculate inside of models
	glFrontFace(GL_CCW); // counter clockwise polys face out (default)


	// enable 2d textures
	glEnable(GL_TEXTURE_2D);

	// enable blending (translucency / trasparency)
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// GL_MULTISAMPLE Why does the include under windows not find it???
	//glEnable(0x809D);

	GLfloat ambientLight[] = { 0.50, 0.50, 0.50 };
	GLfloat diffuseLight[] = { 0.5, 0.5, 0.5 };
	glEnable(GL_LIGHTING);

	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glEnable(GL_LIGHT0);

	//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight); 
	glEnable ( GL_COLOR_MATERIAL );
	glColorMaterial ( GL_FRONT, GL_AMBIENT_AND_DIFFUSE );

	glEnable(GL_NORMALIZE);

	// load textures
	GLuint mapTexture    = 0;
	GLuint tankBaseTex = 0;
	GLuint tankFireTex = 0;
	GLuint tankGunTex = 0;
	GLuint cursorTexture = 0;
	if (   (mapTexture  = loadTexture("./graphics/moon landscape large.png")) < 0
//		|| (tankBaseTex = loadTexture("./graphics/tank3-base.png")) < 0
//		|| (tankFireTex = loadTexture("./graphics/tank3-base-engfire.png")) < 0
//		|| (tankGunTex = loadTexture("./graphics/tank3-gun.png")) < 0
		|| (cursorTexture = loadTexture("./graphics/triangle-green.png")) < 0)
	{
		printf("problem loading textures\n");
		return false;
	}

	map = new Map(4096, 4096, mapTexture);
	p1 = new Tank(map, 100, 100);
	cursor = new Cursor(0, 0, 0, cursorTexture);

	Load3DS(p1->getObject(), "saucer.3ds");

    return true;
}
Пример #11
0
bool CCPrimitive3DS::load(const char* file)
{
    CCText fullFilePath;
    CCFileManager::GetFilePath( fullFilePath, file, Resource_Packaged );

    // Once we've copied out the data, we delete the 3dsobject
    obj3ds_type *object = new obj3ds_type();
	const int result = Load3DS( object, fullFilePath.buffer );
    CCASSERT( result == 1 );

    // Extract the normals
    Calc3DSNormals( object );

    if( result == 1 )
    {
        vertexCount = object->polygons_qty * 3;

        modelUVs = (float*)malloc( sizeof( float ) * vertexCount * 2 );
        vertices = (float*)malloc( sizeof( float ) * vertexCount * 3 );
        normals = (float*)malloc( sizeof( float ) * vertexCount * 3 );

        for( int l_index = 0; l_index < object->polygons_qty; ++l_index )
        {
            const int uvIndex = l_index*3*2;
            const int vertexIndex = l_index*3*3;

            {
                const int index = object->polygon[l_index].a;
                CCASSERT( index >= 0 );
                CCASSERT( index < object->vertices_qty );

                // UVs
                {
                    const float u = object->mapcoord[index].u;
                    const float v = object->mapcoord[index].v;
                    modelUVs[uvIndex+0] = u;
                    modelUVs[uvIndex+1] = 1.0f - v;
                }

                // Vertices
                {
                    float x = object->vertex[index].x;
                    float y = object->vertex[index].y;
                    float z = object->vertex[index].z;
                    vertices[vertexIndex+0] = x;
                    vertices[vertexIndex+1] = y;
                    vertices[vertexIndex+2] = z;

                    mmX.consider( x );
                    mmY.consider( y );
                    mmZ.consider( z );
                }

                // Normals
                {
                    float x = object->normal[index].x;
                    float y = object->normal[index].y;
                    float z = object->normal[index].z;
                    normals[vertexIndex+0] = x;
                    normals[vertexIndex+1] = y;
                    normals[vertexIndex+2] = z;
                }
            }

            {
                const int index = object->polygon[l_index].b;
                CCASSERT( index >= 0 );
                CCASSERT( index < object->vertices_qty );

                // UVs
                {
                    const float u = object->mapcoord[index].u;
                    const float v = object->mapcoord[index].v;
                    modelUVs[uvIndex+2] = u;
                    modelUVs[uvIndex+3] = 1.0f - v;
                }

                // Vertices
                {
                    float x = object->vertex[index].x;
                    float y = object->vertex[index].y;
                    float z = object->vertex[index].z;
                    vertices[vertexIndex+3] = x;
                    vertices[vertexIndex+4] = y;
                    vertices[vertexIndex+5] = z;

                    mmX.consider( x );
                    mmY.consider( y );
                    mmZ.consider( z );
                }

                // Normals
                {
                    float x = object->normal[index].x;
                    float y = object->normal[index].y;
                    float z = object->normal[index].z;
                    normals[vertexIndex+3] = x;
                    normals[vertexIndex+4] = y;
                    normals[vertexIndex+5] = z;
                }
            }

            {
                const int index = object->polygon[l_index].c;
                CCASSERT( index >= 0 );
                CCASSERT( index < object->vertices_qty );

                // UVs
                {
                    const float u = object->mapcoord[index].u;
                    const float v = object->mapcoord[index].v;
                    modelUVs[uvIndex+4] = u;
                    modelUVs[uvIndex+5] = 1.0f - v;
                }

                // Vertices
                {
                    float x = object->vertex[index].x;
                    float y = object->vertex[index].y;
                    float z = object->vertex[index].z;
                    vertices[vertexIndex+6] = x;
                    vertices[vertexIndex+7] = y;
                    vertices[vertexIndex+8] = z;

                    mmX.consider( x );
                    mmY.consider( y );
                    mmZ.consider( z );
                }

                // Normals
                {
                    float x = object->normal[index].x;
                    float y = object->normal[index].y;
                    float z = object->normal[index].z;
                    normals[vertexIndex+6] = x;
                    normals[vertexIndex+7] = y;
                    normals[vertexIndex+8] = z;
                }
            }
        }

        width = mmX.size();
        height = mmY.size();
        depth = mmZ.size();

        delete object;
        return true;
    }

    delete object;
    return false;
}
Пример #12
0
int main(int argc, char *argv[])
{

        /* Scene */
        scene mine;
        mine.materials = NULL;
        mine.spheres = NULL;
        mine.triangles = NULL;
        mine.lights = NULL;
        myScene = &mine;

        perlin noise;
        myNoise = &noise;
        noise_init(myNoise);

        /* Threads */
        thread_info *tinfo;
        pthread_attr_t attr;
        int t;

        /* Allocate memory for pthread_create() arguments */
        tinfo = calloc(NUMTHREADS, sizeof(thread_info));
        if (tinfo == NULL) {
                printf("Error allocating memory for pthread_create() arguments. \n");
                return -1;
        }

        if (argc < 2) {
                printf("specify scene file. \n");
                return -1;
        }

        /* Build the scene */
        if (tokenizer(argv[1], myScene) == -1) {
                exit(-1);
        }


        /* Let's try the 3ds loader... */
        if (argc == 3) {

                obj_type object;
                Load3DS(&object, argv[2]); /* spaceship.3ds */
                int index;

                printf("Polygons: %d \n", object.polygons_qty);

                myScene->triangles = (triangle *)realloc(myScene->triangles, object.polygons_qty*(sizeof(triangle)));
                memset(myScene->triangles,0,sizeof(triangle));

                myScene->numTriangles = object.polygons_qty;
                for (index=0;index<object.polygons_qty;index++) {
                        /* First Vertex */
                        myScene->triangles[index].v2.x = object.vertex[object.polygon[index].a].x+130;
                        myScene->triangles[index].v2.y = object.vertex[object.polygon[index].a].y+150;
                        myScene->triangles[index].v2.z = object.vertex[object.polygon[index].a].z+350;
                        myScene->triangles[index].v2 = vectorScale(2,&myScene->triangles[index].v2);
                        /* Second Vertex */
                        myScene->triangles[index].v1.x = object.vertex[object.polygon[index].b].x+130;
                        myScene->triangles[index].v1.y = object.vertex[object.polygon[index].b].y+150;
                        myScene->triangles[index].v1.z = object.vertex[object.polygon[index].b].z+350;
                        myScene->triangles[index].v1 = vectorScale(2,&myScene->triangles[index].v1);
                        /* Third Vertex */
                        myScene->triangles[index].v3.x = object.vertex[object.polygon[index].c].x+130;
                        myScene->triangles[index].v3.y = object.vertex[object.polygon[index].c].y+150;
                        myScene->triangles[index].v3.z = object.vertex[object.polygon[index].c].z+350;
                        myScene->triangles[index].v3 = vectorScale(2,&myScene->triangles[index].v3);

                        /* Default Material */
                        myScene->triangles[index].material = 3;
                }
        }

        /* Allocate memory for the image */
        if (img)
                free(img);
        img = (unsigned char *)malloc(3*myScene->width*myScene->height);
        memset(img, 0, 3*myScene->width*myScene->height);

        /* Calculate section size per thread */
        sectionsize = myScene->height/NUMTHREADS;
        if ((sectionsize % 2)!=0)
                printf("Warning: Height/numthreads not even - there will be errors in the image! \n");

        /* Pthread options */
        pthread_attr_init(&attr);
        pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);

        /* Create the render threads */
        for (t=0; t<NUMTHREADS; t++) {
                tinfo[t].thread_num = t;
                if (pthread_create(&tinfo[t].thread_id, &attr, renderThread, &tinfo[t])) {
                        printf("Creation of thread %d failed. \n", t);
                        exit(-1);
                }
        }

        if (pthread_attr_destroy(&attr)) {
                printf("Error destroying thread attributes. \n");
        }

        /* Wait for render threads to finish */
        for (t=0; t<NUMTHREADS; t++) {
                if (pthread_join(tinfo[t].thread_id, NULL)) {
                        printf("Error waiting for thread. \n");
                }
        }

        /* Save the image */
        savebmp("out.bmp", img, myScene);

        /* Free allocated memory */
        if (img)
                free(img);
        if (myScene->lights)
                free(myScene->lights);
        if (myScene->triangles)
                free(myScene->triangles);
        if (myScene->spheres)
                free(myScene->spheres);
        if (myScene->materials)
                free(myScene->materials);

        /* Exit */
        pthread_exit(NULL);

        if (tinfo)
                free(tinfo);

        return 0;
}
Пример #13
0
void init(void) {
    float tmp,xmin,xmax,ymin,ymax,zmin,zmax,a[3],b[3];
    int i,j,k,width,height;
    unsigned char *data;
    float plane_S[] = { 1.0, 0.0, 0.0, 0.0 };
    float plane_T[] = { 0.0, 1.0, 0.0, 0.0 };
    float plane_R[] = { 0.0, 0.0, 1.0, 0.0 };
    float plane_Q[] = { 0.0, 0.0, 0.0, 1.0 };

    glClearDepth(1.0);
    glShadeModel(GL_SMOOTH);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    glDisable(GL_CULL_FACE);
    
    glEnable(GL_LIGHT0);
    
    modelsize = 0;
    vertexmodel = Load3DS("data/model.3ds",&num_vertexmodel);
    printf("./data/model.3ds %d face\n",num_vertexmodel / 3);
    
    xmin = xmax = ymin = ymax = zmin = zmax = 0;
    
    for(i = 0; i < num_vertexmodel; i++) {
        if(vertexmodel[(i << 3) + 0] < xmin) xmin = vertexmodel[(i << 3) + 0];
        if(vertexmodel[(i << 3) + 0] > xmax) xmax = vertexmodel[(i << 3) + 0];
        if(vertexmodel[(i << 3) + 1] < ymin) ymin = vertexmodel[(i << 3) + 1];
        if(vertexmodel[(i << 3) + 1] > ymax) ymax = vertexmodel[(i << 3) + 1];
        if(vertexmodel[(i << 3) + 2] < zmin) zmin = vertexmodel[(i << 3) + 2];
        if(vertexmodel[(i << 3) + 2] > zmax) zmax = vertexmodel[(i << 3) + 2];
    }
    
    for(i = 0; i < num_vertexmodel; i++) {
        vertexmodel[(i << 3) + 0] -= (xmax + xmin) / 2.0;
        vertexmodel[(i << 3) + 1] -= (ymax + ymin) / 2.0;
        vertexmodel[(i << 3) + 2] -= (zmax + zmin) / 2.0;
    }
    
    for(i = 0; i < num_vertexmodel; i++) {
        tmp = sqrt(vertexmodel[(i << 3) + 0] * vertexmodel[(i << 3) + 0] +
                    vertexmodel[(i << 3) + 1] * vertexmodel[(i << 3) + 1] +
                    vertexmodel[(i << 3) + 2] * vertexmodel[(i << 3) + 2]);
        if(tmp > modelsize) modelsize = tmp;
    }
    
    tmp = MODELSIZE / modelsize;
    modelsize = MODELSIZE;
    for(i = 0; i < num_vertexmodel; i++) {
        vertexmodel[(i << 3) + 0] *= tmp;
        vertexmodel[(i << 3) + 1] *= tmp;
        vertexmodel[(i << 3) + 2] *= tmp;
    }
    
    vertexground = Load3DS("data/ground.3ds",&num_vertexground);
    printf("./data/ground.3ds %d face\n",num_vertexground / 3);
    
    planeground = (float*)malloc(sizeof(float) * 4 * num_vertexground / 3);
    for(i = 0, j = 0, k = 0; i < num_vertexground; i += 3, j += 24, k += 4) {
        VectorSub(&vertexground[j + 8],&vertexground[j],a);
        VectorSub(&vertexground[j + 16],&vertexground[j],b);
        VectorCrossProduct(a,b,&planeground[k]);
        VectorNormalize(&planeground[k],&planeground[k]);
        planeground[k + 3] = -VectorDotProduct(&planeground[k],&vertexground[j]);
    }
    
    glGenTextures(1,&textureground);
    glBindTexture(GL_TEXTURE_2D,textureground);
    if((data = LoadJPEG("data/ground.jpg",&width,&height))) {
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
        gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_RGBA,GL_UNSIGNED_BYTE,data);
        free(data);
    }
    
    glGenTextures(1,&texturemodel);
    glBindTexture(GL_TEXTURE_2D,texturemodel);
    if((data = LoadJPEG("data/model.jpg",&width,&height))) {
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
        glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
        gluBuild2DMipmaps(GL_TEXTURE_2D,4,width,height,GL_RGBA,GL_UNSIGNED_BYTE,data);
        free(data);
    }
    
    glGenTextures(1,&textureshadow);
    glBindTexture(GL_TEXTURE_2D,textureshadow);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP);
    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP);
    glTexImage2D(GL_TEXTURE_2D,0,4,SHADOWSIZE,SHADOWSIZE,0,GL_RGBA,GL_UNSIGNED_BYTE,NULL);

    glTexGenfv(GL_S,GL_EYE_PLANE,plane_S);
    glTexGenfv(GL_T,GL_EYE_PLANE,plane_T);
    glTexGenfv(GL_R,GL_EYE_PLANE,plane_R);
    glTexGenfv(GL_Q,GL_EYE_PLANE,plane_Q);

    modellist = glGenLists(1);
    glNewList(modellist,GL_COMPILE);
    glPushMatrix();
    glBegin(GL_TRIANGLES);
    for(i = 0; i < num_vertexmodel; i++) {
        glNormal3fv((float*)&vertexmodel[i << 3] + 3);
        glTexCoord2fv((float*)&vertexmodel[i << 3] + 6);
        glVertex3fv((float*)&vertexmodel[i << 3]);
    }
    glEnd();
    glPopMatrix();
    glEndList();
    
    groundlist = glGenLists(1);
    glNewList(groundlist,GL_COMPILE);
    glBegin(GL_TRIANGLES);
    for(i = 0; i < num_vertexground; i++) {
        glNormal3fv((float*)&vertexground[i << 3] + 3);
        glTexCoord2fv((float*)&vertexground[i << 3] + 6);
        glVertex3fv((float*)&vertexground[i << 3]);
    }
    glEnd();
    glEndList();
    
    lightlist = glGenLists(1);
    glNewList(lightlist,GL_COMPILE);
    glutSolidSphere(0.6,16,16);
    glEndList();
}
Пример #14
0
void Object3DS::loadModel()
{
    if (filename[0] != '---')
        Load3DS(&object, filename);
}