2021-01-09

**关于在vs2008中利用OpenGL读取3DSMAX中OBJ格式文件的实现*纯入门篇!在3DSMAX中导出多个模型至一个OBJ文件中,在本源代码中实现了读取,绘制,加载纹理贴图!源代码篇幅较长,且属于实践之作,望批评指正!如果需要文中的OBJ模型文件及BMP位图文件,评论区联系我!试运行该代码,请注意所要读取文件的路径!




#include "stdafx.h"

#include "iostream"
#include "windows.h"
#include <atlimage.h>
#include "malloc.h"
#include "stdlib.h"
#include "stdio.h"
#include "string.h"
#include <gl.h>
#include <glu.h>
#include <glaux.h>
#include "math.h"
#include <glut.h>
#include "string.h"
#include "iomanip"
#include "tchar.h"
#include "glext.h"
int vertexmax=0;
int onevertexmax=0;

#define BMP_Header_Length 54 
#pragma comment (lib,"OpenGL32.lib")
#pragma comment(lib,"ws2_32.lib")
#pragma comment(lib,"opengl32.lib")

#pragma comment(lib,"glut.lib")
#pragma comment(lib,"glut32.lib")

#pragma comment(lib,"glew32.lib")
#pragma comment(lib, "glaux.lib")
GLfloat light1_position[]={1.0,1.0,5.0,1.0};
GLfloat light1_diffuse[]={1.0,1.0,1.0,1.0}; 
GLfloat light1_specular[]={1.0,1.0,1.0,1.0};

GLfloat lmodel_ambient[]={0.2,0.2,0.2,1.0};
int mybitbmp[10];
int loopbit;


int loop;
int xiuzheng[10];
int xiuzheng1[10];
	GLfloat  xrot = 0;   // X 旋转量
GLfloat  yrot = 0;   // Y 旋转量
GLfloat  zrot = 0;   // Z 旋转量
int  countersocend=0;
int counterover=0;

 
using namespace std;
#define MAXTEXTURE 1                
int counterrabbit[100];
int counterrabbit1[100];
int counterrabbit2[100];
int counterrabbit3[100];
int counterloop;
int x;
GLfloat X,Y,Z,X1,Y1,Z1,X2,Y2,Z2,textureX1,textureY1,textureX2,
textureY2,textureX3,textureY3;

GLfloat temp,temp1,temp2;
int temp3,temp4,temp5;

GLuint texture[1];
#define M_PI 3.1415926535898

GLfloat AngleX;
GLfloat AngleY;
int loopsocend=0;
int loopsocend1=0;
int counterthree;
int counterfour;
int countereight;
int counterseven;
int counterend;
int counterend1;
int counterendfifity;
int counterfive;
int countersix;
int counterpig;
int countercow;

static float c = M_PI / 180.0f;             //弧度和角度转换参数
static int du = 90, oldmy = -1, oldmx = -1; //du是视点绕y轴的角度,opengl里默认y轴是上方向
static float r = 15.0f, h = 0.0f;           //r是视点绕y轴的半径,h是视点高度即在y轴上的坐标
FILE *fp,*texImage;
char ch[400000];
char model[350000];
char coord[350000];

int counter=0;
double cow=10.0;
int j=0;
int counterone;
int countertwo;
int modelnumber;
int modelnumber1;
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f }; 	// 环境光参数
GLfloat LightDiffuse[]= { 1.0f, 1.0f, 1.0f, 1.0f };		// 漫射光参数
GLfloat LightPosition[]= { 0.0f, 0.0f, 2.0f, 1.0f };	// 光源位置
bool	keys[256];	
		
int num_texture=-1;
bool identification=true;
char  bitmap[200000];

int  twobitmap[200000];
int  twobitmap1[200000];
GLfloat vertex;
GLfloat modelvertex[200000];
GLfloat endvertex[200000];
GLfloat texturecoord[200000];
GLfloat texturemodel[200000];

int Vcounter;

