nbarber20

java 3d rendering

Oct 28th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.58 KB | None | 0 0
  1. package com.Ray.RayCaster.Rendering;
  2.  
  3.  
  4.  
  5. import com.Ray.RayCaster.Game;
  6. import java.awt.Color;
  7.  
  8. public class Render3D extends Render{
  9.  
  10.    
  11.    
  12.     double[][] DepthBuffer;
  13.     double WorldX;
  14.     double WorldZ;
  15.     double WorldY=0;
  16.     double rotation;
  17.    
  18.     double cos;
  19.     double sine;
  20.     public Render3D(int w, int h) {
  21.         super(w,h);
  22.         DepthBuffer = new double[w][h];
  23.     }
  24.  
  25.    
  26.     public void RenderPlane(double x, double y, double z, double x2, double z2,double y2,Render tex) {
  27.        
  28.         double PlaneWorldX = (x)-WorldX/16*2;
  29.         double PlaneWorldZ = (z)-WorldZ/16*2;
  30.         double PlaneWorldy = (-y)- WorldY/16*2;
  31.        
  32.         double PlaneWorldX2 = PlaneWorldX+x2;
  33.         double PlaneWorldZ2 = PlaneWorldZ+z2;
  34.         double PlaneWorldY2 = PlaneWorldy+y2;
  35.        
  36.         //rotate x and z coords
  37.         double rotLeftSideX = PlaneWorldX*cos -PlaneWorldZ*sine;       
  38.         double rotLeftSideZ = PlaneWorldZ*cos + PlaneWorldX *sine;
  39.         double rotRightSideX = PlaneWorldX2*cos -PlaneWorldZ2*sine;    
  40.         double rotRightSideZ = PlaneWorldZ2 *cos + PlaneWorldX2 *sine;
  41.        
  42.         double tex0 =0;
  43.         double tex1 = tex.width;
  44.             double clip = 0.5;
  45.             if(rotLeftSideZ<clip && rotRightSideZ<clip) {
  46.                 return;
  47.             }
  48.             //Cohen-Sutherland Algorithm
  49.             if(rotLeftSideZ<clip) {
  50.                 double clipped = (clip-rotLeftSideZ)/(rotRightSideZ - rotLeftSideZ);
  51.                 rotLeftSideZ = rotLeftSideZ+(rotRightSideZ - rotLeftSideZ)*clipped;
  52.                 rotLeftSideX = rotLeftSideX+(rotRightSideX - rotLeftSideX)*clipped;
  53.                 tex0 = tex0 + (tex1- tex0) *clipped;
  54.             }
  55.             if(rotRightSideZ<clip) {
  56.                 double clipped = (clip-rotLeftSideZ)/(rotRightSideZ - rotLeftSideZ);
  57.                 rotRightSideZ = rotLeftSideZ+(rotRightSideZ - rotLeftSideZ)*clipped;
  58.                 rotRightSideX = rotLeftSideX+(rotRightSideX - rotLeftSideX)*clipped;
  59.                 tex1 = tex0 + (tex1- tex0) *clipped;
  60.             }
  61.  
  62.        
  63.         double ypixellefttop = (PlaneWorldy/ rotLeftSideZ*height +height/2.0);
  64.         double yPixelleftbottom = (PlaneWorldY2/rotLeftSideZ*height +height/2.0);
  65.         double yPixelRightTop = (PlaneWorldy/rotRightSideZ*height+height /2.0);
  66.         double YPixelRightBottom = (PlaneWorldY2/rotRightSideZ*height +height/2.0);
  67.    
  68.  
  69.    
  70.         //translate to screenspace x
  71.         double XPixelLeft = (rotLeftSideX / rotLeftSideZ * height +width/2);   
  72.         double XPixelRight = (rotRightSideX / rotRightSideZ * height +width/2);
  73.        
  74.         //translate to screenspace x pixels
  75.         int ScreenspaceXPixelLeft = (int)(XPixelLeft);
  76.         int ScreenspaceXPixelRight = (int)(XPixelRight);       
  77.         if(ScreenspaceXPixelLeft<0) ScreenspaceXPixelLeft=0;
  78.         if(ScreenspaceXPixelRight>width) ScreenspaceXPixelRight = width;
  79.        
  80.         for(int dx =ScreenspaceXPixelLeft; dx<ScreenspaceXPixelRight; dx++) {
  81.             double pixelRotation = (dx-XPixelLeft) / (XPixelRight -XPixelLeft);
  82.             double Depth =  (1/rotLeftSideZ + (1/rotRightSideZ - 1/rotLeftSideZ) * pixelRotation);
  83.             int XTexture = (int) (((tex0/rotLeftSideZ) +(tex1/rotRightSideZ- tex0/rotLeftSideZ) * pixelRotation)/ Depth);
  84.            
  85.            
  86.             //translate to screenspace y
  87.             double yPixelTop = ypixellefttop+(yPixelRightTop - ypixellefttop)*pixelRotation;
  88.             double yPixelBottom = yPixelleftbottom +(YPixelRightBottom - yPixelleftbottom)*pixelRotation;
  89.             //translate to screenspace y pixels
  90.             int ScreenSpaceYPixeltop = (int)(yPixelTop);
  91.             int ScreenSpaceYPixelBot = (int)(yPixelBottom);
  92.             if(ScreenSpaceYPixeltop<0)ScreenSpaceYPixeltop=0;
  93.             if(ScreenSpaceYPixelBot>height)ScreenSpaceYPixelBot=height;
  94.            
  95.            
  96.             for(int dy = ScreenSpaceYPixeltop;dy<ScreenSpaceYPixelBot;dy++) {
  97.                
  98.                
  99.                 if(DepthBuffer[dx][dy]>Depth)continue;
  100.                 DepthBuffer[dx][dy] = Depth;
  101.                
  102.                 double pixelRotationY =(dy - yPixelTop) /(yPixelBottom -yPixelTop);
  103.                 int YTexture=(int) (tex.height* pixelRotationY);
  104.  
  105.                 int bitwisex = tex.width-1;
  106.                 int PixelColor = tex.pixels[(int) ((XTexture &bitwisex)+((YTexture &bitwisex)* tex.width))];
  107.                 int brightness = (int) (255*((Depth)*4));
  108.                 if(brightness<0) brightness = 0;
  109.                 if(brightness>255) brightness = 255;                   
  110.                 int r = ((PixelColor>>16) & 0xff)*brightness/255;
  111.                 int g = ((PixelColor>>8) & 0xff)*brightness/255;
  112.                 int b = ((PixelColor) & 0xff)*brightness/255;
  113.                 pixels[dx +dy*width] =  r<<16 | g<<8|b; //PixelColor;//
  114.             }
  115.         }
  116.     }
  117.    
  118.    
  119.    
  120.     public void RenderSprite(double x, double y, double z, double Scalex, Render tex) {
  121.  
  122.         double PlaneWorldX = (x)-WorldX/16*2;
  123.         double PlaneWorldZ = (z)-WorldZ/16*2;
  124.         double PlaneWorldy = (-y)- WorldY/16*2;
  125.        
  126.         double PlaneWorldyTop = PlaneWorldy+Scalex;
  127.        
  128.         //rotate x and z coords
  129.         double rotX =  PlaneWorldX*cos -PlaneWorldZ*sine;  
  130.         double rotZ = PlaneWorldZ*cos + PlaneWorldX *sine;
  131.        
  132.         double XPixel = (rotX / rotZ * height +width/2);
  133.         double YPixel = (PlaneWorldy/ rotZ*height +height/2.0);
  134.         double YPixel2 = (PlaneWorldyTop/ rotZ*height +height/2.0);
  135.        
  136.         //System.out.println(YPixel2-YPixel);
  137.        
  138.         //translate to screenspace y pixels
  139.         int ScreenSpaceX = (int)(XPixel-(YPixel2-YPixel)/2);
  140.         int ScreenSpaceX2 = (int)(XPixel+(YPixel2-YPixel)/2);
  141.         int ScreenSpaceYTop = (int)(YPixel);
  142.         int ScreenSpaceYTop2 = (int)(YPixel2);
  143.        
  144.         if(ScreenSpaceX<0)ScreenSpaceX=0;
  145.         if(ScreenSpaceX2>width)ScreenSpaceX2=width;
  146.         if(ScreenSpaceYTop<0)ScreenSpaceYTop=0;
  147.         if(ScreenSpaceYTop2>height)ScreenSpaceYTop2=height;
  148.        
  149.         for(int dx=ScreenSpaceX;dx<ScreenSpaceX2;dx++) {
  150.             double TextCoordX =(dx - (XPixel-(YPixel2-YPixel)/2)) /((XPixel+(YPixel2-YPixel)/2) -(XPixel-(YPixel2-YPixel)/2)); 
  151.            
  152.             int XTexture=(int) (tex.width* TextCoordX);
  153.             double Depth =  (1/rotZ);
  154.             for(int dy=ScreenSpaceYTop;dy<ScreenSpaceYTop2;dy++) {
  155.                
  156.                
  157.                 double TextCoordY =(dy - YPixel) /(YPixel2 -YPixel);
  158.                 int YTexture=(int) (tex.height* TextCoordY);
  159.                 int bitwise = tex.width-1;
  160.                 int PixelColor = tex.pixels[(int) ((XTexture &bitwise)+((YTexture &bitwise)* tex.width))];
  161.                 if(!new Color(PixelColor).equals(new Color(255,255,255))) {
  162.                     if(DepthBuffer[dx][dy]>Depth)continue;
  163.                     DepthBuffer[dx][dy] = Depth;
  164.                     int brightness = (int) (255*((Depth)*4));
  165.                     if(brightness<0) brightness = 0;
  166.                     if(brightness>255) brightness = 255;                   
  167.                     int r = ((PixelColor>>16) & 0xff)*brightness/255;
  168.                     int g = ((PixelColor>>8) & 0xff)*brightness/255;
  169.                     int b = ((PixelColor) & 0xff)*brightness/255;
  170.                     pixels[dx +dy*width] = r<<16 | g<<8|b;//texture.Textures[tex].pixels[(int) ((XTexture &15)+((YTexture &15)* 16))];
  171.                 }
  172.             }
  173.         }
  174.     }
  175.     public void RenderEntities(double wallx, double wally, double wallz, double rot, Render Tex, int RenderStyle) {
  176.         if(RenderStyle==0) {
  177.             RenderSprite(wallx, -1, wallz, 1, Tex);
  178.         }
  179.         if(RenderStyle==1) {
  180.            
  181.             double Sizex= Math.floor(-1 * Math.cos(rot/10)+ 1 * Math.sin(rot/10));
  182.             double Sizey=Math.floor(1* Math.cos(rot/10)- (-1) * Math.sin(rot/10));
  183.             RenderPlane(wallx+(Sizey*0.65),wally,wallz+(Sizex*0.35),Sizex*2,Sizey*2,3,Tex);
  184.             RenderPlane(wallx+(Sizey*0.35)+(Sizex*2),wally,wallz+(Sizex*0.65)+(Sizey*2),-Sizex*2,-Sizey*2,3,Tex);
  185.         }
  186.         if(RenderStyle==2) {
  187.             NonWall(wallx,1,wallz,1,1,Tex);
  188.             AddWall(wallx,wallz,0,Tex);
  189.         }
  190.        
  191.     }
  192.     public void ReadyScreen(Game game) {
  193.         WorldX = game.playerinput.PlayerX;
  194.         WorldY = game.playerinput.PlayerY;
  195.         WorldZ = game.playerinput.PlayerZ;
  196.         rotation = game.playerinput.PlayerRot;
  197.        
  198.         cos = Math.cos(rotation);
  199.         sine = Math.sin(rotation);
  200.         DepthBuffer = new double[width][height];
  201.         pixels = new int[width*height];
  202.     }
  203.    
  204.     public void RenderCiel(Render tex) {
  205.         double cielHieght = 32+(WorldY*2);
  206.         double ViewDistance =300;
  207.         for(int y=0;y<height;y++) {
  208.             double yDepth = (y + -height/2.0)/height;      
  209.             if(yDepth <0) {
  210.             double z = cielHieght/-yDepth;
  211.             for(int x=0; x<width;x++) {
  212.                
  213.                 double xDepth = (x-width/2.0)/ width;
  214.                 xDepth*=z;
  215.                 int xTexture = (int)((xDepth *cos +z *sine)+WorldX*2);
  216.                 int yTexture = (int)((z *cos - xDepth * sine)+WorldZ*2);
  217.                
  218.                 if(z<ViewDistance*3) {                 
  219.                     int PixelColor = tex.pixels[(xTexture & 15)+(yTexture & 15)*16];
  220.                     int brightness = (int) ((ViewDistance*50)/z);
  221.                    
  222.                     if(brightness<0) brightness = 0;
  223.                     if(brightness>255) brightness = 255;   
  224.                    
  225.                     int r = ((PixelColor>>16) & 0xff)*brightness/255;
  226.                     int g = ((PixelColor>>8) & 0xff)*brightness/255;
  227.                     int b = ((PixelColor) & 0xff)*brightness/255;
  228.                     pixels[x+y*width] = r<<16 | g<<8|b;
  229.                 }  
  230.            
  231.             }
  232.             }
  233.         }
  234.    
  235.     }
  236.    
  237.     public void RenderFloor(Render tex) {
  238.         double cielHieght = 16+WorldY;
  239.         double floorHieght = 32-(WorldY*2);
  240.         double ViewDistance =300;
  241.         for(int y=0;y<height;y++) {
  242.             double yDepth = (y + -height/2.0)/height;          
  243.             double z = floorHieght/yDepth;
  244.             if(yDepth <0) {
  245.             //  z = cielHieght/ -yDepth;
  246.             }
  247.            
  248.             for(int x=0; x<width;x++) {
  249.                
  250.                 double xDepth = (x-width/2.0)/ width;
  251.                 xDepth*=z;
  252.                 int xTexture = (int)((xDepth *cos +z *sine)+WorldX*2);
  253.                 int yTexture = (int)((z *cos - xDepth * sine)+WorldZ*2);
  254.                
  255.                 if(z<ViewDistance*3) {                 
  256.                     int PixelColor = tex.pixels[(xTexture & 15)+(yTexture & 15)*16];
  257.                     int brightness = (int) ((ViewDistance*50)/z);
  258.                    
  259.                     if(brightness<0) brightness = 0;
  260.                     if(brightness>255) brightness = 255;   
  261.                    
  262.                     int r = ((PixelColor>>16) & 0xff)*brightness/255;
  263.                     int g = ((PixelColor>>8) & 0xff)*brightness/255;
  264.                     int b = ((PixelColor) & 0xff)*brightness/255;
  265.                     pixels[x+y*width] = r<<16 | g<<8|b;
  266.                 }  
  267.            
  268.             }
  269.         }
  270.    
  271.     }
  272.     public void NonWall(double worldx, int x1, double worldz, int z1,double worldy, Render tex) {
  273.         double PlaneWorldX = (worldx)-WorldX/16*2;
  274.         double PlaneWorldZ = (worldz)-WorldZ/16*2;
  275.        
  276.         worldx = (worldx*16)-1;
  277.         worldz = (worldz*16)-1;
  278.         double worldx1 = worldx+1+(x1*16);
  279.         double worldz1 = worldz+1 +(z1*16);
  280.         worldy = (worldy*16)-(WorldY*2);
  281.         double ViewDistance =255;
  282.        
  283.        
  284.         double rotZ = PlaneWorldZ*cos + PlaneWorldX *sine;
  285.        
  286.         for(int y=0;y<height;y++) {
  287.             double yDepth = (y + -height/2.0)/height;      
  288.             if(yDepth >0) {
  289.                 double z = worldy/yDepth;
  290.                
  291.                 for(int x=0; x<width;x++) {
  292.                    
  293.                     double xDepth = (x-width/2.0)/ width;
  294.                     xDepth*=z;
  295.                     int xTexture = (int)((xDepth *cos +z *sine)+WorldX*2);
  296.                     int yTexture = (int)((z *cos - xDepth * sine)+WorldZ*2);
  297.                    
  298.                     if(xTexture>worldx&&xTexture<worldx1 &&yTexture>worldz&&yTexture<worldz1) {
  299.                        
  300.                     if(z<50) {                 
  301.                         int PixelColor = tex.pixels[(xTexture & 15)+(yTexture & 15)*16];
  302.                         //if(DepthBuffer[x][y]>1/rotZ)continue;
  303.                         DepthBuffer[x][y] =1/rotZ;
  304.                         pixels[x+y*width] = PixelColor;
  305.                     }  
  306.                      
  307.                 }
  308.                 }
  309.             }
  310.         }
  311.        
  312.        
  313.        
  314.     }
  315.     void AddWall(double x, double y, double z, Render Tex) {
  316.         RenderPlane(x+1,z-1,y,0,1,1,Tex);
  317.         RenderPlane(x,z-1,y,1,0,1,Tex);
  318.         RenderPlane(x,z-1,y+1,0,-1,1,Tex);
  319.         RenderPlane(x+1,z-1,y+1,-1,0,1,Tex);
  320.     }
  321.    
  322.    
  323.    
  324. }
Advertisement
Add Comment
Please, Sign In to add comment