Opengl导入3D模型

关于OpenGL导入3D模型,方法有很多,我这里主要讲我之前用过的一个方法。首先利用3dmax等三维建模软件制作好模型,再利用Deep Explorer导出格式为.cpp的文件。打开这个.cpp文件,会获得一串代码,这些代码里包含了Verticies,Texture Coordinates,Normals,Triangles,Material indicies等信息。

这是我建立的一个零件的例子。

/*
This file was produced by Deep Exploration Plugin: CPP Export filter. 

Copyright (C) 1999-2008 Right Hemisphere
Mail support@righthemisphere.com for support.
Visit http://www.righthemisphere.com/dexp.htm for updates.
*/

#include <windows.h>
#include <GL\gl.h>
#include <GL\glut.h>

GLuint lid;
int angle;

struct sample_MATERIAL
{
	GLfloat ambient[3];
	GLfloat diffuse[3];
	GLfloat specular[3];
	GLfloat emission[3];
	GLfloat alpha;
	GLfloat phExp;
	int texture;
};

static sample_MATERIAL materials [1] = {
 {{0.117647f,0.117647f,0.117647f},	{0.796078f,0.823529f,0.937255f},	{0.150588f,0.150588f,0.150588f},	{0.0f,0.0f,0.0f},	1.0f,8.0f,-1} //mat0
};

// 16 Verticies
// 0 Texture Coordinates
// 7 Normals
// 28 Triangles

static BYTE face_indicies[28][9] = {
// 零件1
	{0,1,2 ,0,0,0 ,0,0,0}, {1,3,2 ,0,0,0 ,0,0,0}, {4,0,5 ,1,1,1 ,0,0,0},
	{0,2,5 ,1,1,1 ,0,0,0}, {6,4,7 ,2,2,2 ,0,0,0}, {4,5,7 ,2,2,2 ,0,0,0},
	{8,6,9 ,1,1,1 ,0,0,0}, {6,7,9 ,1,1,1 ,0,0,0}, {10,8,11 ,3,3,3 ,0,0,0},
	{8,9,11 ,3,3,3 ,0,0,0}, {12,10,13 ,4,4,4 ,0,0,0}, {10,11,13 ,4,4,4 ,0,0,0},
	{14,12,15 ,2,2,2 ,0,0,0}, {12,13,15 ,2,2,2 ,0,0,0}, {1,14,3 ,1,1,1 ,0,0,0},
	{14,15,3 ,1,1,1 ,0,0,0}, {6,8,4 ,5,5,5 ,0,0,0}, {8,10,4 ,5,5,5 ,0,0,0},
	{4,10,0 ,5,5,5 ,0,0,0}, {10,12,0 ,5,5,5 ,0,0,0}, {0,12,1 ,5,5,5 ,0,0,0},
	{12,14,1 ,5,5,5 ,0,0,0}, {3,15,2 ,6,6,6 ,0,0,0}, {15,13,2 ,6,6,6 ,0,0,0},
	{2,13,5 ,6,6,6 ,0,0,0}, {13,11,5 ,6,6,6 ,0,0,0}, {5,11,7 ,6,6,6 ,0,0,0},
	{11,9,7 ,6,6,6 ,0,0,0}
};
static GLfloat vertices [16][3] = {
{0.212803f,-0.0605536f,0.241606f},{0.212803f,0.282007f,0.241606f},{0.212803f,-0.0605536f,-0.241606f},
{0.212803f,0.282007f,-0.241606f},{-0.202422f,-0.0605536f,0.241606f},{-0.202422f,-0.0605536f,-0.241606f},
{-0.202422f,0.282007f,0.241606f},{-0.202422f,0.282007f,-0.241606f},{-0.482699f,0.282007f,0.241606f},
{-0.482699f,0.282007f,-0.241606f},{-0.5f,-0.282007f,0.241606f},{-0.5f,-0.282007f,-0.241606f},
{0.5f,-0.282007f,0.241606f},{0.5f,-0.282007f,-0.241606f},{0.5f,0.282007f,0.241606f},
{0.5f,0.282007f,-0.241606f}
};
static GLfloat normals [7][3] = {
{-1.0f,0.0f,0.0f},{0.0f,1.0f,0.0f},{1.0f,0.0f,0.0f},
{-0.99953f,0.0306604f,0.0f},{0.0f,-1.0f,0.0f},{0.0f,0.0f,1.0f},
{0.0f,0.0f,-1.0f}
};
GLfloat textures[1][2]={{0.0f,0.0f}};
/*Material indicies*/
/*{material index,face count}*/
static int material_ref [1][2] = {
{0,28}
};
void MyMaterial(GLenum mode, GLfloat * f, GLfloat alpha)
{
	GLfloat d[4];
	d[0] = f[0];
	d[1] = f[1];
	d[2] = f[2];
	d[3] = alpha;
	glMaterialfv(GL_FRONT_AND_BACK, mode, d);
}