GLfloat texturecoordone[200000];
AUX_RGBImageRec *LoadBMP(CHAR *Filename)
{cout<<"开始读取位图文件";

    FILE *File = NULL;         // 文件句柄
    if (!Filename)          // 确保文件名已提供
    {cout<<"获取纹理失败";
        return NULL;         // 如果没提供,返回 NULL
    }
    File = fopen(Filename, "r");       // 尝试打开文件
    if (File)           // 判断文件存在与否 
    {
        fclose(File); // 关闭句柄
	cout<<"获取纹理成功";
        return auxDIBImageLoadA(Filename); // 载入位图并返回指针
		

    }
    return NULL;          // 如果载入失败,返回 NULL
}

//载入位图(调用上面的代码)并转换成纹理——固定用法
//参数:纹理指针、bmp文件名、用户指定的纹理编号
int LoadGLTextures(GLuint *texture, char *bmp_file_name, int texture_id)
{
    int Status = FALSE;         // 状态指示器
    // 创建纹理的存储空间
    AUX_RGBImageRec *TextureImage[1];
    memset(TextureImage, 0, sizeof(void *) * 1);   // 将指针设为 NULL
    // 载入位图,检查有无错误,如果位图没找到则退出
    if (TextureImage[0] = LoadBMP(bmp_file_name))
    {
        Status = TRUE;         // 将 Status 设为 TRUE
        //生成(generate)纹理
        glGenTextures(texture_id, texture); //&texture[0]);     
        //绑定2D纹理对象
        glBindTexture(GL_TEXTURE_2D, *texture); //texture[0]);
        //关联图像数据与纹理对象
        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
        //图形绘制时所使用的滤波器参数
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // 线形滤波
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // 线形滤波
glEnable(GL_TEXTURE_2D);//启用纹理贴图 
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);//指定纹理贴图与材质的混合模式 

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL); 
    }

    //释放图像的内存,因为已经生成纹理了,没用了
    if (TextureImage[0])        // 纹理是否存在
    {
        if (TextureImage[0]->data)      // 纹理图像是否存在
        {
            free(TextureImage[0]->data);    // 释放纹理图像占用的内存
        }
        free(TextureImage[0]);       // 释放图像结构
    }
    else
        printf("纹理不存在");
    return Status;          // 返回 Status
}


void display()
{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();


    glPushMatrix();
    {
        // 这glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);            // 清除屏幕和深度缓存
    glLoadIdentity();                            // 重置当前的模型观察矩阵
    glTranslatef(-1.5f,0.0f,0.0f);                    // 左移 1.5 单位,并移入屏幕 6.0
glRotatef(xrot, 1.0f, 0.0f, 0.0f);         // 绕X轴旋转
    glRotatef(yrot, 0.0f, 1.0f, 0.0f);         // 绕Y轴旋转
    glRotatef(zrot, 0.0f, 0.0f, 1.0f);         // 绕Z轴旋转
    glBindTexture(GL_TEXTURE_2D, texture[0]);  
    glBegin(GL_TRIANGLES);   // 绘制三角形
counter=0;	
countertwo=0;


do

	
	{
		X=endvertex[countertwo];


countertwo++;
Y=endvertex[countertwo];



countertwo++;
Z=endvertex[countertwo];


countertwo++;
       
X1=endvertex[countertwo];
countertwo++;
Y1=endvertex[countertwo];
countertwo++;
Z1=endvertex[countertwo];

countertwo++;
X2=endvertex[countertwo];
countertwo++;
Y2=endvertex[countertwo];
countertwo++;
Z2=endvertex[countertwo];
countertwo++;
textureX1=texturecoordone[counter];
counter++;

textureY1=texturecoordone[counter];
counter++;

textureX2=texturecoordone[counter];
counter++;

textureY2=texturecoordone[counter];
counter++;

textureX3=texturecoordone[counter];
counter++;

textureY3=texturecoordone[counter];
counter++;



   glTexCoord2f(textureX1, textureY1); glVertex3f( X, Y, Z);    
   glTexCoord2f(textureX2, textureY2); glVertex3f( X1, Y1, Z1);    
   glTexCoord2f(textureX3, textureY3); glVertex3f( X2, Y2, Z2);    


	}
	while(countertwo<counterover);

		// 上顶点
       
    glEnd();     
 
	}

    glPopMatrix();


    glutSwapBuffers();
xrot += 0.0f;              // X 轴旋转
    yrot -= 0.5f;              // Y 轴旋转
    zrot += 0.0f;              // Z 轴旋转
}

