I am trying to create a Voxel Space Engine In SDL2 C++. However the tutorial I am following reads the X and Y pixels of the heightmap image. I can't find a way to do this.
I am following this tutorial: https://github.com/s-macke/VoxelSpace
Code from the tutorial:
def Render(p, height, horizon, scale_height, distance, screen_width, screen_height):
# Draw from back to the front (high z coordinate to low z coordinate)
for z in range(distance, 1, -1):
# Find line on map. This calculation corresponds to a field of view of 90°
pleft = Point(-z + p.x, -z + p.y)
pright = Point( z + p.x, -z + p.y)
# segment the line
dx = (pright.x - pleft.x) / screen_width
# Raster line and draw a vertical line for each segment
for i in range(0, screen_width):
height_on_screen = (height - heightmap[pleft.x, pleft.y]) / z * scale_height. + horizon
DrawVerticalLine(i, height_on_screen, screen_height, colormap[pleft.x, pleft.y])
pleft.x += dx
# Call the render function with the camera parameters:
# position, height, horizon line position,
# scaling factor for the height, the largest distance,
# screen width and the screen height parameter
Render( Point(0, 0), 50, 120, 120, 300, 800, 600 )
My code:
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <iostream>
using namespace std;
int main(){
SDL_Init(SDL_INIT_VIDEO);
IMG_Init(IMG_INIT_PNG);
SDL_Window* window = SDL_CreateWindow("Voxel Space", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
int w = 640;
int h = 480;
int pixelX = 0;
int pixelY = 0;
int renderDistance = 1;
bool quit = false;
SDL_Surface* cm = IMG_Load("colormap.png");
SDL_Surface* hm = IMG_Load("heightmap.png");
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
while(quit){
SDL_Event e;
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
quit == true;
}
}
SDL_RenderClear(renderer);
for(int z = 128; z > renderDistance; z--){
int pixelLeftX = (-z + pixelX);
int pixelLeftY = (-z + pixelY);
int pixelRightX = (z + pixelX);
int pixelRightY = (-z + pixelY);
double dx = (pixelRightX - pixelLeftX) / w;
for(int i = 0; i < w; i++){
double hm.x = pixelLeftX;
double hm.y = pixelLeftY;
heightOnScreen = (50 - hm.xy) / z * h + 120;
for(int y = 0; y < i; y++){
SDL_SetRenderDrawColor(renderer, 0, 255, 0, 0);
SDL_RenderDrawPoint(renderer, heightOnScreen, w);
pixelLeftX += dx;
}
}
}
SDL_RenderPresent(renderer);
}
return 0;
}
How can I read the X and Y pixels of the heightmap image in SDL2?