/*
 *  SelectMaterial uses OpenGL commands to define facet colors.
 *
 *  Returns:
 *    Nothing
 */

void SelectMaterial(int i)
{
//
// Define the reflective properties of the 3D Object faces.
//
	glEnd();
	GLfloat alpha = materials[i].alpha;
	MyMaterial(GL_AMBIENT, materials[i].ambient, alpha);
	MyMaterial(GL_DIFFUSE, materials[i].diffuse, alpha);
	MyMaterial(GL_SPECULAR, materials[i].specular, alpha);
	MyMaterial(GL_EMISSION, materials[i].emission, alpha);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials[i].phExp);
	glBegin(GL_TRIANGLES);

};

GLint Gen3DObjectList()
{
	int i;
	int j;

 GLint lid=glGenLists(1);
	int mcount=0;
	int mindex=0;
   glNewList(lid, GL_COMPILE);

    glBegin (GL_TRIANGLES);
      for(i=0;i<sizeof(face_indicies)/sizeof(face_indicies[0]);i++)
       {
	if (!mcount)
	{
		SelectMaterial(material_ref[mindex][0]);
		mcount = material_ref[mindex][1];
		mindex ++;
	}
	mcount --;
       for(j=0;j<3;j++)
        {
          int vi=face_indicies[i][j];
          int ni=face_indicies[i][j+3];//Normal index
          int ti=face_indicies[i][j+6];//Texture index
           glNormal3f (normals[ni][0],normals[ni][1],normals[ni][2]);
           glTexCoord2f(textures[ti][0],textures[ti][1]);
           glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
        }
       }
    glEnd ();

   glEndList();
   return lid;
};


如果现在你编译运行这段代码,会看不到任何东西。因为这些信息我们根本没有加以利用,那么如何利用这些信息呢?GLint Gen3DObjectList()函数是关键。它返回的是一个显示列表(关于这个显示列表的知识,我以后会再写出来的)。

下面贴出完整的代码:

/*
This file was produced by Deep Exploration Plugin: CPP Export filter. 

Copyright (C) 1999-2008 Right Hemisphere
Mail support@righthemisphere.com for support.
Visit http://www.righthemisphere.com/dexp.htm for updates.
*/

#include <windows.h>
#include <GL\gl.h>
#include <GL\glut.h>

GLuint lid;
struct sample_MATERIAL
{
	GLfloat ambient[3];
	GLfloat diffuse[3];
	GLfloat specular[3];
	GLfloat emission[3];
	GLfloat alpha;
	GLfloat phExp;
	int texture;
};

static sample_MATERIAL materials [1] = {
 {{0.117647f,0.117647f,0.117647f},	{0.796078f,0.823529f,0.937255f},	{0.150588f,0.150588f,0.150588f},	{0.0f,0.0f,0.0f},	1.0f,8.0f,-1} //mat0
};

// 16 Verticies
// 0 Texture Coordinates
// 7 Normals
// 28 Triangles

