CS306 Processing Systems and Structures Lockwood, Spring 2002

Machine Problem 5:
The Maze (Part III: 3D-View)

Assigned Monday, March 25, 2002
Due Date Tuesday, April 9, 2002, Midnight
Purpose: Video Graphics and image algorithms
Points50

Introduction

In this machine problem you will add a 3-dimensional view of the MP3/MP4 maze that allows viewing the maze from the point-of-view of the mouse in the maze. From MP5, you can switch to graphics mode by hitting the 'g' key. The following image is a screen-dump of the running program:

The internal representation of the maze is the same as it was for MP3. From MP5, you can switch back to the two-dimensional viewpoint by hitting the 't' key. The following diagram is a screen-dump of the corresponding view on the 2-dimensional maze.

The mouse is looking East. Note that there is are two hallways to the right and one hallways to the left. Also note that the colored decision points appear in both viewpoints.


Implementation

A 3D perspective can be implemented by placing a vanishing point in the center of the screen. Objects at an infinite depth would be located in the center of the screen and have a height of zero. Objects at each depth become larger by scaling their size by a constant.

Rectangular objects can be represented by drawing simple geometric shapes that follow the diagnonal, horizontal, or vertical guidelines. A back wall, for example, can be represented by a rectangle. A sidewall can be represented by a trapezoid.

To avoid time-consuming multiplication, arrays can be defined that contain pre-calculated positions and sizes of an object at each depth. Due to the limited resolution of a video screen these arrays can be small. Once the size of an object is less than the size of a pixel, there is no purpose in plotting it on the screen.

For this machine problem, we will be using a screen resolution of 320x200 pixels. At this resolution, it is possible to render full-motion video with hundreds of frames-per-second on a Pentium-III class computer (such as those in our lab). Two arrays will be defined at this resolution: one to hold pixel coordinates and the other to hold object sizes. The sizes of the arrays are defined by the constant MAXDepth3d. The array P3d holds the top-left position of a 3D element at each depth. The array X3d holds the size of a 3D element. To nearly fill the screen and preserve simple X/Y scaling values; we will choose an aspect ratio of 2:1. The diagram above shows the coordinates and size of a back wall and two side walls at a given depth.

The rendering algorithm begins from the most distant point down the hallway. At this depth, a rectangle is drawn to represent the back wall. At each decreasing depth level, rectangles and trapezoids are drawn to represent ceilings, floors, walls, and hallway openings.

The 3-D routines of MP4 interface to MP3 by a single procedure. UpdateGRScreen is called instead of UpateTextScreen when the program is in graphics mode. Maze elements are drawn by looking down a hallway in the direction that the mouse is facing. This machine problem is limited to displaying mazes with hallways that are no more than one unit wide.

You are encouraged to use your MP3 and MP4 routines in MP5. There is, however, no penalty for using library versions of the MP3 and MP4 procedures.


Procedures


Debugging Process

The program includes procedes to help you incrementally develop and test your code. The TestGeometry procedure is executed when the program first begins. The procedures renders a box and two trapezoids on the screen, as shown below.

The source code to TestGeometry, which calls the DrawBox and DrawTrapezoid procedures, is given below:
TestGeometry: ; Geometry Test Cases PUSH AX PUSH BX ; Save Registers PUSH CX PUSH DX GMODE ; Switch to 320x200 Graphics-Mode (MACRO) ; --- Draw a Rectangular Box at the center of screen --- mov AL,REDBR ; Color=Bright RED mov dx,160-10 + 320*(100-10) mov cx,20 ; 20 Pixels Wide mov bx,20 ; 20 Pixels Tall call DrawBox ; Width unchanged KEYWAIT ; Wait for a key to be pressed, quit if 'q' or 'Q' ; --- Draw a Left-Sided Trapezoid at left of screen ---- mov AL,YELLOW ; Color=YELLOW mov dx,160-32 + 320*(100-20) ; Position mov bx,40 ; 40 Pixels Tall mov AH,0 ; Left-Sided trapezoid call DrawTrap ; Width (CX) unchanged KEYWAIT ; Wait for a key to be pressed, quit if 'q' or 'Q' ; --- Draw a Right-Sided Trapezoid at right of screen --- mov AL,BLUEBR ; Color=Bright Blue add dx,32*2 - 2 ; Position (with -2 correction) mov AH,1 ; Right-Sided call DrawTrap ; Width (CX), Height(BX), and Color(AL) Unchanged KEYWAIT ; Wait for a key to be pressed, quit if 'q' or 'Q' ; --- Wait for a keypress while we look at the screen --- TMODE ; Switch back to 80x25 Text-Mode (MACRO) POP DX POP CX POP BX ; Restore Registers POP AX ret