void reshape(int w, int h)
{
    GLfloat aspect = (GLfloat)w / (GLfloat)h;
    GLfloat nRange = 100.0f;        // 如果范围太小会有突然“消失”的感觉,显示范围的问题

    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);    //将当前矩阵指定为投影模式
    glLoadIdentity();

    //设置三维投影区
    if (w <= h)
    {
        glOrtho(-nRange, nRange, -nRange * aspect, nRange * aspect, -nRange, nRange);
    }
    else
    {
        glOrtho(-nRange, nRange, -nRange / aspect, nRange / aspect, -nRange, nRange);
    }
}

/*
↑ ↓ ← → 调整图形
*/
void key_board(GLint key, GLint x, GLint y)
{
    if (key == GLUT_KEY_UP)
    {
        AngleX -= 5.0f;
    }
    if (key == GLUT_KEY_DOWN)
    {
        AngleX += 5.0f;
    }
    if (key == GLUT_KEY_LEFT)
    {
        AngleY -= 5.0f;
    }
    if (key == GLUT_KEY_RIGHT)
    {
        AngleY += 5.0f;
    }
    if (AngleX > 355.0f)
    {
        AngleX = 0.0f;
    }
    if (AngleX < 0.0f)
    {
        AngleX = 355.0f;
    }
    if (AngleY > 355.0f)
        AngleY = 0.0f;

    if (AngleY < 0.0f)
    {
        AngleY = 355.0f;
    }
    glutPostRedisplay();
}

/*
初始化
*/
void init()
{
    // 这个角度展示出来类似于HSV模型的地面六边形
    AngleX = 45.0f;
    AngleY = 315.0f;

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);       // 背景黑色
    //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);     // 背景白色

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_DITHER);
    glShadeModel(GL_SMOOTH);


}

/*
处理鼠标点击
*/
void Mouse(int button, int state, int x, int y) 
{
    if (state == GLUT_DOWN)     //第一次鼠标按下时,记录鼠标在窗口中的初始坐标
        oldmx = x, oldmy = y;
}

/*
处理鼠标拖动
*/
void onMouseMove(int x, int y) 
{
    // printf("%d\n",du);
    du += x - oldmx;            //鼠标在窗口x轴方向上的增量加到视点绕y轴的角度上,这样就左右转了
    h += 0.3f*(y - oldmy);      //鼠标在窗口y轴方向上的改变加到视点的y坐标上,就上下转了,防止转过度,超出范围,会呈现出“消失”的现象
    oldmx = x, oldmy = y;       //把此时的鼠标坐标作为旧值,为下一次计算增量做准备
}

