File size: 42,643 Bytes
00e6582 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 | /*
Copyright (c) 2026 Ethan Mortonson.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "image.h"
#include "string.h"
void Load_Transparent_Image(struct _Image* Image, const char* Filename, const char* Transparency_Map_Filename)
{
FILE * file;
file = fopen(Filename,"rb");
if(!file)
{
file = fopen("resources/artwork/placeholders/no_texture.sei", "rb");
}
unsigned char header[4];
fread(&header, 4, 1, file);
Image->Width = (header[1] << 8) | (header[0] & 0xff);
Image->Height = (header[3] << 8) | (header[2] & 0xff);
//printf("Width: %d\n", Image->Width);
//printf("Height: %d\n", Image->Height);
int RGB_Image_Size = ((Image->Width) * (Image->Height) * 3);
int RGBA_Image_Size = ((Image->Width) * (Image->Height) * 4);
fseek(file, 4, SEEK_SET);
Image->RGB_Canvas = (unsigned char*) calloc (RGB_Image_Size , sizeof(unsigned char));
Image->RGBA_Canvas = (unsigned char*) calloc (RGBA_Image_Size , sizeof(unsigned char));
memset(Image->RGBA_Canvas, 0, RGBA_Image_Size);
fread(Image->RGB_Canvas,1,RGB_Image_Size,file);
fclose(file);
FILE * filetwo;
filetwo = fopen(Transparency_Map_Filename,"rb");
if(!filetwo)
{
filetwo = fopen("resources/artwork/placeholders/no_texture.sei", "rb");
}
unsigned char headertwo[4];
fread(&headertwo, 4, 1, file);
fseek(filetwo, 4, SEEK_SET);
unsigned char* Transparency_Buffer = (unsigned char*) calloc (RGB_Image_Size , sizeof(unsigned char));
fread(Transparency_Buffer,1,RGB_Image_Size,filetwo);
fclose(filetwo);
int in = 0;
int r = 0;
for(int j = 0; j < (RGBA_Image_Size/4); j++)
{
Image->RGBA_Canvas[r] = Image->RGB_Canvas[in+2];
Image->RGBA_Canvas[r + 1] = Image->RGB_Canvas[in + 1];
Image->RGBA_Canvas[r + 2] = Image->RGB_Canvas[in];
Image->RGBA_Canvas[r + 3] = Transparency_Buffer[in];
in += 3;
r += 4;
}
glGenTextures(1, &Image->Texture_ID);
glBindTexture(GL_TEXTURE_2D, Image->Texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Clamp to edge to avoid issues with non-power-of-2 textures
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256,256, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image->RGBA_Canvas);
glEnable(GL_TEXTURE_2D);
}
void drawBillboard(float x, float y, float z, float Width, float Height) {
// 1. Get the current Modelview matrix
// Billboard properties
float billboardWidth = Width;
float billboardHeight = Height;
float billboardPosition[3] = {x, y, z}; // Example position
// Camera position and orientation (for demonstration)
float cameraPosition[3] = {0.0f, 0.0f, 0.0f};
float cameraRotationY = 0.0f;
float modelviewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
// 2. Extract the right (x) and up (y) vectors of the camera
float rightVector[3] = {modelviewMatrix[0], modelviewMatrix[4], modelviewMatrix[8]};
float upVector[3] = {modelviewMatrix[1], modelviewMatrix[5], modelviewMatrix[9]};
// 3. Calculate the billboard's corners
float halfWidth = billboardWidth / 2.0f;
float halfHeight = billboardHeight / 2.0f;
float corner1[3] = {
billboardPosition[0] - rightVector[0] * halfWidth + upVector[0] * halfHeight,
billboardPosition[1] - rightVector[1] * halfWidth + upVector[1] * halfHeight,
billboardPosition[2] - rightVector[2] * halfWidth + upVector[2] * halfHeight
};
float corner2[3] = {
billboardPosition[0] + rightVector[0] * halfWidth + upVector[0] * halfHeight,
billboardPosition[1] + rightVector[1] * halfWidth + upVector[1] * halfHeight,
billboardPosition[2] + rightVector[2] * halfWidth + upVector[2] * halfHeight
};
float corner3[3] = {
billboardPosition[0] + rightVector[0] * halfWidth - upVector[0] * halfHeight,
billboardPosition[1] + rightVector[1] * halfWidth - upVector[1] * halfHeight,
billboardPosition[2] + rightVector[2] * halfWidth - upVector[2] * halfHeight
};
float corner4[3] = {
billboardPosition[0] - rightVector[0] * halfWidth - upVector[0] * halfHeight,
billboardPosition[1] - rightVector[1] * halfWidth - upVector[1] * halfHeight,
billboardPosition[2] - rightVector[2] * halfWidth - upVector[2] * halfHeight
};
// 4. Render the billboard as a quad
glBegin(GL_QUADS);
// Assuming you have texture coordinates (uv) associated with your billboard
glTexCoord2f(0.0f, 1.0f); glVertex3fv(corner1);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(corner2);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(corner3);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(corner4);
glEnd();
}
struct _Image* Create_Image()
{
struct _Image* I = (struct _Image*) calloc(1,sizeof (struct _Image));
I->x = 0;
I->y = 0;
I->Texture_Made = 0;
return I;
}
void Destroy_Image(struct _Image* Image)
{
//free(Image->RGB_Canvas);
//free(Image->RGBA_Canvas);
//free(Image);
}
unsigned short swap_uint16(unsigned short val) {
return (val << 8) | (val >> 8);
}
void Load_Image(struct _Image* Image, const char* Filename)
{
FILE * file;
file = fopen(Filename,"rb");
if(!file)
{
file = fopen("resources/artwork/placeholders/no_image.sei", "rb");
//printf("Error: Could not load image: ");
//printf(Filename);
//printf(".");
//printf("\n");
}
unsigned char header[4];
fread(&header, 4, 1, file);
Image->Width = (header[1] << 8) | (header[0] & 0xff);
Image->Height = (header[3] << 8) | (header[2] & 0xff);
//printf("Width: %d\n", Image->Width);
//printf("Height: %d\n", Image->Height);
int RGB_Image_Size = ((Image->Width) * (Image->Height) * 3);
int RGBA_Image_Size = ((Image->Width) * (Image->Height) * 4);
fseek(file, 4, SEEK_SET);
Image->RGB_Canvas = (unsigned char*) calloc (RGB_Image_Size,sizeof(unsigned char));
Image->RGBA_Canvas = (unsigned char*) calloc (RGBA_Image_Size,sizeof(unsigned char));
memset(Image->RGBA_Canvas, 0, RGBA_Image_Size);
fread(Image->RGB_Canvas,1,RGB_Image_Size,file);
fclose(file);
//Image->RGBA_Canvas = Convert_RGB_To_RGBA(Image->RGB_Canvas,Image->Width, Image->Height);
int in = 0;
int r = 0;
//Image->RGBA_Canvas[0] = Image->RGB_Canvas[0];
for(int j = 0; j < (RGBA_Image_Size/4); j++)
{
Image->RGBA_Canvas[r] = Image->RGB_Canvas[in+2];
Image->RGBA_Canvas[r + 1] = Image->RGB_Canvas[in + 1];
Image->RGBA_Canvas[r + 2] = Image->RGB_Canvas[in];
Image->RGBA_Canvas[r + 3] = 255;
if(Image->RGBA_Canvas[r] == 200)
{
if(Image->RGBA_Canvas[r+1] == 200)
{
if(Image->RGBA_Canvas[r + 2] == 200)
{
Image->RGBA_Canvas[r + 3] = 0;
}
}
}
in += 3;
r += 4;
}
// Generate a texture ID
glGenTextures(1, &Image->Texture_ID);
// Bind the texture to GL_TEXTURE_2D
glBindTexture(GL_TEXTURE_2D, Image->Texture_ID);
// Set texture parameters
// GL_LINEAR for smooth scaling, GL_NEAREST for pixelated look
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// Clamp to edge to avoid issues with non-power-of-2 textures
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Load the image data into the texture
// GL_BGR is used because BMP stores pixels in BGR order
// GL_UNSIGNED_BYTE indicates each color component is an unsigned byte
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image->Width, Image->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image->RGBA_Canvas);
// Enable texture mapping
glEnable(GL_TEXTURE_2D);
}
void Render_Image(struct _Image* Image, float _x, float _y, int Alpha_Present)
{
glBindTexture(GL_TEXTURE_2D, Image->Texture_ID);
float Vertices[] = {_x, _y, _x + Image->Width, _y, _x + Image->Width, _y + Image->Height, _x, _y + Image->Height};
float Texture_Vertices[] = {0, 0, 1, 0, 1, -1, 0, -1};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
glVertexPointer(2, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
}
void Render_Image_As_Billboard(struct _Image* Image, float _x, float _y, float _z, int Alpha_Present, float Scale)
{
glBindTexture(GL_TEXTURE_2D, Image->Texture_ID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image->Width, Image->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image->RGBA_Canvas);
//Image->Texture_Made = 1;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
//glColor3f(0,0,255);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
float billboardWidth = Image->Width*Scale;
float billboardHeight = Image->Height*Scale;
float billboardPosition[3] = {_x, _y, _z}; // Example position
// Camera position and orientation (for demonstration)
float cameraPosition[3] = {0.0f, 0.0f, 0.0f};
float cameraRotationY = 0.0f;
float modelviewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
// 2. Extract the right (x) and up (y) vectors of the camera
float rightVector[3] = {modelviewMatrix[0], modelviewMatrix[4], modelviewMatrix[8]};
float upVector[3] = {modelviewMatrix[1], modelviewMatrix[5], modelviewMatrix[9]};
// 3. Calculate the billboard's corners
float halfWidth = billboardWidth / 2.0f;
float halfHeight = billboardHeight / 2.0f;
float corner1[3] = {
billboardPosition[0] - rightVector[0] * halfWidth + upVector[0] * halfHeight,
billboardPosition[1] - rightVector[1] * halfWidth + upVector[1] * halfHeight,
billboardPosition[2] - rightVector[2] * halfWidth + upVector[2] * halfHeight
};
float corner2[3] = {
billboardPosition[0] + rightVector[0] * halfWidth + upVector[0] * halfHeight,
billboardPosition[1] + rightVector[1] * halfWidth + upVector[1] * halfHeight,
billboardPosition[2] + rightVector[2] * halfWidth + upVector[2] * halfHeight
};
float corner3[3] = {
billboardPosition[0] + rightVector[0] * halfWidth - upVector[0] * halfHeight,
billboardPosition[1] + rightVector[1] * halfWidth - upVector[1] * halfHeight,
billboardPosition[2] + rightVector[2] * halfWidth - upVector[2] * halfHeight
};
float corner4[3] = {
billboardPosition[0] - rightVector[0] * halfWidth - upVector[0] * halfHeight,
billboardPosition[1] - rightVector[1] * halfWidth - upVector[1] * halfHeight,
billboardPosition[2] - rightVector[2] * halfWidth - upVector[2] * halfHeight
};
float Vertices[] = {corner1[0],corner1[1],corner1[2], corner2[0],corner2[1],corner2[2], corner3[0],corner3[1],corner3[2], corner4[0],corner4[1],corner4[2],};
float Texture_Vertices[] = {0, 1, 1, 1, 1, 0, 0, 0};
glVertexPointer(3, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
}
void Render_Image_With_Vertex_Color(struct _Image* Image, float _x, float _y, int Alpha_Present, float R, float G, float B)
{
glBindTexture(GL_TEXTURE_2D, Image->Texture_ID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image->Width, Image->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image->RGBA_Canvas);
//Image->Texture_Made = 1;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
float Vertices[] = {_x, _y, _x + Image->Width, _y, _x + Image->Width, _y + Image->Height, _x, _y + Image->Height};
float Texture_Vertices[] = {0, 0, 1, 0, 1, -1, 0, -1};
glColor3f(R,G,B);
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
glVertexPointer(2, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
}
void Copy_Pixel(int Color_Depth, unsigned char* Source, unsigned char* Destination, int Source_Width, int Source_Height, int Destination_Width, int Destination_Height, int Source_X, int Source_Y, int Destination_X, int Destination_Y)
{
/* TIMES 3!??
* COPY PIXELS
* 1. Calculate Source and Destination array Sizes
* 2. Calculate Source and Destination array locations
* 3. Send Pixel
*/
int Source_Array_Size;
int Destination_Array_Size;
int Source_Array_Location;
int Destination_Array_Location;
Source_Array_Size = Source_Width * Source_Height * Color_Depth;
Destination_Array_Size = Destination_Width * Destination_Height * Color_Depth;
Source_Array_Location = (Source_Y * ( Source_Width * Color_Depth)) + (Source_X * Color_Depth);
Destination_Array_Location = (Destination_Y * ( Destination_Width * Color_Depth)) + (Destination_X * Color_Depth);
if(Color_Depth == 1)
{
memcpy(&Destination[Destination_Array_Location], &Source[Source_Array_Location], 1);
//Destination[Destination_Array_Location] = Source[Source_Array_Location];
}
else if(Color_Depth != 1)
{
memcpy(&Destination[Destination_Array_Location], &Source[Source_Array_Location], 1);
memcpy(&Destination[Destination_Array_Location+1], &Source[Source_Array_Location+1], 1);
memcpy(&Destination[Destination_Array_Location+2], &Source[Source_Array_Location+2], 1);
}
}
void Copy_Pixel_Alpha(int Color_Depth, unsigned char* Source, unsigned char* Destination, int Source_Width, int Source_Height, int Destination_Width, int Destination_Height, int Source_X, int Source_Y, int Destination_X, int Destination_Y)
{
int Source_Array_Size;
int Destination_Array_Size;
int Source_Array_Location;
int Destination_Array_Location;
Source_Array_Size = Source_Width * Source_Height * Color_Depth;
Destination_Array_Size = Destination_Width * Destination_Height * Color_Depth;
Source_Array_Location = (Source_Y * ( Source_Width * Color_Depth)) + (Source_X * Color_Depth);
Destination_Array_Location = (Destination_Y * ( Destination_Width * Color_Depth)) + (Destination_X * Color_Depth);
////printf("Source Array Location: %d\n",Source_Array_Location);
////printf("Dest Array Location: %d\n",Destination_Array_Location);
if(Color_Depth == 1)
{
memcpy(&Destination[Destination_Array_Location], &Source[Source_Array_Location], 1);
//intf("The color depth is 1\n");
//Destination[Destination_Array_Location] = Source[Source_Array_Location];
}
else if(Color_Depth != 1)
{
memcpy(&Destination[Destination_Array_Location], &Source[Source_Array_Location], 1);
memcpy(&Destination[Destination_Array_Location+1], &Source[Source_Array_Location+1], 1);
memcpy(&Destination[Destination_Array_Location+2], &Source[Source_Array_Location+2], 1);
memcpy(&Destination[Destination_Array_Location+3], &Source[Source_Array_Location+3], 1);
// printf("The color depth is 4\n");
}
}
void Copy_Row(int Color_Depth, unsigned char* Source, unsigned char* Destination, int Source_Width, int Source_Height, int Destination_Width, int Destination_Height, int Source_X, int Source_Y, int Destination_X, int Destination_Y, int N_Pixels_To_Copy)
{
int Line_Counter = 0;
while(Line_Counter < N_Pixels_To_Copy)
{
if(Color_Depth == 4)
{
Copy_Pixel_Alpha(4,Source,Destination,Source_Width,Source_Height,Destination_Width,Destination_Height,Source_X+Line_Counter,Source_Y,Destination_X+Line_Counter,Destination_Y);
Line_Counter += 1;
}
else if(Color_Depth == 3)
{
Copy_Pixel(3,Source,Destination,Source_Width,Source_Height,Destination_Width,Destination_Height,Source_X+Line_Counter,Source_Y,Destination_X+Line_Counter,Destination_Y);
Line_Counter += 1;
}
else if(Color_Depth == 1)
{
Copy_Pixel(1,Source,Destination,Source_Width,Source_Height,Destination_Width,Destination_Height,Source_X+Line_Counter,Source_Y,Destination_X+Line_Counter,Destination_Y);
Line_Counter += 1;
}
}
}
void Copy_Section(int Color_Depth, unsigned char* Source, unsigned char* Destination, int Source_Width, int Source_Height, int Destination_Width, int Destination_Height, int Source_X, int Source_Y, int Destination_X, int Destination_Y, int N_Pixels_To_Copy, int Rows_To_Copy)
{
//printf("The color depth is %d\n", Color_Depth);
int Row_Counter = 0;
while(Row_Counter < Rows_To_Copy)
{
if(Color_Depth == 4)
{
Copy_Row(4,Source,Destination,Source_Width,Source_Height,Destination_Width,Destination_Height,Source_X,Source_Y+Row_Counter,Destination_X,Destination_Y+Row_Counter,N_Pixels_To_Copy);
Row_Counter += 1;
}
else if(Color_Depth == 3)
{
Copy_Row(3,Source,Destination,Source_Width,Source_Height,Destination_Width,Destination_Height,Source_X,Source_Y+Row_Counter,Destination_X,Destination_Y+Row_Counter,N_Pixels_To_Copy);
Row_Counter += 1;
}
else if(Color_Depth == 1)
{
Copy_Row(1,Source,Destination,Source_Width,Source_Height,Destination_Width,Destination_Height,Source_X,Source_Y+Row_Counter,Destination_X,Destination_Y+Row_Counter,N_Pixels_To_Copy);
Row_Counter += 1;
}
}
}
void Create_BIN()
{
}
void Load_BIN()
{
}
unsigned char* Convert_RGB_To_RGBA(unsigned char* Input, int Input_Width, int Input_Height)
{
int Input_Size = (Input_Width+1) * (Input_Height+1);
unsigned char* Result = (unsigned char*) calloc ((Input_Size * 4),sizeof(unsigned char));
int in = 0;
int r = 0;
for(int j = 0; j < 64; j++)
{
Result[j] = 32;
}
return Result;
}
void Render_Billboard(struct _Image* Image, float _x, float _y, Vector Camera_Vector, int Alpha_Present)
{
};
void Render_Animated_Image(struct _Image* Image, float _x, float _y, int Alpha_Present, int Cell_Width, int Cell_Height, int Cell_Y, int Frame, int Total_Frames, float Rate)
{
};
void Write_TwoD_Sprite_Animation(const char* Filename)
{
struct _TwoD_Sprite_Animation_Header Header;
memset(Header.Sprite_Sheet_Filename, 0,256);
memset(Header.Sprite_Sheet_Alpha_Channel_Filename, 0,256);
sprintf(Header.Sprite_Sheet_Filename, "explosion_bubble.sei");
sprintf(Header.Sprite_Sheet_Alpha_Channel_Filename, "explosion_bubble.trm");
Header.Sprite_Sheet_Filename[20] = '\0';
Header.Sprite_Sheet_Alpha_Channel_Filename[20] = '\0';
Header.Width = 640;
Header.Height = 64;
Header.Cel_X_Division = 0.1; //If 8 frames, this is 1/8 = 0.125
Header.Cel_Y_Division = 0.1; //If 8 frames, this is 1/8 = 0.125
Header.Number_Of_Animations = 1;
Header.Offset_To_Animations = sizeof(struct _TwoD_Sprite_Animation_Header);
struct _TwoD_Sprite_Animation_Parameters Animation;
Animation.Is_A_Billboard = 1;
Animation.Number_Of_Frames = 10;
Animation.Cel_X_ID = 0; //If I want to start mid-animation.
Animation.Cel_Y_ID = 0;
Animation.Animation_Speed = 1;
Animation.Loop = 0;
Animation.Rotational_Velocity = 0;
Animation.Rotational_Acceleration = 0;
Animation.Billboard_Scale = 1;
Animation.Scale_Velocity = 0;
Animation.Scale_Acceleration = 0;
Animation.x = 0;
Animation.y = 0;
Animation.z = 0;
Animation.dx = 1;
Animation.dy = 0;
Animation.dz = 0;
Animation.ddx = 0;
Animation.ddy = 0;
Animation.ddz = 0;
Animation.Loop_Motion_Once_Too_Far = 1;
Animation.Maximum_Distance_From_Origin = 30;
Animation.Start_x = 0;
Animation.Start_y = 0;
Animation.Start_z = 0;
Animation.Rotation=0;
FILE* f;
f = fopen(Filename, "wb");
fwrite(&Header, 1, sizeof(struct _TwoD_Sprite_Animation_Header),f);
rewind(f);
fseek(f, Header.Offset_To_Animations, SEEK_SET);
fwrite(&Animation, 1, sizeof(struct _TwoD_Sprite_Animation_Parameters),f);
fclose(f);
};
void Load_TwoD_Sprite_Animation(struct _TwoD_Sprite_Object* TwoD_Sprite_Object, const char* Filename, unsigned int ID)
{
FILE* f;
f = fopen(Filename, "rb");
long File_Size;
fseek(f, 0, SEEK_END);
File_Size = ftell(f);
rewind(f);
unsigned char Buffer[File_Size];
fread(&Buffer, sizeof(unsigned char), File_Size, f);
fclose(f);
long Buffer_Location = 0;
int Current_Buffer_Location =0;
char Sprite_Filepath[256];
char Transparency_Filepath[256];
for(int j = 0; j < 256; j++)
{
Sprite_Filepath[j] = Buffer[j];
}
FILE * file;
file = fopen(Sprite_Filepath,"rb");
//printf(Sprite_Filepath);
unsigned char header[54];
fread(header, 1, 54, file);
TwoD_Sprite_Object->Width = *(int*)&(header[0x12]);
TwoD_Sprite_Object->Height = *(int*)&(header[0x16]);
int RGB_Image_Size = ((TwoD_Sprite_Object->Width) * (TwoD_Sprite_Object->Height) * 3);
int RGBA_Image_Size = ((TwoD_Sprite_Object->Width) * (TwoD_Sprite_Object->Height) * 4);
fseek(file, 54, SEEK_SET);
TwoD_Sprite_Object->RGB_Canvas = (unsigned char*) calloc (RGB_Image_Size,sizeof(unsigned char));
TwoD_Sprite_Object->RGBA_Canvas = (unsigned char*) calloc (RGBA_Image_Size,sizeof(unsigned char));
memset(TwoD_Sprite_Object->RGBA_Canvas, 0, RGBA_Image_Size);
fread(TwoD_Sprite_Object->RGB_Canvas,1,RGB_Image_Size,file);
fclose(file);
//Image->RGBA_Canvas = Convert_RGB_To_RGBA(Image->RGB_Canvas,Image->Width, Image->Height);
int in = 0;
int r = 0;
//Image->RGBA_Canvas[0] = Image->RGB_Canvas[0];
for(int j = 0; j < (RGBA_Image_Size/4); j++)
{
TwoD_Sprite_Object->RGBA_Canvas[r] = TwoD_Sprite_Object->RGB_Canvas[in+2];
TwoD_Sprite_Object->RGBA_Canvas[r + 1] = TwoD_Sprite_Object->RGB_Canvas[in + 1];
TwoD_Sprite_Object->RGBA_Canvas[r + 2] = TwoD_Sprite_Object->RGB_Canvas[in];
TwoD_Sprite_Object->RGBA_Canvas[r + 3] = 255;
if(TwoD_Sprite_Object->RGBA_Canvas[r] == 200)
{
if(TwoD_Sprite_Object->RGBA_Canvas[r+1] == 200)
{
if(TwoD_Sprite_Object->RGBA_Canvas[r + 2] == 200)
{
TwoD_Sprite_Object->RGBA_Canvas[r + 3] = 0;
}
}
}
in += 3;
r += 4;
}
// Generate a texture ID
glGenTextures(1, &TwoD_Sprite_Object->Texture_ID);
// Bind the texture to GL_TEXTURE_2D
glBindTexture(GL_TEXTURE_2D, TwoD_Sprite_Object->Texture_ID);
// Set texture parameters
// GL_LINEAR for smooth scaling, GL_NEAREST for pixelated look
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Clamp to edge to avoid issues with non-power-of-2 textures
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
// Load the image data into the texture
// GL_BGR is used because BMP stores pixels in BGR order
// GL_UNSIGNED_BYTE indicates each color component is an unsigned byte
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TwoD_Sprite_Object->Width, TwoD_Sprite_Object->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, TwoD_Sprite_Object->RGBA_Canvas);
// Enable texture mapping
glEnable(GL_TEXTURE_2D);
TwoD_Sprite_Object->Width = *(int*)&(Buffer[256+256]);
TwoD_Sprite_Object->Height = *(int*)&(Buffer[256+256+4]);
TwoD_Sprite_Object->Cel_X_Division = *(float*)&(Buffer[256+256+4+4]);
TwoD_Sprite_Object->Cel_Y_Division = *(float*)&(Buffer[256+256+4+4+4]);
TwoD_Sprite_Object->Number_Of_Animations = *(int*)&(Buffer[256+256+4+4+4+4]);
//printf("Cel X Division: %f\n", TwoD_Sprite_Object->Cel_X_Division);
TwoD_Sprite_Object->Animations = (struct _TwoD_Sprite_Animation_Parameters*)calloc(TwoD_Sprite_Object->Number_Of_Animations, sizeof(struct _TwoD_Sprite_Animation_Parameters));
//printf("Animations: %d\n", TwoD_Sprite_Object->Number_Of_Animations);
for(int j = 0; j < TwoD_Sprite_Object->Number_Of_Animations; j++) //272
{
Current_Buffer_Location = (sizeof(struct _TwoD_Sprite_Animation_Header)) + (j*sizeof(struct _TwoD_Sprite_Animation_Parameters));
TwoD_Sprite_Object->Animations[j].Is_A_Billboard = *(int*)&(Buffer[Current_Buffer_Location]);
TwoD_Sprite_Object->Animations[j].Number_Of_Frames = *(int*)&(Buffer[Current_Buffer_Location+4]);
TwoD_Sprite_Object->Animations[j].Cel_X_ID = *(int*)&(Buffer[Current_Buffer_Location+8]); //If I want to start mid-animation.
TwoD_Sprite_Object->Animations[j].Cel_Y_ID = *(int*)&(Buffer[Current_Buffer_Location+12]);
TwoD_Sprite_Object->Animations[j].Animation_Speed = *(float*)&(Buffer[Current_Buffer_Location+16]);
TwoD_Sprite_Object->Animations[j].Loop = *(int*)&(Buffer[Current_Buffer_Location+20]);
TwoD_Sprite_Object->Animations[j].Rotational_Velocity = *(float*)&(Buffer[Current_Buffer_Location+24]);
TwoD_Sprite_Object->Animations[j].Rotational_Acceleration = *(float*)&(Buffer[Current_Buffer_Location+28]);
TwoD_Sprite_Object->Animations[j].Billboard_Scale = *(float*)&(Buffer[Current_Buffer_Location+32]);
TwoD_Sprite_Object->Animations[j].Scale_Velocity = *(float*)&(Buffer[Current_Buffer_Location+36]);
TwoD_Sprite_Object->Animations[j].Scale_Acceleration = *(float*)&(Buffer[Current_Buffer_Location+40]);
TwoD_Sprite_Object->Animations[j].x = *(float*)&(Buffer[Current_Buffer_Location+44]);
TwoD_Sprite_Object->Animations[j].y = *(float*)&(Buffer[Current_Buffer_Location+48]);
TwoD_Sprite_Object->Animations[j].z = *(float*)&(Buffer[Current_Buffer_Location+52]);
TwoD_Sprite_Object->Animations[j].dx = *(float*)&(Buffer[Current_Buffer_Location+56]);
TwoD_Sprite_Object->Animations[j].dy = *(float*)&(Buffer[Current_Buffer_Location+60]);
TwoD_Sprite_Object->Animations[j].dz = *(float*)&(Buffer[Current_Buffer_Location+64]);
TwoD_Sprite_Object->Animations[j].ddx = *(float*)&(Buffer[Current_Buffer_Location+68]);
TwoD_Sprite_Object->Animations[j].ddy = *(float*)&(Buffer[Current_Buffer_Location+72]);
TwoD_Sprite_Object->Animations[j].ddz = *(float*)&(Buffer[Current_Buffer_Location+76]);
TwoD_Sprite_Object->Animations[j].Loop_Motion_Once_Too_Far = *(int*)&(Buffer[Current_Buffer_Location+80]);
TwoD_Sprite_Object->Animations[j].Maximum_Distance_From_Origin = *(float*)&(Buffer[Current_Buffer_Location+84]);
TwoD_Sprite_Object->Animations[j].Start_x = *(float*)&(Buffer[Current_Buffer_Location+88]);
TwoD_Sprite_Object->Animations[j].Start_y = *(float*)&(Buffer[Current_Buffer_Location+92]);
TwoD_Sprite_Object->Animations[j].Start_z = *(float*)&(Buffer[Current_Buffer_Location+96]);
TwoD_Sprite_Object->Animations[j].Rotation = *(float*)&(Buffer[Current_Buffer_Location+100]);
}
TwoD_Sprite_Object->Current_Start_Cel = TwoD_Sprite_Object->Animations->Cel_X_ID;
TwoD_Sprite_Object->Current_End_Cel = TwoD_Sprite_Object->Animations->Number_Of_Frames-1;
TwoD_Sprite_Object->Current_Cel = TwoD_Sprite_Object->Current_Start_Cel;
};
void Render_TwoD_Sprite_Animation(struct _TwoD_Sprite_Object* TwoD_Sprite_Object, float x, float y)
{
x = TwoD_Sprite_Object->Animations->x;
y = TwoD_Sprite_Object->Animations->y;
glBindTexture(GL_TEXTURE_2D, TwoD_Sprite_Object->Texture_ID);
//glPushMatrix();
// glRotatef(23,0,0,1);
float Vertices[] = {x, y, x + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Width*TwoD_Sprite_Object->Cel_X_Division), y, x + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Width*TwoD_Sprite_Object->Cel_X_Division), y + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Height), x, y + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Height)};
float Texture_Vertex_X_One = TwoD_Sprite_Object->Cel_X_Division * TwoD_Sprite_Object->Current_Cel; //Top left corner
float Texture_Vertex_X_Two = Texture_Vertex_X_One + TwoD_Sprite_Object->Cel_X_Division;
// //printf("Texture Vertex X One: %f\n", Texture_Vertex_X_One);
// //printf("Texture Vertex X Two: %f\n", Texture_Vertex_X_Two);
float Texture_Vertex_Y_One = TwoD_Sprite_Object->Cel_Y_Division * TwoD_Sprite_Object->Animations->Cel_Y_ID;
float Texture_Vertex_Y_Two = Texture_Vertex_Y_One + TwoD_Sprite_Object->Cel_Y_Division;
float Texture_Vertices[] = {Texture_Vertex_X_One, 0, Texture_Vertex_X_Two, 0, Texture_Vertex_X_Two, -1, Texture_Vertex_X_One, -1};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
glVertexPointer(2, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
// glPopMatrix();
//float Vertices[] = {x, y, x + TwoD_Sprite_Object->Width*TwoD_Sprite_Object->Cel_X_Division, y, x + TwoD_Sprite_Object->Width*TwoD_Sprite_Object->Cel_X_Division, y + TwoD_Sprite_Object->Height*TwoD_Sprite_Object->Cel_X_Division, x, y + TwoD_Sprite_Object->Height*TwoD_Sprite_Object->Cel_X_Division};
//float Texture_Vertices[] = {0, 0, 1, 0, 1, -1, 0, -1};
//glEnableClientState(GL_VERTEX_ARRAY);
//glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
//glVertexPointer(2, GL_FLOAT, 0, Vertices);
//glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
//glDrawArrays(GL_QUADS, 0, 4);
//glDisableClientState(GL_VERTEX_ARRAY);
};
void Process_TwoD_Sprite_Animation(struct _TwoD_Sprite_Object* TwoD_Sprite_Object)
{
TwoD_Sprite_Object->Animations->x += TwoD_Sprite_Object->Animations->dx;
TwoD_Sprite_Object->Animations->y += TwoD_Sprite_Object->Animations->dy;
TwoD_Sprite_Object->Animations->z += TwoD_Sprite_Object->Animations->dz;
TwoD_Sprite_Object->Animations->dx += TwoD_Sprite_Object->Animations->ddx;
TwoD_Sprite_Object->Animations->dy += TwoD_Sprite_Object->Animations->ddy;
TwoD_Sprite_Object->Animations->dz += TwoD_Sprite_Object->Animations->ddz;
TwoD_Sprite_Object->Animations->Rotation += TwoD_Sprite_Object->Animations->Rotational_Velocity;
TwoD_Sprite_Object->Animations->Rotational_Velocity += TwoD_Sprite_Object->Animations->Rotational_Acceleration;
TwoD_Sprite_Object->Animations->Billboard_Scale += TwoD_Sprite_Object->Animations->Scale_Velocity;
TwoD_Sprite_Object->Animations->Scale_Velocity += TwoD_Sprite_Object->Animations->Scale_Acceleration;
TwoD_Sprite_Object->Current_Cel += 1;
if(TwoD_Sprite_Object->Current_Cel > TwoD_Sprite_Object->Current_End_Cel)
{
TwoD_Sprite_Object->Current_Start_Cel;
}
float Distance = Get_Distance(TwoD_Sprite_Object->Animations->x,TwoD_Sprite_Object->Animations->y,TwoD_Sprite_Object->Animations->z,TwoD_Sprite_Object->Animations->Start_x,TwoD_Sprite_Object->Animations->Start_y,TwoD_Sprite_Object->Animations->Start_z);
////printf("Distance: %f\n", Distance);
if(Distance >= TwoD_Sprite_Object->Animations->Maximum_Distance_From_Origin)
{
if(TwoD_Sprite_Object->Animations->Loop_Motion_Once_Too_Far == 1)
{
TwoD_Sprite_Object->Animations->x = TwoD_Sprite_Object->Animations->Start_x;
TwoD_Sprite_Object->Animations->y = TwoD_Sprite_Object->Animations->Start_y;
TwoD_Sprite_Object->Animations->z = TwoD_Sprite_Object->Animations->Start_z;
}
}
};
void Render_TwoD_Sprite_Animation_As_Billboard(struct _TwoD_Sprite_Object* TwoD_Sprite_Object, float x, float y, float z,float Width, float Height)
{
// 1. Get the current Modelview matrix
// Billboard properties
float billboardWidth = Width;
float billboardHeight = Height;
float billboardPosition[3] = {x, y, z}; // Example position
// Camera position and orientation (for demonstration)
float cameraPosition[3] = {0.0f, 0.0f, 0.0f};
float cameraRotationY = 0.0f;
float modelviewMatrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, modelviewMatrix);
// 2. Extract the right (x) and up (y) vectors of the camera
float rightVector[3] = {modelviewMatrix[0], modelviewMatrix[4], modelviewMatrix[8]};
float upVector[3] = {modelviewMatrix[1], modelviewMatrix[5], modelviewMatrix[9]};
// 3. Calculate the billboard's corners
float halfWidth = billboardWidth / 2.0f;
float halfHeight = billboardHeight / 2.0f;
float corner1[3] = {
billboardPosition[0] - rightVector[0] * halfWidth + upVector[0] * halfHeight,
billboardPosition[1] - rightVector[1] * halfWidth + upVector[1] * halfHeight,
billboardPosition[2] - rightVector[2] * halfWidth + upVector[2] * halfHeight
};
float corner2[3] = {
billboardPosition[0] + rightVector[0] * halfWidth + upVector[0] * halfHeight,
billboardPosition[1] + rightVector[1] * halfWidth + upVector[1] * halfHeight,
billboardPosition[2] + rightVector[2] * halfWidth + upVector[2] * halfHeight
};
float corner3[3] = {
billboardPosition[0] + rightVector[0] * halfWidth - upVector[0] * halfHeight,
billboardPosition[1] + rightVector[1] * halfWidth - upVector[1] * halfHeight,
billboardPosition[2] + rightVector[2] * halfWidth - upVector[2] * halfHeight
};
float corner4[3] = {
billboardPosition[0] - rightVector[0] * halfWidth - upVector[0] * halfHeight,
billboardPosition[1] - rightVector[1] * halfWidth - upVector[1] * halfHeight,
billboardPosition[2] - rightVector[2] * halfWidth - upVector[2] * halfHeight
};
// 4. Render the billboard as a quad
glBegin(GL_QUADS);
// Assuming you have texture coordinates (uv) associated with your billboard
glTexCoord2f(0.0f, 1.0f); glVertex3fv(corner1);
glTexCoord2f(1.0f, 1.0f); glVertex3fv(corner2);
glTexCoord2f(1.0f, 0.0f); glVertex3fv(corner3);
glTexCoord2f(0.0f, 0.0f); glVertex3fv(corner4);
glEnd();
x = TwoD_Sprite_Object->Animations->x;
y = TwoD_Sprite_Object->Animations->y;
glBindTexture(GL_TEXTURE_2D, TwoD_Sprite_Object->Texture_ID);
float Vertices[] = {x, y, x + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Width*TwoD_Sprite_Object->Cel_X_Division), y, x + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Width*TwoD_Sprite_Object->Cel_X_Division), y + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Height), x, y + TwoD_Sprite_Object->Animations->Billboard_Scale*(TwoD_Sprite_Object->Height)};
float Texture_Vertex_X_One = TwoD_Sprite_Object->Cel_X_Division * TwoD_Sprite_Object->Current_Cel; //Top left corner
float Texture_Vertex_X_Two = Texture_Vertex_X_One + TwoD_Sprite_Object->Cel_X_Division;
// //printf("Texture Vertex X One: %f\n", Texture_Vertex_X_One);
// //printf("Texture Vertex X Two: %f\n", Texture_Vertex_X_Two);
float Texture_Vertex_Y_One = TwoD_Sprite_Object->Cel_Y_Division * TwoD_Sprite_Object->Animations->Cel_Y_ID;
float Texture_Vertex_Y_Two = Texture_Vertex_Y_One + TwoD_Sprite_Object->Cel_Y_Division;
float Texture_Vertices[] = {Texture_Vertex_X_One, 0, Texture_Vertex_X_Two, 0, Texture_Vertex_X_Two, -1, Texture_Vertex_X_One, -1};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
glVertexPointer(2, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
};
void Load_Image_And_Convert_To_Bitmap(const char* Filename, const char* Output)
{
FILE * file;
file = fopen(Filename,"rb");
unsigned char header[54];
fread(header, 1, 54, file);
int Width = *(int*)&(header[0x12]);
int Height = *(int*)&(header[0x16]);
int RGB_Image_Size = ((Width) * (Height) * 3);
unsigned char* RGB_Canvas;
fseek(file, 54, SEEK_SET);
RGB_Canvas = (unsigned char*) calloc (RGB_Image_Size,sizeof(unsigned char));
memset(RGB_Canvas,0,RGB_Image_Size);
fread(RGB_Canvas,1,RGB_Image_Size,file);
fclose(file);
unsigned char Output_Width = (unsigned char)Width;
unsigned char Output_Height = (unsigned char)Height;
unsigned char Color_Mode = Color_Mode;
unsigned char Canvas[Output_Width * Output_Height];
memset(Canvas,0,Output_Width * Output_Height);
//printf("Width: %d\n", Output_Width);
int l = 0;
int j = 0;
while(l < (Output_Width * Output_Height))
{
Canvas[l] = RGB_Canvas[j];
l+=1;
j+=3;
}
FILE* fOut;
fOut = fopen(Output, "wb");
fwrite(&Output_Width,1,sizeof(unsigned char),fOut);
fwrite(&Output_Height,1,sizeof(unsigned char),fOut);
fwrite(&Color_Mode,1,sizeof(unsigned char),fOut);
fwrite(Canvas,1,Output_Width * Output_Height,fOut);
fclose(fOut);
};
void BMPtoSEI(const char* Filename, const char* Output_Filename)
{
FILE * file;
file = fopen(Filename,"rb");
unsigned char header[54];
fread(header, 1, 54, file);
unsigned int Width = *(int*)&(header[0x12]);
unsigned int Height = *(int*)&(header[0x16]);
int RGB_Image_Size = ((Width) * (Height) * 3);
int RGBA_Image_Size = ((Width) * (Height) * 4);
fseek(file, 54, SEEK_SET);
unsigned char* RGB_Canvas = (unsigned char*) calloc (RGB_Image_Size,sizeof(unsigned char));
unsigned char* RGBA_Canvas = (unsigned char*) calloc (RGBA_Image_Size,sizeof(unsigned char));
memset(RGBA_Canvas, 0, RGBA_Image_Size);
fread(RGB_Canvas,1,RGB_Image_Size,file);
fclose(file);
//Image->RGBA_Canvas = Convert_RGB_To_RGBA(Image->RGB_Canvas,Image->Width, Image->Height);
int in = 0;
int r = 0;
//Image->RGBA_Canvas[0] = Image->RGB_Canvas[0];
FILE* fTwo;
fTwo=fopen(Output_Filename, "wb");
fwrite(&Width, sizeof(uint16_t), 1, file);
fwrite(&Height, sizeof(uint16_t), 1, file);
fwrite(RGB_Canvas,sizeof(unsigned char),RGB_Image_Size,fTwo);
fclose(fTwo);
}
void Load_Bitmap_And_Convert_To_Image(const char* Filename, struct _Image* Image)
{
FILE * file;
file = fopen(Filename,"rb");
if(!file)
{
file = fopen("resources/artwork/placeholders/no_image.sei", "rb");
}
unsigned char header[3];
fread(header, 1, 3, file);
Image->Width = *(unsigned char*)&(header[0x00]);
Image->Height = *(unsigned char*)&(header[0x01]);
unsigned char Color_Mode = *(unsigned char*)&(header[0x02]);
//printf("Loaded Image Width: %d\n", Image->Width);
//printf("Loaded Image Height: %d\n", Image->Height);
int RGB_Image_Size = ((Image->Width) * (Image->Height));
int RGBA_Image_Size = ((Image->Width) * (Image->Height) * 4);
fseek(file, 3, SEEK_SET);
Image->RGB_Canvas = (unsigned char*) calloc (RGB_Image_Size,sizeof(unsigned char));
Image->RGBA_Canvas = (unsigned char*) calloc (RGBA_Image_Size,sizeof(unsigned char));
memset(Image->RGB_Canvas, 0, RGB_Image_Size);
memset(Image->RGBA_Canvas, 0, RGBA_Image_Size);
fread(Image->RGB_Canvas,1,RGB_Image_Size,file);
fclose(file);
//Image->RGBA_Canvas = Convert_RGB_To_RGBA(Image->RGB_Canvas,Image->Width, Image->Height);
int in = 0;
int r = 0;
//Image->RGBA_Canvas[0] = Image->RGB_Canvas[0];
for(int j = 0; j < (RGBA_Image_Size/4); j++)
{
Image->RGBA_Canvas[r] = Image->RGB_Canvas[in];
Image->RGBA_Canvas[r + 1] = Image->RGB_Canvas[in];
Image->RGBA_Canvas[r + 2] = Image->RGB_Canvas[in];
Image->RGBA_Canvas[r + 3] = 255;
in += 1;
r += 4;
}
glGenTextures(1, &Image->Texture_ID);
glBindTexture(GL_TEXTURE_2D, Image->Texture_ID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image->Width, Image->Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Image->RGBA_Canvas);
glEnable(GL_TEXTURE_2D);
};
void Render_Image_At_Size(struct _Image* Image, float _x, float _y, float Width, float Height, int Alpha_Present)
{
glBindTexture(GL_TEXTURE_2D, Image->Texture_ID);
//glColor3f(1,1,1);
float Vertices[] = {_x, _y, _x + Width, _y, _x + Width, _y + Height, _x, _y + Height};
float Texture_Vertices[] = {0, 0, 1, 0, 1, -1, 0, -1};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY_EXT);
glVertexPointer(2, GL_FLOAT, 0, Vertices);
glTexCoordPointer(2, GL_FLOAT, 0, Texture_Vertices);
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_VERTEX_ARRAY);
};
|