您的位置:首页 > 运维架构

opengl生成线框山地

2014-09-11 16:36 239 查看
#include "stdafx.h"

#include <GL/glut.h>
#include <time.h>
#include <stdlib.h>

#define MAP_W 32
#define MAP_SCALE 4.0f
float g_terren[MAP_W*MAP_W][3];
int g_index[MAP_W*MAP_W*2];
float g_texcoord[MAP_W*MAP_W][2];

void init(void)
{  glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
}

void display(void)
{
srand(time(NULL));
int index = 0;
int Vertex;
float h = 0.5;
for(int z=0;z<MAP_W;z++)
for(int x=0;x<MAP_W;x++){
Vertex = z*MAP_W+x;
g_terren[Vertex][0]=float(x)*MAP_SCALE;
g_terren[Vertex][1]=(float)(h+rand()%100/10*h);
g_terren[Vertex][2]=float(z)*MAP_SCALE;
/*g_texcoord[Vertex][0]=(float)x;
g_texcoord[Vertex][1]=(float)z;*/
g_index[index++]=Vertex;
g_index[index++]=Vertex+MAP_W;
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3,GL_FLOAT,0,g_terren);
glColor3f(1.0,1.0,1.0);
for(int z=0;z<MAP_W-1;z++)
{
glDrawElements(GL_LINE_STRIP,MAP_W*2,GL_UNSIGNED_INT,&g_index[z*MAP_W*2]);

}
//glRotatef(-90.0,1.0,0.0,0.0);
gluLookAt(70.0,60.0,110.0,70.0,0.0,0.0,0.0,1.0,0.0);
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)w/(GLfloat)h, 1.0, 30000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (500, 500);
glutInitWindowPosition (50, 50);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: