Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
3.9 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: util.cxx
  3. *
  4. * Copyright (c) 1997 Microsoft Corporation
  5. *
  6. \**************************************************************************/
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include <math.h>
  10. #include <sys/types.h>
  11. #include <stdlib.h>
  12. #include "mtk.hxx"
  13. #include "util.hxx"
  14. GLuint defaultQuad; // display list for quad
  15. // Local copies of tranf. matrices, for determing rect bounds
  16. static GLdouble modelMatrix[16];
  17. static GLdouble projMatrix[16];
  18. static GLint viewport[4];
  19. /**************************************************************************\
  20. *
  21. \**************************************************************************/
  22. void
  23. UpdateLocalTransforms( int bits )
  24. {
  25. // Get current transformation info
  26. if( bits & UPDATE_MODELVIEW_MATRIX_BIT )
  27. glGetDoublev( GL_MODELVIEW_MATRIX, modelMatrix );
  28. if( bits & UPDATE_PROJECTION_MATRIX_BIT )
  29. glGetDoublev( GL_PROJECTION_MATRIX, projMatrix );
  30. if( bits & UPDATE_VIEWPORT_BIT )
  31. glGetIntegerv( GL_VIEWPORT, viewport );
  32. }
  33. void
  34. TransformObjectToWindow( POINT3D *pIn, POINT3D *pOut, int nPts )
  35. {
  36. DPOINT3D outD;
  37. for( ; nPts; nPts--, pIn++, pOut++ ) {
  38. gluProject( pIn->x, pIn->y, pIn->z,
  39. modelMatrix, projMatrix, viewport,
  40. &outD.x, &outD.y, &outD.z );
  41. pOut->x = (float) outD.x;
  42. pOut->y = (float) outD.y;
  43. pOut->z = (float) outD.z;
  44. }
  45. }
  46. void
  47. TransformWindowToObject( POINT3D *pIn, POINT3D *pOut )
  48. {
  49. DPOINT3D outD;
  50. gluUnProject( pIn->x, pIn->y, pIn->z,
  51. modelMatrix, projMatrix, viewport,
  52. &outD.x, &outD.y, &outD.z );
  53. pOut->x = (float) outD.x;
  54. pOut->y = (float) outD.y;
  55. pOut->z = (float) outD.z;
  56. }
  57. void
  58. DrawRect( FSIZE *fSize )
  59. {
  60. float w = fSize->width / 2.0f;
  61. float h = fSize->height / 2.0f;
  62. glNormal3f( 0.0f, 0.0f, 1.0f );
  63. glBegin( GL_QUADS );
  64. glTexCoord2f( 1.0f, 1.0f );
  65. glVertex3f( w, h, 0.0f );
  66. glTexCoord2f( 0.0f, 1.0f );
  67. glVertex3f( -w, h, 0.0f );
  68. glTexCoord2f( 0.0f, 0.0f );
  69. glVertex3f( -w, -h, 0.0f );
  70. glTexCoord2f( 1.0f, 0.0f );
  71. glVertex3f( w, -h, 0.0f );
  72. glEnd();
  73. }
  74. void
  75. AddSwapHintRect( GLIRECT *pRect )
  76. {
  77. glAddSwapHintRect( pRect->x, pRect->y, pRect->width, pRect->height );
  78. }
  79. #define BloatRect( pRect, n ) \
  80. pRect->x -= n; \
  81. pRect->y -= n; \
  82. pRect->width += (n << 2); \
  83. pRect->height+= (n << 2);
  84. // Calculate the 2d view rect, using view parameters and window size. The
  85. // rect is in the z=0 plane.
  86. // Calculate the 2d window rect for each log object
  87. // !!! Assumes no rotation of the object !!!
  88. void
  89. CalcRect( POINT3D *pbl, POINT3D *ptr, GLIRECT *pRect )
  90. {
  91. //mf: check for inclusive/exclusive and rounding problems
  92. // 1) truncate both
  93. pRect->x = (int) (pbl->x);
  94. pRect->y = (int) (pbl->y);
  95. pRect->width = ((int) (ptr->x )) - pRect->x + 1;
  96. pRect->height = ((int) (ptr->y )) - pRect->y + 1;
  97. //mf: while this works for polygons, it does not work for lines, since
  98. // the rasterization rules are different for lines. Lines can be off by
  99. // +1, so bloat the box by 1 on all sides.
  100. BloatRect( pRect, 1 );
  101. }
  102. // Calcs window rects for the objects at REST (pObj->dest)
  103. void
  104. CalcMinMaxRect( POINT3D *pts, GLIRECT *pRect, int nPts )
  105. {
  106. POINT2D min, max;
  107. // Use first point to init min,max
  108. min.x = max.x = pts->x;
  109. min.y = max.y = pts->y;
  110. nPts--;
  111. pts++;
  112. for( ; nPts; nPts--, pts++ ) {
  113. if( pts->x < min.x )
  114. min.x = pts->x;
  115. if( pts->x > max.x )
  116. max.x = pts->x;
  117. if( pts->y < min.y )
  118. min.y = pts->y;
  119. if( pts->y > max.y )
  120. max.y = pts->y;
  121. }
  122. pRect->x = (int) (min.x);
  123. pRect->y = (int) (min.y);
  124. pRect->width = ((int) max.x) - pRect->x + 1;
  125. pRect->height = ((int) max.y) - pRect->y + 1;
  126. BloatRect( pRect, 1 );
  127. }