3D Game Programming - Episode 4.5 - How Rendering Works





The interactive transcript could not be loaded.



Rating is available when the video has been rented.
This feature is not available right now. Please try again later.
Published on Mar 1, 2012

Episode 4: http://www.youtube.com/watch?v=qwodDR...

Quick video, just to explain rendering a bit better. Think of this as a bonus episode; Episode 5 will be uploaded in a few hours. ;)

Welcome to a series of videos where we will create a 3D game (probably a first person shooter) from scratch, using just the included libraries in Java 1.6. If you have any questions or problems, leave a comment or send me a message, or tweet at me.

In this tutorial:
How rendering works

Tell me if you want more!

Twitter: http://twitter.com/#!/thecherno
Livestream: http://www.twitch.tv/thecherno

Comments • 65

For those of you that don't get pixels and are sure that your xPix, x, width and yPix, y, height values are set correctly, put "run();" under everything else in the "start() {" section. This runs the rendering and ticks. I'm not sure if this not working is part of a new feature in latest Java updates, but it worked for me.
Nicholas Grantham
I am getting the exception: Exception in thread "Thread-2" java.lang.ArrayIndexOutOfBoundsException: 2
Something was wrong with your start thread. Check and see if it matches his.
Why do we even need to draw pixels?
If you don't, your game will do things but it will all just be a white screen with nothing there. You want to be able to see your game, and the window is made up of tiny pixels that need to be updated.
Adit Kirtani
this is a test
Dogu Kan
Can somebody explain me these: screen = new Screen(WIDTH,HEIGHT); img = new BufferedImage(WIDTH,HEIGHT , BufferedImage.TYPE_INT_RGB); pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData(); these: BufferStrategy bs = this.getBufferStrategy(); if(bs == null){ createBufferStrategy(3); return; } screen.render(); for (int i = 0; i < WIDTH * HEIGHT; i++){ pixels[i] = screen.pixels[i]; } Graphics g = bs.getDrawGraphics(); g.drawImage(img, 0, 0, WIDTH,HEIGHT,null); g.dispose(); bs.show(); and this: pixels[xPix+yPix*width] = render.pixels[x+y*render.width];
The Beginning
+Dogu Kan Just a way to make a varable that controlls the Graphics and pixels and size.
Cosmic Gaming
Cant find the comment but someone posted the code for public void render() and well I spent an hour trying to work out the problem and that code fixed it. Thx who ever posted that comment
Peter Cole
sorry my code looks messy I don't have a square I have lines... indents are missing would anyone look through this code and see if you can see what I'm missing? if I get ride of random.nextInt(); and replace it with 1111111111. I get a solid blue line. //render]\ package Graphics; public class Render { public final int width; public final int height; public final int[] pixels; public Render(int width,int height){ this.height=height; this.width=width; pixels= new int[height*width]; } public void draw(Render render,int xoffset,int yoffset){ for(int y=0;y<render.height;y++){//render.height int yPix=y + yoffset;     for(int x=0;x<render.width;x++){         int xPix=x + xoffset;                    pixels[yPix+xPix* width]=render.pixels[y+x*render.width]; } } } //end of render ///__________________________________________________________________ //Screen package Graphics; import java.util.Random; public class Screen extends Render{ private Render test; public Screen(int width, int height) { super(width, height); test=new Render(256,256); Random random=new Random(); for(int i=0; i<256;i++){ test.pixels[i]=random.nextInt(); } } public void render(){ draw(test,100,0); } } //end //________________________________________________________________ //display package Display; import java.awt.Canvas; import java.awt.Color; import java.awt.Graphics; import java.awt.image.BufferStrategy; import java.awt.image.BufferedImage; import java.awt.image.DataBufferInt; import javax.swing.JFrame; import Graphics.Render; import Graphics.Screen; public class Display extends Canvas implements Runnable {      /**   * display   * this class draws the frame for the game   */  private static final long serialVersionUID = 1L;     private static String title="3D Game";  private static final int WIDTH=800, HEIGHT=600;      private boolean running=false;    private BufferedImage img;    private Thread thread;  private Screen screen;  private Render render;    private int[] pixels;    public Display(){   screen= new Screen(WIDTH,HEIGHT);   img=new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_RGB);   pixels=((DataBufferInt)img.getRaster().getDataBuffer()).getData();  }        private  void start(){   if(running)return;   running=true;   thread =new Thread(this);   thread.start();     }  private  void stop(){   if(!running)    return;   running = false;   System.out.println("Stop");   try {    thread.join();   } catch (InterruptedException e) {    e.printStackTrace();   }       }  private void tick(){     }  private void render() {   BufferStrategy bs = this.getBufferStrategy();   if (bs == null) {    createBufferStrategy(3);    System.out.println("Buffer created!");    return;   }   for (int i = 0; i < WIDTH * HEIGHT; i++) {    pixels[i] = screen.pixels[i];   }   Graphics g = bs.getDrawGraphics();   screen.render();   g.drawImage(img, 0, 0, WIDTH, HEIGHT, null);   g.dispose();   bs.show();   }  /*private void render(){   BufferStrategy bs=this.getBufferStrategy();   if(bs == null){    System.out.println("working Bs");    createBufferStrategy(3);    return;   }      screen.render();      for(int i=0;i<WIDTH * HEIGHT;i++){    pixels[i]=screen.pixels[i];   }   Graphics g = bs.getDrawGraphics();   //Clear Screen         //Draw Here!         g.drawImage(img, 0, 0, WIDTH, HEIGHT,null);      //End Drawing!   g.dispose();   bs.show();        }*/  public  void run() {   System.out.println("working");   while(running){    tick();    render();      }     }    public static void main(String[] args) {   Display game=new Display();   JFrame frame =new JFrame();   frame.add(game);   frame.pack();     frame.setTitle(title);   frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   frame.setSize(WIDTH, HEIGHT);   frame.setLocationRelativeTo(null);   frame.setResizable(true);   frame.setVisible(true);             game.start();  }     } //end thanks for looking
Peter Cole
Is it possible that switching test=new Render(256,256); Random random=new Random();ToRandom random=new Random();test=new Render(256,256);will fix it? i know most coding languages is sensitive to where you place it;
What is the music at the end of the video? I have heard it somewhere before but I have no idea where.
Diego Church
when i debug a debug console appears
Canadian Potato
A note to all those getting grey screens and no pixels: I did some debugging by placing print statements throughout the code and noticed that none of them were printing. I'm under Netbeans. What I believed fixed it was Renaming the "Render" class to "Renders" and then updating all references to the Render object in the code to Renders (although refactoring will do most of that for ya. I believe that calling the class Render was conflicting with another built in class called Render. Either way, by calling it Renders and updating all references to Render to Renders, my code ran successfully, print statements included, and giant pixels on screen. Hope this works out for you guys.  
Aaron Krupp
It really bugs the hell out out of me when people comment that theirs isn't working, but don't post the freaking code. How can anyone help if they don't know what you typed wrong? 
When autoplay is enabled, a suggested video will automatically play next.

Up next

to add this to Watch Later

Add to

Loading playlists...