Added heightmap loading

This commit is contained in:
2016-05-23 14:51:04 +02:00
parent 66cc034b4a
commit 8bd092c9e9
13 changed files with 2234 additions and 7 deletions
+51
View File
@@ -0,0 +1,51 @@
#include "HeightMap.h"
#include "stb_image.h"
#include <GL/freeglut.h>
#include <iostream>
#include <string>
HeightMap::HeightMap(const std::string &file)
{
int bpp;
unsigned char* imgData = stbi_load(file.c_str(), &width, &height, &bpp, 4);
for (int h = 0; h < height; h++)
{
for (int w = 0; w < width; w++)
{
int offsets[4][2] = { { 0, 0 },{ 1, 0 },{ 1, 1 },{ 0, 1 } };
for (int i = 0; i < 4; i++)
{
float height = ((float)imgData[((h + offsets[i][0]) + (w + offsets[i][1]) * width) * 4]);
height = (height / 256.0f) * 100.0f;
vertices.push_back(Vertex{ (float)(h + offsets[i][0]), height, (float)(w + offsets[i][1]),
0, 1, 0,
h*0.1f, (w*0.f) + offsets[i][1] });
}
}
}
stbi_image_free(imgData);
}
HeightMap::~HeightMap()
{
}
void HeightMap::Draw()
{
glEnableClientState(GL_VERTEX_ARRAY);
//glEnableClientState(GL_COLOR_ARRAY);
//glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Vertex), ((float*)vertices.data()) + 0);
//glNormalPointer(GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 3);
//glColorPointer(4, GL_FLOAT, sizeof(Vertex), ((float*)cubeVertices.data()) + 6);
glDrawArrays(GL_QUADS, 0, vertices.size());
glDisableClientState(GL_VERTEX_ARRAY);
//glDisableClientState(GL_COLOR_ARRAY);
//glDisableClientState(GL_NORMAL_ARRAY);
}
+31
View File
@@ -0,0 +1,31 @@
#pragma once
#include <string>
#include <vector>
class HeightMap
{
private:
int height;
int width;
public:
HeightMap(const std::string &file);
~HeightMap();
void Draw();
struct Vertex {
float x;
float y;
float z;
float normalX;
float normalY;
float normalZ;
float texX;
float texY;
};
std::vector<Vertex> vertices;
};
+27 -7
View File
@@ -4,18 +4,22 @@
#include <cmath>
#include <vector>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "ObjModel.h"
#include "HeightMap.h"
float lastFrameTime = 0;
int width, height;
int currentModel = 0;
vector<ObjModel*> models;
HeightMap *heightmap;
struct Camera
{
float posX = 0;
float posY = -4;
float posZ = 0;
float rotX = 0;
float rotY = 0;
} camera;
@@ -59,13 +63,13 @@ void display()
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0f, (float)width/height, 0.1, 30);
gluPerspective(60.0f, (float)width/height, 0.1, 5000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(camera.rotX, 1, 0, 0);
glRotatef(camera.rotY, 0, 1, 0);
glTranslatef(camera.posX, 0, camera.posY);
glTranslatef(camera.posX, camera.posZ, camera.posY);
float pos[4] = { 0.5, 1, -1, 0 };
glLightfv(GL_LIGHT0, GL_POSITION, pos);
@@ -79,6 +83,7 @@ void display()
glVertex3f(-15, -1, 15);
glEnd();
/*
for (int x = -10; x <= 10; x += 5)
{
for (int y = -10; y <= 10; y += 5)
@@ -90,6 +95,14 @@ void display()
}
}
glColor3f(0.1f, 1.0f, 1.0f);
models[currentModel]->draw();
*/
heightmap->Draw();
glutSwapBuffers();
}
@@ -99,17 +112,24 @@ void move(float angle, float fac)
camera.posY += (float)sin((camera.rotY + angle) / 180 * M_PI) * fac;
}
void moveVert(float angle, float fac)
{
camera.posZ += angle * fac;
}
void idle()
{
float frameTime = glutGet(GLUT_ELAPSED_TIME)/1000.0f;
float deltaTime = frameTime - lastFrameTime;
lastFrameTime = frameTime;
const float speed = 3;
const float speed = 50;
if (keys['a']) move(0, deltaTime*speed);
if (keys['d']) move(180, deltaTime*speed);
if (keys['w']) move(90, deltaTime*speed);
if (keys['s']) move(270, deltaTime*speed);
if (keys['q']) moveVert(1, deltaTime*speed);
if (keys['e']) moveVert(-1, deltaTime*speed);
glutPostRedisplay();
}
@@ -166,7 +186,7 @@ int main(int argc, char* argv[])
glutKeyboardUpFunc(keyboardUp);
glutPassiveMotionFunc(mousePassiveMotion);
heightmap = new HeightMap("worlds/HMCSHeightmap.gif");
cubeVertices.push_back(Vertex{ -1, -1, -1, 0,0,1, 1,1,1,1 });
cubeVertices.push_back(Vertex{ -1, 1, -1, 0,0,1, 1,1,1,1 });
@@ -199,7 +219,7 @@ int main(int argc, char* argv[])
cubeVertices.push_back(Vertex{ 1, 1, -1, 1,0,0, 1,1,1,1 });
models.push_back(new ObjModel("models/ship/shipA_OBJ.obj"));
glutWarpPointer(width / 2, height / 2);
+2
View File
@@ -150,10 +150,12 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="HeightMap.cpp" />
<ClCompile Include="Main.cpp" />
<ClCompile Include="ObjModel.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="HeightMap.h" />
<ClInclude Include="ObjModel.h" />
<ClInclude Include="stb_image.h" />
</ItemGroup>
+6
View File
@@ -21,6 +21,9 @@
<ClCompile Include="ObjModel.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="HeightMap.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="ObjModel.h">
@@ -29,5 +32,8 @@
<ClInclude Include="stb_image.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="HeightMap.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>
Binary file not shown.

After

Width:  |  Height:  |  Size: 3.0 MiB

Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 04.05.2010 13:43:14
newmtl shipA_mat
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.5880 0.5880 0.5880
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000
map_Ka s_1024_C.tga
map_Kd s_1024_C.tga
map_Ke s_1024_I.tga
map_bump s_1024_N.tga
File diff suppressed because it is too large Load Diff
Binary file not shown.

After

Width:  |  Height:  |  Size: 163 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 23 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 60 KiB