opengl生成线框山地

本文将介绍如何使用C++进行游戏引擎开发,包括选择合适的工具、理解游戏引擎架构、编写高效代码等关键步骤。
#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;     
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值