作为我项目的一部分,我需要绘制一些3D模型。
没有时间了解3ds文件以及如何加载它们, 看起来很复杂, 所以我在网上找到了功能, 但是物体不会渲染(或者渲染但不会被渲染)。
这里是相关的源代码:
void initGL(int *argc, char **argv)
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width, height);
glutCreateWindow("CUDA Particle System");
GLenum err = glewInit();
if (GLEW_OK != err)
{
printf ("Error: openGL is not supported");
exit (0);
}
glEnable(GL_DEPTH_TEST);
glEnable (GL_CULL_FACE);
glCullFace(GL_FRONT);
glClearColor( 0.7, 0.7 , 0.9 , 0 );
glutReportErrors();
glFrustum(-1,1,-1,1,0.3,100);
Chesspawn.load( &chesspawn ,"chesspawn.3DS");
/*glClear (GL_COLOR_BUFFER_BIT);
glColor3f ( 0.7, 0.7 , 0.9 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1,1,-1,1);
gluLookAt(0,0,0,0,0,-100,0,1,0);
glViewport (250, 250, width, height);*/
}
void display()
{
sdkStartTimer(&timer.timer);
p_system->update(time_Step);
//set Positions vbo
renderer->setVertexBuffer(p_system->getPosVbo(),p_system->getNumParticles());
//render
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// view transform
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetFloatv(GL_MODELVIEW_MATRIX, modelView);
gluLookAt (Params_Camera[CAMERA_X], Params_Camera[CAMERA_Y], Params_Camera[CAMERA_Z], // camera
Params_Camera[CAMERA_X] + Params_Camera[DIR_X], Params_Camera[CAMERA_Y] , Params_Camera[CAMERA_Z] + Params_Camera[DIR_Z], // center
0, 1, 0 // up
);
renderer->display();
drawFloor();
glScaled (0.001,0.001,1);
Chesspawn.render(&chesspawn);
//glutSolidSphere (0.5,50,50);
/*glTranslated (0,0,-10);
glutSolidSphere(0.1,20,20);*/
sdkStopTimer(&timer.timer);
glutSwapBuffers();
glutReportErrors();
char sFps[256];
fps = timer.computeFps();
sprintf(sFps, " %s: %d particles %3.1f fps",proc, numParticles, fps);
if (fps <24.0)
printf ("Slow fps : %f \n", fps);
glutSetWindowTitle(sFps);
}
这是加载和渲染函数:
static char C_3dsObject::load (obj_type_ptr p_object, char *p_filename)
{
int i; //Index variable
int File_Length = 0;
FILE *l_file; //File pointer
unsigned short l_chunk_id; //Chunk identifier
unsigned int l_chunk_lenght; //Chunk lenght
unsigned char l_char; //Char variable
unsigned short l_qty; //Number of elements in each chunk
unsigned short l_face_flags; //Flag that stores some face information
if ((l_file=fopen (p_filename, "rb"))== NULL)
return 0; //Open the file
fseek (l_file, 0, SEEK_END); // get file length
File_Length = ftell (l_file);
fseek(l_file, 0L, SEEK_SET);
while (ftell (l_file) < File_Length ) //Loop to scan the whole file
{
//getche(); //Insert this command for debug (to wait for keypress for each chuck reading)
fread (&l_chunk_id, 2, 1, l_file); //Read the chunk header
//printf("ChunkID: %x\n",l_chunk_id);
fread (&l_chunk_lenght, 4, 1, l_file); //Read the lenght of the chunk
//printf("ChunkLenght: %x\n",l_chunk_lenght);
switch (l_chunk_id)
{
//----------------- MAIN3DS -----------------
// Description: Main chunk, contains all the other chunks
// Chunk ID: 4d4d
// Chunk Lenght: 0 + sub chunks
//-------------------------------------------
case 0x4d4d:
break;
//----------------- EDIT3DS -----------------
// Description: 3D Editor chunk, objects layout info
// Chunk ID: 3d3d (hex)
// Chunk Lenght: 0 + sub chunks
//-------------------------------------------
case 0x3d3d:
break;
//--------------- EDIT_OBJECT ---------------
// Description: Object block, info for each object
// Chunk ID: 4000 (hex)
// Chunk Lenght: len(object name) + sub chunks
//-------------------------------------------
case 0x4000:
i=0;
do
{
fread (&l_char, 1, 1, l_file);
p_object->name[i]=l_char;
i++;
}while(l_char != '\0' && i<20);
break;
//--------------- OBJ_TRIMESH ---------------
// Description: Triangular mesh, contains chunks for 3d mesh info
// Chunk ID: 4100 (hex)
// Chunk Lenght: 0 + sub chunks
//-------------------------------------------
case 0x4100:
break;
//--------------- TRI_VERTEXL ---------------
// Description: Vertices list
// Chunk ID: 4110 (hex)
// Chunk Lenght: 1 x unsigned short (number of vertices)
// + 3 x float (vertex coordinates) x (number of vertices)
// + sub chunks
//-------------------------------------------
case 0x4110:
fread (&l_qty, sizeof (unsigned short), 1, l_file);
p_object->vertices_qty = l_qty;
//printf("Number of vertices: %d\n",l_qty);
for (i=0; i<l_qty; i++)
{
fread (&p_object->vertex[i].x, sizeof(float), 1, l_file);
//printf("Vertices list x: %f\n",p_object->vertex[i].x);
fread (&p_object->vertex[i].y, sizeof(float), 1, l_file);
//printf("Vertices list y: %f\n",p_object->vertex[i].y);
fread (&p_object->vertex[i].z, sizeof(float), 1, l_file);
//printf("Vertices list z: %f\n",p_object->vertex[i].z);
//Insert into the database
}
break;
//--------------- TRI_FACEL1 ----------------
// Description: Polygons (faces) list
// Chunk ID: 4120 (hex)
// Chunk Lenght: 1 x unsigned short (number of polygons)
// + 3 x unsigned short (polygon points) x (number of polygons)
// + sub chunks
//-------------------------------------------
case 0x4120:
fread (&l_qty, sizeof (unsigned short), 1, l_file);
p_object->polygons_qty = l_qty;
//printf("Number of polygons: %d\n",l_qty);
for (i=0; i<l_qty; i++)
{
fread (&p_object->polygon[i].a, sizeof (unsigned short), 1, l_file);
//printf("Polygon point a: %d\n",p_object->polygon[i].a);
fread (&p_object->polygon[i].b, sizeof (unsigned short), 1, l_file);
//printf("Polygon point b: %d\n",p_object->polygon[i].b);
fread (&p_object->polygon[i].c, sizeof (unsigned short), 1, l_file);
//printf("Polygon point c: %d\n",p_object->polygon[i].c);
fread (&l_face_flags, sizeof (unsigned short), 1, l_file);
//printf("Face flags: %x\n",l_face_flags);
}
break;
//------------- TRI_MAPPINGCOORS ------------
// Description: Vertices list
// Chunk ID: 4140 (hex)
// Chunk Lenght: 1 x unsigned short (number of mapping points)
// + 2 x float (mapping coordinates) x (number of mapping points)
// + sub chunks
//-------------------------------------------
//----------- Skip unknow chunks ------------
//We need to skip all the chunks that currently we don't use
//We use the chunk lenght information to set the file pointer
//to the same level next chunk
//-------------------------------------------
default:
fseek(l_file, l_chunk_lenght-6, SEEK_CUR);
}
}
fclose (l_file); // Closes the file stream
return (1); // Returns ok
}
void render (obj_type_ptr object)
{
int l_index;
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glMatrixMode(GL_MODELVIEW); // Modeling transformation
glLoadIdentity();
//glTranslatef(0.0,0.0,-500.0);
glColor3d(1,1,0);
// Rotations of the object (the model matrix is multiplied by the rotation matrices)
glRotatef(rotation_x,1.0,0.0,0.0);
glRotatef(rotation_y,0.0,1.0,0.0);
glRotatef(rotation_z,0.0,0.0,1.0);
// TODO: Add your message handler code here
//rotation_x = rotation_x + rotation_x_increment; /////
//rotation_y = rotation_y + rotation_y_increment;
//rotation_z = rotation_z + rotation_z_increment;
if (rotation_x > 359) rotation_x = 0;
if (rotation_y > 359) rotation_y = 0;
if (rotation_z > 359) rotation_z = 0;
// glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)
glBegin(GL_TRIANGLES);
for (l_index=0;l_index < object->polygons_qty;l_index++)
{
//----------------- FIRST VERTEX -----------------
// Coordinates of the first vertex
glVertex3f( object->vertex[ object->polygon[l_index].a ].x,
object->vertex[ object->polygon[l_index].a ].y,
object->vertex[ object->polygon[l_index].a ].z);
//Vertex definition
//----------------- SECOND VERTEX -----------------
// Coordinates of the second vertex
//float x= object.vertex[ object.polygon[l_index].b ].x;
glVertex3f( object->vertex[ object->polygon[l_index].b ].x,
object->vertex[ object->polygon[l_index].b ].y,
object->vertex[ object->polygon[l_index].b ].z);
//----------------- THIRD VERTEX -----------------
// Coordinates of the Third vertex
glVertex3f( object->vertex[ object->polygon[l_index].c ].x,
object->vertex[ object->polygon[l_index].c ].y,
object->vertex[ object->polygon[l_index].c ].z);
}
glEnd();
glutSwapBuffers();
}
到目前为止我尝试了什么:
对于附加到代码的3ds文件 - 似乎是的 我找到的其他3ds文件 - 没有。
尝试上下缩放对象 - 无结果
用glutSolidSphere(0.5,50,50)替换render()中的所有代码 - 球体可见
我禁用了glClear()和glMatrixMode() 多数民众赞成因为我认为他们不需要, 如果启用它们,则会在不看到3ds对象的情况下使所有其他对象消失。
我的问题是:
load()看起来是否正确?
还有其他3ds文件的顶点,这个load()不是“foot”吗?
是否有任何设置为openGL我可以尝试更改以查看对象?
您知道其他3d模型格式,它更容易加载并且分发良好 并且很容易找到,因为3ds是?
答案 0 :(得分:0)
你知道其他3d模型格式,它更容易加载并且分布均匀且易于查找,就像3ds一样吗?
要从3d studio max加载对象,您可以尝试使用obj波前格式(3ds max中有一个选项可以以obj格式导入)它是人类可读的格式,包含顶点,顶点法线和顶点纹理三元组,可以很容易地解析并传递给过剩以进行渲染。
我写了一些用于加载3d模型的代码,可用的源代码here
它解析obj文件并将顶点,纹理坐标和法线存储在数组中,这些数组传递给glut函数进行渲染
有关obj格式的更多信息here
答案 1 :(得分:0)
如果您是渲染3D模型的初学者,请不要尝试使用obj格式的3ds模型。 Obj简单易读,易于理解并编写自己的解析器。您可以从 here 学习简单的教程,然后开始编写自己的解析器。