Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.Ray.RayCaster.Rendering;
- import com.Ray.RayCaster.Game;
- import java.awt.Color;
- public class Render3D extends Render{
- double[][] DepthBuffer;
- double WorldX;
- double WorldZ;
- double WorldY=0;
- double rotation;
- double cos;
- double sine;
- public Render3D(int w, int h) {
- super(w,h);
- DepthBuffer = new double[w][h];
- }
- public void RenderPlane(double x, double y, double z, double x2, double z2,double y2,Render tex) {
- double PlaneWorldX = (x)-WorldX/16*2;
- double PlaneWorldZ = (z)-WorldZ/16*2;
- double PlaneWorldy = (-y)- WorldY/16*2;
- double PlaneWorldX2 = PlaneWorldX+x2;
- double PlaneWorldZ2 = PlaneWorldZ+z2;
- double PlaneWorldY2 = PlaneWorldy+y2;
- //rotate x and z coords
- double rotLeftSideX = PlaneWorldX*cos -PlaneWorldZ*sine;
- double rotLeftSideZ = PlaneWorldZ*cos + PlaneWorldX *sine;
- double rotRightSideX = PlaneWorldX2*cos -PlaneWorldZ2*sine;
- double rotRightSideZ = PlaneWorldZ2 *cos + PlaneWorldX2 *sine;
- double tex0 =0;
- double tex1 = tex.width;
- double clip = 0.5;
- if(rotLeftSideZ<clip && rotRightSideZ<clip) {
- return;
- }
- //Cohen-Sutherland Algorithm
- if(rotLeftSideZ<clip) {
- double clipped = (clip-rotLeftSideZ)/(rotRightSideZ - rotLeftSideZ);
- rotLeftSideZ = rotLeftSideZ+(rotRightSideZ - rotLeftSideZ)*clipped;
- rotLeftSideX = rotLeftSideX+(rotRightSideX - rotLeftSideX)*clipped;
- tex0 = tex0 + (tex1- tex0) *clipped;
- }
- if(rotRightSideZ<clip) {
- double clipped = (clip-rotLeftSideZ)/(rotRightSideZ - rotLeftSideZ);
- rotRightSideZ = rotLeftSideZ+(rotRightSideZ - rotLeftSideZ)*clipped;
- rotRightSideX = rotLeftSideX+(rotRightSideX - rotLeftSideX)*clipped;
- tex1 = tex0 + (tex1- tex0) *clipped;
- }
- double ypixellefttop = (PlaneWorldy/ rotLeftSideZ*height +height/2.0);
- double yPixelleftbottom = (PlaneWorldY2/rotLeftSideZ*height +height/2.0);
- double yPixelRightTop = (PlaneWorldy/rotRightSideZ*height+height /2.0);
- double YPixelRightBottom = (PlaneWorldY2/rotRightSideZ*height +height/2.0);
- //translate to screenspace x
- double XPixelLeft = (rotLeftSideX / rotLeftSideZ * height +width/2);
- double XPixelRight = (rotRightSideX / rotRightSideZ * height +width/2);
- //translate to screenspace x pixels
- int ScreenspaceXPixelLeft = (int)(XPixelLeft);
- int ScreenspaceXPixelRight = (int)(XPixelRight);
- if(ScreenspaceXPixelLeft<0) ScreenspaceXPixelLeft=0;
- if(ScreenspaceXPixelRight>width) ScreenspaceXPixelRight = width;
- for(int dx =ScreenspaceXPixelLeft; dx<ScreenspaceXPixelRight; dx++) {
- double pixelRotation = (dx-XPixelLeft) / (XPixelRight -XPixelLeft);
- double Depth = (1/rotLeftSideZ + (1/rotRightSideZ - 1/rotLeftSideZ) * pixelRotation);
- int XTexture = (int) (((tex0/rotLeftSideZ) +(tex1/rotRightSideZ- tex0/rotLeftSideZ) * pixelRotation)/ Depth);
- //translate to screenspace y
- double yPixelTop = ypixellefttop+(yPixelRightTop - ypixellefttop)*pixelRotation;
- double yPixelBottom = yPixelleftbottom +(YPixelRightBottom - yPixelleftbottom)*pixelRotation;
- //translate to screenspace y pixels
- int ScreenSpaceYPixeltop = (int)(yPixelTop);
- int ScreenSpaceYPixelBot = (int)(yPixelBottom);
- if(ScreenSpaceYPixeltop<0)ScreenSpaceYPixeltop=0;
- if(ScreenSpaceYPixelBot>height)ScreenSpaceYPixelBot=height;
- for(int dy = ScreenSpaceYPixeltop;dy<ScreenSpaceYPixelBot;dy++) {
- if(DepthBuffer[dx][dy]>Depth)continue;
- DepthBuffer[dx][dy] = Depth;
- double pixelRotationY =(dy - yPixelTop) /(yPixelBottom -yPixelTop);
- int YTexture=(int) (tex.height* pixelRotationY);
- int bitwisex = tex.width-1;
- int PixelColor = tex.pixels[(int) ((XTexture &bitwisex)+((YTexture &bitwisex)* tex.width))];
- int brightness = (int) (255*((Depth)*4));
- if(brightness<0) brightness = 0;
- if(brightness>255) brightness = 255;
- int r = ((PixelColor>>16) & 0xff)*brightness/255;
- int g = ((PixelColor>>8) & 0xff)*brightness/255;
- int b = ((PixelColor) & 0xff)*brightness/255;
- pixels[dx +dy*width] = r<<16 | g<<8|b; //PixelColor;//
- }
- }
- }
- public void RenderSprite(double x, double y, double z, double Scalex, Render tex) {
- double PlaneWorldX = (x)-WorldX/16*2;
- double PlaneWorldZ = (z)-WorldZ/16*2;
- double PlaneWorldy = (-y)- WorldY/16*2;
- double PlaneWorldyTop = PlaneWorldy+Scalex;
- //rotate x and z coords
- double rotX = PlaneWorldX*cos -PlaneWorldZ*sine;
- double rotZ = PlaneWorldZ*cos + PlaneWorldX *sine;
- double XPixel = (rotX / rotZ * height +width/2);
- double YPixel = (PlaneWorldy/ rotZ*height +height/2.0);
- double YPixel2 = (PlaneWorldyTop/ rotZ*height +height/2.0);
- //System.out.println(YPixel2-YPixel);
- //translate to screenspace y pixels
- int ScreenSpaceX = (int)(XPixel-(YPixel2-YPixel)/2);
- int ScreenSpaceX2 = (int)(XPixel+(YPixel2-YPixel)/2);
- int ScreenSpaceYTop = (int)(YPixel);
- int ScreenSpaceYTop2 = (int)(YPixel2);
- if(ScreenSpaceX<0)ScreenSpaceX=0;
- if(ScreenSpaceX2>width)ScreenSpaceX2=width;
- if(ScreenSpaceYTop<0)ScreenSpaceYTop=0;
- if(ScreenSpaceYTop2>height)ScreenSpaceYTop2=height;
- for(int dx=ScreenSpaceX;dx<ScreenSpaceX2;dx++) {
- double TextCoordX =(dx - (XPixel-(YPixel2-YPixel)/2)) /((XPixel+(YPixel2-YPixel)/2) -(XPixel-(YPixel2-YPixel)/2));
- int XTexture=(int) (tex.width* TextCoordX);
- double Depth = (1/rotZ);
- for(int dy=ScreenSpaceYTop;dy<ScreenSpaceYTop2;dy++) {
- double TextCoordY =(dy - YPixel) /(YPixel2 -YPixel);
- int YTexture=(int) (tex.height* TextCoordY);
- int bitwise = tex.width-1;
- int PixelColor = tex.pixels[(int) ((XTexture &bitwise)+((YTexture &bitwise)* tex.width))];
- if(!new Color(PixelColor).equals(new Color(255,255,255))) {
- if(DepthBuffer[dx][dy]>Depth)continue;
- DepthBuffer[dx][dy] = Depth;
- int brightness = (int) (255*((Depth)*4));
- if(brightness<0) brightness = 0;
- if(brightness>255) brightness = 255;
- int r = ((PixelColor>>16) & 0xff)*brightness/255;
- int g = ((PixelColor>>8) & 0xff)*brightness/255;
- int b = ((PixelColor) & 0xff)*brightness/255;
- pixels[dx +dy*width] = r<<16 | g<<8|b;//texture.Textures[tex].pixels[(int) ((XTexture &15)+((YTexture &15)* 16))];
- }
- }
- }
- }
- public void RenderEntities(double wallx, double wally, double wallz, double rot, Render Tex, int RenderStyle) {
- if(RenderStyle==0) {
- RenderSprite(wallx, -1, wallz, 1, Tex);
- }
- if(RenderStyle==1) {
- double Sizex= Math.floor(-1 * Math.cos(rot/10)+ 1 * Math.sin(rot/10));
- double Sizey=Math.floor(1* Math.cos(rot/10)- (-1) * Math.sin(rot/10));
- RenderPlane(wallx+(Sizey*0.65),wally,wallz+(Sizex*0.35),Sizex*2,Sizey*2,3,Tex);
- RenderPlane(wallx+(Sizey*0.35)+(Sizex*2),wally,wallz+(Sizex*0.65)+(Sizey*2),-Sizex*2,-Sizey*2,3,Tex);
- }
- if(RenderStyle==2) {
- NonWall(wallx,1,wallz,1,1,Tex);
- AddWall(wallx,wallz,0,Tex);
- }
- }
- public void ReadyScreen(Game game) {
- WorldX = game.playerinput.PlayerX;
- WorldY = game.playerinput.PlayerY;
- WorldZ = game.playerinput.PlayerZ;
- rotation = game.playerinput.PlayerRot;
- cos = Math.cos(rotation);
- sine = Math.sin(rotation);
- DepthBuffer = new double[width][height];
- pixels = new int[width*height];
- }
- public void RenderCiel(Render tex) {
- double cielHieght = 32+(WorldY*2);
- double ViewDistance =300;
- for(int y=0;y<height;y++) {
- double yDepth = (y + -height/2.0)/height;
- if(yDepth <0) {
- double z = cielHieght/-yDepth;
- for(int x=0; x<width;x++) {
- double xDepth = (x-width/2.0)/ width;
- xDepth*=z;
- int xTexture = (int)((xDepth *cos +z *sine)+WorldX*2);
- int yTexture = (int)((z *cos - xDepth * sine)+WorldZ*2);
- if(z<ViewDistance*3) {
- int PixelColor = tex.pixels[(xTexture & 15)+(yTexture & 15)*16];
- int brightness = (int) ((ViewDistance*50)/z);
- if(brightness<0) brightness = 0;
- if(brightness>255) brightness = 255;
- int r = ((PixelColor>>16) & 0xff)*brightness/255;
- int g = ((PixelColor>>8) & 0xff)*brightness/255;
- int b = ((PixelColor) & 0xff)*brightness/255;
- pixels[x+y*width] = r<<16 | g<<8|b;
- }
- }
- }
- }
- }
- public void RenderFloor(Render tex) {
- double cielHieght = 16+WorldY;
- double floorHieght = 32-(WorldY*2);
- double ViewDistance =300;
- for(int y=0;y<height;y++) {
- double yDepth = (y + -height/2.0)/height;
- double z = floorHieght/yDepth;
- if(yDepth <0) {
- // z = cielHieght/ -yDepth;
- }
- for(int x=0; x<width;x++) {
- double xDepth = (x-width/2.0)/ width;
- xDepth*=z;
- int xTexture = (int)((xDepth *cos +z *sine)+WorldX*2);
- int yTexture = (int)((z *cos - xDepth * sine)+WorldZ*2);
- if(z<ViewDistance*3) {
- int PixelColor = tex.pixels[(xTexture & 15)+(yTexture & 15)*16];
- int brightness = (int) ((ViewDistance*50)/z);
- if(brightness<0) brightness = 0;
- if(brightness>255) brightness = 255;
- int r = ((PixelColor>>16) & 0xff)*brightness/255;
- int g = ((PixelColor>>8) & 0xff)*brightness/255;
- int b = ((PixelColor) & 0xff)*brightness/255;
- pixels[x+y*width] = r<<16 | g<<8|b;
- }
- }
- }
- }
- public void NonWall(double worldx, int x1, double worldz, int z1,double worldy, Render tex) {
- double PlaneWorldX = (worldx)-WorldX/16*2;
- double PlaneWorldZ = (worldz)-WorldZ/16*2;
- worldx = (worldx*16)-1;
- worldz = (worldz*16)-1;
- double worldx1 = worldx+1+(x1*16);
- double worldz1 = worldz+1 +(z1*16);
- worldy = (worldy*16)-(WorldY*2);
- double ViewDistance =255;
- double rotZ = PlaneWorldZ*cos + PlaneWorldX *sine;
- for(int y=0;y<height;y++) {
- double yDepth = (y + -height/2.0)/height;
- if(yDepth >0) {
- double z = worldy/yDepth;
- for(int x=0; x<width;x++) {
- double xDepth = (x-width/2.0)/ width;
- xDepth*=z;
- int xTexture = (int)((xDepth *cos +z *sine)+WorldX*2);
- int yTexture = (int)((z *cos - xDepth * sine)+WorldZ*2);
- if(xTexture>worldx&&xTexture<worldx1 &&yTexture>worldz&&yTexture<worldz1) {
- if(z<50) {
- int PixelColor = tex.pixels[(xTexture & 15)+(yTexture & 15)*16];
- //if(DepthBuffer[x][y]>1/rotZ)continue;
- DepthBuffer[x][y] =1/rotZ;
- pixels[x+y*width] = PixelColor;
- }
- }
- }
- }
- }
- }
- void AddWall(double x, double y, double z, Render Tex) {
- RenderPlane(x+1,z-1,y,0,1,1,Tex);
- RenderPlane(x,z-1,y,1,0,1,Tex);
- RenderPlane(x,z-1,y+1,0,-1,1,Tex);
- RenderPlane(x+1,z-1,y+1,-1,0,1,Tex);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment