opengl 读取obj模型
生活随笔
收集整理的這篇文章主要介紹了
opengl 读取obj模型
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
```cpp
在這里插入代碼片
// #include <glut.h>
#include <iostream>
#include <math.h>
#include <string>
#include <stdio.h>
#include <sstream>
#include <fstream>
#include <windows.h>
using namespace std;int v_num = 0; //��?�������
int f_num = 0; //��?�������
int vn_num = 0; //��?���������
int vt_num = 0; //��?�������������GLfloat **vArr; //��?�?�?����
GLfloat **vnArr; //��?���?�?����
GLfloat **vtArr; //�����������?�?����int **fvArr; //����涥��?�?����
int **fnArr; //����淨��?�?����
int **ftArr; //�������������?�?����GLfloat angle_galaxy = 0.0f; //��?��?���?�
GLfloat galaxy_r = 0.5;
GLfloat galaxy_g = 0.0;
GLfloat galaxy_b = 0.0;
GLfloat emission_galaxy[3];GLfloat blank_Emission[4] = {0.0f, 0.0f, 0.0f, 0.0f};
void destoryObj()
{v_num = 0; //��?�������f_num = 0; //��?�������vn_num = 0; //��?���������vt_num = 0; //��?�������������vArr = NULL;vnArr = NULL;vtArr = NULL;fvArr = NULL;fnArr = NULL;ftArr = NULL;
}void getLineNum(string addrstr) //��?����������
{v_num = 0; //��?�������f_num = 0; //��?�������vn_num = 0; //��?���������vt_num = 0; //��?�������������ifstream infile(addrstr.c_str()); //��?���?�string sline; //??��while (getline(infile, sline)) //��?���?�����{if (sline.empty())continue;if (sline[0] == 'v'){if (sline[1] == 'n')vn_num++;if (sline[1] == 't')vt_num++;elsev_num++;}if (sline[0] == 'f'){f_num++;}}infile.close();
}int readfile(string addrstr) //���?����?���������?
{//new��?����int i;vArr = new GLfloat *[v_num]; //��������for (i = 0; i < v_num; i++){vArr[i] = new GLfloat[3];}vnArr = new GLfloat *[vn_num]; //����for (i = 0; i < vn_num; i++){vnArr[i] = new GLfloat[3];}vtArr = new GLfloat *[vt_num]; //��������for (i = 0; i < vt_num; i++){vtArr[i] = new GLfloat[2];}fvArr = new int *[f_num];fnArr = new int *[f_num];ftArr = new int *[f_num];for (i = 0; i < f_num; i++){fvArr[i] = new int[3];fnArr[i] = new int[3];ftArr[i] = new int[3];}/////��?��?��?����ifstream infile(addrstr.c_str());string sline; //??��int vIndex = 0, vnIndex = 0, vtIndex = 0;int fIndex = 0;int v, t, n;string str;while (getline(infile, sline)){if (sline.empty())continue;//��������if (sline[0] == 'v'){if (sline[1] == 'n') //vn{istringstream sin(sline); //istringstream������?�?���?�����?���??�?�?���?��з?������sin >> str >> vnArr[vnIndex][0] >> vnArr[vnIndex][1] >> vnArr[vnIndex][2];vnIndex++;}else if (sline[1] == 't') //vt{istringstream sin(sline);sin >> str >> vtArr[vtIndex][0] >> vtArr[vtIndex][1] >> vtArr[vtIndex][2];vtIndex++;}else //v{istringstream sin(sline);sin >> str >> vArr[vIndex][0] >> vArr[vIndex][1] >> vArr[vIndex][2];vIndex++;}} //end if(sline[0]=='v')//������if (sline[0] == 'f') //�洢��{istringstream in(sline); //sline can be one of v, v//n, v/t, v/t/nif (sline.find("//") != string::npos) //?����v//n npos ��?��������������?�����?�λ�� ?��?-1{in >> str; //?��f// in >> v0 >> "//" >> vn0; //���������������?�?��sscanf���������?��?�?�?�����sscanf�?�in >> str;sscanf_s(str.c_str(), "%d//%d", &fvArr[fIndex][0], &fnArr[fIndex][0]);in >> str;sscanf_s(str.c_str(), "%d//%d", &fvArr[fIndex][1], &fnArr[fIndex][1]);in >> str;sscanf_s(str.c_str(), "%d//%d", &fvArr[fIndex][2], &fnArr[fIndex][2]);}else if (sscanf_s(sline.c_str(), "f %d/%d/%d", &v, &t, &n) == 3){fvArr[fIndex][0] = v;ftArr[fIndex][0] = t;fnArr[fIndex][0] = n;in >> str >> str; //��f�?�?��������?�������?in >> str;sscanf_s(str.c_str(), "%d/%d/%d", &fvArr[fIndex][1], &ftArr[fIndex][1], &fnArr[fIndex][1]);in >> str;sscanf_s(str.c_str(), "%d/%d/%d", &fvArr[fIndex][2], &ftArr[fIndex][2], &fnArr[fIndex][2]);}else if (sscanf_s(sline.c_str(), "f %d/%d", &v, &t) == 2){fvArr[fIndex][0] = v;ftArr[fIndex][0] = t;in >> str >> str; //��f�?�?��������?�������?in >> str;sscanf_s(str.c_str(), "%d/%d", &fvArr[fIndex][1], &ftArr[fIndex][1]);in >> str;sscanf_s(str.c_str(), "%d/%d", &fvArr[fIndex][2], &ftArr[fIndex][2]);}else //v{in >> str; //fin >> str;sscanf_s(str.c_str(), "%d", &fvArr[fIndex][0]);in >> str;sscanf_s(str.c_str(), "%d", &fvArr[fIndex][1]);in >> str;sscanf_s(str.c_str(), "%d", &fvArr[fIndex][2]);}fIndex++;} //end if(sline[0]=='f')} //end whileinfile.close();return 0;
}glPushMatrix();galaxy_r = fabs(sin(angle_galaxy * 0.1)) * 0.8;galaxy_g = fabs(cos(angle_galaxy * 0.1)) * 0.6;galaxy_b = fabs(sin(angle_galaxy * 0.1)) * 0.4;emission_galaxy[0] = galaxy_r;emission_galaxy[1] = galaxy_g;emission_galaxy[2] = galaxy_b;glRotatef(angle_galaxy, 0, 0, 1);glTranslatef(20, -10, 3);drawObj(GL_POINTS);glTranslatef(-30, -40, 3);drawObj(GL_POINTS);glMaterialfv(GL_FRONT, GL_EMISSION, blank_Emission);glPopMatrix();void drawObj(int mode)
{glBegin(mode);glScalef(0.4, 0.4, 0.4);for (int i = 0; i < f_num; i++) //??���÷����?��?�?��?�??����?����??��{glMaterialfv(GL_FRONT, GL_EMISSION, emission_galaxy);glNormal3f(vnArr[fnArr[i][0] - 1][0], vnArr[fnArr[i][0] - 1][1], vnArr[fnArr[i][0] - 1][2]);glVertex3f(vArr[fvArr[i][0] - 1][0], vArr[fvArr[i][0] - 1][1], vArr[fvArr[i][0] - 1][2]);glMaterialfv(GL_FRONT, GL_EMISSION, vEmission_sun); //vEmission_sunglNormal3f(vnArr[fnArr[i][1] - 1][0], vnArr[fnArr[i][1] - 1][1], vnArr[fnArr[i][1] - 1][2]);glVertex3f(vArr[fvArr[i][1] - 1][0], vArr[fvArr[i][1] - 1][1], vArr[fvArr[i][1] - 1][2]);emission_galaxy[0] = fabs(cos(angle_galaxy * 0.1)) * 0.3;emission_galaxy[1] = fabs(sin(angle_galaxy * 0.1)) * 0.5;emission_galaxy[2] = fabs(cos(angle_galaxy * 0.1)) * 0.7;glMaterialfv(GL_FRONT, GL_EMISSION, emission_galaxy);glNormal3f(vnArr[fnArr[i][2] - 1][0], vnArr[fnArr[i][2] - 1][1], vnArr[fnArr[i][2] - 1][2]);glVertex3f(vArr[fvArr[i][2] - 1][0], vArr[fvArr[i][2] - 1][1], vArr[fvArr[i][2] - 1][2]);}glEnd();
}
總結
以上是生活随笔為你收集整理的opengl 读取obj模型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 牛年上班第一天,祝大家工作顺利,还在路上
- 下一篇: P(B|A)和P(AB)的理解