3条回答
- apple1234a 2016-07-08 08:27关注
#include
#include
#include
#include
#include
#include#include
#include
#include
#include
#include
#include#define KEY_ESCAPE 27
using namespace std;
/************************************************************************
Window
************************************************************************/typedef struct {
int width;
int height;
char* title;float field_of_view_angle; float z_near; float z_far;
} glutWindow;
/***************************************************************************
OBJ Loading
***************************************************************************/class Model_OBJ
{
public:
Model_OBJ();
float calculateNormal(const float* coord1,float* coord2,float* coord3 );
int Load(char *filename); // Loads the model
void Draw(); // Draws the model on the screen
void Release(); // Release the modelfloat* normals; // Stores the normals float* Faces_Triangles; // Stores the triangles float* vertexBuffer; // Stores the points which make the object long TotalConnectedPoints; // Stores the total number of connected verteces long TotalConnectedTriangles; // Stores the total number of connected triangles
};
#define POINTS_PER_VERTEX 3
#define TOTAL_FLOATS_IN_TRIANGLE 9
using namespace std;Model_OBJ::Model_OBJ()
{
this->TotalConnectedTriangles = 0;
this->TotalConnectedPoints = 0;
}float Model_OBJ::calculateNormal( const float* coord1,float* coord2,float* coord3 )
{
/* calculate Vector1 and Vector2 */
float va[3], vb[3], vr[3], val;
va[0] = coord1[0] - coord2[0];
va[1] = coord1[1] - coord2[1];
va[2] = coord1[2] - coord2[2];vb[0] = coord1[0] - coord3[0];
vb[1] = coord1[1] - coord3[1];
vb[2] = coord1[2] - coord3[2];/* cross product */
vr[0] = va[1] * vb[2] - vb[1] * va[2];
vr[1] = vb[0] * va[2] - va[0] * vb[2];
vr[2] = va[0] * vb[1] - vb[0] * va[1];/* normalization factor */
val = sqrt( vr[0]*vr[0] + vr[1]*vr[1] + vr[2]*vr[2] );float norm[3]; norm[0] = vr[0]/val; norm[1] = vr[1]/val; norm[2] = vr[2]/val; return* norm;
}
int Model_OBJ::Load(char* filename)
{
string line;
ifstream objFile (filename);
if (objFile.is_open()) // If obj file is open, continue
{
objFile.seekg (0, ios::end); // Go to end of the file,
long fileSize = objFile.tellg(); // get file size
objFile.seekg (0, ios::beg); // we'll use this to register memory for our 3d modelvertexBuffer = (float*) malloc (fileSize); // Allocate memory for the verteces Faces_Triangles = (float*) malloc(fileSize*sizeof(float)); // Allocate memory for the triangles normals = (float*) malloc(fileSize*sizeof(float)); // Allocate memory for the normals int triangle_index = 0; // Set triangle index to zero int normal_index = 0; // Set normal index to zero while (! objFile.eof() ) // Start reading file data { getline (objFile,line); // Get line from file if (line.c_str()[0] == 'v') // The first character is a v: on this line is a vertex stored. { line[0] = ' '; // Set first character to 0. This will allow us to use sscanf sscanf(line.c_str(),"%f %f %f ", // Read floats from the line: v X Y Z &vertexBuffer[TotalConnectedPoints], &vertexBuffer[TotalConnectedPoints+1], &vertexBuffer[TotalConnectedPoints+2]); TotalConnectedPoints += POINTS_PER_VERTEX; // Add 3 to the total connected points } if (line.c_str()[0] == 'f') // The first character is an 'f': on this line is a point stored { line[0] = ' '; // Set first character to 0. This will allow us to use sscanf int vertexNumber[4] = { 0, 0, 0 }; sscanf(line.c_str(),"%i%i%i", // Read integers from the line: f 1 2 3 &vertexNumber[0], // First point of our triangle. This is an &vertexNumber[1], // pointer to our vertexBuffer list &vertexNumber[2] ); // each point represents an X,Y,Z. vertexNumber[0] -= 1; // OBJ file starts counting from 1 vertexNumber[1] -= 1; // OBJ file starts counting from 1 vertexNumber[2] -= 1; // OBJ file starts counting from 1 /******************************************************************** * Create triangles (f 1 2 3) from points: (v X Y Z) (v X Y Z) (v X Y Z). * The vertexBuffer contains all verteces * The triangles will be created using the verteces we read previously */ int tCounter = 0; for (int i = 0; i < POINTS_PER_VERTEX; i++) { Faces_Triangles[triangle_index + tCounter ] = vertexBuffer[3*vertexNumber[i] ]; Faces_Triangles[triangle_index + tCounter +1 ] = vertexBuffer[3*vertexNumber[i]+1 ]; Faces_Triangles[triangle_index + tCounter +2 ] = vertexBuffer[3*vertexNumber[i]+2 ]; tCounter += POINTS_PER_VERTEX; } /********************************************************************* * Calculate all normals, used for lighting */ float coord1[3] = { Faces_Triangles[triangle_index], Faces_Triangles[triangle_index+1],Faces_Triangles[triangle_index+2]}; float coord2[3] = {Faces_Triangles[triangle_index+3],Faces_Triangles[triangle_index+4],Faces_Triangles[triangle_index+5]}; float coord3[3] = {Faces_Triangles[triangle_index+6],Faces_Triangles[triangle_index+7],Faces_Triangles[triangle_index+8]}; float* norm = this->calculateNormal( coord1, coord2, coord3 ); tCounter = 0; for (int i = 0; i < POINTS_PER_VERTEX; i++) { normals[normal_index + tCounter ] = norm[0]; normals[normal_index + tCounter +1] = norm[1]; normals[normal_index + tCounter +2] = norm[2]; tCounter += POINTS_PER_VERTEX; } triangle_index += TOTAL_FLOATS_IN_TRIANGLE; normal_index += TOTAL_FLOATS_IN_TRIANGLE; TotalConnectedTriangles += TOTAL_FLOATS_IN_TRIANGLE; } } objFile.close(); // Close OBJ file }
解决 无用评论 打赏 举报
悬赏问题
- ¥15 安卓adb backup备份应用数据失败
- ¥15 eclipse运行项目时遇到的问题
- ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
- ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
- ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
- ¥50 成都蓉城足球俱乐部小程序抢票
- ¥15 yolov7训练自己的数据集
- ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
- ¥15 电力市场出清matlab yalmip kkt 双层优化问题
- ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)