3DBothScreens.pp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. program BothScreens3D;
  2. {$mode objfpc}
  3. uses
  4. ctypes, nds9;
  5. procedure renderCube(angle: cint);
  6. begin
  7. glPushMatrix();
  8. glTranslatef(0, 0, -4);
  9. glRotatef32i(degreesToAngle(angle), inttof32(1), inttof32(1), inttof32(1));
  10. glBegin(GL_QUADS);
  11. glColor3b(255,0,0); glVertex3f(-1.0, 1.0, 1.0);
  12. glColor3b(0,255,0); glVertex3f( 1.0, 1.0, 1.0);
  13. glColor3b(0,0,255); glVertex3f( 1.0, -1.0, 1.0);
  14. glColor3b(255,255,0); glVertex3f(-1.0, -1.0, 1.0);
  15. glColor3b(255,0,0); glVertex3f(-1.0, 1.0, -1.0);
  16. glColor3b(0,255,0); glVertex3f( 1.0, 1.0, -1.0);
  17. glColor3b(0,0,255); glVertex3f( 1.0, -1.0, -1.0);
  18. glColor3b(255,255,0); glVertex3f(-1.0, -1.0, -1.0);
  19. glColor3b(255,0,0); glVertex3f(-1.0, 1.0, 1.0);
  20. glColor3b(0,255,0); glVertex3f( 1.0, 1.0, 1.0);
  21. glColor3b(0,0,255); glVertex3f( 1.0, 1.0, -1.0);
  22. glColor3b(255,255,0); glVertex3f(-1.0, 1.0, -1.0);
  23. glColor3b(255,0,0); glVertex3f(-1.0, -1.0, 1.0);
  24. glColor3b(0,255,0); glVertex3f( 1.0, -1.0, 1.0);
  25. glColor3b(0,0,255); glVertex3f( 1.0, -1.0, -1.0);
  26. glColor3b(255,255,0); glVertex3f(-1.0, -1.0, -1.0);
  27. glColor3b(255,0,0); glVertex3f( 1.0, 1.0, -1.0);
  28. glColor3b(0,255,0); glVertex3f( 1.0, 1.0, 1.0);
  29. glColor3b(0,0,255); glVertex3f( 1.0,-1.0, 1.0);
  30. glColor3b(255,255,0); glVertex3f( 1.0,-1.0, -1.0);
  31. glColor3b(255,0,0); glVertex3f(-1.0, 1.0, -1.0);
  32. glColor3b(0,255,0); glVertex3f(-1.0, 1.0, 1.0);
  33. glColor3b(0,0,255); glVertex3f(-1.0,-1.0, 1.0);
  34. glColor3b(255,255,0); glVertex3f(-1.0,-1.0, -1.0);
  35. glEnd();
  36. glPopMatrix(1);
  37. end;
  38. procedure renderPyramid(angle: cint);
  39. begin
  40. glPushMatrix();
  41. glTranslatef(0, 0, -4);
  42. glRotatef32i(degreesToAngle(angle), inttof32(1),inttof32(1),inttof32(1));
  43. glBegin(GL_QUADS);
  44. glColor3b(255,0,0); glVertex3f(-1.0, -1.0, 1.0);
  45. glColor3b(0,255,0); glVertex3f( 1.0, -1.0, 1.0);
  46. glColor3b(0,0,255); glVertex3f( 1.0, -1.0, -1.0);
  47. glColor3b(255,255,0); glVertex3f(-1.0, -1.0, -1.0);
  48. glEnd();
  49. glBegin(GL_TRIANGLES);
  50. glColor3b(255,0,0); glVertex3f( 0.0, 1.0, 0.0);
  51. glColor3b(0,255,0); glVertex3f(-1.0, -1.0, 1.0);
  52. glColor3b(0,0,255); glVertex3f( 1.0, -1.0, 1.0);
  53. glColor3b(255,0,0); glVertex3f( 0.0, 1.0, 0.0);
  54. glColor3b(0,255,0); glVertex3f(-1.0, -1.0, -1.0);
  55. glColor3b(0,0,255); glVertex3f( 1.0, -1.0, -1.0);
  56. glColor3b(255,0,0); glVertex3f( 0.0, 1.0, 0.0);
  57. glColor3b(0,255,0); glVertex3f(-1.0, -1.0, 1.0);
  58. glColor3b(0,0,255); glVertex3f(-1.0, -1.0, -1.0);
  59. glColor3b(255,0,0); glVertex3f( 0.0, 1.0, 0.0);
  60. glColor3b(0,255,0); glVertex3f( 1.0, -1.0, 1.0);
  61. glColor3b(0,0,255); glVertex3f( 1.0, -1.0, -1.0);
  62. glEnd();
  63. glPopMatrix(1);
  64. end;
  65. var
  66. angle: cshort = 0;
  67. procedure renderScene(top: boolean);
  68. begin
  69. if (top) then
  70. renderCube(angle)
  71. else
  72. renderPyramid(angle);
  73. inc(angle);
  74. end;
  75. //-------------------------------------------------------
  76. // set up a 2D layer construced of bitmap sprites
  77. // this holds the image when rendering to the top screen
  78. //-------------------------------------------------------
  79. procedure initSubSprites();
  80. var
  81. x: integer;
  82. y: integer;
  83. id: integer;
  84. offset: pcuint16;
  85. begin
  86. oamInit(oamSub, SpriteMapping_Bmp_2D_256, false);
  87. x := 0;
  88. y := 0;
  89. //set up a 4x3 grid of 64x64 sprites to cover the screen
  90. for y := 0 to 2 do
  91. for x := 0 to 3 do
  92. begin
  93. // offset := pcuint16(@SPRITE_GFX_SUB[(x * 64) + (y * 64 * 256)]);
  94. // oamSet(oamSub, x + y * 4, x * 64, y * 64, 0, 15, SpriteSize_64x64,
  95. // SpriteColorFormat_Bmp, offset, -1, false,false,false,false,false);
  96. oamSub.oamMemory[id].attribute[0] := ATTR0_BMP or ATTR0_SQUARE or (64 * y);
  97. oamSub.oamMemory[id].attribute[1] := ATTR1_SIZE_64 or (64 * x);
  98. oamSub.oamMemory[id].attribute[2] := ATTR2_ALPHA(1) or (8 * 32 * y) or (8 * x);
  99. inc(id);
  100. end;
  101. swiWaitForVBlank();
  102. oamUpdate(oamSub);
  103. end;
  104. var
  105. top: boolean = true;
  106. begin
  107. videoSetMode(MODE_0_3D);
  108. videoSetModeSub(MODE_5_2D);
  109. glInit();
  110. // sub sprites hold the bottom image when 3D directed to top
  111. initSubSprites();
  112. // sub background holds the top image when 3D directed to bottom
  113. bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
  114. //-------------------------------------------------------
  115. // Setup gl
  116. //-------------------------------------------------------
  117. glEnable(GL_ANTIALIAS);
  118. glClearColor(0,0,0,31);
  119. glClearPolyID(63);
  120. glClearDepth($7FFF);
  121. glViewport(0,0,255,191);
  122. glMatrixMode(GL_PROJECTION);
  123. glLoadIdentity();
  124. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  125. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE);
  126. //-------------------------------------------------------
  127. // main loop
  128. //-------------------------------------------------------
  129. while true do
  130. begin
  131. // wait for capture unit to be ready
  132. while (REG_DISPCAPCNT^ and DCAP_ENABLE) <> 0 do;
  133. //-------------------------------------------------------
  134. // Switch render targets
  135. //-------------------------------------------------------
  136. top := not top;
  137. if (top) then
  138. begin
  139. lcdMainOnBottom();
  140. vramSetBankC(VRAM_C_LCD);
  141. vramSetBankD(VRAM_D_SUB_SPRITE);
  142. REG_DISPCAPCNT^ := DCAP_BANK(2) or DCAP_ENABLE or DCAP_SIZE(3);
  143. end else
  144. begin
  145. lcdMainOnTop();
  146. vramSetBankD(VRAM_D_LCD);
  147. vramSetBankC(VRAM_C_SUB_BG);
  148. REG_DISPCAPCNT^ := DCAP_BANK(3) or DCAP_ENABLE or DCAP_SIZE(3);
  149. end;
  150. //-------------------------------------------------------
  151. // Render the scene
  152. //-------------------------------------------------------
  153. glMatrixMode(GL_MODELVIEW);
  154. renderScene(top);
  155. glFlush(0);
  156. end;
  157. end.