Created
February 21, 2016 17:56
-
-
Save MEDVEDx64/396f5ff7b94dbc564613 to your computer and use it in GitHub Desktop.
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 <stdint.h> | |
#include <SDL/SDL_image.h> | |
#include <GL/gl.h> | |
#include <GL/glu.h> | |
#include <mario/engine/timer.h> | |
#include <mario/engine/event.h> | |
#define SW 960 | |
#define SH 480 | |
/** | |
MEDVEDx64 | |
2013-05-28 | |
GPL | |
ACHTUNG! Опасно для моска. | |
**/ | |
int init() { | |
if(SDL_Init(SDL_INIT_VIDEO)) return 1; | |
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); | |
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); | |
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); | |
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); | |
if(!SDL_SetVideoMode(SW,SH,0,SDL_OPENGL|SDL_OPENGLBLIT)) return 1; | |
SDL_WM_SetCaption("medvedish terrainview", NULL); | |
glShadeModel(GL_SMOOTH); | |
glClearColor(0,0,0,0); | |
glClearDepth(1.0f); | |
glEnable(GL_DEPTH_TEST); | |
glDepthFunc(GL_LEQUAL); | |
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); | |
glViewport(0,0,SW,SH); | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
gluPerspective(45.0f,(GLfloat)SW/(GLfloat)SH, 0.1f,100.0f); | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
ev_Reset(); | |
tim_SetFPSLimit(60); | |
glTranslatef(-5, -1, -5); | |
return 0; | |
} | |
void let_there_be_grid() { | |
glColor3f(1,0,0); | |
glBegin(GL_LINES); | |
glVertex3f(0,100.0f,0); | |
glVertex3f(0,-100.0f,0); | |
glEnd(); | |
glColor3f(0,0,0.64f); | |
GLfloat i; | |
for(i = -100.0f; i < 100.1f; i++) { | |
glBegin(GL_LINES); | |
glVertex3f(-100.0f, -1, i); | |
glVertex3f(100.0f, -1, i); | |
glVertex3f(i, -1, -100.0f); | |
glVertex3f(i, -1, 100.0f); | |
glEnd(); | |
} | |
} | |
#define SPEED_F 0.364f | |
#define R_SPEED_F 0.064f | |
#define R_ANGLE_F 2.5f | |
void ev_loop() { | |
ev_Query(); | |
if(ev_GetKeyState(SDLK_w) == K_PRESSED) glTranslatef(0,0, SPEED_F); | |
if(ev_GetKeyState(SDLK_s) == K_PRESSED) glTranslatef(0,0, -SPEED_F); | |
if(ev_GetKeyState(SDLK_a) == K_PRESSED) glTranslatef(SPEED_F,0,0); | |
if(ev_GetKeyState(SDLK_d) == K_PRESSED) glTranslatef(-SPEED_F,0,0); | |
if(ev_GetKeyState(SDLK_q) == K_PRESSED) glTranslatef(0,-SPEED_F,0); | |
if(ev_GetKeyState(SDLK_e) == K_PRESSED) glTranslatef(0,SPEED_F,0); | |
if(ev_GetKeyState(SDLK_LEFT) == K_PRESSED) glRotatef(R_ANGLE_F, 0, -R_SPEED_F, 0); | |
if(ev_GetKeyState(SDLK_RIGHT) == K_PRESSED) glRotatef(R_ANGLE_F, 0, R_SPEED_F, 0); | |
if(ev_GetKeyState(SDLK_UP) == K_PRESSED) glRotatef(R_ANGLE_F, -R_SPEED_F, 0, 0); | |
if(ev_GetKeyState(SDLK_DOWN) == K_PRESSED) glRotatef(R_ANGLE_F, R_SPEED_F, 0, 0); | |
if(ev_CaughtQuitSignal()) exit(0); | |
} | |
char *argv_2__ = NULL; | |
#define HEIGHT_MUL (GLfloat)(atoi(argv_2__))*0.001 | |
uint8_t *pixels__ = NULL; | |
uint8_t *pixels_r__ = NULL; | |
uint8_t *pixels_g__ = NULL; | |
uint8_t *pixels_b__ = NULL; | |
uint16_t w__,h__; | |
void render_terrain(uint8_t *pixels, uint16_t w, uint16_t h) | |
{ | |
glColor4f(0,0.78f,0,1); | |
register uint16_t x,z; | |
for(x = 2; x < w-2; ++x){ | |
for(z = 2; z < h-2; ++z){ | |
glBegin(GL_TRIANGLES); | |
glColor3f(pixels_r__? (GLfloat)pixels_r__[(z*w+x)]/256.0f: 0, | |
pixels_g__? (GLfloat)pixels_g__[(z*w+x)]/256.0f: | |
((GLfloat)pixels[(z*w+x)])/256.0f, | |
pixels_b__? (GLfloat)pixels_b__[(z*w+x)]/256.0f: 0); | |
glVertex3f((GLfloat)x, ((GLfloat)pixels[(z*w+x)])*HEIGHT_MUL, (GLfloat)z); | |
glColor3f(pixels_r__? (GLfloat)pixels_r__[z*w+(x+1)]/256.0f: 0, | |
pixels_g__? (GLfloat)pixels_g__[z*w+(x+1)]/256.0f: | |
((GLfloat)pixels[z*w+(x+1)])/256.0f, | |
pixels_b__? (GLfloat)pixels_b__[z*w+(x+1)]/256.0f: 0); | |
glVertex3f((GLfloat)x+1, ((GLfloat)pixels[z*w+(x+1)])*HEIGHT_MUL, (GLfloat)z); | |
glColor3f(pixels_r__? (GLfloat)pixels_r__[(z+1)*w+x]/256.0f: 0, | |
pixels_g__? (GLfloat)pixels_g__[(z+1)*w+x]/256.0f: | |
((GLfloat)pixels[(z+1)*w+x])/256.0f, | |
pixels_b__? (GLfloat)pixels_b__[(z+1)*w+x]/256.0f: 0); | |
glVertex3f((GLfloat)x, ((GLfloat)pixels[(z+1)*w+x])*HEIGHT_MUL, (GLfloat)z+1); | |
glColor3f(pixels_r__? (GLfloat)pixels_r__[(z+1)*w+x]/256.0f: 0, | |
pixels_g__? (GLfloat)pixels_g__[(z+1)*w+x]/256.0f: | |
((GLfloat)pixels[(z+1)*w+x])/256.0f, | |
pixels_b__? (GLfloat)pixels_b__[(z+1)*w+x]/256.0f: 0); | |
glVertex3f((GLfloat)x, ((GLfloat)pixels[(z+1)*w+x])*HEIGHT_MUL, (GLfloat)z+1); | |
glColor3f(pixels_r__? (GLfloat)pixels_r__[z*w+(x+1)]/256.0f: 0, | |
pixels_g__? (GLfloat)pixels_g__[z*w+(x+1)]/256.0f: | |
((GLfloat)pixels[z*w+(x+1)])/256.0f, | |
pixels_b__? (GLfloat)pixels_b__[z*w+(x+1)]/256.0f: 0); | |
glVertex3f((GLfloat)x+1, ((GLfloat)pixels[z*w+(x+1)])*HEIGHT_MUL, (GLfloat)z); | |
glColor3f(pixels_r__? (GLfloat)pixels_r__[(z+1)*w+(x+1)]/256.0f: 0, | |
pixels_g__? (GLfloat)pixels_g__[(z+1)*w+(x+1)]/256.0f: | |
((GLfloat)pixels[(z+1)*w+(x+1)])/256.0f, | |
pixels_b__? (GLfloat)pixels_b__[(z+1)*w+(x+1)]/256.0f: 0); | |
glVertex3f((GLfloat)x+1, ((GLfloat)pixels[(z+1)*w+(x+1)])*HEIGHT_MUL, (GLfloat)z+1); | |
glEnd(); | |
} | |
} | |
#undef X | |
#undef Z | |
} | |
void draw() { | |
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); | |
let_there_be_grid(); | |
render_terrain(pixels__, w__, h__); | |
SDL_GL_SwapBuffers(); | |
} | |
int main(int argc, char *argv[]) | |
{ | |
if(argc < 3) return 0; | |
printf("terrainview\n"); | |
argv_2__ = argv[2]; | |
if(init()) { | |
printf("init() has crashed.\n"); | |
return 1; | |
} | |
SDL_Surface *img = IMG_Load(argv[1]); | |
if(img == NULL) | |
{ | |
printf("img_load() fail.\n"); | |
return 1; | |
} | |
w__ = img->w; | |
h__ = img->h; | |
if(img->format->BytesPerPixel == 1) | |
pixels__ = img->pixels; | |
else if(img->format->BytesPerPixel == 3) { | |
Uint8 *pix = img->pixels; | |
pixels__ = malloc(w__*h__); | |
pixels_r__ = malloc(w__*h__); | |
pixels_g__ = malloc(w__*h__); | |
pixels_b__ = malloc(w__*h__); | |
register int i; | |
for(i = 0; i < w__*h__; ++i) { | |
pixels__[i] = (pix[i*3]+pix[i*3+1]+pix[i*3+2])/3; | |
pixels_r__[i] = pix[i*3]; | |
pixels_g__[i] = pix[i*3+1]; | |
pixels_b__[i] = pix[i*3+2]; | |
} | |
} | |
else if(img->format->BytesPerPixel == 4) { | |
Uint8 *pix = img->pixels; | |
pixels__ = malloc(w__*h__); | |
pixels_r__ = malloc(w__*h__); | |
pixels_g__ = malloc(w__*h__); | |
pixels_b__ = malloc(w__*h__); | |
register int i; | |
for(i = 0; i < w__*h__; ++i) | |
{ | |
pixels__[i] = (pix[i*4]+pix[i*4+1]+pix[i*4+2])/3; | |
pixels_r__[i] = pix[i*4]; | |
pixels_g__[i] = pix[i*4+1]; | |
pixels_b__[i] = pix[i*4+2]; | |
} | |
} | |
while(1) { | |
tim_fps_control(); | |
ev_loop(); | |
draw(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment