3DBothScreens.pp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. keys: integer;
  107. begin
  108. videoSetMode(MODE_0_3D);
  109. videoSetModeSub(MODE_5_2D);
  110. glInit();
  111. // sub sprites hold the bottom image when 3D directed to top
  112. initSubSprites();
  113. // sub background holds the top image when 3D directed to bottom
  114. bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
  115. //-------------------------------------------------------
  116. // Setup gl
  117. //-------------------------------------------------------
  118. glEnable(GL_ANTIALIAS);
  119. glClearColor(0,0,0,31);
  120. glClearPolyID(63);
  121. glClearDepth($7FFF);
  122. glViewport(0,0,255,191);
  123. glMatrixMode(GL_PROJECTION);
  124. glLoadIdentity();
  125. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  126. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE);
  127. //-------------------------------------------------------
  128. // main loop
  129. //-------------------------------------------------------
  130. while true do
  131. begin
  132. // wait for capture unit to be ready
  133. while (REG_DISPCAPCNT^ and DCAP_ENABLE) <> 0 do;
  134. scanKeys();
  135. keys := keysDown();
  136. if (keys and KEY_START) <> 0 then break;
  137. //-------------------------------------------------------
  138. // Switch render targets
  139. //-------------------------------------------------------
  140. top := not top;
  141. if (top) then
  142. begin
  143. lcdMainOnBottom();
  144. vramSetBankC(VRAM_C_LCD);
  145. vramSetBankD(VRAM_D_SUB_SPRITE);
  146. REG_DISPCAPCNT^ := DCAP_BANK(2) or DCAP_ENABLE or DCAP_SIZE(3);
  147. end else
  148. begin
  149. lcdMainOnTop();
  150. vramSetBankD(VRAM_D_LCD);
  151. vramSetBankC(VRAM_C_SUB_BG);
  152. REG_DISPCAPCNT^ := DCAP_BANK(3) or DCAP_ENABLE or DCAP_SIZE(3);
  153. end;
  154. //-------------------------------------------------------
  155. // Render the scene
  156. //-------------------------------------------------------
  157. glMatrixMode(GL_MODELVIEW);
  158. renderScene(top);
  159. glFlush(0);
  160. end;
  161. end.