Advertisement
Luninariel

289 - Simple Renderer

Apr 8th, 2019
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. //
  2. //  SimpleRenderer.java
  3. //  Easel
  4. //
  5. //  Created by Philip Rhodes on 7/22/05.
  6. //  Copyright 2005 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. import java.awt.*;
  10. import java.awt.image.*;
  11.  
  12. /** SimpleRenderer renders Shapes to the screen by simply ignoring the z
  13.  *  coordinate of every vertex. There is no perspective.  Triangles are
  14.  *  drawn using lines, so there is no shading either. Also, triangles that
  15.  *  are in the "back" of an object are still drawn, so objects look
  16.  *  transparent.
  17.  */
  18.  public class SimpleRenderer extends VolatileImageRenderer {
  19.  
  20.    float [] poly_vertices;
  21.    Polygon poly;
  22.    
  23.    Matrix projectionMatrix;
  24.  
  25.  
  26.    /** Create a SimpleRenderer that renders the shapes in the World w.
  27.     */
  28.    SimpleRenderer(World w, int width, int height, boolean showFPS){
  29.    
  30.       super(w, width, height, showFPS);
  31.    
  32.      
  33.       Matrix s = new ScalingMatrix(1, -1, 0);
  34.       Matrix t = new TranslationMatrix(width/2, height/2, 0);
  35.      
  36.       this.projectionMatrix = new Matrix(t,s);
  37.      
  38.       this.poly = new Polygon();
  39.      
  40.       this.poly_vertices = new float[9];
  41.      
  42.    }
  43.  
  44.    /** Draw the shapes in the World associated with this renderer to the
  45.     *  backing image, which will then be drawn to the screen when
  46.     *  paintComponent is called by Swing.
  47.     */
  48.    public  void render(){
  49.                
  50.       super.render();
  51.            
  52.       int numTriangles = this.world.numTriangles();
  53.      
  54.       this.world.computeWorldCoords();
  55.  
  56.       synchronized(this.backingImage){
  57.      
  58.          waitUntilPainted();
  59.      
  60.          this.imageGraphics.setColor(Color.blue);
  61.          
  62.          for(int ti=0; ti < numTriangles; ti++){
  63.          
  64.             this.world.getTriangleVertices(ti, this.poly_vertices);
  65.             this.poly.reset();
  66.            
  67.             this.projectionMatrix.transform(this.poly_vertices);
  68.  
  69.             for(int vi=0; vi<3; vi++){
  70.                
  71.                this.poly.addPoint((int) (this.poly_vertices[vi*3]),
  72.                                   (int) (this.poly_vertices[vi*3 + 1]));
  73.                
  74.             }
  75.          
  76.             this.imageGraphics.drawPolygon(this.poly);
  77.            
  78.          }
  79.          
  80.  
  81.          readyForPainting();
  82.       }
  83.      
  84.      
  85.      
  86.    }
  87.    
  88.    
  89.  
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement