1
Graphics / Re: How to draw a buffer as a line
« on: October 10, 2021, 01:28:24 am »
Oh yeh, here is the code:
Drawing Line:
Drawing Line:
sf::Color color(255, 255, 0);
sf::Vertex line[2] =
{
sf::Vertex(sf::Vector2f(x, drawStart), color),
sf::Vertex(sf::Vector2f(x, drawEnd), color)
};
window.draw(line , 2, sf::Lines);
Colouring Buffer:sf::Vertex line[2] =
{
sf::Vertex(sf::Vector2f(x, drawStart), color),
sf::Vertex(sf::Vector2f(x, drawEnd), color)
};
window.draw(line , 2, sf::Lines);
//texturing calculations
int texNum = map[mapX][mapY] - 1;
//calculate value of wallX
double wallX; //where exactly the wall was hit
if(side==0) wallX = posY + perpWallDist * rayDirY;
else wallX = posX + perpWallDist * rayDirX;
wallX -= floor((wallX));
//x coordinate on the texture
int texX = int(wallX * double(texWidth));
if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1;
double step = 1.0 * texHeight / lineHeight;
//starting texture coordinate
double texPos = (drawStart - h / 2 + lineHeight / 2) * step;
for(int y=drawStart; y<drawEnd; y++){
int texY = (int)texPos & (texHeight - 1);
texPos += step;
uint32_t color = texture[texNum][texHeight * texY + texX];
if(side==1) color = (color >> 1) & 8255711;
buffer[y][x] = color;
}
Setting Textures:int texNum = map[mapX][mapY] - 1;
//calculate value of wallX
double wallX; //where exactly the wall was hit
if(side==0) wallX = posY + perpWallDist * rayDirY;
else wallX = posX + perpWallDist * rayDirX;
wallX -= floor((wallX));
//x coordinate on the texture
int texX = int(wallX * double(texWidth));
if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1;
double step = 1.0 * texHeight / lineHeight;
//starting texture coordinate
double texPos = (drawStart - h / 2 + lineHeight / 2) * step;
for(int y=drawStart; y<drawEnd; y++){
int texY = (int)texPos & (texHeight - 1);
texPos += step;
uint32_t color = texture[texNum][texHeight * texY + texX];
if(side==1) color = (color >> 1) & 8255711;
buffer[y][x] = color;
}
for(int x=0; x<texWidth; x++)
{
for(int y=0; y<texHeight; y++)
{
int xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
int ycolor = y * 256 / texHeight;
int xycolor = y * 128 + x * 128/ texWidth;
texture[0][texWidth * y + x] = 65536 * 254 * (x!= y && x != texWidth - y);
texture[1][texWidth * y + x] = xycolor + 256 * xycolor + 65536 * xycolor;
texture[2][texWidth * y + x] = 256 * xycolor + 65536 * xycolor; //sloped yellow gradient
texture[3][texWidth * y + x] = xorcolor + 256 * xorcolor + 65536 * xorcolor; //xor greyscale
texture[4][texWidth * y + x] = 256 * xorcolor; //xor green
texture[5][texWidth * y + x] = 65536 * 192 * (x % 16 && y % 16); //red bricks
texture[6][texWidth * y + x] = 65536 * ycolor; //red gradient
texture[7][texWidth * y + x] = 128 + 256 * 128 + 65536 * 128; //flat grey texture
}
}
{
for(int y=0; y<texHeight; y++)
{
int xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
int ycolor = y * 256 / texHeight;
int xycolor = y * 128 + x * 128/ texWidth;
texture[0][texWidth * y + x] = 65536 * 254 * (x!= y && x != texWidth - y);
texture[1][texWidth * y + x] = xycolor + 256 * xycolor + 65536 * xycolor;
texture[2][texWidth * y + x] = 256 * xycolor + 65536 * xycolor; //sloped yellow gradient
texture[3][texWidth * y + x] = xorcolor + 256 * xorcolor + 65536 * xorcolor; //xor greyscale
texture[4][texWidth * y + x] = 256 * xorcolor; //xor green
texture[5][texWidth * y + x] = 65536 * 192 * (x % 16 && y % 16); //red bricks
texture[6][texWidth * y + x] = 65536 * ycolor; //red gradient
texture[7][texWidth * y + x] = 128 + 256 * 128 + 65536 * 128; //flat grey texture
}
}