#include "ObjLoader.h"
#include <fstream>
#include <iostream>
using namespace std;
//GLfloat m = 1.0f;
ObjLoader::ObjLoader(string filename)
{
string line;
fstream f;
f.open(filename, ios::in);
if (!f.is_open()){
cout << "Something Went Wrong When Opening Objfiles" << endl;
}
while (!f.eof()){
getline(f, line);
vector<string>parameters;
string tailMark = " ";
string ans = "";
line = line.append(tailMark);
for (int i = 0; i < line.length(); i++) {
char ch = line[i];
if (ch != ' ') {
ans+=ch;
}
else {
parameters.push_back(ans);
ans = "";
}
}
//cout << parameters.size() << endl;
if (parameters.size() != 4) {
cout << "the size is not correct" << endl;
}
else {
if (parameters[0] == "v") {
vector<GLfloat>Point;
for (int i = 1; i < 4; i++) {
GLfloat xyz = atof(parameters[i].c_str());
Point.push_back(xyz);
}
vSets.push_back(Point);
}
else if (parameters[0] == "f") {
vector<GLint>vIndexSets;
for (int i = 1; i < 4; i++){
string x = parameters[i];
string ans = "";
for (int j = 0; j < x.length(); j++) {
char ch = x[j];
if (ch != '/') {
ans += ch;
}
else {
break;
}
}
GLint index = atof(ans.c_str());
index = index--;
vIndexSets.push_back(index);
}
fSets.push_back(vIndexSets);
}
/*else if (parameters[0] == "vn") {
vector<GLfloat>vnValueSets;
for (int i = 1; i < 4; i++) {
GLfloat vnxyz = atof(parameters[i].c_str());
vnValueSets.push_back(vnxyz);
}
vnSets.push_back(vnValueSets);
}
else if (parameters[0] == "vt") {
vector<GLfloat>vtValueSets;
for (int i = 1; i < 4; i++) {
GLfloat vnxyz = atof(parameters[i].c_str());
vtValueSets.push_back(vnxyz);
}
vtSets.push_back(vtValueSets);
}*/
}
}
f.close();
}
//void ObjLoader::Init()
//{
// lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
// animationLastingTime = 0;
//}
//
//GLfloat morphRadis(vector<vector<GLfloat>>x) {
// GLfloat maxL = 0.0f;
// for (int i = 0; i < x.size(); i++)
// {
// GLfloat vertex[3];
// vertex[0] = (x[i])[0];
// vertex[1] = (x[i])[1];
// vertex[2] = (x[i])[2];
//
// GLfloat L = sqrt(vertex[0] * vertex[0] + vertex[1] * vertex[1] + vertex[2] * vertex[2]);
// if (L > maxL) {
// maxL = L;
// }
// }
//
// return maxL;
//}
void ObjLoader::Draw(){
//GLfloat morphR = morphRadis(vSets);
//cout << morphR << endl;
//const GLint animationEndTime = 4000;
//GLint currentFrameTime = glutGet(GLUT_ELAPSED_TIME);
//animationLastingTime += currentFrameTime - lastFrameTime;
/*GLfloat step = (GLfloat)animationLastingTime / animationEndTime;
if (step > 1.0f) {
step = 1.0f;
glutSolidSphere(morphR, 40, 50);
}*/
glBegin(GL_TRIANGLES);
for (int i = 0; i < fSets.size(); i++) {
GLfloat VN[3];
//three vertex represrnt a surface
GLfloat SV1[3];
GLfloat SV2[3];
GLfloat SV3[3];
/*GLfloat EV1[3];
GLfloat EV2[3];
GLfloat EV3[3];
GLfloat SV1[3];
GLfloat SV2[3];
GLfloat SV3[3];*/
if ((fSets[i]).size() != 3) {
cout << "the fSetsets_Size is not correct" << endl;
}
else {
GLint firstVertexIndex = (fSets[i])[0];
GLint secondVertexIndex = (fSets[i])[1];
GLint thirdVertexIndex = (fSets[i])[2];
/*V1[0] = (vSets[firstVertexIndex])[0];
V1[1] = (vSets[firstVertexIndex])[1];
V1[2] = (vSets[firstVertexIndex])[2];
V2[0] = (vSets[secondVertexIndex])[0];
V2[1] = (vSets[secondVertexIndex])[1];
V2[2] = (vSets[secondVertexIndex])[2];
V3[0] = (vSets[thirdVertexIndex])[0];
V3[1] = (vSets[thirdVertexIndex])[1];
V3[2] = (vSets[thirdVertexIndex])[2];*/
SV1[0] = (vSets[firstVertexIndex])[0];
SV1[1] = (vSets[firstVertexIndex])[1];
SV1[2] = (vSets[firstVertexIndex])[2];
SV2[0] = (vSets[secondVertexIndex])[0];
SV2[1] = (vSets[secondVertexIndex])[1];
SV2[2] = (vSets[secondVertexIndex])[2];
SV3[0] = (vSets[thirdVertexIndex])[0];
SV3[1] = (vSets[thirdVertexIndex])[1];
SV3[2] = (vSets[thirdVertexIndex])[2];
/*GLfloat lv1 = sqrt(V1[0] * V1[0] + V1[1] * V1[1] + V1[2] * V1[2]);
GLfloat lv2 = sqrt(V2[0] * V2[0] + V2[1] * V2[1] + V2[2] * V2[2]);
GLfloat lv3 = sqrt(V3[0] * V3[0] + V3[1] * V3[1] + V3[2] * V3[2]);
GLfloat proportion1 = lv1 / morphR;
GLfloat proportion2 = lv2 / morphR;
GLfloat proportion3 = lv3 / morphR;
EV1[0] = V1[0] / proportion1;
EV1[1] = V1[1] / proportion1;
EV1[2] = V1[2] / proportion1;
EV2[0] = V2[0] / proportion2;
EV2[1] = V2[1] / proportion2;
EV2[2] = V2[2] / proportion2;
EV3[0] = V3[0] / proportion3;
EV3[1] = V3[1] / proportion3;
EV3[2] = V3[2] / proportion3;
SV1[0] = EV1[0] * step + (1 - step) * V1[0];
SV1[1] = EV1[1] * step + (1 - step) * V1[1];
SV1[2] = EV1[2] * step + (1 - step) * V1[2];
SV2[0] = EV2[0] * step + (1 - step) * V2[0];
SV2[1] = EV2[1] * step + (1 - step) * V2[1];
SV2[2] = EV2[2] * step + (1 - step) * V2[2];
SV3[0] = EV3[0] * step + (1 - step) * V3[0];
SV3[1] = EV3[1] * step + (1 - step) * V3[1];
SV3[2] = EV3[2] * step + (1 - step) * V3[2];*/
//if there is no vn in the objfile
//if (vnSets.size() == 0) {
GLfloat vec1[3], vec2[3], vec3[3];
//(x2-x1,y2-y1,z2-z1)
vec1[0] = SV1[0] - SV2[0];
vec1[1] = SV1[1] - SV2[1];
vec1[2] = SV1[2] - SV2[2];
//(x3-x2,y3-y2,z3-z2)
vec2[0] = SV1[0] - SV3[0];
vec2[1] = SV1[1] - SV3[1];
vec2[2] = SV1[2] - SV3[2];
//(x3-x1,y3-y1,z3-z1)
vec3[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1];
vec3[1] = vec2[0] * vec1[2] - vec2[2] * vec1[0];
vec3[2] = vec2[1] * vec1[0] - vec2[0] * vec1[1];
GLfloat D = sqrt(pow(vec3[0], 2) + pow(vec3[1], 2) + pow(vec3[2], 2));
VN[0] = vec3[0] / D;
VN[1] = vec3[1] / D;
VN[2] = vec3[2] / D;
glNormal3f(VN[0], VN[1], VN[2]);
//}
//if already have vn
/*else {
if (i < vnSets.size()) {
VN[0] = (vnSets[i])[0];
VN[1] = (vnSets[i])[1];
VN[2] = (vnSets[i])[2];
glNormal3f(VN[0], VN[1], VN[2]);
}
}*/
glVertex3f(SV1[0], SV1[1], SV1[2]);
glVertex3f(SV2[0], SV2[1], SV2[2]);
glVertex3f(SV3[0], SV3[1], SV3[2]);
}
}
glEnd();
//lastFrameTime = currentFrameTime;
}
妙为
- 粉丝: 969
- 资源: 211
最新资源
- 博途S7-1200主站与S7-200从站实现RS485通讯程序 S7-200可以当作一个仪表
- C#、C++分别开发的OPC DA CLIENT软件. 1、枚举服务器名称; 2、连接服务器以后枚举出TAG; 3、根据TAG名称自动读取服务器数据; 4、图片内有OPC SERVER和CLIENT实
- python-workspace.zip.005
- 龙门上下料样本程序,四轴 用台达AS228T和台达触摸屏编写 注意软件是用台达新款软件ISPSOFT ,借鉴价值高,程序有注释
- 一款window下的串口监视抓包工具
- 欧姆龙CP1H与3台力士乐VFC-x610变频器通讯程序 功能:原创程序,可直接用于现场程序 欧姆龙CP1H的CIF11通讯板,实现对3台力士乐VFC-x610变频器 设定频率,控制正反转,读取实际
- dp111113333
- CV-密集人群图像数据集(5800张图片).rar
- 福特汽车主观评价规范,性能开发参考,英文原版直译,评价条目、规则描述非常细致 包含平顺舒适性,转向,操稳,NVH,制动,加速感,驾驶性等等性能,并详细描述了评价的准备工作 评价条目细分至第四级,共
- 三菱FX3S两轴标准程序,XZ两轴,包含轴点动,回零,相对与绝对定位,只要弄明白这个程序,就可以非常了解整个项目的程序如何去编写,从哪里开始下手,可提供程序问题解答,程序流程清晰明了,注释完整
- MATLAB代码:考虑P2G与碳捕集机组的多能微网低碳经济调度 关键词:碳交易 阶梯碳交易 碳捕集 多能微网 低碳调度 仿真平台:MATLAB+yalmip+cplex 主要内容:代码主要做的是一个
- 本程序采用matlab编写,主要是实现电流注入型牛拉法 除此之外,本人还编写了很多种关于潮流计算的程序,主要有牛拉法,前推回代法,以还有相和三相潮流计算程序
- 智能门锁架构图,供大家参考
- 三菱FX3U六轴标准程序,程序包含本体3轴控制,扩展3个1PG定位模块,一共六轴 程序有轴点动控制,回零控制,相对定位,绝对定位 另有气缸数个,一个大是DD马达控制的转盘,整个是转盘多工位流水作业
- 批量登录到远程Linux服务器检查服务器时间差的shell
- MATLAB电动车七自由度整车模型 MATLAB Simulink电动车转弯制动abs模型asr转弯制动防抱死abs模型+模糊控制算法+七自由度整车模型+纵向运动+侧向运动+横摆运动+四轮魔术公式+四
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