文本文件(txt或者ttt格式)数据导入OpenGL中绘制花房子。
使用“85-84hg.ttt”文件实现如下效果:
经过一步一步的做法,实现了。
没有实现最终的贴图的效果,现公开源代码,请各位朋友帮忙一起解决,一起学习。
#include
#include
#include
using namespace std;
static int Mpoint,Ntri;
static GLfloat theta[] = {0.0,0.0,0.0};
static GLint axis = 2;
static GLdouble viewer[]= { 539350.0,378520.0,-500.0};
static GLfloat ta[] = { 0.2,0.2,-1.0 };
struct point
{
GLdouble x,y,z,w;
};
struct trangle
{
int a,b,c,d;
};
void load()
{
int i, j,flag,t,mm,m,n,s;
GLdouble k;
ifstream in;
//in.open("gh.txt",ios::in);
//Mpoint = 198;
//in.open("result.txt", ios::in);
//Mpoint = 16;
in.open("85-84hg.ttt",ios::in);
Mpoint = 3720;
point *points=new point [Mpoint];
k = 1.0;
t = 364;
mm = 0;
m = 0;
n = 0;
while (t>0)
{
for (i = m, j = n; i < Mpoint; i++)
{
in >> points[j].x;
if (points[j].x == k)
{
k++;
continue;
}
if (points[j].x == 0)
{
//i++;
//in >> points[i].w;
t--;
mm = 1;
break;
}
in >> points[j].y;
in >> points[j].z;
in >> points[j].w;
j++;
}
flag = j;
glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_POLYGON);
//glBegin(GL_LINES);
for (s = n;s < flag; s++)
{
glColor3f(0.0, 1.0, 1.0);
glVertex3f(points[s].x, points[s].y, points[s].z);
//glVertex3f(points[j].x,points[j].y,points[j].z);
//glVertex3f(points[j].x,points[j].y,points[j].z);
//printf("%lf %lf %lf\n", points[s].x, points[s].y, points[s].z);
}
//glEnd();
glBegin(GL_POLYGON);
//glBegin(GL_LINES);
for (s = n; s < flag; s++)
{
glColor3f(0.4,0.6, 0.7);
glVertex3f(points[s].x, points[s].y, points[s].z + points[s].w);
//glVertex3f(points[j].x,points[j].y,points[j].z);
//glVertex3f(points[j].x,points[j].y,points[j].z);
//printf("%lf %lf %lf\n", points[s].x, points[s].y, points[s].z);
}
glEnd();
//glPolygonMode(GL_FRONT, GL_FILL);
glBegin(GL_POLYGON);
//glBegin(GL_LINES);
for (s = n; s < flag; s++)
{
glColor3f(0.6, 0.3, 0.5);
glVertex3f(points[s].x, points[s].y, points[s].z);
glVertex3f(points[s].x , points[s].y, points[s].z + points[s].w);
//glVertex3f(points[j].x,points[j].y,points[j].z);
//glVertex3f(points[j].x,points[j].y,points[j].z);
//printf("%lf %lf %lf\n", points[s].x, points[s].y, points[s].z);
}
glEnd();
mm = 0;
m = i;
n = j;
}
in.close();
delete[] points;
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], 538900.0, 377500.0, 0.0, ta[0], ta[1], ta[2]);
glRotatef(theta[0], 1.0, 0.0, 0.0);
glRotatef(theta[1], 0.0, 1.0, 0.0);
glRotatef(theta[2], 0.0, 0.0, 1.0);
//glScalef(5.0,5.0,5.0);//太小了 就放大了5倍
load();
glFlush();
glutSwapBuffers();
}
void mouse(int btn, int state, int x, int y)
{
if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;
theta[axis] += 0.01;
if( theta[axis] > 360.0 )
theta[axis] -= 360.0;
//glScalef(5.0,5.0,5.0);//太小了 就放大了5倍
display();
}
void keys(unsigned char key, int x, int y)
{
if(key == 'x') viewer[0]-= 10.0;
if(key == 'X') viewer[0]+= 10.0;
if(key == 'y') viewer[1]-= 10.0;
if(key == 'Y') viewer[1]+= 10.0;
if(key == 'z') viewer[2]-= 10.0;
if(key == 'Z') viewer[2]+= 10.0;
if (key == 'q') ta[0] -= 0.1;
if (key == 'Q') ta[0] += 0.1;
if (key == 'w') ta[1] -= 0.1;
if (key == 'W') ta[1] += 0.1;
if (key == 'e') ta[2] -= 0.1;
if (key == 'E') ta[2] += 0.1;
display();
}
void myReshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//if(w<=h) glFrustum(-2.0, 20.0, -2.0 * (GLfloat) h/ (GLfloat) w,
// 2.0* (GLfloat) h / (GLfloat) w, 2.0, 20.0);
//else glFrustum(-2.0, 20.0, -2.0 * (GLfloat) w/ (GLfloat) h,
// 2.0* (GLfloat) w / (GLfloat) h, 2.0, 20.0);
//glFrustum(537100, 5410000, -2.0 * (GLfloat)w / (GLfloat)h,
// 2.0* (GLfloat) w / (GLfloat) h, 10, 1800);
//gluLookAt(10, 10, 20, 0.0, 0.0, -1.0, 0.0, 1.0, 0.0);
gluPerspective(60.0, (GLfloat)w / (GLfloat)h, 50, 2500.0);
//glTranslatef(0, 0, 5);
//glFrustum(0.0, 1000.0, 0.0, 1000.0, 2.0, 800.0);
//glOrtho(537100, 539800, 376400, 377900, 10, 8000);
glMatrixMode(GL_MODELVIEW);
}
void main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 800);
glutCreateWindow("fangzi");
glEnable(GL_DEPTH_TEST);
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(keys);
glutMainLoop();
}