Skip to content
Myr

pg_test.c

This file

Path

tests/pg_test/pg_test.c

124 lines · 3.1 KiB · C

Last changed by

Not known.

History of this file

Take it

1#define STB_DEFINE2#include "stb.h"3#define STB_PG_IMPLEMENTATION4#include "stb_pg.h"5#define STB_IMAGE_IMPLEMENTATION6#include "stb_image.h"7#define STB_IMAGE_WRITE_IMPLEMENTATION8#include "stb_image_write.h"910static float *hf;11static int hf_width = 10001;12static int hf_height = 10001;1314static float get_height(float x, float y)15{16   float h00,h01,h10,h11,h0,h1;17   int ix,iy;18   if (x < 0) x = 0;19   if (x > hf_width-1) x = (float) hf_width-1;20   if (y < 0) y = 0;21   if (y > hf_height-1) y = (float) hf_height-1;22   ix = (int) x; x -= ix;23   iy = (int) y; y -= iy;24   h00 = hf[(iy+0)*hf_height+(ix+0)];25   h10 = hf[(iy+0)*hf_height+(ix+1)];26   h01 = hf[(iy+1)*hf_height+(ix+0)];27   h11 = hf[(iy+1)*hf_height+(ix+1)];28   h0 = stb_lerp(y, h00, h01);29   h1 = stb_lerp(y, h10, h11);30   return stb_lerp(x, h0, h1);31}3233void stbpg_tick(float dt)34{35   int i=0,j=0;36   int step = 1;3738   glUseProgram(0);3940   glClearColor(0.6f,0.7f,1.0f,1.0f);41   glClearDepth(1.0f);42   glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);4344   glDepthFunc(GL_LESS);45   glEnable(GL_DEPTH_TEST);46#if 147   glEnable(GL_CULL_FACE);4849   glMatrixMode(GL_PROJECTION);50   glLoadIdentity();51   gluPerspective(60.0, 1920/1080.0f, 0.02f, 8000.0f);52   //glOrtho(-8,8,-6,6, -100, 100);5354   glMatrixMode(GL_MODELVIEW);55   glLoadIdentity();56   glRotatef(-90, 1,0,0); // z-up5758   {59      float x,y;60      stbpg_get_mouselook(&x,&y);61      glRotatef(-y, 1,0,0);62      glRotatef(-x, 0,0,1);63   }6465   {66      static float cam_x = 1000;67      static float cam_y = 1000;68      static float cam_z = 700;69      float x=0,y=0;70      stbpg_get_keymove(&x,&y);71      cam_x += x*dt*5.0f;72      cam_y += y*dt*5.0f;73      glTranslatef(-cam_x, -cam_y, -cam_z);74      if (cam_x >= 0 && cam_x < hf_width && cam_y >= 0 && cam_y < hf_height)75         cam_z = get_height(cam_x, cam_y) + 1.65f; // average eye height in meters76   }7778   for (j=501; j+1 < 1500+0*hf_height; j += step) {79      glBegin(GL_QUAD_STRIP);80      for (i=501; i < 1500+0*hf_width; i += step) {81         static int flip=0;82         if (flip)83            glColor3f(0.5,0.5,0.5);84         else85            glColor3f(0.4f,0.4f,0.4f);86         flip = !flip;87         glVertex3f((float) i, (float) j+step,hf[(j+step)*hf_width+i]);88         glVertex3f((float) i, (float) j   ,hf[  j    *hf_width+i]);89      }90      glEnd();91   }9293   glBegin(GL_LINES);94      glColor3f(1,0,0); glVertex3f(10,0,0); glVertex3f(0,0,0);95      glColor3f(0,1,0); glVertex3f(0,10,0); glVertex3f(0,0,0);96      glColor3f(0,0,1); glVertex3f(0,0,10); glVertex3f(0,0,0);97   glEnd();98#endif99}100101void stbpg_main(int argc, char **argv)102{103   int i,j;104105   #if 0106   int w,h,c;107   unsigned short *data = stbi_load_16("c:/x/ned_1m/test2.png", &w, &h, &c, 1);108   stb_filewrite("c:/x/ned_1m/x73_y428_10012_10012.bin", data, w*h*2);109   #else110   unsigned short *data = stb_file("c:/x/ned_1m/x73_y428_10012_10012.bin", NULL);111   int w=10012, h = 10012;112   #endif113114   hf = malloc(hf_width * hf_height * 4);115   for (j=0; j < hf_height; ++j)116      for (i=0; i < hf_width; ++i)117         hf[j*hf_width+i] = data[j*w+i] / 32.0f;118119   stbpg_gl_compat_version(1,1);   120   stbpg_windowed("terrain_edit", 1920, 1080);121   stbpg_run();122123   return;124}