obj格式文件导入到opengl中 OBJLoader

.cpp代码

/*********************************************************
               3D model loader  by gameloftyou
             use for obj model
 *********************************************************/

#include <windows.h>
#include <iostream>
#include <iterator>
#include <vector>
#include <string>
#include <math.h>
#include <map>
#include <set>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdlib>
#include <gl/glut.h>
#include "object.h"
#include <ctime>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")

using namespace std; 

map<string,Object> objmap1;
set<string> objname1;
map<string,Material> matname;

map<string,Object> objmap2;
set<string> objname2;


 
GLfloat dx = 0,dy = 0,dz = 0;
GLfloat ax = 0,ay = 0,az = 0;
GLfloat bx = 0,by = 0,bz = 0;
GLfloat arot;
GLint mx = 0,my = 0;
GLint MouseDown = 0;
GLfloat aspect = 1;

const float piover180 = 0.0174532925f;    //折算因子用来折算度和弧度
float heading;
float xpos;
float zpos;

GLfloat yrot;    // Y Rotation
GLfloat walkbias = 0;
GLfloat walkbiasangle = 0;
static int sy = 0;
 
 

int power_of_two(int n)
{
 if(n<=0) return 0;
 return n&(n-1)==0;
}

GLuint load_texture(const char *file_name)
{
 GLint width,height,total_bytes;
 GLuint last_texture_ID,texture_ID = 0;
 GLubyte *pixels = NULL;
 FILE *pFile;

 if((pFile=fopen(file_name,"rb"))==NULL)
 {
  cout<<"Read texture error"<<endl;
  return 0;
 }

 fseek(pFile,18,SEEK_SET);
 fread(&width,sizeof(width),1,pFile);
 fread(&height,sizeof(height),1,pFile);
 fseek(pFile,54,SEEK_SET);

 total_bytes = (width*3+(4-width*3%4)%4)*height;

 if((pixels=(GLubyte *)malloc(total_bytes))==NULL)
 {
  fclose(pFile);
  return 0;
 }

 if(fread(pixels,total_bytes,1,pFile)<=0)
 {
  free(pixels);
  fclose(pFile);
  return 0;
 }

 {
  GLint max;
  glGetIntegerv(GL_MAX_TEXTURE_SIZE,&max);
  if(!power_of_two(width)||!power_of_two(height)||width>max||height>max)
  {
   const GLint new_width = 1024;
   const GLint new_height = 1024;
   GLint new_total_bytes;
   GLubyte *new_pixels = NULL;

   new_total_bytes = (new_width*3+(4-new_width*3%4)%4)*new_height;
   new_pixels = (GLubyte *)malloc(new_total_bytes);
   if(new_pixels==NULL)
   {
    free(pixels);
    fclose(pFile);
    return 0;
   }

   gluScaleImage(GL_RGB,width,height,GL_UNSIGNED_BYTE,pixels,new_width,new_height,GL_UNSIGNED_BYTE,new_pixels);

   free(pixels);
   pixels = new_pixels;
   width = new_width;
   height = new_height;
  }
 }

 glGenTextures(1,&texture_ID);
 if(texture_ID==0)
 {
  free(pixels);
  fclose(pFile);
  return 0;
 }

 glGetIntegerv(GL_TEXTURE_BINDING_2D,(GLint *)&last_texture_ID);
 glBindTexture(GL_TEXTURE_2D,texture_ID);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
 glTexEnvf(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE);
 glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,width,height,0,GL_BGR_EXT,GL_UNSIGNED_BYTE,pixels);
 glBindTexture(GL_TEXTURE_2D,last_texture_ID);

 free(pixels);
 fclose(pFile);
 return texture_ID;
}

void ReadMtl(string mtlfile,map<string,Material> &mat)
{
 ifstream in;
 string line,word,ptname,ntname,fname;
 unsigned map;
 bool hasmap = false;
 float ambient[3] = {0.0,0.0,0.0};
 float diffuse[3] = {0.0,0.0,0.0};
 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值