float cspec = pow(max(dot(r, vdir), 0.0), shininess);
vec4 texel = texture2D(tex, tex_coord);
- //color.xyz = diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec;
- color.xyz = diffuse.xyz * cdiff + specular.xyz * cspec;
+ color.xyz = diffuse.xyz * cdiff * texel.xyz + specular.xyz * cspec;
color.w = 1.0;
}
varying vec3 ldir;
varying vec2 tex_coord;
-const vec3 lpos = vec3(50.0, 50.0, -200.0);
+const vec3 lpos = vec3(-10.0, 100.0, 10.0);
/* attributes */
layout(location = 1) in vec3 attr_pos;
img_destroy(&ipm);
return true;
+}
+
+Vec4 Image::lookup_nearest(float u, float v) const
+{
+ int x = (int)(u * w) % w;
+ int y = (int)(v * h) % h;
+
+ if(is_float) {
+ return ((Vec4*)pixels)[y * w + x];
+ }
+
+ Vec4 color;
+ unsigned char *pptr = ((unsigned char*)pixels) + (y * w + x) * 4;
+ color.x = pptr[0] / 255.0;
+ color.y = pptr[1] / 255.0;
+ color.z = pptr[2] / 255.0;
+ color.w = pptr[3] / 255.0;
+
+ return color;
}
\ No newline at end of file
#ifndef IMAGE_H_
#define IMAGE_H_
+#include <gmath/gmath.h>
+
class Image {
public:
int w;
Image &operator =(Image &&image);
bool load(const char *fname);
+
+ Vec4 lookup_nearest(float u, float v) const;
+ //TODO lookup_linear
};
#endif // IMAGE_H_
\ No newline at end of file
static float aspect;
static OrbitCamera *camera;
-static Scene *scene_cow;
-static Renderer *rcow;
+static Scene *cow_scene;
+static Renderer *cow_rend;
-static Scene *scene_ground;
-static Renderer *rground; // default renderer
-static Texture *gskybox;
+static Scene *ground_scene;
+static Renderer *ground_rend; // default renderer
+static Texture *skybox_tex;
-static Renderer *tr;
-static Material tmat;
-static Terrain t;
+static Renderer *terrain_rend;
+static Material terrain_mat;
+static Texture *terrain_tex;
+static Terrain terrain;
/* *** */
camera = new OrbitCamera;
- // scene_ground = new Scene;
- // if(!scene_ground->load("data/ground.obj")) {
+ // ground_scene = new Scene;
+ // if(!ground_scene->load("data/ground.obj")) {
// fprintf(stderr, "Failed to load scene: ground.obj.\n");
// return false;
// }
- // rground = new Renderer;
- // rground->camera = camera;
- // rground->scene = scene_ground;
+ // ground_rend = new Renderer;
+ // ground_rend->camera = camera;
+ // ground_rend->scene = ground_scene;
- // if(!rground->create()) {
+ // if(!ground_rend->create()) {
// fprintf(stderr, "Failed to create default renderer.\n");
// return false;
// }
- // gskybox = gfx_create_texture();
- // gskybox->load("data/cubemap/cubemap.hdr");
- // rground->set_sky_tex(gskybox);
+ // skybox_tex = gfx_create_texture();
+ // skybox_tex->load("data/cubemap/cubemap.hdr");
+ // ground_rend->set_sky_tex(skybox_tex);
- scene_cow = new Scene;
- if(!scene_cow->load("data/spot/spot.obj")) {
+ cow_scene = new Scene;
+ if(!cow_scene->load("data/spot/spot.obj")) {
fprintf(stderr, "Failed to load scene: spot.obj.\n");
return false;
}
- rcow = new Renderer;
- rcow->camera = camera;
- rcow->scene = scene_cow;
+ cow_rend = new Renderer;
+ cow_rend->camera = camera;
+ cow_rend->scene = cow_scene;
- if(!rcow->create()) {
+ if(!cow_rend->create()) {
fprintf(stderr, "Failed to create renderer for cows.\n");
return false;
}
+ terrain_tex = gfx_create_texture();
+ if(!terrain_tex->load("data/grass.jpeg")) {
+ fprintf(stderr, "Failed to load ground texture.\n");
+ return false;
+ }
+
+ Image ter_hmap;
+ if(!ter_hmap.load("data/terhmap.png")) {
+ fprintf(stderr, "Failed to load terrain heightmap.\n");
+ return false;
+ }
+
TerrainParams p;
p.xsz = 50;
p.ysz = 50;
- p.max_height = 2;
+ p.max_height = 5;
p.xtiles = 20;
p.ytiles = 20;
p.tile_usub = 10;
p.tile_vsub = 10;
p.num_octaves = 3;
p.noise_freq = 10;
- p.coarse_heightmap = 0;
+ p.coarse_heightmap = &ter_hmap;
- t.init();
- t.generate(p);
+ terrain.init();
+ terrain.generate(p);
- tmat.diffuse = Vec3(1, 0, 0);
- tmat.specular = Vec3(0.5, 0, 0);
- tmat.shininess = 40;
- tmat.dtex = 0;
- tmat.name = "tt";
+ terrain_mat.diffuse = Vec3(1, 1, 1);
+ terrain_mat.specular = Vec3(0, 0, 0);
+ terrain_mat.shininess = 40;
+ terrain_mat.dtex = terrain_tex;
+ terrain_mat.name = "tt";
- t.material = tmat;
+ terrain.material = terrain_mat;
- tr = new Renderer;
- tr->camera = camera;
- tr->scene = t.get_visible(camera);
- if(!tr->create()) {
+ terrain_rend = new Renderer;
+ terrain_rend->camera = camera;
+ terrain_rend->scene = terrain.get_visible(camera);
+ if(!terrain_rend->create()) {
fprintf(stderr, "terrain fail\n");
return false;
}
// TODO delete: debugging
- // for(size_t i=0; i<scene_ground->objects.size(); ++i) {
+ // for(size_t i=0; i<ground_scene->objects.size(); ++i) {
// printf("object: %d\n", (int)i);
- // printf("mesh: %s\n", scene_ground->objects[i]->mesh->name.c_str());
- // printf("material: %s\n", scene_ground->objects[i]->material->name.c_str());
+ // printf("mesh: %s\n", ground_scene->objects[i]->mesh->name.c_str());
+ // printf("material: %s\n", ground_scene->objects[i]->material->name.c_str());
// printf("transform:\n");
- // scene_ground->objects[i]->transform.print();
+ // ground_scene->objects[i]->transform.print();
// }
return true;
}
delete sdr_man;
delete camera;
- delete scene_cow;
- delete rcow;
+ delete cow_scene;
+ delete cow_rend;
- // delete scene_ground;
- // delete rground;
+ // delete ground_scene;
+ // delete ground_rend;
//TODO
- delete tr;
+ delete terrain_tex;
+ delete terrain_rend;
gfx_cleanup();
}
// gfx_clear(0.76, 0.3, 0.43);
gfx_clear(0.1, 0.1, 0.1);
- tr->draw();
-// rground->draw();
- // rcow->draw();
+ terrain_rend->draw();
+// ground_rend->draw();
+ cow_rend->draw();
}
\ No newline at end of file
Vec3 bitangent = hfield_vertex(u, v + dv, calc_height(u, v + dv, ptr),
xsz, ysz, height) - vtx;
- Vec3 normal = normalize(cross(tangent, bitangent));
+ Vec3 normal = normalize(cross(bitangent, tangent));
mesh->vertices[vidx] = vtx;
mesh->normals[vidx] = normal;
/* getting material uniform locations: diffuse, specular, specular exponent (strength) */
- if((diff_loc = sprog->get_uniform_location("diffuse")) == -1) {
- fprintf(stderr, "Invalid uniform location: can't find \"diffuse\".\n");
- return false;
- }
-
- if((spec_loc = sprog->get_uniform_location("specular")) == -1) {
- fprintf(stderr, "Invalid uniform location: can't find \"specular\".\n");
- return false;
- }
-
- if((shin_loc = sprog->get_uniform_location("shininess")) == -1) {
- fprintf(stderr, "Invalid uniform location: can't find \"shininess\".\n");
- return false;
- }
+ diff_loc = sprog->get_uniform_location("diffuse");
+ spec_loc = sprog->get_uniform_location("specular");
+ shin_loc = sprog->get_uniform_location("shininess");
/* uniform locations for matrices */
- if((mmviewproj_loc = sprog->get_uniform_location("mmviewproj")) == -1) {
- fprintf(stderr, "Invalid uniform location: can't find \"mmviewproj\".\n");
- return false;
- }
-
- if((mview_loc = sprog->get_uniform_location("mview")) == -1) {
- fprintf(stderr, "Invalid uniform location: can't find \"mview\".\n");
- return false;
- }
+ mmviewproj_loc = sprog->get_uniform_location("mmviewproj");
+ mview_loc = sprog->get_uniform_location("mview");
return true;
}
/* setting uniforms for material */
- sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
- sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
- sprog->set_uniformf(shin_loc, m->shininess);
+ if(diff_loc != -1)
+ sprog->set_uniformf(diff_loc, m->diffuse.x, m->diffuse.y, m->diffuse.z, 1);
+
+ if(spec_loc != -1)
+ sprog->set_uniformf(spec_loc, m->specular.x, m->specular.y, m->specular.z, 1);
+
+ if(shin_loc != -1)
+ sprog->set_uniformf(shin_loc, m->shininess);
/* texture */
/* setting uniforms for matrices */
Mat4 mmviewproj = object->transform * camera->get_view_matrix() * mprojection;
- sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
+ if(mmviewproj_loc != -1)
+ sprog->set_uniform_matrix(mmviewproj_loc, mmviewproj);
Mat4 mview = object->transform * camera->get_view_matrix();
- sprog->set_uniform_matrix(mview_loc, mview);
+ if(mview_loc != -1)
+ sprog->set_uniform_matrix(mview_loc, mview);
object->mesh->draw();
}
nprog->use();
object->mesh->draw_normals(1.0);
- }*/
+ }
+ */
// if(m->dtex)
// m->dtex->unbind();
GenData *data = (GenData*)ptr;
const TerrainParams *tp = data->tp;
- // float ufreq = tp->noise_freq / tp->xtiles;
- // float vfreq = tp->noise_freq / tp->ytiles;
-
u = u / tp->xtiles + data->xoffs;
v = v / tp->ytiles + data->yoffs;
float sn = gph::fbm(u * tp->noise_freq, v * tp->noise_freq, tp->num_octaves);
- /* todo use the image later */
+
+ if(tp->coarse_heightmap) {
+ Vec4 texel = tp->coarse_heightmap->lookup_nearest(u, v);
+ sn *= texel.x;
+ }
return sn;
}
\ No newline at end of file