int main(int argc, char* argv[])
{
	static GLuint texName;
fp = fopen("d:\\940.OBJ", "rb");
if (fp == NULL){
      cout<<"Cannot open file\n";
    }


    else{
	cout<<"文件读取成功"<<'\n';
    counter=0;
counterrabbit[0]=0;//整个模型文件的搜索起始字节;
        fread(ch,300000,1,fp);
	for (int i=1;i<=300000;i++)
		{
			if ((ch[i]=='f')&&(ch[i+1]=='a')&&(ch[i+2]=='c')&&(ch[i+3]=='e')&&(ch[i+4]=='s'))//搜索模型的个数;
			{counter++;
			counterrabbit[counter]=i;


			}
	}
	}

	modelnumber=counter;//记录模型数量;
cout<<"模型的数量modelnumber="<<modelnumber<<'\n';


for (int i=0;i<4;i++)
{
	
	cout<<"第"<<(i+1)<<"个模型开始或结束字节数"<<counterrabbit[i]<<'\n';


}
loopsocend=0;

do
{cout<<"第"<<(loopsocend+1)<<"个模型开始读取数据"<<'\n';

	

	
	cout<<"第"<<(loopsocend+1)<<"个模型的起始字节"<<counterrabbit[loopsocend]<<'\n';

	cout<<"第"<<(loopsocend+1)<<"个模型的结束字节"<<counterrabbit[loopsocend+1]<<'\n';

		for (int i=counterrabbit[loopsocend];i<=counterrabbit[loopsocend+1];i++)
		{
			if ((ch[i]=='v')&&(ch[i+1]==' '))//如果搜索到顶点数据域的第一个标识
			{counterfour=i;//记录起始起始位置;

				break;}
		
		}

		
		
		
		
for (int i=counterfour;i<=counterrabbit[loopsocend+1];i++)
		{

			
			if ((ch[i]=='v')&&(ch[i+1]=='e')&&(ch[i+2]=='r')&&(ch[i+3]=='t')&&(ch[i+4]=='i')&&(ch[i+5]=='c')&&(ch[i+6]=='e')&&(ch[i+7]=='s'))//如果搜索到‘vretices'则意味着顶点数据搜索结束;
			{counterfive=i;
				break;}
		}




counter=0;
model[counter]=' ';
counter++;
	
		
           for(int i=counterfour;i<counterfive;i++)//循环读取数字符号,负号及小数点;
		   
		   {
			   
			   if ((ch[i]=='0')||(ch[i]=='1')||(ch[i]=='2')||(ch[i]=='3')||(ch[i]=='4')||(ch[i]=='5')||(ch[i]=='6')||(ch[i]=='7')||(ch[i]=='8')||(ch[i]=='9')||(ch[i]=='-')||(ch[i]=='.')||(ch[i]==' '))
			  
			   {
				  



			model[counter]=ch[i];//存储字符;






		

counter++;


			   }
			   
		   }
	

		   counter=0;
	
			   countertwo=0;




cout<<"第"<<(loopsocend+1)<<"个模型的顶点初始数据"<<'\n';

			   do
			   {


 if ((model[counter]==' ')||(model[counter]=='0')||(model[counter]=='1')||(model[counter]=='2')||(model[counter]=='3')||(model[counter]=='4')||(model[counter]=='5')||(model[counter]=='6')||(model[counter]=='7')||(model[counter]=='8')||(model[counter]=='9')||(model[counter]==NULL))
              {
				   counter++;//如果字符为非'.',计数器递增;

			 }

			   if (model[counter]=='-')
			   {  identification=false  ;//如果是负号;则符位置flase;
			   counter++;//计数器递增;

			   
			   }//如果搜索到负数符号,则将标识符置为负数标识;

               if ((model[counter]=='.'))//如果搜索到小数点;
			   {counterone=counter;//计数值转移;


				 j=1;//位数赋初值;
                    do
					{ temp1=model[counterone-j]-'0';//字符转换为整型值;
	                    temp1=temp1*(double)(pow(cow,(j-1)));//根据数值所处的具体位置(个位?十位?百位?)算出具体数值;即根据该字符位于小数点左边位数计算具体数值;
						temp2=temp1+temp2;//累加数值;

						j++;//位数递增;

                    



					}
				   while((model[counterone-j]!=' ')&&(model[counterone-j]!='-'));//重复计算过程;直至由小数点向左搜索到空字符或者“-”字符则结束;
				   loop=j-1;//以下计算小数值;

				   temp1=0.0;//变量置零;

                    counterone=counter;//计数值转移;
				   
				   j=1;
				   do
				   {temp1=model[counterone+j]-'0';//字符转换为整型值;
				 

				   int n=-j;//由于是计算小数值,幂取相反值;

	                    temp1=temp1*(float)(pow(cow,(n)));//根据该字符位于小数点右边的位数计算具体数值;
						temp2=temp1+temp2;//累加数值;

					

					
					 j++;//位数递增;

				   }
				   while (j<8);//取小数点后有效数字七位;



if(identification==false)
{
	temp2=-(temp2);
identification=true;
}//如果标识位为假;则数值取反;意味着为负值;然后标识位置真值;

				   counter=counter+7;//计数器递增七位;


 modelvertex[countertwo]=temp2;//存储数值;










 countertwo++;//数组下标递增;



			   }
			   
	
	  
temp=0.0;
temp1=0.0;
temp2=0.0;//变量重新置零;以便重新计算顶点数值;




			   
           










			   }
			   while(counter<(counterfive-counterfour));//counter始终小于counterfive导致死循环
			   
cout<< "aaaaaaaaa"<<(countertwo/3)<<"   ";
vertexmax+=countertwo/3;

xiuzheng[loopsocend+1]=vertexmax;

cout<<"dddddddddddd"<<xiuzheng[loopsocend]<<'\n';









			   

	for (int i=counterrabbit[loopsocend];i<=counterrabbit[loopsocend+1];i++)
		{
			if ((ch[i]=='f')&&(ch[i+1]==' '))//如果搜索到顶点及贴图坐标序列的第一组特征值;
			{countereight=i;//计数器递增;

				break;}//结束循环;
		}
cout<<"顶点坐标排序数组起始字节:"<<countereight<<'\n';

		
		
for (int i=countereight;i<=counterrabbit[loopsocend+1];i++)
		{


			
			if ((ch[i]=='#')&&(ch[i+1]==' '))//如果搜索到‘vretices'则意味着顶点数据搜索结束;
			{counterseven=i;
				break;}
		}
cout<<"顶点坐标排序数结束字节:"<<counterseven<<'\n';



counter=countereight;
countertwo=0;



do
{

	if ((ch[counter]=='0')||(ch[counter]=='1')||(ch[counter]=='2')||(ch[counter]=='3')||(ch[counter]=='4')||(ch[counter]=='5')||(ch[counter]=='6')||(ch[counter]=='7')||(ch[counter]=='8')||(ch[counter]=='9')||(ch[counter]=='/')||(ch[counter]==' '))

	{   if (ch[counter]==' ')
	{



bitmap[countertwo]='/';
	}

	else
	{

bitmap[countertwo]= ch[counter];

	}


	  counter++;
countertwo++;

}
	else
	{

counter++;
	}

}

while (counter<counterseven);

	

counter=1;
countertwo=0;
counterthree=0;

	int loopone =0;
	int looptwo=0;
	int loopthree=0;


	do
	{ 
		if ((bitmap[counter]=='/'))
	

	{



	counterthree=counter-1;


		loopone=loopone+1;
	for (int i=0;i<looptwo;i++)
	{
		temp3=(int)(bitmap[counterthree-i]-'0');

	temp4=(int)temp3*(double)pow(10.0,i);



	temp5=temp5+temp4;







	}
	if (temp5!=0)
	{

	twobitmap[countertwo]=temp5;
	twobitmap1[counterpig]=temp5;
counterpig++;

	countertwo++;
	}
	looptwo=0;
temp3=0;

temp4=0;
temp5=0;
counter++;
counterthree=0;

	}
	else
	{
		looptwo=looptwo+1;
		counter++;



		}





	}
		while(counter<(counterseven-countereight));

		counterend=countertwo;
      mybitbmp[0]+=countertwo;
     


cout<<"counterend="<<counterend<<'\n';
cout<<"第"<<(loopsocend+1)<<"个模型的顶点序列数据"<<'\n';

		counter=0;

		do
		{
			cout<<" "<<twobitmap[counter]<<" ";
		

counter++;
		}
		while (counter<counterend);



counter=0;
countertwo=0;
counterthree=0;



cout<<"xiuzheng[loopsocend]="<<xiuzheng[loopsocend]<<'\n';
cout<<"第"<<(loopsocend+1)<<"个模型的顶点数据"<<'\n';
cout<<"第"<<(loopsocend+1)<<"个模型顶点排序数的修正值"<<xiuzheng[loopsocend+1]<<"已经确定"<<'\n';
cout<<"这是counterend="<<counterend<<'\n';
do

{

	counterthree=twobitmap[counter];
	
counterthree=counterthree-xiuzheng[loopsocend];





	counterthree=(counterthree-1)*3;

	endvertex[countersocend]=modelvertex[counterthree];












    countersocend++;
 
	 
	counterthree++;

	
endvertex[countersocend]=modelvertex[counterthree];


	


countersocend++;


	counterthree++;

endvertex[countersocend]=modelvertex[counterthree];


	

countersocend++;

	
	counterthree=0;
	counter=counter+3;





}
while (counter<=(counterend-3));

cout<<"这是counterend="<<counterend<<'\n';




cout<<"         "<<"vertexmax="<<vertexmax<<'\n';


xiuzheng[loopsocend+1]=vertexmax;






counterover=countersocend;



 loopsocend++;
counterthree=0;
counterfour=0;
countereight=0;
counterseven=0;

counterfive=0;
 countersix=0;
 counterone=0;
 countertwo=0;


countertwo=0;






 }

while (loopsocend<=(modelnumber));

cout<<"模型数据已经读取并排列完毕"<<'\n';
cout<<"开始读取纹理贴图坐标"<<'\n';


for (int i=0;i<=300000;i++)
		{
			if ((ch[i]=='v')&&(ch[i+1]=='t'))//搜索模型的个数;
			{counter++;
			counterrabbit1[0]=i;

break;
			}
	}
counter=0;

for (int i=1;i<=300000;i++)
		{
			if ((ch[i]=='t')&&(ch[i+1]=='e')&&(ch[i+2]=='x')&&(ch[i+3]=='t')&&(ch[i+4]=='u')&&(ch[i+5]=='r')&&(ch[i+6]=='e'))//搜索模型的个数;
			{counter++;
			counterrabbit1[counter]=i;

modelnumber1++;

			}
	}

for (int i=0;i<4;i++)
{
	cout<<"wwwwww"<<counterrabbit1[i]<<"wwww";

}
cout<<"wwwwww"<<modelnumber1<<"wwww";
loopsocend1=0;

do
{

for (int i=counterrabbit1[loopsocend1];i<=counterrabbit1[loopsocend1+1];i++)
		{
			if ((ch[i]=='v')&&(ch[i+1]=='t')&&(ch[i+2]==' '))
			{counterfour=i;

				break;}
		}



		cout<<"贴图坐标起始字节:"<<counterfour<<'\n';

		
for (int i=counterfour;i<=counterrabbit1[loopsocend1+1];i++)
		{
			
			if ((ch[i]=='t')&&(ch[i+1]=='e')&&(ch[i+2]=='x')&&(ch[i+3]=='t')&&(ch[i+4]=='u')&&(ch[i+5]=='r')&&(ch[i+6]=='e'))//如果搜索到‘vretices'则意味着顶点数据搜索结束;
			{counterfive=i;
				break;}
		}
cout<<"贴图坐标占用字节数:"<<(counterfive-counterfour)<<'\n';
counter=0;

  for(int i=counterfour;i<counterfive;i++)
		   
		   {
			   
			   if ((ch[i]=='0')||(ch[i]=='1')||(ch[i]=='2')||(ch[i]=='3')||(ch[i]=='4')||(ch[i]=='5')||(ch[i]=='6')||(ch[i]=='7')||(ch[i]=='8')||(ch[i]=='9')||(ch[i]=='-')||(ch[i]=='.')||(ch[i]==' '))
			  
			   {
				  



			coord[counter]=ch[i];

           cout<<coord[counter]<<" ";










		

counter++;


			   }
			   
		   }

			   counter=0;
			   countertwo=0;






			   do
			   {


 if ((coord[counter]==' ')||(coord[counter]=='0')||(coord[counter]=='1')||(coord[counter]=='2')||(coord[counter]=='3')||(coord[counter]=='4')||(coord[counter]=='5')||(coord[counter]=='6')||(coord[counter]=='7')||(coord[counter]=='8')||(coord[counter]=='9')||(coord[counter]==NULL))
              {
				   counter++;//如果为非'.',计数器递增;

			 }

			   if (coord[counter]=='-')
			   {  identification=false  ;//如果是负号;则符位置flase;
			   counter++;//计数器递增;

			   
			   }//如果搜索到负数符号,则将标识符置为负数标识;

               if ((coord[counter]=='.'))//如果搜索到小数点;
			   {counterone=counter;//计数值转移;


				 j=1;//位数赋初值;
                    do
					{ temp1=coord[counterone-j]-'0';//字符转换为整型值;
	                    temp1=temp1*(double)(pow(cow,(j-1)));//根据数值所处的具体位置(个位?十位?百位?)算出具体数值;即根据该字符位于小数点左边位数计算具体数值;
						temp2=temp1+temp2;//累加数值;

						j++;//位数递增;

                    



					}
				   while((coord[counterone-j]!=' ')&&(coord[counterone-j]!='-'));//重复计算过程;直至由小数点向左搜索到空字符或者“-”字符则结束;
				   loop=j-1;//以下计算小数值;

				   temp1=0.0;//变量置零;

                    counterone=counter;//计数值转移;
				   
				   j=1;
				   do
				   {temp1=coord[counterone+j]-'0';//字符转换为整型值;
				 

				   int n=-j;//由于是计算小数值,幂取相反值;

	                    temp1=temp1*(float)(pow(cow,(n)));//根据该字符位于小数点右边的位数计算具体数值;
						temp2=temp1+temp2;//累加数值;

					

					
					 j++;//位数递增;

				   }
				   while (j<8);//取小数点后有效数字七位;



if(identification==false)
{
	temp2=-(temp2);
identification=true;
}//如果标识位为假;则数值取反;意味着为负值;然后标识位置真值;

				   counter=counter+7;//计数器递增七位;


texturecoord[countertwo]=temp2;//存储数值;


counterrabbit3[countercow]=temp2;
cout<<" "<<counterrabbit3[countercow]<<" ";

countercow++;








 countertwo++;//数组下标递增;



			   }
			   
	
	  
temp=0.0;
temp1=0.0;
temp2=0.0;//变量重新置零;以便重新计算顶点数值;




			   
           










			   }
			   while(counter<(counterfive-counterfour));//counter始终小于counterfive导致死循环

cout<<"纹理贴图坐标读取并计算完毕"<<'\n';

loopsocend1++;

}
while (loopsocend1<modelnumber1);

xiuzheng1[loopsocend1+1]+=(countertwo/3);


counter=1;
countertwo=0;
counterthree=0;


cout<<"counterendten="<<counterend<<'\n';

cout<<"纹理坐标数据"<<'\n';


for (int i=0;i<100;i++)
{
cout<<" "<<counterrabbit3[i]<<" ";
}


cout<<" "<<"uuuuuuuuu"<<" ";
do

{

	counterthree=twobitmap1[counter];
	
counter=counter+3;


	texturecoordone[counterendfifity]=counterrabbit3[counterthree];

cout<<" "<<texturecoordone[counterendfifity]<<" ";




   counterendfifity++;

	
	counterthree++;
	
texturecoordone[counterendfifity]=counterrabbit3[counterthree];
cout<<" "<<texturecoordone[counterendfifity]<<" ";

counterendfifity++;


	

	
	counterthree=0;
	






}
while (counter<=(mybitbmp[0]));


cout<<"纹理贴图坐标排序完毕"<<'\n';




 glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(1345, 768);
    glutCreateWindow("OpenGL颜色立方体");
 init();
LoadGLTextures(&texture[0], "d:\\mixiang.bmp", 1);
    glutReshapeFunc(reshape);


    glutDisplayFunc(display);
    glutIdleFunc(display);          // 设置不断调用显示函数
    glutSpecialFunc(key_board);     // 设置键盘↑ ↓ ← → 调整函数
    glutMouseFunc(Mouse);           // 鼠标处理函数
    glutMotionFunc(onMouseMove);
 
 
    glutMainLoop();

 




    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值