EnvMapping.pp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. program EnvMapping;
  2. {$L build/teapot.bin.o}
  3. {$L build/cafe.bin.o}
  4. {$mode objfpc}
  5. uses
  6. ctypes, nds9;
  7. {$include inc/teapot.bin.inc}
  8. {$include inc/cafe.bin.inc}
  9. var
  10. prev_pen: array [0..1] of cint = ($7FFFFFFF, $7FFFFFFF);
  11. procedure get_pen_delta(dx, dy: pcint);
  12. var
  13. keys: u32;
  14. touchXY: touchPosition;
  15. begin
  16. keys := keysHeld();
  17. if (keys and KEY_TOUCH) <> 0 then
  18. begin
  19. touchRead(touchXY);
  20. if (prev_pen[0] <> $7FFFFFFF) then
  21. begin
  22. dx^ := (prev_pen[0] - touchXY.rawx);
  23. dy^ := (prev_pen[1] - touchXY.rawy);
  24. end;
  25. prev_pen[0] := touchXY.rawx;
  26. prev_pen[1] := touchXY.rawy;
  27. end else
  28. begin
  29. prev_pen[0] := $7FFFFFFF;
  30. prev_pen[1] := $7FFFFFFF;
  31. dx^ := 0;
  32. dy^ := 0;
  33. end;
  34. end;
  35. var
  36. rotateX: integer = 0;
  37. rotateY: integer = 0;
  38. tex_scale: GLvector;
  39. keys: u32;
  40. cafe_texid: cint;
  41. pen_delta: array [0..1] of cint;
  42. begin
  43. //set mode 0, enable BG0 and set it to 3D
  44. videoSetMode(MODE_0_3D);
  45. // intialize gl
  46. glInit();
  47. // enable antialiasing
  48. glEnable(GL_ANTIALIAS);
  49. // setup the rear plane
  50. glClearColor(0,0,0,31); // BG must be opaque for AA to work
  51. glClearPolyID(63); // BG must have a unique polygon ID for AA to work
  52. glClearDepth($7FFF);
  53. //this should work the same as the normal gl call
  54. glViewport(0,0,255,191);
  55. vramSetBankA(VRAM_A_TEXTURE);
  56. glEnable(GL_TEXTURE_2D);
  57. glGenTextures(1, @cafe_texid);
  58. glBindTexture(0, cafe_texid);
  59. glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, GL_TEXTURE_WRAP_S or GL_TEXTURE_WRAP_T or TEXGEN_NORMAL, pcuint8(@cafe_bin));
  60. //any floating point gl call is being converted to fixed prior to being implemented
  61. glMatrixMode(GL_PROJECTION);
  62. glLoadIdentity();
  63. gluPerspective(70, 256.0 / 192.0, 0.1, 40);
  64. while true do
  65. begin
  66. //TEXGEN_NORMAL helpfully pops our normals into this matrix and uses the result as texcoords
  67. glMatrixMode(GL_TEXTURE);
  68. glLoadIdentity();
  69. tex_scale.x := (64 shl 16);
  70. tex_scale.y := (-64 shl 16);
  71. tex_scale.z := (1 shl 16);
  72. glScalev( @tex_scale ); //scale normals up from (-1,1) range into texcoords
  73. glRotateXi(rotateX); //rotate texture-matrix to match the camera
  74. glRotateYi(rotateY);
  75. glMatrixMode(GL_POSITION);
  76. glLoadIdentity();
  77. glTranslatef32(0, 0, floattof32(-3));
  78. glRotateXi(rotateX);
  79. glRotateYi(rotateY);
  80. glMaterialf(GL_EMISSION, RGB15(31,31,31));
  81. glPolyFmt(POLY_ALPHA(31) or POLY_CULL_BACK );
  82. scanKeys();
  83. keys := keysHeld();
  84. if ( keys and KEY_UP ) <> 0 then rotateX := rotateX + (3 shl 3);
  85. if ( keys and KEY_DOWN ) <> 0 then rotateX := rotateX - (3 shl 3);
  86. if ( keys and KEY_LEFT ) <> 0 then rotateY := rotateY + (3 shl 3);
  87. if ( keys and KEY_RIGHT ) <> 0 then rotateY := rotateY - (3 shl 3);
  88. get_pen_delta( @pen_delta[0], @pen_delta[1] );
  89. rotateY := rotateY - pen_delta[0];
  90. rotateX := rotateX - pen_delta[1];
  91. glBindTexture( 0, cafe_texid );
  92. glCallList(@teapot_bin);
  93. glFlush(0);
  94. end;
  95. end.