--- /dev/null
+#version 450
+
+out vec4 color;
+
+void main()
+{
+ color = vec4(1.0, 0.0, 1.0, 1.0);
+}
--- /dev/null
+#version 450
+
+uniform mat4 mmviewproj;
+
+layout(location = 1) in vec3 attr_pos;
+
+void main()
+{
+ gl_Position = mmviewproj * vec4(attr_pos, 1.0);
+}
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 * texel.xyz + specular.xyz * cspec;
+ color.xyz = diffuse.xyz * cdiff + specular.xyz * cspec;
color.w = 1.0;
}
#include "object.h"
#include "renderer.h"
#include "scene.h"
+#include "terrain.h"
#include "texture.h"
#include "opengl/opengl.h"
static Renderer *rground; // default renderer
static Texture *gskybox;
+static Renderer *tr;
+static Material tmat;
+static Terrain t;
+
/* *** */
int main(int argc, char **argv)
camera = new OrbitCamera;
camera->set_orbit_params(phi, theta, dist);
- scene_ground = new Scene;
- if(!scene_ground->load("data/ground.obj")) {
- fprintf(stderr, "Failed to load scene: ground.obj.\n");
- return false;
- }
+ // scene_ground = new Scene;
+ // if(!scene_ground->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;
+
+ // if(!rground->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);
scene_cow = new Scene;
if(!scene_cow->load("data/spot/spot.obj")) {
return false;
}
- rground = new Renderer;
- rground->camera = camera;
- rground->scene = scene_ground;
-
- if(!rground->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);
-
rcow = new Renderer;
rcow->camera = camera;
rcow->scene = scene_cow;
return false;
}
-// TODO delete: debugging
- for(size_t i=0; i<scene_ground->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("transform:\n");
- scene_ground->objects[i]->transform.print();
+ TerrainParams p;
+ p.xsz = 50;
+ p.ysz = 50;
+ p.max_height = 1;
+ p.xtiles = 1; //40;
+ p.ytiles = 1; // 40;
+ p.tile_usub = 8;
+ p.tile_vsub = 8;
+ p.num_octaves = 3;
+ p.noise_freq = 10;
+ p.coarse_heightmap = 0;
+
+ t.init();
+ t.generate(p);
+
+ tmat.diffuse = Vec3(1, 0, 0);
+ tmat.specular = Vec3(0.5, 0, 0);
+ tmat.shininess = 40;
+ tmat.dtex = 0;
+ tmat.name = "tt";
+
+ t.material = tmat;
+
+ tr = new Renderer;
+ tr->camera = camera;
+ tr->scene = t.get_visible(camera);
+ if(!tr->create()) {
+ fprintf(stderr, "terrain fail\n");
+ return false;
}
+
+
+// TODO delete: debugging
+ // for(size_t i=0; i<scene_ground->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("transform:\n");
+ // scene_ground->objects[i]->transform.print();
+ // }
return true;
}
delete scene_cow;
delete rcow;
- delete scene_ground;
- delete rground;
+ // delete scene_ground;
+ // delete rground;
+//TODO
+ delete tr;
gfx_cleanup();
}
{
camera->set_orbit_params(phi, theta, dist);
- gfx_clear(0.76, 0.3, 0.43);
+ // gfx_clear(0.76, 0.3, 0.43);
+ gfx_clear(0.1, 0.1, 0.1);
- rground->draw();
- rcow->draw();
+ tr->draw();
+// rground->draw();
+ // rcow->draw();
}
\ No newline at end of file
virtual ~Mesh() = 0;
virtual void draw() const = 0;
+ virtual void draw_normals(float scale) const = 0;
virtual void update_vertex_data() = 0;
};
{
float x = u * xsz - xsz / 2.0;
float y = h * height;
- float z = v * ysz - ysz / 2.0;
+ float z = -(v * ysz - ysz / 2.0);
return Vec3(x, y, z);
}
int c = d + 1;
/* 1st triangle */
-
*iptr++ = a;
*iptr++ = b;
*iptr++ = c;
num_vertices = 0;
num_indices = 0;
+
+ /* draw normals */
+ nvao = nvbo = 0;
}
MeshGL::MeshGL(const MeshGL &mesh)
ibo = 0;
vao = 0;
+ /* draw normals */
+ nvao = nvbo = 0;
+
/*
* if we set these to the actual
* vertices.size() and indices.size()
glBindVertexArray(0);
}
+void MeshGL::draw_normals(float scale) const
+{
+ if(!nvao) {
+ glGenVertexArrays(1, &nvao);
+ glBindVertexArray(nvao);
+
+ glGenBuffers(1, &nvbo);
+ glBindBuffer(GL_ARRAY_BUFFER, nvbo);
+
+ glBufferData(GL_ARRAY_BUFFER, normals.size() * sizeof(Vec3) * 2, 0, GL_STATIC_DRAW);
+ Vec3 *data = (Vec3 *)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
+
+ for (size_t i = 0; i < normals.size(); i++)
+ {
+ *data++ = vertices[i];
+ *data++ = vertices[i] + normals[i] * scale;
+ }
+ glUnmapBuffer(GL_ARRAY_BUFFER);
+
+ glVertexAttribPointer(MESH_VERTEX, 3, GL_FLOAT, GL_FALSE, sizeof(Vec3), 0);
+ glEnableVertexAttribArray(MESH_VERTEX);
+ glBindBuffer(GL_ARRAY_BUFFER, 0);
+ } else {
+ glBindVertexArray(nvao);
+ }
+
+ glDrawArrays(GL_LINES, 0, normals.size() * 2);
+ glBindVertexArray(0);
+}
+
void MeshGL::update_vertex_data()
{
update_vbo();
if(vbo_tex_coords)
glDeleteBuffers(1, &vbo_tex_coords);
if(ibo)
- if(vao)
- glDeleteVertexArrays(1, &vao);
+ glDeleteBuffers(1, &ibo);
+ if (vao)
+ glDeleteVertexArrays(1, &vao);
+
+ if(nvbo)
+ glDeleteBuffers(1, &nvbo);
+ if(nvao)
+ glDeleteVertexArrays(1, &nvao);
}
\ No newline at end of file
void update_vbo();
+ /* used by draw_normals to debug stuff: */
+ mutable unsigned int nvao;
+ mutable unsigned int nvbo;
+
public:
MeshGL();
MeshGL(const MeshGL &mesh);
virtual ~MeshGL();
virtual void draw() const override;
+ virtual void draw_normals(float scale) const override;
virtual void update_vertex_data() override;
void destroy_vbo();
};
-#endif // MESH_GL_H_
+#endif // MESH_GL_H_
\ No newline at end of file
{
unsigned int target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
glBindTexture(target, tex);
+}
+
+void TextureGL::unbind()
+{
+ unsigned int target = is_cubemap() ? GL_TEXTURE_CUBE_MAP : GL_TEXTURE_2D;
+ glBindTexture(target, 0);
}
\ No newline at end of file
virtual ~TextureGL();
virtual void bind() override;
+ virtual void unbind() override;
};
#endif // TEXTURE_GL_H_
bool Renderer::create()
{
+ //debug
+ if(!(nprog = sdr_man->create_shader_program("debug.v.glsl", "debug.f.glsl"))) {
+ fprintf(stderr, "Failed to load debug shaders.\n");
+ }
+
if(!(sprog = sdr_man->create_shader_program("default.v.glsl", "default.f.glsl"))) {
return false;
}
object->mesh->update_vertex_data();
object->mesh->draw();
+
+ /* debug */
+ if(nprog) {
+ int loc = nprog->get_uniform_location("mmviewproj");
+ if(loc != -1) {
+ nprog->set_uniform_matrix(loc, mmviewproj);
+ }
+ nprog->use();
+ object->mesh->draw_normals(1.0);
+ }
+
+ // if(m->dtex)
+ // m->dtex->unbind();
}
void Renderer::set_sky_tex(Texture *stex)
ShaderProgram *sprog;
+ /* debug shader to draw normals */
+ ShaderProgram *nprog;
+
Texture *skytex, *dskytex;
virtual void draw_object(Object *object) const;
+#include <gmath/gmath.h>
+
#include "camera.h"
+#include "gfxapi.h"
#include "image.h"
+#include "mesh.h"
+#include "meshgen.h"
+#include "object.h"
#include "scene.h"
#include "terrain.h"
-Terrain::Terrain() {}
+static float calc_height(float u, float v, void *ptr);
+
+Terrain::Terrain()
+{
+ vis_scene = 0;
+}
Terrain::~Terrain()
{
}
+bool Terrain::init()
+{
+ vis_scene = new Scene;
+
+ return true;
+}
+
+void Terrain::destroy()
+{
+ delete vis_scene;
+}
+
bool Terrain::generate(const TerrainParams ¶ms)
{
- // if(xsz <= 0 || ysz <=0) {
- // fprintf(stderr, "Invalid terrain size.\n");
- // return false;
- // }
+ tiles.clear();
- // if(xtiles <= 0 || ytiles <= 0) {
- // fprintf(stderr, "Invalid number of terrain tiles.\n");
- // return false;
- // }
+ float txsz = params.xsz / params.xtiles;
+ float tysz = params.ysz / params.ytiles;
- // if(tiles)
- // tiles.clear();
+ for(int i=0; i<params.ytiles; i++) {
+ for(int j=0; j<params.xtiles; j++) {
+ TerrainTile tile;
+ tile.mesh = gfx_create_mesh();
- // int tsz = xtiles * ytiles;
- // tiles.resize(tsz);
+ gen_heightfield(tile.mesh, txsz, tysz, params.max_height,
+ params.tile_usub, params.tile_vsub, calc_height,
+ (void*)¶ms);
- // for(int i=0; i<tsz; i++) {
+ tile.mesh->update_vertex_data();
+ tiles.push_back(tile);
- // }
+/*
+ the terrain scene stores objects only
+ no need to fill the mat, mesh std::vectors
+*/
+ Object *o = new Object;
+ o->mesh = tile.mesh;
+ o->material = &material;
+ o->transform = Mat4::identity;
+
+ vis_scene->objects.push_back(o);
+ }
+ }
return true;
}
Scene *Terrain::get_visible(const Camera *camera) const
{
- return 0;
+ return vis_scene;
}
+
+static float calc_height(float u, float v, void *ptr)
+{
+ if(!ptr) {
+ fprintf(stderr, "Terrain parameters not found.\n");
+ return 0;
+ }
+
+ TerrainParams *tp = (TerrainParams*)ptr;
+ float sn = gph::fbm(u * tp->noise_freq, v * tp->noise_freq, tp->num_octaves);
+ /* todo use the image later */
+ return sn;
+}
\ No newline at end of file
private:
Mesh *mesh;
+ friend class Terrain;
};
/* parameters needed in terrain generation */
int tile_usub;
int tile_vsub;
int num_octaves; /* Perlin noise sums */
+ float noise_freq; /* Perlin noise scaling factor */
Image *coarse_heightmap; /* mask for low detail heightmap */
};
std::vector<TerrainTile> tiles;
public:
+ Material material;
+
Terrain();
~Terrain();
+ bool init();
+ void destroy();
+
bool generate(const TerrainParams ¶ms);
Scene *get_visible(const Camera *camera) const;
};
virtual bool is_cubemap() const;
virtual void bind() = 0;
+ virtual void unbind() = 0;
};
#endif // TEXTURE_H_
\ No newline at end of file
void MeshVK::draw() const
{
+}
+
+void MeshVK::draw_normals(float scale) const
+{
}
\ No newline at end of file
virtual ~MeshVK();
virtual void draw() const override;
+ virtual void draw_normals(float scale) const override;
};
#endif // MESH_VK_H_
\ No newline at end of file
void TextureVK::bind()
{
+}
+
+void TextureVK::unbind()
+{
}
\ No newline at end of file
virtual ~TextureVK();
virtual void bind() override;
+ virtual void unbind() override;
};
#endif // TEXTURE_VK_H_
\ No newline at end of file