3DBothScreens.pp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. offset: pcuint16;
  84. begin
  85. oamInit(oamSub, SpriteMapping_Bmp_2D_256, false);
  86. x := 0;
  87. y := 0;
  88. //set up a 4x3 grid of 64x64 sprites to cover the screen
  89. for y := 0 to 2 do
  90. for x := 0 to 3 do
  91. begin
  92. offset := pcuint16(@SPRITE_GFX_SUB[(x * 64) + (y * 64 * 256)]);
  93. oamSet(oamSub, x + y * 4, x * 64, y * 64, 0, 15, SpriteSize_64x64,
  94. SpriteColorFormat_Bmp, offset, -1, false,false,false,false,false);
  95. end;
  96. swiWaitForVBlank();
  97. oamUpdate(oamSub);
  98. end;
  99. var
  100. top: boolean = true;
  101. begin
  102. videoSetMode(MODE_0_3D);
  103. videoSetModeSub(MODE_5_2D);
  104. glInit();
  105. // sub sprites hold the bottom image when 3D directed to top
  106. initSubSprites();
  107. // sub background holds the top image when 3D directed to bottom
  108. bgInitSub(3, BgType_Bmp16, BgSize_B16_256x256, 0, 0);
  109. //-------------------------------------------------------
  110. // Setup gl
  111. //-------------------------------------------------------
  112. glEnable(GL_ANTIALIAS);
  113. glClearColor(0,0,0,31);
  114. glClearPolyID(63);
  115. glClearDepth($7FFF);
  116. glViewport(0,0,255,191);
  117. glMatrixMode(GL_PROJECTION);
  118. glLoadIdentity();
  119. gluPerspective(70, 256.0 / 192.0, 0.1, 100);
  120. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_NONE);
  121. //-------------------------------------------------------
  122. // main loop
  123. //-------------------------------------------------------
  124. while true do
  125. begin
  126. // wait for capture unit to be ready
  127. while (REG_DISPCAPCNT^ and DCAP_ENABLE) <> 0 do;
  128. //-------------------------------------------------------
  129. // Switch render targets
  130. //-------------------------------------------------------
  131. top := not top;
  132. if (top) then
  133. begin
  134. lcdMainOnBottom();
  135. vramSetBankC(VRAM_C_LCD);
  136. vramSetBankD(VRAM_D_SUB_SPRITE);
  137. REG_DISPCAPCNT^ := DCAP_BANK(2) or DCAP_ENABLE or DCAP_SIZE(3);
  138. end else
  139. begin
  140. lcdMainOnTop();
  141. vramSetBankD(VRAM_D_LCD);
  142. vramSetBankC(VRAM_C_SUB_BG);
  143. REG_DISPCAPCNT^ := DCAP_BANK(3) or DCAP_ENABLE or DCAP_SIZE(3);
  144. end;
  145. //-------------------------------------------------------
  146. // Render the scene
  147. //-------------------------------------------------------
  148. glMatrixMode(GL_MODELVIEW);
  149. renderScene(top);
  150. glFlush(0);
  151. end;
  152. end.