I am creating a 3D game engine in java and I am having problems with my texturing. I get these weird artifacts:image of game with bad texture. (I’ll make a separate post to figure out why the game isn’t filling the entire screen, unless y’all got a quick fix. my main issue that I want to address right now is the texture) I’ve been trying to fix it on my own for a few days now and am now turning to y’all for help. here is my render3D class:
package com.base.Engine.Texture;
import com.base.Engine.Game;
public class Render3D extends Render {
public double[] zBuffer;
private double renderDistance = 50000;
public Render3D(int width, int height) {
super(width, height);
// TODO Auto-generated constructor stub
zBuffer = new double[width * height];
}
/*
* ===========================================================================
* | Future goal is to move all the movement values into their own classes in|
* | com.base.engine.Player.Controls |
* ===========================================================================
*/
public void floor(Game game) //and ceiling
{
//RotationEngine re = new RotationEngine(game);
//re.rotation = game.time / 100.0;
//MovementEngine me = new MovementEngine(game);
double floorPos = 15;
double ceilPos = 15;
double FB = game.control.z; // forward and backwards
double LR = game.control.x; // left and Right
double rotation = game.control.rotation;
double cosine = Math.cos(rotation);
double sine = Math.sin(rotation);
for ( int y = 0; y < height; y++ )
{
double yDepth = (y - height / 2.0) / height; //yDepth is sky
double z = floorPos / yDepth; // this can move the floor up and down
if (yDepth < 0)
{
z = ceilPos / -yDepth; // makes the ceiling move the same direction as the floor & the int in there changes ceiling height
}
for ( int x = 0; x < width; x++ )
{
double xDepth = ((x - width / 2.0)) / height;
xDepth *= z;
double xx = xDepth * cosine + z * sine;/* adding numbers here will make the "player" go to the right and vice versa */
double yy = z * cosine - xDepth * sine; /* "+,-" moves forward and backwards */
//double xx = xDepth * re.cosine + z * re.sine;/* adding numbers here will make the "player" go to the right and vice versa */
//double yy = z * re.cosine - xDepth * re.sine; /* "+,-" moves forward and backwards */
int xPix = (int) (xx + LR);
int yPix = (int) (yy + FB);
//int xPix = (int) (xx + me.LR);
//int yPix = (int) (yy + me.FB);
//zBuffer[x + y * width] = z;
//pixels[ x + y * width] = texture.floor.pixels[(xPix & 10) + (yPix & 10) * 626];
pixels[ x + y * width] = texture.floor.pixels[(xPix & 7) + (yPix & 7) * 8];
//pixels[ x + y * width] = ( (xPix & 10) * 15) | ( (yPix & 10) * 15) << 8;
if ( z > 200 ) {
pixels[x + y * width] = 0;
}
}
}
}
public void renderDistanceLimiter() {
for ( int i = 0; i < width* height; i++ )
{
int color = pixels[i];
int brightness = (int) (renderDistance / (zBuffer[i]));
if (brightness < 0)
{
brightness = 0;
}
if (brightness > 255)
{
brightness = 255;
}
int r = (color >> 16) & 0xff;
int g = (color >> 8) & 0xff;
int b = (color) & 0xff;
r = r * brightness / 255;
g = g * brightness / 255;
b = b * brightness / 255;
pixels[i] = r << 16 | g << 8 | b;
}
}
}
Im making this from scratch without Java3D or any other things like that.
Ive tried changing every variable I believe is connected to the problem but I only ended up making it worse. Im a novice programmer and can’t figure it out on my own. The code is supposed to create an endless walkway and ceiling, using the same texture because I haven’t gotten far enough to create a separate sky texture. This is what the texture looks like: light blue square with a darker blue outline
JT Salter is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.