add terrain

This commit is contained in:
2025-02-28 15:27:15 +08:00
parent aef4f555c1
commit c5c5376e5a
9 changed files with 69 additions and 75 deletions

View File

@@ -26,7 +26,13 @@ int exponent = 1;
int mouseX, mouseY;
float cameraX, cameraY, cameraZ;
float lookX, lookY, lookZ;
float p_center, p_leftp_right, p_down, p_up;
int drawMode = GL_TRIANGLE_STRIP;
#define LEVEL 256
float colorRamp[LEVEL][4];
int MIN_HEIGHT = 10;
int heightColorDivision = 20;
void Render(void);
// 顶点着色器源码
@@ -102,7 +108,41 @@ void CleanUp()
free(imageData);
free(landTexture);
}
void initColorRamp() {
for (int i = 0; i < LEVEL; i++) {
colorRamp[i][0] = i / (GLfloat)LEVEL;
colorRamp[i][1] = 1 - i / (GLfloat)LEVEL;
colorRamp[i][2] = 0;
colorRamp[i][3] = 1;
}
}
GLfloat* setLevelColor(GLfloat h) {
/*if(h<(MIN_HEIGHT+colorDivision)) return colorRamp[0];
else if(h<(MIN_HEIGHT+colorDivision*1)) return colorRamp[0];*///这种效率太低,根据与最低高程的差值与颜色分度值的除运算进行
//例如分5类时颜色分度值为(99--62)/5=32.2;高程-60与最低高程插值为22与颜色分度值32.22/32.2取色带编号0
//高程-29.7与最低高程差值为32.3,32.3/32.2=1,取色带编号1
//高程99,与最低高差161161/32.2=5.0这里可能会出现浮点数的问题这里只能去4所以统一在得到index时,如果大于LEVEL-1就取LEVEL-1,如果小于0就取0
int index = ((h - MIN_HEIGHT) / heightColorDivision);
if (index > LEVEL - 1) index = LEVEL - 1;
if (index < 0) index = 0;
return colorRamp[index];
}
void Initialize()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
@@ -114,7 +154,7 @@ void Initialize()
glEnable(GL_TEXTURE_2D);
const char *textureFileName = "Terrain2.bmp";
// const char* textureFileName = "t1.bmp";
//const char* textureFileName = "t1.bmp";
char *tempName = new char[100];
strcpy(tempName, textureFileName);
imageData = LoadBitmapFile(tempName, &bitmapInfoHeader);
@@ -312,7 +352,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);
LPCWSTR title = _T("MyClass");
LPCWSTR WINDOW_TITLE = _T("点( 键'1'), 线 ( 键'2'),三角形( 键'3')");
LPCWSTR WINDOW_TITLE = _T("点( 键'1'), 线 ( 键'2'),三角形( 键'3'),顶点着色( 键'4')");
hwnd = CreateWindowEx(NULL,
title,
@@ -368,7 +408,9 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine
return msg.wParam;
}
void RenderState4() {
}
void Render()
{
radians = float(PI * (angle - 90.0f) / 180.0f);

View File

@@ -1,5 +1,3 @@
//#include <glut.h>
#include <iostream>
#include <fstream>
#include <math.h>
@@ -8,46 +6,16 @@
using namespace std;
#define _CRT_SECURE_NO_WARNINGS
#define WINDOW_WIDTH 640 // Window Width Default
#define WINDOW_HEIGHT 480 // Window Height Default
#define WINDOW_WIDTH 1024
#define WINDOW_HEIGHT 720
// definition of PI
#define PI 3.14159265
// Used to defien the title of the window
// A simple structure to define a point whose coordinates are integers
/*typedef struct { GLint x, y; } GLintPoint;
// This structure is used to store the vertices of a polyline
typedef struct { int num; GLintPoint pt[100]; } GLintPointArray;
// Data for an Icosahedron
#define ICO_X 0.525731112119133606
#define ICO_Z 0.850650808352039932*/
/*static GLfloat vdataICO[12][3] =
{
{ -ICO_X, 0.0, ICO_Z }, { ICO_X, 0.0, ICO_Z }, { -ICO_X, 0.0, -ICO_Z }, { ICO_X, 0.0, -ICO_Z },
{ 0.0, ICO_Z, ICO_X }, { 0.0, ICO_Z, -ICO_X }, { 0.0, -ICO_Z, ICO_X }, { 0.0, -ICO_Z, -ICO_X },
{ ICO_Z, ICO_X, 0.0 }, { -ICO_Z, ICO_X, 0.0 }, { ICO_Z, -ICO_X, 0.0 }, { -ICO_Z, -ICO_X, 0.0 }
};
static GLuint tindicesICO[20][3] =
{
{ 1, 4, 0 }, { 4, 9, 0 }, { 4, 5, 9 }, { 8, 5, 4 }, { 1, 8, 4 },
{ 1, 10, 8 }, { 10, 3, 8 }, { 8, 3, 5 }, { 3, 2, 5 }, { 3, 7, 2 },
{ 3, 10, 7 }, { 10, 6, 7 }, { 6, 11, 7 }, { 6, 0, 11 }, {6, 1, 0 },
{ 10, 1, 6 }, { 11, 0, 9 }, { 2, 11, 9 }, { 5, 2, 9 }, { 11, 2, 7 }
};*/
// Data for Tetrahedron
static GLfloat P1T[3] = { -2, 3, 0 };
static GLfloat P2T[3] = { -3, 0, 0 };
static GLfloat P3T[3] = { -1, 0, 3 };
static GLfloat P4T[3] = { -4, 0, 0 };
// Calculating the Normalized Cross Product of Two Vectors
void normalize( float v[3] )
{
GLfloat d = sqrt( float(v[0]*v[0] + v[1]*v[1] + v[2]*v[2]) );
@@ -69,62 +37,50 @@ void normcrossprod( float v1[3], float v2[3], float out[3] )
normalize( out );
}
////// Defines
#define BITMAP_ID 0x4D42 // the universal bitmap ID
#define MAP_X 32 // size of map along x-axis
#define MAP_Z 32 // size of map along z-axis
#define MAP_SCALE 20.0f // the scale of the terrain map
#define BITMAP_ID 0x4D42
#define MAP_X 32
#define MAP_Z 32
#define MAP_SCALE 20.0f
////// Texture Information
BITMAPINFOHEADER bitmapInfoHeader; // temp bitmap info header
BITMAPINFOHEADER landInfo; // land texture info header
BITMAPINFOHEADER waterInfo; // water texture info header
BITMAPINFOHEADER bitmapInfoHeader;
BITMAPINFOHEADER landInfo;
BITMAPINFOHEADER waterInfo;
//AUX_RGBImageRec
unsigned char* imageData; // the map image data
unsigned char* landTexture; // land texture data
unsigned int land; // the land texture object
////// Terrain Data
unsigned char* imageData;
unsigned char* landTexture;
unsigned int land;
float terrain[MAP_X][MAP_Z][3]; // heightfield terrain data (0-255); 256x256
// LoadBitmapFile
// desc: Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr; // the file pointer
BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
unsigned char *bitmapImage; // bitmap image data
int imageIdx = 0; // image index counter
unsigned char tempRGB; // swap variable
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;
// open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;
// read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
// verify that this is a bitmap by checking for the universal bitmap id
if (bitmapFileHeader.bfType != BITMAP_ID)
{
fclose(filePtr);
return NULL;
}
// read the bitmap information header
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
// move file pointer to beginning of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
// allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
// verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
@@ -132,17 +88,14 @@ unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader
return NULL;
}
// read in the bitmap image data
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
// make sure bitmap image data was read
if (bitmapImage == NULL)
{
fclose(filePtr);
return NULL;
}
// swap the R and B values to get RGB since the bitmap color format is in BGR
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
@@ -150,7 +103,6 @@ unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader
bitmapImage[imageIdx + 2] = tempRGB;
}
// close the file and return the bitmap image data
fclose(filePtr);
return bitmapImage;
}
@@ -164,7 +116,6 @@ bool LoadTextures()
if (!landTexture)
return false;
// generate the land texture as a mipmap
glGenTextures(1, &land);
glBindTexture(GL_TEXTURE_2D, land);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

