Mesa3d Source Code Reading Notes (4) _ GLResize () function debugging and execution Analysis

Source: Internet
Author: User

After executing the preceding function, enter the GLResize () function. Its source code is as follows:

// OpenGLDC. cpp 99th rows to 115th rows GLvoid COpenGLDC: GLResize (GLsizei nWidth, GLsizei nHeight) {// reset the OpenGL window size if (nHeight = 0) // prevent division by zero nHeight = 1; glViewport (0, 0, nWidth, nHeight); glMatrixMode (GL_PROJECTION); // select the projection matrix glLoadIdentity (); // reset the projection matrix gluPerspective (45.0f, // The Perspective angle is set to 45 degrees (GLfloat) nWidth/(GLfloat) nHeight, // The window width and height ratio is 0.6f, 100366f ); // view depth: near point. 1f far away. 0 fglMatrixMode (GL_MODELVIEW); // select the model observation matrix glLoadIdentity ();}

The GLResize () function calls glViewport in line 104th and then transfers it to line 45th _ mesa_Viewport in viewport. c:

_ Mesa_Viewport // viewport. c from row 44th to row 50 void GLAPIENTRY_mesa_Viewport (GLint x, GLint y, GLsizei width, GLsizei height) {GET_CURRENT_CONTEXT (ctx); substring (ctx); _ mesa_set_viewport (ctx, x, y, width, height );}

Replace the 47th rows macro, that is, assign the value of _ glapi_Context to the declared GLcontext-type ctx variable.
48th the row is an assertion check, which requires that this function be used outside of glBegin ()/glEnd () and immediately outputs the vertex FLUSH_VERTICES with a buffer;
Line 3 calls _ mesa_set_viewport to officially handle the glviewport requirement;

