Skip to content
Snippets Groups Projects
Commit 32d86794 authored by Zohaib Butt's avatar Zohaib Butt
Browse files

coloring the terrain based on height value

parent 3fa73b15
No related branches found
No related tags found
No related merge requests found
#version 330 core
out vec4 FragColor;
out vec3 color;
in vec2 TexCoords;
in vec3 ambient;
in vec3 diffuse;
in vec3 specular;
in vec3 position;
uniform sampler2D texture_diffuse1;
uniform vec4 color;
//uniform vec3 color;
void main()
{
if(position.y <= 10.0f) {
color = vec3(0.0f, 0.0f, 0.7f);
//printf("Passed2\n");
} else if(position.y > 10.0f && position.y <= 15.0f){
color = vec3(0.0f, 0.7f, 0.0f);
} else if(position.y > 15.0f && position.y <= 20.0f){
color = vec3(0.6f, 0.5f, 0.2f);
//printf("Passed3\n");
} else {
color = vec3(1.0f, 1.0f, 1.0f);
}
vec3 phong = ambient + diffuse + specular;
FragColor = color; // vec4((texture(texture_diffuse1, TexCoords).rgb * vec3(0.0f, 1.0f, 0.0f)) * phong, 1.0);
FragColor = vec4(color , 1.0f); // vec4((texture(texture_diffuse1, TexCoords).rgb * vec3(0.0f, 1.0f, 0.0f)) * phong, 1.0);
}
\ No newline at end of file
......@@ -26,7 +26,7 @@ out vec3 ambient;
out vec3 diffuse;
out vec3 specular;
out vec2 TexCoords;
out vec3 position;
// this function specifies spreading of light.
......@@ -65,6 +65,8 @@ void main()
{
aPos0 = vec3((model * inverse(view) * vec4(aPos, 0.0f)));
aNormal0 = normalize(vec3((model * vec4(aNormal,0.0f))));
position = aPos;
ambient = ambientComponent();
diffuse = diffuseComponent();
specular = specularComponent();
......
......@@ -3,7 +3,7 @@
#include <GLFW/glfw3.h>
environment::LightSource::LightSource(){
this->position = {0.0f, 0.0f, 5.0f};
this->position = {10.0f, 10.0f, 5.0f};
this->lightColor = {1.0f, 1.0f, 1.0f};
this->attenuation = {1.0f, 1.0f, 1.0f};
this->ambientCoefficient = 0.1f;
......
......@@ -10,21 +10,27 @@ extern modeler::ShaderManager* shaderManager;
extern environment::LightSource* lightSource;
float rotationTime = 0.0f;
game::HeightMap::HeightMap(char* path, int x, int z) : Model()
game::HeightMap::HeightMap(char* path, float x, float z) : Model()
{
this->position = glm::vec3(0.0f, 0.0f, 0.0f);
this->width = x;
this->length = z;
// Allocate memory for map height values.
map = new float*[length];
for (int i = 0; i < length; i++)
{
map[i] = new float[width];
}
loadMap(path);
computeVertices();
computeIndices();
for (int i = 0; i < length; ++i)
{
for (int i = 0; i < width; ++i)
{
/* code */
}
}
std::vector<modeler::TextureA> textures;
......@@ -67,8 +73,8 @@ auto game::HeightMap::draw(float dt) -> void
{"attenuationCID", "attenuationC"},
{"ambientCoefficientID", "ambientCoefficient"},
{"specularExponentID", "specularExponent"},
{"lightColorID", "lightColor"},
{"colorID", "color"}
{"lightColorID", "lightColor"}
// {"colorID", "color"}
}));
glUniform1f(uniforms["attenuationAID"], attenuation.x);
......@@ -78,7 +84,36 @@ auto game::HeightMap::draw(float dt) -> void
glUniform1i(uniforms["specularExponentID"], specularExponent);
glUniform3fv(uniforms["lightColorID"], 1, value_ptr(lightColor));
glUniform3fv(uniforms["lightSourcePositionID"], 1, value_ptr(lightPosition));
glUniform4fv(uniforms["colorID"], 1, value_ptr(glm::vec4(0.0f, 1.0f, 0.0f, 1.0f)));
/*
for (int i = 0; i < vertices.size(); ++i)
{
printf("All heightvalue: %f\n", vertices[i].Position.y);
if(highestPoint >= vertices[i].Position.y){
highestPoint = vertices[i].Position.y;
//printf("heightvalue: %f\n", vertices[i].Position.y);
}
if(vertices[i].Position.y < 15.0f) {
color = glm::vec3(0.0f, 0.0f, 7.0f);
//printf("Passed1\n");
} else if(vertices[i].Position.y > 15.0f && vertices[i].Position.y <= 20.0f) {
color = glm::vec3(0.0f, 7.0f, 0.0f);
//printf("Passed2\n");
} else if(vertices[i].Position.y > 20.0f && vertices[i].Position.y <= 25.0f){
color = glm::vec3(0.4f, 0.5f, 0.0f);
//printf("Passed3\n");
} else {
color = glm::vec3(1.0f, 1.0f, 1.0f);
}
glUniform3fv(uniforms["colorID"], 1, value_ptr(this->color));
}*/
glUniform3fv(uniforms["camPosID"], 1, value_ptr(camera->getPos())); //glm::mat4 model = glm::rotate(glm::mat4(), time, glm::vec3(0, 1, 0));
......@@ -89,7 +124,7 @@ auto game::HeightMap::draw(float dt) -> void
rotationTime += dt;
modelm = glm::translate(modelm, this->position); // Translate it down so it's at the center of the scene.
//printf("%f, %f, %f\n",position.x, position.y, position.z );
modelm = glm::scale(modelm, glm::vec3(0.02f, 0.02f, 0.02f));
//modelm = glm::scale(modelm, glm::vec3(0.002f, 0.002f, 0.002f));
glUniformMatrix4fv(uniforms["modelID"], 1, GL_FALSE, glm::value_ptr(modelm));
glm::mat3 normalMatrix = glm::transpose(glm::inverse(glm::mat3(view*modelm)));
......@@ -115,22 +150,16 @@ auto game::HeightMap::loadMap(char* path) -> void{
int w, h;
unsigned char* image = SOIL_load_image(path, &w, &h, 0, SOIL_LOAD_L);
this->width = w;
this->length = h;
// Allocate memory for map height values.
map = new float*[length];
for (int i = 0; i < length; i++)
{
map[i] = new float[width];
}
this->size = glm::vec3(w / this->width, (this->width / 4.0f) / 255.0f, h / this->length);
for (int y = 0; y < h; y++)
printf("Size: %f\n", size.y);
for (int y = 0; y < this->length; y++)
{
for (int x = 0; x < w; x++)
for (int x = 0; x < this->width; x++)
{
color = image[(y * w + x)];
float height = (this->width/4) * (color / 225.0f) * 0.5;
float height = size.y * color;
map[y][x] = height;
......@@ -139,8 +168,11 @@ auto game::HeightMap::loadMap(char* path) -> void{
}
auto game::HeightMap::computeNormals() -> void {
auto game::HeightMap::computeNormals(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) -> void {
glm::vec3 normal;
//normal.x = v2.x - v3
}
auto game::HeightMap::computeVertices() -> void {
......@@ -151,43 +183,38 @@ auto game::HeightMap::computeVertices() -> void {
for (int x = 0; x < this->width; x++)
{
vertex.Position = glm::vec3(x, map[z][x], z);
vertex.Position = glm::vec3(x * size.x, map[z][x], z * size.z);
vertices.push_back(vertex);
}
}
for (int i = 0; i < vertices.size(); ++i)
{
printf("%f\n", vertices[i].Position.y);
}
}
auto game::HeightMap::computeIndices() -> void {
unsigned int index = 0;
for (int i = 0; i < this->length - 1; ++i)
{
for (int j = 0; j < this->width - 1; ++j)
{
indices.push_back(index + 1);
indices.push_back(index + 2);
indices.push_back((index + 1) * width);
indices.push_back(index + 2);
indices.push_back((index + 1) * width);
indices.push_back((index + 3) * width);
/*
indices.push_back(i * width + j);
indices.push_back((i + 1) * width + j);
*/
indices.push_back(j);
indices.push_back(j + 1);
indices.push_back(j + (width * (i + 1)));
index++;
indices.push_back(j + (width * (i + 1)));
indices.push_back((j + 1));
indices.push_back((j + 1) + (width * (i + 1)));
}
}
for (int i = 0; i < indices.size() /10000; ++i)
{
printf("%d\t", indices[i]);
//printf("%d\t", indices[i]);
}
}
\ No newline at end of file
......@@ -21,7 +21,7 @@ namespace game {
/**
* @brief HeightMap constructor.
*/
HeightMap(char* path, int x, int z);
HeightMap(char* path, float x, float z);
/**
* @brief
......@@ -67,7 +67,7 @@ namespace game {
auto loadMap(char* path) -> void;
auto computeNormals() -> void;
auto computeNormals(glm::vec3 v1, glm::vec3 v2, glm::vec3 v3) -> void;
auto computeVertices() -> void;
......@@ -76,7 +76,7 @@ namespace game {
private:
//std::vector<components::IComponent*> componentList;
glm::vec3 size;
int width; //!< x plane
int length; //!< z plane
float **map; //!< height values
......@@ -84,7 +84,8 @@ namespace game {
std::vector<modeler::Vertex> vertices; //!< vertrices
std::vector<unsigned int> indices; //!< vertrices
glm::vec3 position; //!< Origin of board.
float highestPoint = 0.0f;
glm::vec3 color;
modeler::Shader* shaderProgram; //!< Shaderprogram used by board for drawing.
};
......
......@@ -21,7 +21,7 @@ int main(int argc, char const *argv[])
{
// Create camera
printf("%s Setting up camera\n",TAG_INFO.c_str());
camera = new environment::Camera(glm::vec3(0, 0, 5), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0));
camera = new environment::Camera(glm::vec3(0, 0, 0), glm::vec3(0, 0, -1), glm::vec3(0, 1, 0));
// Setting up light
printf("%s Setting up LightSource\n",TAG_INFO.c_str());
......@@ -40,7 +40,7 @@ int main(int argc, char const *argv[])
// Make cube
printf("%s Creating board\n", TAG_INFO.c_str());
hm = new game::HeightMap("../asset/heightmap/height100.png", 200, 200);
hm = new game::HeightMap("../asset/heightmap/height100.png", 200.0f, 200.0f);
//hm->registerComponent(component);
//component = nullptr;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment