Created
April 17, 2011 16:36
-
-
Save stevenlr/924193 to your computer and use it in GitHub Desktop.
heightmap opengl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <math.h> | |
#include <GL/gl.h> | |
#include <GL/glu.h> | |
#include <SDL/SDL.h> | |
#define min(a,b) ((a < b) ? a : b) | |
int main(int argc, char *argv) | |
{ | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_WM_SetCaption("OpenGL landscape", NULL); | |
SDL_SetVideoMode(800, 450, 32, SDL_OPENGL); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(56, 800.0 / 450.0, 1, 1000); | |
glEnable(GL_DEPTH_TEST); | |
glClearColor(1, 1, 1, 1); | |
srand(time(NULL)); | |
int loop = 1; | |
SDL_Event event; | |
int precision = 9, i, x, y, j, dx, dy, mx, my; | |
long size = 1 << precision; | |
float persistence = 0.5; | |
unsigned char *map, *tmpmap; | |
map = (unsigned char *) malloc(sizeof(unsigned char) * size * size); | |
tmpmap = (unsigned char *) malloc(sizeof(unsigned char) * (size + 1) * (size + 1)); | |
for(i = 0; i < size * size; i++) map[i] = 0; | |
for(i = 2; i <= precision; i++) | |
{ | |
int lprec = 1 << i; | |
int lsize = size / lprec; | |
for(x = 0; x < size + 1; x += lsize) for(y = 0; y < size + 1; y += lsize) | |
{ | |
tmpmap[x + y * (size + 1)] = rand() % 256; | |
} | |
for(x = 0; x < size; x++) for(y = 0; y < size; y++) | |
{ | |
dx = x % lsize; dy = y % lsize; | |
float a, b, c, d, ab, cd, abcd; | |
if(dx != 0 || dy != 0) { | |
mx = x - dx; my = y - dy; | |
a = tmpmap[mx + my * (size + 1)] * 1.0; | |
b = tmpmap[mx + lsize + my * (size + 1)] * 1.0; | |
c = tmpmap[mx + (my + lsize) * (size + 1)] * 1.0; | |
d = tmpmap[mx + lsize + (my + lsize) * (size + 1)] * 1.0; | |
ab = a + ((float) dx / (float) lsize) * (b - a); | |
cd = c + ((float) dx / (float) lsize) * (d - c); | |
abcd = ab + ((float) dy / (float) lsize) * (cd - ab); | |
tmpmap[x + y * (size + 1)] = floor(abcd); | |
} | |
map[x + y * size] = min(255, map[x + y * size] + (int) (floor((float) (tmpmap[x + y * (size + 1)]) * pow(0.5, i - 1)))); | |
} | |
} | |
printf("Heightmap generated\n"); | |
free(tmpmap); | |
gluLookAt(5, 100, 5, size, 25, size, 0, 1, 0); | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glBegin(GL_TRIANGLES); | |
glColor3ub(127, 127, 127); | |
for(x = 0; x < size - 1; x++) for(y = 0; y < size - 1; y++) | |
{ | |
float a, b, c, d, co; | |
a = map[x + y * size]; | |
b = map[x + 1 + y * size]; | |
c = map[x + (y + 1) * size]; | |
d = map[x + 1 + (y + 1) * size]; | |
a /= 2; | |
b /= 2; | |
c /= 2; | |
d /= 2; | |
co = x + y; | |
co /= 2 * size; | |
co *= 255; | |
glColor3ub(floor(co), floor(co), floor(co)); | |
glVertex3d(x, a, y); | |
glVertex3d(x + 1, b, y); | |
glVertex3d(x, c, y + 1); | |
glVertex3d(x + 1, b, y); | |
glVertex3d(x, c, y + 1); | |
glVertex3d(x + 1, d, y + 1); | |
} | |
glEnd(); | |
SDL_GL_SwapBuffers(); | |
while(loop) | |
{ | |
SDL_WaitEvent(&event); | |
switch(event.type) | |
{ | |
case SDL_QUIT: | |
loop = 0; | |
exit(0); | |
break; | |
} | |
} | |
glDeleteShader(shader); | |
shader = 0; | |
SDL_Quit(); | |
free(map); | |
free(src); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment