main.cpp
#include <GL/glut.h> #include <GL/gl.h> #include <GL/glu.h> #include "Texture.hpp" #include "xLoader.h" float xrot, yrot, zrot; TTexture *tex1; GLfloat texCoord[6][4][2] = { {{0.0f, 0.0f},{1.0f, 0.0f},{1.0f, 1.0f},{0.0f, 1.0f}}, {{1.0f, 0.0f},{1.0f, 1.0f},{0.0f, 1.0f},{0.0f, 0.0f}}, {{0.0f, 1.0f},{0.0f, 0.0f},{1.0f, 0.0f},{1.0f, 1.0f}}, {{1.0f, 1.0f},{0.0f, 1.0f},{0.0f, 0.0f},{1.0f, 0.0f}}, {{1.0f, 0.0f},{1.0f, 1.0f},{0.0f, 1.0f},{0.0f, 0.0f}}, {{0.0f, 0.0f},{1.0f, 0.0f},{1.0f, 1.0f},{0.0f, 1.0f}} }; const GLubyte ix2[6][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,10,11}, {12,13,14,15}, {16,17,18,19},{20,21,22,23} }; const GLubyte ix[24] = { 0,1,2,3, 4,5,6,7, 8,9,10,11, 12,13,14,15, 16,17,18,19,20,21,22,23 }; GLfloat normal[6][4][3] ={ {{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f},{0.0f,0.0f,1.0f}},//front {{0.0f,0.0f,-1.0f},{0.0f,0.0f,-1.0f},{0.0f,0.0f,-1.0f},{0.0f,0.0f,-1.0f}},//back {{0.0f,1.0f,0.0f},{0.0f,1.0f,0.0f},{0.0f,1.0f,0.0f},{0.0f,1.0f,0.0f}},//top {{0.0f,-1.0f,0.0f},{0.0f,-1.0f,0.0f},{0.0f,-1.0f,0.0f},{0.0f,-1.0f,0.0f}},//botom {{1.0f,0.0f,0.0f},{1.0f,0.0f,0.0f},{1.0f,0.0f,0.0f},{1.0f,0.0f,0.0f}},//right {{-1.0f,0.0f,0.0f},{-1.0f,0.0f,0.0f},{-1.0f,0.0f,0.0f},{-1.0f,0.0f,0.0f}}//left }; GLfloat col[6][4][3] ={ {{1.0f,0.0f,0.0f},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f , -1.0f, 1.0f, -1.0f , -1.0f, 1.0f, 1.0f , 1.0f, 1.0f, 1.0f , 1.0f, 1.0f, -1.0f , -1.0f, -1.0f, -1.0f , 1.0f, -1.0f, -1.0f , 1.0f, -1.0f, 1.0f , -1.0f, -1.0f, 1.0f , 1.0f, -1.0f, -1.0f , 1.0f, 1.0f, -1.0f , 1.0f, 1.0f, 1.0f , 1.0f, -1.0f, 1.0f , -1.0f, -1.0f, -1.0f , -1.0f, -1.0f, 1.0f , -1.0f, 1.0f, 1.0f , -1.0f, 1.0f, -1.0f }; void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0,0,-15); glRotatef(xrot,1.0f,0.0f,0.0f); glRotatef(yrot,0.0f,1.0f,0.0f); glRotatef(zrot,0.0f,0.0f,1.0f); //glEnable(GL_TEXTURE_2D); glEnableClientState(GL_VERTEX_ARRAY); //glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState( GL_COLOR_ARRAY ); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3 , GL_FLOAT , 0 , &v1[0]); //glTexCoordPointer(2, GL_FLOAT, 0, texCoord); glColorPointer( 3, GL_FLOAT, 0, col ); glNormalPointer(GL_FLOAT, 0, &nv1[0]); //glBindTexture(GL_TEXTURE_2D, tex1->ID); //glDrawElements(GL_QUADS , 24 , GLint , ix); glDrawElements(GL_QUADS , indexNum , GL_UNSIGNED_INT , &ix1[0]); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); //glDisableClientState(GL_TEXTURE_COORD_ARRAY); //glDisable(GL_TEXTURE_2D); xrot+=1.0f; yrot+=1.0f; zrot+=0.0f; glutSwapBuffers(); } void onKeyboard(unsigned char key, int x, int y) { switch (key) { case '\033': exit(0); default: break; } } void onMouse(int button, int state, int x, int y) { switch (button) { case GLUT_LEFT_BUTTON: break; case GLUT_MIDDLE_BUTTON: break; case GLUT_RIGHT_BUTTON: break; default: break; } } void onReshape(int width, int height) { if (height==0) height=1; if (width==0) width=1; glViewport(0,0,width,height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } void timer(int value) { glutPostRedisplay(); glutTimerFunc(30 , timer , 0); } void glInit(void) { glClearColor(0.9, 0.9, 0.9, 1.0); glShadeModel(GL_SMOOTH); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); } void glLightingInit() { glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); glEnable(GL_LIGHT0); glEnable(GL_LIGHTING); glEnable(GL_NORMALIZE); } void init() { //tex1 = new TTexture("tartan1.bmp"); xFileLoad("aa1.x"); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); glutInitWindowSize(750,750); int window = glutCreateWindow(argv[0]); glutDisplayFunc(display); glutReshapeFunc(onReshape); glutMouseFunc(onMouse); glutKeyboardFunc(onKeyboard); glutTimerFunc(50 , timer , 0); glInit(); init(); glLightingInit(); glutMainLoop(); delete tex1; return 1; }
xLoader.h
#ifndef XLOADER_H #define XLOADER_H #include <vector> #include <stdio.h> #include <cstring> #include <string.h> #include <iostream> //#include <GL/gl.h> //#include <GL/glu.h> using namespace std; typedef struct VertexStruct//頂点データ 構造体 { float x,y,z,w; }TVertex; typedef struct PlateStruct//面データ 構造体 { int size; int ix1,ix2,ix3,ix4; }TPlate; //配列vector //vector<TVertex> v1;//頂点データ格納配列 vector<float> v1;//頂点データ格納配列 vector<int> ix1;//インデックスデータ格納配列 vector<float> nv1;//法線データ格納配列 //個数データ int vertexNum = 0;//頂点の総数 int plateNum = 0;//面の総数 int indexNum = 0;//インデックスの総数 int normalNum = 0;//法線の総数 //フラグ変数 bool meshFlag = false; bool vertexNumFlag = false; bool vertexDataFlag = false; bool plateNumFlag = false; //bool indexNumFlag = false; bool indexDataFlag = false; bool normalWordFlag = false; bool normalNumFlag = false;//法線総数を得れたかどうか bool normalDataFlag = false; void xFileLoad(char* xFileName) { //変数 FILE *fp; //tmp用 char buf1[256]; float x,y,z,w;//頂点用tmp char str1[256]; char str2[256]; int plateSize,ixp1,ixp2,ixp3,ixp4; int plateOneWithVertexNum = 0;//1つの面の頂点数 //ファイル 読み込み fp = fopen(xFileName, "r"); if (fp == NULL) {printf("can't open the file");}; int tmp_eof = 0; //mesh の場所まで移動 while(feof(fp) != true) { memset(buf1, 0, sizeof(buf1)); fgets(buf1, 256, fp);//一行進める tmp_eof = fscanf(fp, "%s %s", &str1, &str2); if (strcmp(str1, "Mesh") == 0) { printf("mesh OK\n"); meshFlag = true; break; }; if (tmp_eof == EOF) break; if (feof(fp)) break; }; //vertex 頂点の総数 読み込み if (meshFlag) { fgets(buf1, 256, fp);//一行進める tmp_eof = fscanf(fp, "%d;", &vertexNum); printf("vertexNum is %d \n", vertexNum); if (vertexNum > 0) vertexNumFlag = true; }; TVertex tmpV1; //vertex 頂点 配列 読み込み if (vertexNumFlag) { for (int i=0; i < vertexNum; i++) { fgets(buf1, 256, fp); tmp_eof = fscanf(fp, "%f,%f,%f;", &x,&y,&z); v1.push_back(x); v1.push_back(y); v1.push_back(z); if (tmp_eof == EOF) break; if (feof(fp)) break; } if (v1.size() > 0) {vertexDataFlag = true;printf("vertex data readed\n");}; } //頂点データ 表示 if (vertexDataFlag) { for(auto itr = v1.begin(); itr != v1.end(); ++itr) { //cout << itr->x << "," << itr->y << "," << itr->z << "\n"; } } //インデックスの総数を得る if (vertexDataFlag) { //2行進める fgets(buf1, 256, fp); fgets(buf1, 256, fp); //面の総数 読み込み tmp_eof = fscanf(fp, "%d;", &plateNum); if (tmp_eof == EOF) printf("eof indexNum"); printf("plateNum is %d \n", plateNum); if (plateNum > 0) plateNumFlag = true; } //fgets(buf1, 256, fp);//1行進める //インデックス配列データを得る if (plateNumFlag) { for (int i=0; i < plateNum; i++) { fgets(buf1, 256, fp);//1行進める tmp_eof = fscanf(fp, "%d;%d,%d,%d,%d;", &plateSize,&ixp1,&ixp2,&ixp3,&ixp4); printf("plate size is %d\n", plateSize); tmp_eof = fscanf(fp, "%d;%d,%d,%d,%d;", &plateSize,&ixp1,&ixp2,&ixp3,&ixp4); if (plateSize == 4) { printf("ixp1 is %d\n", ixp1); ix1.push_back(ixp1); printf("ixp2 is %d\n", ixp2); ix1.push_back(ixp2); printf("ixp3 is %d\n", ixp3); ix1.push_back(ixp3); printf("ixp4 is %d\n", ixp4); ix1.push_back(ixp4); } if (plateSize == 3) { ix1.push_back(ixp1); ix1.push_back(ixp2); ix1.push_back(ixp3); } if (tmp_eof == EOF) break; if (feof(fp)) break; } //indexNum = plateNum * plateSize;//面の数 * 面の頂点数 = インデックスの総数 indexNum = ix1.size(); printf("indexNum is %d\n", indexNum); if (ix1.size() > 0) {indexDataFlag = true; printf("index data readed\n");}; } //インデックスデータ 表示 if (indexDataFlag) { for(auto itr = ix1.begin(); itr != ix1.end(); ++itr) { cout << *itr << "\n"; } } if (indexDataFlag) { //MeshNormals の場所まで移動 while(feof(fp) != true) { memset(buf1, 0, sizeof(buf1)); //クリア fgets(buf1, 256, fp);//一行進める tmp_eof = fscanf(fp, "%s %s", &str1, &str2); if (strcmp(str1, "MeshNormals") == 0) { printf("normalWord OK\n"); normalWordFlag = true; break; }; if (tmp_eof == EOF) break; if (feof(fp)) break; }; }; //normal 法線の総数を得る if (normalWordFlag) { fgets(buf1, 256, fp);//一行進める tmp_eof = fscanf(fp, "%d;", &normalNum); printf("normalNum is %d \n", normalNum); if (normalNum > 0) normalNumFlag = true; }; int dummy; if (normalNumFlag) { for (int i=0; i < normalNum; i++) { fgets(buf1, 256, fp); tmp_eof = fscanf(fp, "%f,%f,%f;", &x,&y,&z); nv1.push_back(x); nv1.push_back(y); nv1.push_back(z); if (tmp_eof == EOF) break; if (feof(fp)) break; } if (nv1.size() > 0) {normalDataFlag = true;printf("normal data readed\n");}; } //法線 データ 表示 if (vertexDataFlag) { for(auto itr = nv1.begin(); itr != nv1.end(); ++itr) { cout << *itr << "\n"; } } fclose(fp); }; #endif
コメントをかく