Blob Blame Raw

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glext.h>
#include <GL/glu.h>

#include <helianthus.h>


#include "gl3d.inc.c"
#include "hmap.inc.c"
#include "water.inc.c"


double mx, my;
HMap hmap, wmap;
Water water;


void generate() {
  hmapGenerate(&hmap);
  for(int i = 0; i < HMAP_SIZE; ++i)
    for(int j = 0; j < HMAP_SIZE; ++j)
      wmap.map[i][j] = hmap.map[i][j] + 0.1;
  for(int i = 0; i < 100; ++i)
    waterCycle(&water, 1, 0.8, 0.0);
  hmapCalcNormals(&hmap);
  hmapCalcNormals(&wmap);
  hmapBuildBuf(&hmap);
  hmapBuildBuf(&wmap);
}


void init() {
  hmapInit(&hmap, 1, 1, 1, 1, 1, 1);
  hmapInit(&wmap, 1, 1, 0, 0, 1, 0.25);
  waterInit(&water, &hmap, &wmap);
  generate();

  background(COLOR_BLACK);
}


void deinit() {
  hmapDeinit(&hmap);
  hmapDeinit(&wmap);
}


void draw() {
  double pmx = mx, pmy = my;
  mx = mouseX();
  my = mouseY();
  double dmx = mx - pmx;
  double dmy = my - pmy;

  if (keyWentDown("space"))
    generate();

  if (mouseDown("left")) {
    cameraRot[1] += dmx;
    cameraRot[0] += dmy;
  }

  if (mouseDown("right")) {
    cameraPos[2] *= pow(1.01, dmy);
    if (cameraPos[2] > -2) cameraPos[2] = -2;
  }

  begin3d();
  cubeDraw(1);
  hmapDraw(&hmap);

  glDepthMask(GL_FALSE);
  glPushMatrix();
  glTranslated(0, 0, -0.01);
  for(int i = 0; i < 1; ++i) {
    hmapDraw(&wmap);
    glTranslated(0, 0, 0.5);
  }
  glPopMatrix();
  glDepthMask(GL_TRUE);

  end3d();
}


int main() {
  windowSetResizable(TRUE);
  windowSetVariableFrameRate();
  windowSetInit(&init);
  windowSetDeinit(&deinit);
  windowSetDraw(&draw);
  windowRun();
  return 0;
}