static BYTE face_indicies[28][9] = {
// 零件1
	{0,1,2 ,0,0,0 ,0,0,0}, {1,3,2 ,0,0,0 ,0,0,0}, {4,0,5 ,1,1,1 ,0,0,0},
	{0,2,5 ,1,1,1 ,0,0,0}, {6,4,7 ,2,2,2 ,0,0,0}, {4,5,7 ,2,2,2 ,0,0,0},
	{8,6,9 ,1,1,1 ,0,0,0}, {6,7,9 ,1,1,1 ,0,0,0}, {10,8,11 ,3,3,3 ,0,0,0},
	{8,9,11 ,3,3,3 ,0,0,0}, {12,10,13 ,4,4,4 ,0,0,0}, {10,11,13 ,4,4,4 ,0,0,0},
	{14,12,15 ,2,2,2 ,0,0,0}, {12,13,15 ,2,2,2 ,0,0,0}, {1,14,3 ,1,1,1 ,0,0,0},
	{14,15,3 ,1,1,1 ,0,0,0}, {6,8,4 ,5,5,5 ,0,0,0}, {8,10,4 ,5,5,5 ,0,0,0},
	{4,10,0 ,5,5,5 ,0,0,0}, {10,12,0 ,5,5,5 ,0,0,0}, {0,12,1 ,5,5,5 ,0,0,0},
	{12,14,1 ,5,5,5 ,0,0,0}, {3,15,2 ,6,6,6 ,0,0,0}, {15,13,2 ,6,6,6 ,0,0,0},
	{2,13,5 ,6,6,6 ,0,0,0}, {13,11,5 ,6,6,6 ,0,0,0}, {5,11,7 ,6,6,6 ,0,0,0},
	{11,9,7 ,6,6,6 ,0,0,0}
};
static GLfloat vertices [16][3] = {
{0.212803f,-0.0605536f,0.241606f},{0.212803f,0.282007f,0.241606f},{0.212803f,-0.0605536f,-0.241606f},
{0.212803f,0.282007f,-0.241606f},{-0.202422f,-0.0605536f,0.241606f},{-0.202422f,-0.0605536f,-0.241606f},
{-0.202422f,0.282007f,0.241606f},{-0.202422f,0.282007f,-0.241606f},{-0.482699f,0.282007f,0.241606f},
{-0.482699f,0.282007f,-0.241606f},{-0.5f,-0.282007f,0.241606f},{-0.5f,-0.282007f,-0.241606f},
{0.5f,-0.282007f,0.241606f},{0.5f,-0.282007f,-0.241606f},{0.5f,0.282007f,0.241606f},
{0.5f,0.282007f,-0.241606f}
};
static GLfloat normals [7][3] = {
{-1.0f,0.0f,0.0f},{0.0f,1.0f,0.0f},{1.0f,0.0f,0.0f},
{-0.99953f,0.0306604f,0.0f},{0.0f,-1.0f,0.0f},{0.0f,0.0f,1.0f},
{0.0f,0.0f,-1.0f}
};
GLfloat textures[1][2]={{0.0f,0.0f}};
/*Material indicies*/
/*{material index,face count}*/
static int material_ref [1][2] = {
{0,28}
};
void MyMaterial(GLenum mode, GLfloat * f, GLfloat alpha)
{
	GLfloat d[4];
	d[0] = f[0];
	d[1] = f[1];
	d[2] = f[2];
	d[3] = alpha;
	glMaterialfv(GL_FRONT_AND_BACK, mode, d);
}

/*
 *  SelectMaterial uses OpenGL commands to define facet colors.
 *
 *  Returns:
 *    Nothing
 */

void SelectMaterial(int i)
{
//
// Define the reflective properties of the 3D Object faces.
//
	glEnd();
	GLfloat alpha = materials[i].alpha;
	MyMaterial(GL_AMBIENT, materials[i].ambient, alpha);
	MyMaterial(GL_DIFFUSE, materials[i].diffuse, alpha);
	MyMaterial(GL_SPECULAR, materials[i].specular, alpha);
	MyMaterial(GL_EMISSION, materials[i].emission, alpha);
	glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, materials[i].phExp);
	glBegin(GL_TRIANGLES);

};

//返回一个显示列表
GLint Gen3DObjectList()
{
	int i;
	int j;

 GLint lid=glGenLists(1);
	int mcount=0;
	int mindex=0;
   glNewList(lid, GL_COMPILE);

    glBegin (GL_TRIANGLES);
      for(i=0;i<sizeof(face_indicies)/sizeof(face_indicies[0]);i++)
       {
	if (!mcount)
	{
		SelectMaterial(material_ref[mindex][0]);
		mcount = material_ref[mindex][1];
		mindex ++;
	}
	mcount --;
       for(j=0;j<3;j++)
        {
          int vi=face_indicies[i][j];
          int ni=face_indicies[i][j+3];//Normal index
          int ti=face_indicies[i][j+6];//Texture index
           glNormal3f (normals[ni][0],normals[ni][1],normals[ni][2]);
           glTexCoord2f(textures[ti][0],textures[ti][1]);
           glVertex3f (vertices[vi][0],vertices[vi][1],vertices[vi][2]);
        }
       }
    glEnd ();

   glEndList();
   return lid;
};

void myDisplay(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
	lid = Gen3DObjectList();
	//调用这个显示列表,这个是最关键的
	glCallList(lid);
	glFlush();
}

void main(void)
{  
	 glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	 glutInitWindowPosition(100, 100); 
	 glutInitWindowSize(400, 400); 
	 glutCreateWindow("opengl"); 
	 glutDisplayFunc(&myDisplay); 
	 glutMainLoop(); 
}

现在这个时候编译运行的话,就会有东西显示出来了。


最后是关于这个模型的材质和贴图问题,有兴趣的可以讨论交流。因为我现在在做android增强现实方面的东西,之前有涉及到模型导入的问题,就粗略的看了一下关于opengl导入3d模型的知识。也在android AR中用上面的一些点和面等等的数据做了实验,发现简单的模型是可行的,但是复杂的模型的话,其中这些

  • 2
    点赞
  • 43
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值