View File

Binary file not shown.

View File

@@ -1,6 +1,7 @@
 GLTerrain.cpp
C:\Project\opengl\GLTerrain\GLTerrain\vkgllib.h(9,1): warning C4005: “_CRT_SECURE_NO_WARNINGS”: 宏重定义
C:\Project\opengl\GLTerrain\GLTerrain\vkgllib.h(7,1): warning C4005: “_CRT_SECURE_NO_WARNINGS”: 宏重定义
C:\Project\opengl\GLTerrain\GLTerrain\GLTerrain.cpp : message : 参见“_CRT_SECURE_NO_WARNINGS”的前一个定义
C:\Project\opengl\GLTerrain\GLTerrain\vkgllib.h(146,30): warning C4018: “<”: 有符号/无符号不匹配
C:\Project\opengl\GLTerrain\GLTerrain\GLTerrain.cpp(369,12): warning C4244: “return”: 从“WPARAM”转换到“int”可能丢失数据
C:\Project\opengl\GLTerrain\GLTerrain\vkgllib.h(99,30): warning C4018: “<”: 有符号/无符号不匹配
C:\Project\opengl\GLTerrain\GLTerrain\GLTerrain.cpp(137,12): warning C4244: “初始化”: 从“GLfloat”转换到“int”可能丢失数据
C:\Project\opengl\GLTerrain\GLTerrain\GLTerrain.cpp(409,12): warning C4244: “return”: 从“WPARAM”转换到“int”可能丢失数据
GLTerrain.vcxproj -> C:\Project\opengl\GLTerrain\x64\Debug\GLTerrain.exe

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.