_mesa_set_viewportvoid_mesa_set_viewport(GLcontext *ctx, GLint x, GLint y,                    GLsizei width, GLsizei height){   if (MESA_VERBOSE & VERBOSE_API)      _mesa_debug(ctx, "glViewport %d %d %d %d\n", x, y, width, height);   if (width < 0 || height < 0) {      _mesa_error(ctx,  GL_INVALID_VALUE,                   "glViewport(%d, %d, %d, %d)", x, y, width, height);      return;   }   /* clamp width and height to the implementation dependent range */   width  = MIN2(width, (GLsizei) ctx->Const.MaxViewportWidth);   height = MIN2(height, (GLsizei) ctx->Const.MaxViewportHeight);   ctx->Viewport.X = x;   ctx->Viewport.Width = width;   ctx->Viewport.Y = y;   ctx->Viewport.Height = height;   ctx->NewState |= _NEW_VIEWPORT;#if 1   /* XXX remove this someday.  Currently the DRI drivers rely on    * the WindowMap matrix being up to date in the driver's Viewport    * and DepthRange functions.    */   _math_matrix_viewport(&ctx->Viewport._WindowMap,                         ctx->Viewport.X, ctx->Viewport.Y,                         ctx->Viewport.Width, ctx->Viewport.Height,                         ctx->Viewport.Near, ctx->Viewport.Far,                         ctx->DrawBuffer->_DepthMaxF);#endif   if (ctx->Driver.Viewport) {      /* Many drivers will use this call to check for window size changes       * and reallocate the z/stencil/accum/etc buffers if needed.       */      ctx->Driver.Viewport(ctx, x, y, width, height);   }}

Starting from row 3, set the new viewport parameter to CTX-> viewport, and write the status _ new_viewport;
Call _ math_matrix_viewport from row 3 to calculate the PV matrix. The calculation formula is temporarily recorded in this book;
Line 3 calls the address pointer of the driver-driven viewport function pointed to by CTX, that is, the wmesa_viewport function in wmesa. C. Do some work such as resetting the frame cache area;

_ Math_matrix_viewport: used to calculate the matrix of the view. The element address of the first line is 0, 4, 8, and 12.
[Width/2 0 0 width/2 + x]
[0 height/2 0 height/2 + y]
[0 0 depthMax * (zFar-zNear)/2 0 depthMax * (zFar-zNear)/2 0 + zNear]
[0 0 0 0]

After glViewport is executed, glMatrixMode (GL_PROJECTION) is started. The statement is executed. The _ mesa_MatrixMode at the row of matrix. c 146th:

_ Mesa_MatrixMode // matrix. c from row 145th to row 228 void GLAPIENTRY_mesa_MatrixMode (GLenum mode) {GET_CURRENT_CONTEXT (ctx); ASSERT_OUTSIDE_BEGIN_END (ctx); if (ctx-> Transform. matrixMode = mode & mode! = GL_TEXTURE) return; FLUSH_VERTICES (ctx, _ NEW_TRANSFORM); switch (mode) {case GL_MODELVIEW: ctx-> CurrentStack = & ctx-> ModelviewMatrixStack; break; case GL_PROJECTION: ctx-> CurrentStack = & ctx-> ProjectionMatrixStack; break; case GL_TEXTURE:/* This error check is disabled because if we're called from * glPopAttrib () when the active texture unit is> = MaxTextureCoordUnits * we'll generate an unexpected error. * From the GL_ARB_vertex_shader spec it sounds like we shoshould instead * do error checking in other places when we actually try to access * texture matrices beyond MaxTextureCoordUnits. */# if 0 if (ctx-> Texture. currentUnit> = ctx-> Const. maxTextureCoordUnits) {_ mesa_error (ctx, GL_INVALID_OPERATION, "glMatrixMode (invalid tex unit % d)", ctx-> Texture. currentUnit); return ;}# endif ASSERT (ctx-> Texture. currentUnit <Elements (ctx-> TextureMatrixStack); ctx-> CurrentStack = & ctx-> TextureMatrixStack [ctx-> Texture. currentUnit]; break; case GL_COLOR: ctx-> CurrentStack = & ctx-> ColorMatrixStack; break; case when: case GL_MATRIX1_NV: case GL_MATRIX2_NV: case GL_MATRIX3_NV: case when: case GL_MATRIX6_NV: case GL_MATRIX7_NV: if (ctx-> Extensions. NV_vertex_program) {ctx-> CurrentStack = & ctx-> ProgramMatrixStack [mode-GL_MATRIX0_NV];} else {_ mesa_error (ctx, success, "glMatrixMode (mode)"); return ;} break; case GL_MATRIX0_ARB: case GL_MATRIX1_ARB: case GL_MATRIX2_ARB: case GL_MATRIX3_ARB: case when: case GL_MATRIX5_ARB: case when: if (ctx-> Extensions. ARB_vertex_program | ctx-> Extensions. ARB_fragment_program) {const GLuint m = mode-GL_MATRIX0_ARB; if (m> ctx-> Const. maxProgramMatrices) {_ mesa_error (ctx, GL_INVALID_ENUM, "glMatrixMode (GL_MATRIX % d_ARB)", m); return;} ctx-> CurrentStack = & ctx-> ProgramMatrixStack [m];} else {_ mesa_error (ctx, GL_INVALID_ENUM, "glMatrixMode (mode)"); return;} break; default: _ mesa_error (ctx, GL_INVALID_ENUM, "glMatrixMode (mode )"); return;} ctx-> Transform. matrixMode = mode ;}

151st check the row. If the current pattern matches exactly, and the texture pattern is not used, the result is returned without repeated operations;
Row 3 sets the matrix stack to be operated based on the value of the current mode. Currently, you only need to understand the projection matrix stack and Model View matrix stack;
Row 3 sets ctx-> Transform. MatrixMode to represent the current matrix mode;

Then, start the glLoadIdentity () of the 107th rows in OpenGLDC. cpp; statement execution, corresponding to the _ mesa_LoadIdentity () function at the row of matrix. c 319th:

_ Mesa_loadidentity // matrix from row 318th TO row 329 void glapientry_mesa_loadidentity (void) {get_current_context (CTX); equals (CTX); If (mesa_verbose & verbose_api) _ mesa_debug (CTX, "glloadidentity ()"); _ math_matrix_set_identity (CTX-> currentstack-> top); CTX-> newstate | = CTX-> currentstack-> dirtyflag ;}

Row 3 calls the _ math_matrix_set_identity function to set the top matrix of the current matrix stack to the identity matrix, which is located in Row 3 of m_matrix.c, copy the data of an identity matrix to the glmatrix in the form of memcpy memory copy;

Then opengldc. the gluperspective statement at the 109th line of CPP, which is a release function of the Glu project, in the project. the gluperspective position of the C 65th row. Its matrix calculation is temporarily recorded in this book. Then, glumultmatrixd is used to multiply the Matrix to the top of the current matrix stack CTX-> currentstack-> top;

Formula for Calculating the projection matrix of the gluperspective View:
[Cos (fovy/2)/(sin (fovy/2) * aspect) 0 0]

[0 cos (fovy/2)/sin (fovy/2) 0 0]

[0 0-(zFar + zNear)/(zFar-zNear)-(2 * zFar * zNear)/(zFar-zNear)]

[0 0-1 0]

Then execute glMatrixMode (GL_MODELVIEW) In line 113rd of OpenGLDC. cpp. It points the current matrix stack ctx-> CurrentStack to ModelviewMatrixStack, And the next glLoadIdentity will reset the top stack matrix to the unit matrix;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.