The TestDrawWallsAndSides procedure is executed next. This procedures shows how the DrawBackWall and DrawSideWall routines operate. Each are given a value for the depth in register SI and use the [P3d], [X3d], and [w3d] arrays to determine where the actual box and trapezoids should be drawn on the screen. The image below shows the back wall at a depth of 6, and all of the sidewalls at levels: 6, 5, 4, 3, 2, 1, and 0.

The source code to TestWallsAndSides is given below:
TestDrawWallsAndSides: GMODE MOV SI, 6 ; Set Level to 6 (Wall is far away, appears small) Call DrawBackWall; ; Draw a back wall at depth of 6 KEYWAIT ; Wait for a key to be pressed, quit if 'q' or 'Q' ; Pre-load the Side3D matrix with sample values. Mov byte [Side3d+6], LSW | RSW ; Both side walls at depth=6 Call DrawSides ; Draw side walls at depth of 6 KEYWAIT ; Wait for a key to be pressed, quit if 'q' or 'Q' ; Pre-load the Side3D matrix with sample values. Mov byte [Side3d+5], 0 ; Open walls at depth=5 Mov byte [Side3d+4], LSW | RSW ; Both side walls at depth=4 Mov byte [Side3d+3], RSW ; Rigth side wall at depth=3 Mov byte [Side3d+2], LSW | RSW ; Both side walls at depth=2 Mov byte [Side3d+1], LSW ; Left side wall at depth=1 Mov byte [Side3d+0], RSW ; Rigth side wall at depth=0 ; Usually, the values of Side3D are re-generated by reading ; the variables _MAZE, _POS, and DIR MOV SI,5 TestDrawMore: ; Draw the rest of floors and ceilings Call DrawSides; ; Draw side walls at a depth of 5 KEYWAIT ; Wait for a key to be pressed, quit if 'q' or 'Q' DEC SI CMP SI,0 ; Loop through depths of 5,4,3,2,1,0 JGE TestDrawMore TMODE ret


Points

You earn points by replacing each subroutine with your own code. Your score will be proportional to the percentage of the code that your write yourself. The breakdown in points is given below. Your routine MUST perform all functions of the subroutine to receive credit.

You are urged to test each routine as you write it. It is nearly impossible to debug a program if there are errors in the routines that it calls.

You can earn two points by demonstrating that your program, in graphics mode, at delay=0 can use AutoSolve to find the solution as fast as or faster than Lockwood's code (recall that the library code purposefully contains extraneous code). You will earn 1 point if your code is no more than 25% slower. Benchmarks will be done in the lab using the Pentium computers at the time you demonstrate your MP.


Starting Files

You will begin MP3 with the following files:

Major Updates to MP5.ASM from MP3.ASM and MP4.ASM

