this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Made GPU_MatrixStack grow as needed. Fixed some const-correctness issues with matrix functions. Renamed GPU_Multiply4x4() to GPU_MatrixMultiply(). Added GPU_LoadMatrix() to set the current matrix from a given one.

+200 -50
+17 -14
include/SDL_gpu.h
··· 330 330 #define GPU_MODELVIEW 0 331 331 #define GPU_PROJECTION 1 332 332 333 - #ifndef GPU_MATRIX_STACK_MAX 334 - #define GPU_MATRIX_STACK_MAX 5 335 - #endif 336 - 337 333 /*! \ingroup Matrix 338 334 * Matrix stack data structure for global vertex transforms. */ 339 335 typedef struct GPU_MatrixStack 340 336 { 337 + unsigned int storage_size; 341 338 unsigned int size; 342 - float matrix[GPU_MATRIX_STACK_MAX][16]; 339 + float** matrix; 343 340 } GPU_MatrixStack; 344 341 345 342 ··· 1237 1234 // Basic vector operations (3D) 1238 1235 1239 1236 /*! Returns the magnitude (length) of the given vector. */ 1240 - DECLSPEC float SDLCALL GPU_VectorLength(float* vec3); 1237 + DECLSPEC float SDLCALL GPU_VectorLength(const float* vec3); 1241 1238 1242 1239 /*! Modifies the given vector so that it has a new length of 1. */ 1243 1240 DECLSPEC void SDLCALL GPU_VectorNormalize(float* vec3); 1244 1241 1245 1242 /*! Returns the dot product of two vectors. */ 1246 - DECLSPEC float SDLCALL GPU_VectorDot(float* A, float* B); 1243 + DECLSPEC float SDLCALL GPU_VectorDot(const float* A, const float* B); 1247 1244 1248 1245 /*! Performs the cross product of vectors A and B (result = A x B). Do not use A or B as 'result'. */ 1249 - DECLSPEC void SDLCALL GPU_VectorCross(float* result, float* A, float* B); 1246 + DECLSPEC void SDLCALL GPU_VectorCross(float* result, const float* A, const float* B); 1250 1247 1251 1248 /*! Overwrite 'result' vector with the values from vector A. */ 1252 - DECLSPEC void SDLCALL GPU_VectorCopy(float* result, float* A); 1249 + DECLSPEC void SDLCALL GPU_VectorCopy(float* result, const float* A); 1253 1250 1254 1251 /*! Multiplies the given matrix into the given vector (vec3 = matrix*vec3). */ 1255 - DECLSPEC void SDLCALL GPU_VectorApplyMatrix(float* vec3, float* matrix_4x4); 1252 + DECLSPEC void SDLCALL GPU_VectorApplyMatrix(float* vec3, const float* matrix_4x4); 1256 1253 1257 1254 1258 1255 ··· 1288 1285 /*! Multiplies matrices A and B and stores the result in the given 'result' matrix (result = A*B). Do not use A or B as 'result'. 1289 1286 * \see GPU_MultiplyAndAssign 1290 1287 */ 1291 - DECLSPEC void SDLCALL GPU_Multiply4x4(float* result, float* A, float* B); 1288 + DECLSPEC void SDLCALL GPU_MatrixMultiply(float* result, const float* A, const float* B); 1292 1289 1293 1290 /*! Multiplies matrices 'result' and B and stores the result in the given 'result' matrix (result = result * B). */ 1294 - DECLSPEC void SDLCALL GPU_MultiplyAndAssign(float* result, float* B); 1291 + DECLSPEC void SDLCALL GPU_MultiplyAndAssign(float* result, const float* B); 1295 1292 1296 1293 1297 1294 // Matrix stack accessors 1298 1295 1299 1296 /*! Returns an internal string that represents the contents of matrix A. */ 1300 - DECLSPEC const char* SDLCALL GPU_GetMatrixString(float* A); 1297 + DECLSPEC const char* SDLCALL GPU_GetMatrixString(const float* A); 1301 1298 1302 1299 /*! Returns the current matrix from the top of the matrix stack. Returns NULL if stack is empty. */ 1303 1300 DECLSPEC float* SDLCALL GPU_GetCurrentMatrix(void); ··· 1314 1311 1315 1312 // Matrix stack manipulators 1316 1313 1314 + /*! Allocate new matrices for the given stack. */ 1315 + DECLSPEC void SDLCALL GPU_InitMatrixStack(GPU_MatrixStack* stack); 1316 + 1317 1317 /*! Changes matrix mode to either GPU_PROJECTION or GPU_MODELVIEW. Further matrix stack operations manipulate that particular stack. */ 1318 1318 DECLSPEC void SDLCALL GPU_MatrixMode(int matrix_mode); 1319 1319 ··· 1325 1325 1326 1326 /*! Fills current matrix with the identity matrix. */ 1327 1327 DECLSPEC void SDLCALL GPU_LoadIdentity(void); 1328 + 1329 + /*! Copies a given matrix to be the current matrix. */ 1330 + DECLSPEC void SDLCALL GPU_LoadMatrix(const float* matrix4x4); 1328 1331 1329 1332 /*! Multiplies an orthographic projection matrix into the current matrix. */ 1330 1333 DECLSPEC void SDLCALL GPU_Ortho(float left, float right, float bottom, float top, float near, float far); ··· 1342 1345 DECLSPEC void SDLCALL GPU_Rotate(float degrees, float x, float y, float z); 1343 1346 1344 1347 /*! Multiplies a given matrix into the current matrix. */ 1345 - DECLSPEC void SDLCALL GPU_MultMatrix(float* matrix4x4); 1348 + DECLSPEC void SDLCALL GPU_MultMatrix(const float* matrix4x4); 1346 1349 1347 1350 // End of Matrix 1348 1351 /*! @} */
+90 -20
src/SDL_gpu_matrix.c
··· 51 51 52 52 53 53 54 + void GPU_InitMatrixStack(GPU_MatrixStack* stack) 55 + { 56 + if(stack == NULL) 57 + return; 58 + 59 + stack->storage_size = 1; 60 + stack->size = 1; 61 + 62 + stack->matrix = (float**)SDL_malloc(sizeof(float*) * stack->storage_size); 63 + stack->matrix[0] = (float*)SDL_malloc(sizeof(float) * 16); 64 + GPU_MatrixIdentity(stack->matrix[0]); 65 + } 66 + 67 + 54 68 // Column-major 55 69 #define INDEX(row,col) ((col)*4 + (row)) 56 70 57 71 58 - float GPU_VectorLength(float* vec3) 72 + float GPU_VectorLength(const float* vec3) 59 73 { 60 74 return sqrtf(vec3[0] * vec3[0] + vec3[1] * vec3[1] + vec3[2] * vec3[2]); 61 75 } ··· 68 82 vec3[2] /= mag; 69 83 } 70 84 71 - float GPU_VectorDot(float* A, float* B) 85 + float GPU_VectorDot(const float* A, const float* B) 72 86 { 73 87 return A[0] * B[0] + A[1] * B[1] + A[2] * B[2]; 74 88 } 75 89 76 - void GPU_VectorCross(float* result, float* A, float* B) 90 + void GPU_VectorCross(float* result, const float* A, const float* B) 77 91 { 78 92 result[0] = A[1] * B[2] - A[2] * B[1]; 79 93 result[1] = A[2] * B[0] - A[0] * B[2]; 80 94 result[2] = A[0] * B[1] - A[1] * B[0]; 81 95 } 82 96 83 - void GPU_VectorCopy(float* result, float* A) 97 + void GPU_VectorCopy(float* result, const float* A) 84 98 { 85 99 result[0] = A[0]; 86 100 result[1] = A[1]; 87 101 result[2] = A[2]; 88 102 } 89 103 90 - void GPU_VectorApplyMatrix(float* vec3, float* matrix_4x4) 104 + void GPU_VectorApplyMatrix(float* vec3, const float* matrix_4x4) 91 105 { 92 - float x = matrix_4x4[INDEX(0, 0)] * vec3[0] + matrix_4x4[INDEX(0, 1)] * vec3[1] + matrix_4x4[INDEX(0, 2)] * vec3[2] + matrix_4x4[INDEX(0, 3)]; 93 - float y = matrix_4x4[INDEX(1, 0)] * vec3[0] + matrix_4x4[INDEX(1, 1)] * vec3[1] + matrix_4x4[INDEX(1, 2)] * vec3[2] + matrix_4x4[INDEX(1, 3)]; 94 - float z = matrix_4x4[INDEX(2, 0)] * vec3[0] + matrix_4x4[INDEX(2, 1)] * vec3[1] + matrix_4x4[INDEX(2, 2)] * vec3[2] + matrix_4x4[INDEX(2, 3)]; 95 - float w = matrix_4x4[INDEX(3, 0)] * vec3[0] + matrix_4x4[INDEX(3, 1)] * vec3[1] + matrix_4x4[INDEX(3, 2)] * vec3[2] + matrix_4x4[INDEX(3, 3)]; 106 + float x = matrix_4x4[0] * vec3[0] + matrix_4x4[4] * vec3[1] + matrix_4x4[8] * vec3[2] + matrix_4x4[12]; 107 + float y = matrix_4x4[1] * vec3[0] + matrix_4x4[5] * vec3[1] + matrix_4x4[9] * vec3[2] + matrix_4x4[13]; 108 + float z = matrix_4x4[2] * vec3[0] + matrix_4x4[6] * vec3[1] + matrix_4x4[10] * vec3[2] + matrix_4x4[14]; 109 + float w = matrix_4x4[3] * vec3[0] + matrix_4x4[7] * vec3[1] + matrix_4x4[11] * vec3[2] + matrix_4x4[15]; 96 110 vec3[0] = x / w; 97 111 vec3[1] = y / w; 98 112 vec3[2] = z / w; 113 + } 114 + 115 + void GPU_Vector4ApplyMatrix(float* vec4, const float* matrix_4x4) 116 + { 117 + float x = matrix_4x4[0] * vec4[0] + matrix_4x4[4] * vec4[1] + matrix_4x4[8] * vec4[2] + matrix_4x4[12] * vec4[3]; 118 + float y = matrix_4x4[1] * vec4[0] + matrix_4x4[5] * vec4[1] + matrix_4x4[9] * vec4[2] + matrix_4x4[13] * vec4[3]; 119 + float z = matrix_4x4[2] * vec4[0] + matrix_4x4[6] * vec4[1] + matrix_4x4[10] * vec4[2] + matrix_4x4[14] * vec4[3]; 120 + float w = matrix_4x4[3] * vec4[0] + matrix_4x4[7] * vec4[1] + matrix_4x4[11] * vec4[2] + matrix_4x4[15] * vec4[3]; 121 + 122 + vec4[0] = x; 123 + vec4[1] = y; 124 + vec4[2] = z; 125 + vec4[3] = w; 126 + if(w != 0.0f) 127 + { 128 + vec4[0] = x / w; 129 + vec4[1] = y / w; 130 + vec4[2] = z / w; 131 + vec4[3] = 1; 132 + } 99 133 } 100 134 101 135 ··· 322 356 } 323 357 324 358 // Matrix multiply: result = A * B 325 - void GPU_Multiply4x4(float* result, float* A, float* B) 359 + void GPU_MatrixMultiply(float* result, const float* A, const float* B) 326 360 { 327 361 float (*matR)[4] = (float(*)[4])result; 328 362 float (*matA)[4] = (float(*)[4])A; ··· 345 379 matR[3][3] = matB[3][0] * matA[0][3] + matB[3][1] * matA[1][3] + matB[3][2] * matA[2][3] + matB[3][3] * matA[3][3]; 346 380 } 347 381 348 - void GPU_MultiplyAndAssign(float* result, float* B) 382 + void GPU_MultiplyAndAssign(float* result, const float* B) 349 383 { 350 384 float temp[16]; 351 - GPU_Multiply4x4(temp, result, B); 385 + GPU_MatrixMultiply(temp, result, B); 352 386 GPU_MatrixCopy(result, temp); 353 387 } 354 388 ··· 357 391 358 392 359 393 // Can be used up to two times per line evaluation... 360 - const char* GPU_GetMatrixString(float* A) 394 + const char* GPU_GetMatrixString(const float* A) 361 395 { 362 396 static char buffer[512]; 363 397 static char buffer2[512]; ··· 438 472 return; 439 473 440 474 stack = (target->context->matrix_mode == GPU_MODELVIEW? &target->context->modelview_matrix : &target->context->projection_matrix); 441 - if(stack->size + 1 >= GPU_MATRIX_STACK_MAX) 475 + if(stack->size + 1 >= stack->storage_size) 442 476 { 443 - GPU_PushErrorCode(__func__, GPU_ERROR_USER_ERROR, "Matrix stack is full (%d)", GPU_MATRIX_STACK_MAX); 444 - return; 477 + // Grow matrix stack (1, 6, 16, 36, ...) 478 + 479 + // Alloc new one 480 + unsigned int new_storage_size = stack->storage_size*2 + 4; 481 + float** new_stack = (float**)SDL_malloc(sizeof(float*) * new_storage_size); 482 + int i; 483 + for(i = 0; i < new_storage_size; ++i) 484 + { 485 + new_stack[i] = (float*)SDL_malloc(sizeof(float) * 16); 486 + } 487 + // Copy old one 488 + for(i = 0; i < stack->size; ++i) 489 + { 490 + GPU_MatrixCopy(new_stack[i], stack->matrix[i]); 491 + } 492 + // Free old one 493 + for(i = 0; i < stack->storage_size; ++i) 494 + { 495 + SDL_free(stack->matrix[i]); 496 + } 497 + SDL_free(stack->matrix); 498 + 499 + // Switch to new one 500 + stack->storage_size = new_storage_size; 501 + stack->matrix = new_stack; 445 502 } 446 503 GPU_MatrixCopy(stack->matrix[stack->size], stack->matrix[stack->size-1]); 447 504 stack->size++; ··· 460 517 if(stack->size == 0) 461 518 { 462 519 GPU_PushErrorCode(__func__, GPU_ERROR_USER_ERROR, "Matrix stack is empty."); 463 - return; 464 520 } 465 - stack->size--; 521 + else if(stack->size == 1) 522 + { 523 + GPU_PushErrorCode(__func__, GPU_ERROR_USER_ERROR, "Matrix stack would become empty!"); 524 + } 525 + else 526 + stack->size--; 466 527 } 467 528 468 529 void GPU_LoadIdentity(void) ··· 475 536 GPU_MatrixIdentity(result); 476 537 } 477 538 539 + void GPU_LoadMatrix(const float* A) 540 + { 541 + float* result = GPU_GetCurrentMatrix(); 542 + if(result == NULL) 543 + return; 544 + GPU_FlushBlitBuffer(); 545 + GPU_MatrixCopy(result, A); 546 + } 547 + 478 548 void GPU_Ortho(float left, float right, float bottom, float top, float near, float far) 479 549 { 480 550 GPU_FlushBlitBuffer(); ··· 505 575 GPU_MatrixRotate(GPU_GetCurrentMatrix(), degrees, x, y, z); 506 576 } 507 577 508 - void GPU_MultMatrix(float* A) 578 + void GPU_MultMatrix(const float* A) 509 579 { 510 580 float* result = GPU_GetCurrentMatrix(); 511 581 if(result == NULL) ··· 519 589 void GPU_GetModelViewProjection(float* result) 520 590 { 521 591 // MVP = P * MV 522 - GPU_Multiply4x4(result, GPU_GetProjection(), GPU_GetModelView()); 592 + GPU_MatrixMultiply(result, GPU_GetProjection(), GPU_GetModelView()); 523 593 }
+20 -9
src/renderer_GL_common.inl
··· 1440 1440 target->context->refcount = 1; 1441 1441 target->context->data = cdata; 1442 1442 target->context->context = NULL; 1443 + 1444 + GPU_InitMatrixStack(&target->context->projection_matrix); 1445 + GPU_InitMatrixStack(&target->context->modelview_matrix); 1446 + 1447 + target->context->matrix_mode = GPU_MODELVIEW; 1443 1448 1444 1449 cdata->last_image = NULL; 1445 1450 cdata->last_target = NULL; ··· 1612 1617 1613 1618 1614 1619 // Set up GL state 1615 - 1616 - target->context->projection_matrix.size = 1; 1617 - GPU_MatrixIdentity(target->context->projection_matrix.matrix[0]); 1618 - 1619 - target->context->modelview_matrix.size = 1; 1620 - GPU_MatrixIdentity(target->context->modelview_matrix.matrix[0]); 1621 - 1622 - target->context->matrix_mode = GPU_MODELVIEW; 1623 1620 1624 1621 // Modes 1625 1622 #ifndef SDL_GPU_SKIP_ENABLE_TEXTURE_2D ··· 3888 3885 static void FreeContext(GPU_Context* context) 3889 3886 { 3890 3887 GPU_CONTEXT_DATA* cdata; 3888 + int i; 3891 3889 3892 3890 if(context == NULL) 3893 3891 return; ··· 3920 3918 if(context->context != 0) 3921 3919 SDL_GL_DeleteContext(context->context); 3922 3920 #endif 3921 + 3922 + for(i = 0; i < context->projection_matrix.storage_size; ++i) 3923 + { 3924 + SDL_free(context->projection_matrix.matrix[i]); 3925 + } 3926 + SDL_free(context->projection_matrix.matrix); 3927 + 3928 + for(i = 0; i < context->modelview_matrix.storage_size; ++i) 3929 + { 3930 + SDL_free(context->modelview_matrix.matrix[i]); 3931 + } 3932 + SDL_free(context->modelview_matrix.matrix); 3933 + 3923 3934 3924 3935 SDL_free(cdata); 3925 3936 SDL_free(context); ··· 4677 4688 } 4678 4689 4679 4690 // MVP = P * MV 4680 - GPU_Multiply4x4(mvp, p, mv); 4691 + GPU_MatrixMultiply(mvp, p, mv); 4681 4692 4682 4693 glUniformMatrix4fv(context->current_shader_block.modelViewProjection_loc, 1, 0, mvp); 4683 4694 }
+2
tests/CMakeLists.txt
··· 8 8 set(TEST_LIBS ${TEST_LIBS} mingw32) 9 9 endif(MINGW) 10 10 11 + SET(CMAKE_BUILD_TYPE Debug) 12 + 11 13 add_executable(create-test create/main.c) 12 14 target_link_libraries (create-test ${TEST_LIBS}) 13 15
+5 -6
tests/renderer/main.c
··· 105 105 target->context->windowID = windowID; 106 106 target->context->data = NULL; // Allocate a data structure as needed for other context data 107 107 target->context->context = NULL; 108 + 109 + GPU_InitMatrixStack(&target->context->projection_matrix); 110 + GPU_InitMatrixStack(&target->context->modelview_matrix); 111 + 112 + target->context->matrix_mode = GPU_MODELVIEW; 108 113 } 109 114 else 110 115 { ··· 139 144 target->context->use_texturing = 1; 140 145 target->context->shapes_use_blending = 1; 141 146 target->context->shapes_blend_mode = GPU_GetBlendModeFromPreset(GPU_BLEND_NORMAL); 142 - 143 - target->context->projection_matrix.size = 1; 144 - GPU_MatrixIdentity(target->context->projection_matrix.matrix[0]); 145 - 146 - target->context->modelview_matrix.size = 1; 147 - GPU_MatrixIdentity(target->context->modelview_matrix.matrix[0]); 148 147 149 148 target->context->matrix_mode = GPU_MODELVIEW; 150 149
+22 -1
tests/transform-matrix/main.c
··· 167 167 168 168 GPU_MultMatrix(matrix); 169 169 170 - 171 170 GPU_SetCamera(screen, &camera); 171 + 172 172 GPU_Blit(image, NULL, screen, 0, 0); 173 173 174 + GPU_SetLineThickness(10.0f); 175 + GPU_Rectangle(screen, -image->w/2, -image->w/2, image->w/2, image->w/2, GPU_MakeColor(0, 0, 255, 255)); 176 + GPU_Circle(screen, 0, 0, image->w/2, GPU_MakeColor(255, 0, 0, 255)); 177 + 178 + GPU_RectangleFilled(screen, -image->w/8, -image->w/8, image->w/8, image->w/8, GPU_MakeColor(0, 255, 0, 255)); 179 + GPU_SetLineThickness(1.0f); 174 180 175 181 GPU_BlitScale(image, NULL, screen, 200, 200, 0.5f, 0.5f); 182 + 183 + float scale = 200; 184 + GPU_MatrixMode(GPU_MODELVIEW); 185 + GPU_PushMatrix(); 186 + GPU_SetLineThickness(4.0f); 187 + GPU_Translate(40, 40, 0.0f); 188 + GPU_Rotate(90, 0.0f, 0.0f, 1.0f); 189 + //GPU_Translate(-screen->w/2, -screen->h/2, 0.0f); 190 + GPU_Translate(-40, -40, 0.0f); 191 + GPU_Line(screen, 0, 0, scale, 0, GPU_MakeColor(255, 0, 0, 255)); 192 + GPU_CircleFilled(screen, 0, 0, scale/16, GPU_MakeColor(255, 0, 0, 255)); 193 + GPU_Circle(screen, 0, 0, scale, GPU_MakeColor(255, 0, 0, 255)); 194 + GPU_Circle(screen, 0, 0, scale*4, GPU_MakeColor(0, 255, 0, 255)); 195 + GPU_SetLineThickness(1.0f); 196 + GPU_PopMatrix(); 176 197 177 198 GPU_Flip(screen); 178 199
+44
tests/triangle-batch/main.c
··· 380 380 GPU_Clear(screen); 381 381 382 382 GPU_TriangleBatch(image, screen, num_vertices, vertex_values, num_vertices, indices, GPU_BATCH_XY_ST_RGBA); 383 + 384 + { 385 + // Quad 386 + int n_v = 4; 387 + float v[] = {0, 0, 0, 0, 388 + 100, 0, 1, 0, 389 + 0, 100, 0, 1, 390 + 100, 100, 1, 1}; 391 + 392 + int n_i = 6; 393 + unsigned short i[] = {0, 1, 2, 1, 3, 2}; 394 + GPU_TriangleBatch(image, screen, n_v, v, n_i, i, GPU_BATCH_XY_ST); 395 + } 396 + 397 + { 398 + // Star 399 + int n_v = 10; 400 + float v[] = {0, -300, 0.5, 0, // top 401 + 100, -110, 0.667, 0.317, // top to right 402 + 300, -70, 1, 0.383, // right 403 + 160, 90, 0.767, 0.65, // right to bot 404 + 190, 300, 0.817, 1, // bot right 405 + 0, 210, 0.5, 0.75, // bot right to bot left 406 + -190, 300, 0.183, 1, // bot left 407 + -160, 90, 0.233, 0.65, // bot left to left 408 + -300, -70, 0, 0.383, // left 409 + -100, -110, 0.333, 0.317}; // left to top 410 + 411 + int n_i = 15+9; 412 + unsigned short i[] = {0, 1, 9, 413 + 1, 2, 3, 414 + 3, 4, 5, 415 + 5, 6, 7, 416 + 7, 8, 9, 417 + 9, 1, 7, 418 + 1, 3, 7, 419 + 3, 5, 7}; 420 + 421 + GPU_MatrixMode(GPU_MODELVIEW); 422 + GPU_PushMatrix(); 423 + GPU_Translate(300, 300, 0); 424 + GPU_TriangleBatch(image, screen, n_v, v, n_i, i, GPU_BATCH_XY_ST); 425 + GPU_PopMatrix(); 426 + } 383 427 384 428 GPU_Flip(screen); 385 429