(Full changes appear in mp5.asm, which is included in the downloadable zip file) ; You Name Here : ____________________________________ ; CS306: Machine Problem 5, Spring 2002 ; The Maze (Part III : 3D Graphic View) ; Prof. John W. Lockwood ; Washington University, Department of Computer Science ; Ver. 2.1 ; ================ Constants / Definitions / MACROs ===================== TEXTMODE EQU 0 GRMODE EQU 1 ; Video Memory Segments (Store in ES) VIDTEXTSEG EQU 0B800h ; Text-mode video (for the maze of mp3/mp4) VIDGRSEG EQU 0A000h ; Grahphis video (for the 3d maze of mp5) ; New, for MP5: Predefined Palette Colors (default palette in Mode 13h) RED EQU 4 REDBR EQU 8+4 ; Bright RED GREEN EQU 2 GREENBR EQU 8+2 ; Bright GREEN BLUE EQU 1 BLUEBR EQU 8+1 ; Bright Blue CYAN EQU 3 GRAY EQU 8 ; Color for the ground (concrete) YELLOW EQU 8+6 ; Bright RED+GREEN = YELLOW MAGENTA EQU 5 ; RED+BLUE ; Constants for Side3d Array LSW EQU 10000000b ; Left Side Wall RSW EQU 01000000b ; Right Side Wall LRSW EQU 11000000b ; Left & Right Side Wall ; Maximum depth of 3d-view MAXDepth3d EQU 9 ; ================== GLOBAL Variables & Procedures ====================== ; Global Variables (available to library code) GLOBAL Side3d GLOBAL View3d ;====== Begin Code/Data =================================================== VidMode db TEXTMODE ; Can be TEXTMODE (default) or GRMODE ; --- New Variables for Mp5 --- ; An array that defines the type of elements at each distance ; while looking forward down a hallway. ; The array is initialized with sample values, but will need ; to be updated for every new view. View3d db HALL,HALL,HALL,HALL,HALL,HALL,HALL,HALL,HALL,HALL,WALL ; An array that defines the type of side walls at each distance forward ; while looking forward down a hallway. ; Each element can be: 0 (No Left wall, No Right Wall) ; LSW (Left Side Wall) ; RSW (Right Side Wall) ; LRSW (Both Left and Right Side Wall) ; The array is initialized with sample values, but will need ; to be updated for every new view. Side3d db LRSW,LRSW,LRSW,LRSW,LRSW,LRSW,LRSW,LRSW,LRSW,LRSW,LRSW ; An array that defines the locations of points at various depths ; Each value indicates the starting point for the back wall at each depth. ; For example, the first element of the array has value 0. P3d dw 0 dw 20+320*10 dw 20+30+320*(10+15) dw 20+30+24+320*(10+15+12) dw 20+30+24+20+320*(10+15+12+10) dw 20+30+24+20+16+320*(10+15+12+10+8) dw 20+30+24+20+16+14+320*(10+15+12+10+8+7) dw 20+30+24+20+16+14+12+320*(10+15+12+10+8+7+6) dw 20+30+24+20+16+14+12+10+320*(10+15+12+10+8+7+6+5) dw 20+30+24+20+16+14+12+10+8+320*(10+15+12+10+8+7+6+5+4) dw 20+30+24+20+16+14+12+10+8+4+320*(10+15+12+10+8+7+6+5+4+2) dw 20+30+24+20+16+14+12+10+8+4+2+320*(10+15+12+10+8+7+6+5+4+2+1) ; See the diagram in the writup. ; Size of a wall for each depth level. ; See the diagram in the writup. X3d dw 160,140,110, 86, 66, 50, 36, 24, 14, 6, 2 ; Width of a sidewall trapezoid at for each depth level. ; Values equals difference between [X3d+index] and [X3d+index+1] w3d dw 20, 30, 24, 20, 16, 14, 12, 10, 8, 4, 2 ; ================= Procedures (Your code goes here) ==================== DrawBox: Call LibDrawBox ; Replace this with your own code ret ; ------------------------------------------------------------------------ DrawTrap: Call LibDrawTrap ; Replace this with your own code ret ; ------------------------------------------------------------------------ DrawBackWall: Call LibDrawBackWall ; Replace this with your own code ret ; ------------------------------------------------------------------------ DrawFloorCeiling: Call LibDrawFloorCeiling ; Replace this with your own code ret ; ------------------------------------------------------------------------ DrawSides: Call LibDrawSides; Replace this with your own code ret ; ------------------------------------------------------------------------ DrawGrScreen: Call LibDrawGrScreen ; Replace this with your own code ret ; ------------------------------------------------------------------------ UpdateGrScreen: Call LibUpdateGrScreen; Replace this with your own code ret ; ------------------------------------------------------------------------ UpdateScreen: ; This code is given to you for free ; For MP5 it has been expanded to support both ; Video Modes (Text mode or Graphics Mode) CMP byte [VidMode], GRMODE JE UpdateGMode ; TextMode call UpdateTextScreen ret UpdateGMode: call UpdateGrScreen ret ; ------------------------------------------------------------------------ ..start MOV AX,CS ; Set Code Segment = Data (Default) Segment MOV DS,AX MOV AX,VIDTEXTSEG ; Use extra segment for Text mode video MOV ES,AX MOV AX, stkseg ; Stack segment MOV SS, AX MOV SP, stacktop ; BODY OF MAIN PROGRAM BEGINS HERE! Call TestGeometry ; Call mpxit ; Uncomment if you need to debug Geometry only. Call TestDrawWallsAndSides ; Call mpxit ; Uncomment if you need to debug Draw routines only. Call ReadMazeFile ; Read in the contents of the maze call MarkDecisionPoints TMODE ; MACRO to switch to text mode video Call ShowMaze ; Display original maze and mouse on screen Call ShowMode ; Display mazemode and mazedelay values on screen MOV SI, [_POS] ; Store position in register SI MOV DI, [_POS] ; Store position in register SI Call _MazeManual Call mpxit ; no RET opcode needed for this procedure. ; mpxit will terminates the program.
Copyright 1996-2002 John Lockwood