void dataDump(FILE *file){ unsigned int nfaces = 0; fprintf(file,"v16 __%s_vertex[][3]={ {0,0,0},\n",modelname); while(vertex!=NULL){ max[0] = vertex->x > max[0] ? vertex->x : max[0]; max[1] = vertex->y > max[1] ? vertex->y : max[1]; max[2] = vertex->z > max[2] ? vertex->z : max[2]; min[0] = vertex->x < min[0] ? vertex->x : min[0]; min[1] = vertex->y < min[1] ? vertex->y : min[1]; min[2] = vertex->z < min[2] ? vertex->z : min[2]; fprintf(file," {%d,%d,%d}", floattov16(vertex->x*adjust ), floattov16(vertex->y*adjust ), floattov16(vertex->z*adjust ) ); vertex=vertex->next; if(vertex!=NULL) fprintf(file,",\n"); } fprintf(file,"};\n\n"); fprintf(file,"float __%s_max[3] = { %f, %f, %f };\n",modelname,max[0]*adjust,max[1]*adjust,max[2]*adjust); fprintf(file,"float __%s_min[3] = { %f, %f, %f };\n\n",modelname,min[0]*adjust,min[1]*adjust,min[2]*adjust); fprintf(file,"v16 __%s_texture[][2]={ {0,0},\n",modelname); while(texture!=NULL){ fprintf(file," {%d,%d}",floattov16(texture->x), floattov16(texture->y) ); texture=texture->next; if(texture!=NULL) fprintf(file,",\n"); } fprintf(file,"};\n\n"); fprintf(file,"int __%s_normal[]={ 0,\n",modelname); while(normal!=NULL){ fprintf(file," %d", NORMAL_PACK(floattov10(normal->x), floattov10(normal->y), floattov10(normal->z)) ); normal=normal->next; if(normal!=NULL) fprintf(file,",\n"); } fprintf(file,"};\n\n"); fprintf(file,"int __%s_face[][3][3]={\n",modelname); while(figure!=NULL){ fprintf(file," {{%d,%d,%d},",figure->points[0][0], figure->points[0][1], figure->points[0][2]); fprintf(file," {%d,%d,%d},",figure->points[1][0], figure->points[1][1], figure->points[1][2]); fprintf(file," {%d,%d,%d}}",figure->points[2][0], figure->points[2][1], figure->points[2][2]); figure=figure->next; nfaces++; if(figure!=NULL) fprintf(file,",\n"); } fprintf(file,"};\n\n"); fprintf(file,"unsigned int __%s_nfaces = %u;",modelname,nfaces); fflush(file); }
void Map2D::DrawBG( int PlayerX, int PlayerY, s32 z, const glImage *BGTiles ) { glPushMatrix(); glScalef32( 12 << 12, 12 << 12, 5 << 12); for( int y = -2; y < 3; y++ ) { for( int x = -2; x < 4; x++ ) { glQuad3D( x * TILE_SIZE_3D, y * TILE_SIZE_3D, z, floattov16(1), floattov16(1), GL_FLIP_NONE, &BGTiles[0] ); } } glPopMatrix(1); }
void logo() { glResetMatrixStack(); glMatrixMode(GL_PROJECTION); gluPerspective(20, 256.0 / 192.0, 0.1, 40); gluLookAt( 0.0, .55, 0.0 , //camera possition 0.0, 0.0, 0.0, //look at 0.0, 0.0, -1.0); //up glLight(0, RGB15(31,31,31), 0, floattov10(1.0)-1, 0); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glMaterialf(GL_AMBIENT, RGB15(8,8,8)); glMaterialf(GL_DIFFUSE, RGB15(31,31,31)); glMaterialf(GL_SPECULAR, RGB15(31,31,31)); glMaterialf(GL_EMISSION, RGB15(8,8,8)); glMaterialShinyness(); glPolyFmt( POLY_ALPHA(31) | POLY_CULL_BACK | POLY_FORMAT_LIGHT0 ); glGenTextures(1, &logotex); glBindTexture(0, logotex); glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_256 , TEXTURE_SIZE_256, 0, TEXGEN_TEXCOORD , (u8*)billkuker_bmp_bin); glBindTexture(0, logotex); glBegin(GL_QUAD); glNormal(NORMAL_PACK(0,inttov10(1),0)); glTexCoord1i(TEXTURE_PACK(0,0)); glVertex3v16(floattov16(-0.5), floattov16(-0.5), floattov16(0.5) ); glTexCoord1i(TEXTURE_PACK(inttot16(256), inttot16(0))); glVertex3v16(floattov16(0.5), floattov16(-0.5), floattov16(0.5) ); glTexCoord1i(TEXTURE_PACK(inttot16(256),inttot16(256))); glVertex3v16(floattov16(0.5), floattov16(-0.5), floattov16(-0.5) ); glTexCoord1i(TEXTURE_PACK(0, inttot16(256))); glVertex3v16(floattov16(-0.5), floattov16(-0.5), floattov16(-0.5) ); glEnd(); glFlush(0); playGenericSound(down_raw, down_raw_size); playGenericSound(up_raw, up_raw_size); swiWaitForVBlank(); iprintf("Press any button"); while( !keysHeld() ) scanKeys(); glResetTextures(); iprintf("\x1b[2J"); }
int main() { int textureID; float rotateX = 0.0; float rotateY = 0.0; //set mode 0, enable BG0 and set it to 3D videoSetMode(MODE_0_3D); // initialize gl glInit(); //enable textures glEnable(GL_TEXTURE_2D); // enable antialiasing glEnable(GL_ANTIALIAS); // setup the rear plane glClearColor(0,0,0,31); // BG must be opaque for AA to work glClearPolyID(63); // BG must have a unique polygon ID for AA to work glClearDepth(0x7FFF); //this should work the same as the normal gl call glViewport(0,0,255,191); vramSetBankA(VRAM_A_TEXTURE); glGenTextures(1, &textureID); glBindTexture(0, textureID); glTexImage2D(0, 0, GL_RGB, TEXTURE_SIZE_128 , TEXTURE_SIZE_128, 0, TEXGEN_TEXCOORD, (u8*)texture_bin); //any floating point gl call is being converted to fixed prior to being implemented glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(70, 256.0 / 192.0, 0.1, 40); gluLookAt( 0.0, 0.0, 1.0, //camera possition 0.0, 0.0, 0.0, //look at 0.0, 1.0, 0.0); //up while(1) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); //move it away from the camera glTranslate3f32(0, 0, floattof32(-1)); glRotateX(rotateX); glRotateY(rotateY); glMaterialf(GL_AMBIENT, RGB15(16,16,16)); glMaterialf(GL_DIFFUSE, RGB15(16,16,16)); glMaterialf(GL_SPECULAR, BIT(15) | RGB15(8,8,8)); glMaterialf(GL_EMISSION, RGB15(16,16,16)); //ds uses a table for shinyness..this generates a half-ass one glMaterialShinyness(); //not a real gl function and will likely change glPolyFmt(POLY_ALPHA(31) | POLY_CULL_BACK); scanKeys(); u16 keys = keysHeld(); if((keys & KEY_UP)) rotateX += 3; if((keys & KEY_DOWN)) rotateX -= 3; if((keys & KEY_LEFT)) rotateY += 3; if((keys & KEY_RIGHT)) rotateY -= 3; glBindTexture(0, textureID); //draw the obj glBegin(GL_QUAD); glNormal(NORMAL_PACK(0,inttov10(-1),0)); GFX_TEX_COORD = (TEXTURE_PACK(0, inttot16(128))); glVertex3v16(floattov16(-0.5), floattov16(-0.5), 0 ); GFX_TEX_COORD = (TEXTURE_PACK(inttot16(128),inttot16(128))); glVertex3v16(floattov16(0.5), floattov16(-0.5), 0 ); GFX_TEX_COORD = (TEXTURE_PACK(inttot16(128), 0)); glVertex3v16(floattov16(0.5), floattov16(0.5), 0 ); GFX_TEX_COORD = (TEXTURE_PACK(0,0)); glVertex3v16(floattov16(-0.5), floattov16(0.5), 0 ); glEnd(); glPopMatrix(1); glFlush(0); swiWaitForVBlank(); } return 0; }//end main
//--------------------------------------------------------------------------------- int BoxTestf(float x, float y, float z, float width, float height, float depth) //--------------------------------------------------------------------------------- { return BoxTest(floattov16(x), floattov16(y), floattov16(z), floattov16(width), floattov16(height), floattov16(depth)); }
//--------------------------------------------------------------------------------- void BoxTestf_Asynch(float x, float y, float z, float width, float height, float depth) //--------------------------------------------------------------------------------- { BoxTest_Asynch(floattov16(x), floattov16(y), floattov16(z), floattov16(width), floattov16(height), floattov16(depth)); }
#include <nds.h> #include <stdlib.h> //texture_bin.h is created automagicaly from the texture.bin placed in arm9/resources //texture.bin is a raw 128x128 16 bit image. I will release a tool for texture conversion //later #include "texture_bin.h" //verticies for the cube v16 CubeVectors[] = { floattov16(-0.5), floattov16(-0.5), floattov16(0.5), floattov16(0.5), floattov16(-0.5), floattov16(0.5), floattov16(0.5), floattov16(-0.5), floattov16(-0.5), floattov16(-0.5), floattov16(-0.5), floattov16(-0.5), floattov16(-0.5), floattov16(0.5), floattov16(0.5), floattov16(0.5), floattov16(0.5), floattov16(0.5), floattov16(0.5), floattov16(0.5), floattov16(-0.5), floattov16(-0.5), floattov16(0.5), floattov16(-0.5) }; //polys u8 CubeFaces[] = { 3,2,1,0, 0,1,5,4, 1,2,6,5, 2,3,7,6, 3,0,4,7, 5,6,7,4 };
void TunnelPartInit(void) { T_Mesh* mesh; int i; /* * Initialisation vidéo */ /* Modes video */ videoSetMode(MODE_5_3D); videoSetModeSub(MODE_5_2D); lcdMainOnBottom(); vramSetBankA(VRAM_A_MAIN_BG_0x06000000); vramSetBankB(VRAM_B_MAIN_BG_0x06020000); vramSetBankC(VRAM_C_SUB_BG_0x06200000); vramSetBankD(VRAM_D_TEXTURE_SLOT3); videoBgEnable(DISPLAY_BG0_ACTIVE | DISPLAY_BG2_ACTIVE); videoBgEnableSub(DISPLAY_BG2_ACTIVE | DISPLAY_BG3_ACTIVE); /* Le BG qui servira pour le tunnel */ TunnelBgId = bgInitSub(2, BgType_Bmp16, BgSize_B16_256x256, 0, 0); TunnelBgPixels = (u16*)bgGetGfxPtr(TunnelBgId); bgSetPriority(TunnelBgId, 2); /* Le tunnel en fond */ /* L'avant plan de l'éléphant */ FrontLayerId = bgInit(2, BgType_Bmp8, BgSize_B8_256x256, 8, 0); FrontLayerPixels = (u8*)bgGetGfxPtr(FrontLayerId); bgSetPriority(FrontLayerId, 0); /* En avant plan */ decompress(bouleBitmap, FrontLayerPixels, LZ77Vram); decompress(boulePal, BG_PALETTE, LZ77Vram); bgSetPriority(0, 1); /* La 3D entre les deux */ /* Initialisation de la 3D */ Init3DEngine(); TunnelFxInit(TunnelBgPixels); mesh = CreateMesh(); mesh->display_list = DiscoElephant_mesh; mesh->alpha = 31; mesh->texturing = TRUE; mesh->lighting = TRUE; DiscoElephant = CreateObj3D(mesh); Scene = CreateScene(); AddObject(Scene, DiscoElephant); Scene->lights[0] = CreateLight(floattov10(0), floattov10(0), floattov10(-1), RGB15(16, 0, 01), TRUE); Scene->lights[1] = CreateLight(floattov10(0), floattov10(0), floattov10(-1), RGB15(0, 16, 0), TRUE); Scene->lights[2] = CreateLight(floattov10(0), floattov10(0), floattov10(-1), RGB15(0, 0, 16), TRUE); Scene->lights[3] = CreateLight(floattov10(0), floattov10(0), floattov10(-1), RGB15(16, 16, 0), TRUE); glClearColor(0, 0, 0, 0); flare_tex = CreateTextureFromMemory((u8*)flareBitmap, GL_RGBA, 0, 16, 64, TRUE, LZ77Vram); for(i=0; i<4; i++) { flares[i] = malloc(sizeof(*flares[i])); } flares[0]->position[0] = 0; flares[0]->position[1] = 0; flares[0]->position[2] = floattof32(-1); flares[0]->poly_alpha = 16; flares[0]->color = RGB15(31, 0, 0); flares[0]->size = floattov16(0.15f); flares[0]->texture_id = flare_tex->id; flares[0]->poly_id = 5; flares[1]->position[0] = 0; flares[1]->position[1] = 0; flares[1]->position[2] = floattof32(-1); flares[1]->poly_alpha = 16; flares[1]->color = RGB15(0, 31, 0); flares[1]->size = floattov16(0.15f); flares[1]->texture_id = flare_tex->id; flares[1]->poly_id = 6; flares[2]->position[0] = 0; flares[2]->position[1] = 0; flares[2]->position[2] = floattof32(-1); flares[2]->poly_alpha = 16; flares[2]->color = RGB15(0, 0, 31); flares[2]->size = floattov16(0.15f); flares[2]->texture_id = flare_tex->id; flares[2]->poly_id = 6; flares[3]->position[0] = 0; flares[3]->position[1] = 0; flares[3]->position[2] = floattof32(-1); flares[3]->poly_alpha = 16; flares[3]->color = RGB15(31, 31, 0); flares[3]->size = floattov16(0.15f); flares[3]->texture_id = flare_tex->id; flares[3]->poly_id = 7; tex = CreateTextureFromMemory((u8*)DiscoElephantBitmap, GL_RGB, 0, 16, 64, TRUE, LZ77Vram); DiscoElephant->mesh->texture_id = tex->id; DiscoElephant->mesh->texturing = TRUE; /* Initialisation du fade in depuis le blanc */ REG_MASTER_BRIGHT = (1<<14); REG_MASTER_BRIGHT_SUB = REG_MASTER_BRIGHT; StartFlash(-1000); // pour éviter un flash à la première seconde glMaterialf(GL_AMBIENT, RGB15(16,16,16)); glMaterialf(GL_SPECULAR, BIT(15) | RGB15(31,31,31)); glMaterialShinyness(); }
struct MODEL *loadModel(FILE * file) { int i; struct MODEL * model = malloc(sizeof(struct MODEL)); if(model == NULL) iprintf("creating space in memory failed...\n\n"); //Read header fread(&model->header.BKDM, 32, 1, file); if(model->header.BKDM == 0x4D444B42) { //Read materials fread(&model->materials.mateSIGN, 4, 1, file); if(model->materials.mateSIGN == 0x4554414D) { int materialCounter = model->header.materialCount; model->materials.material = malloc(materialCounter * sizeof(struct MATERIAL)); int i; for(i = 0; i < materialCounter; i++) { fread(&model->materials.material[i].materialID, 1, 1, file); fread(&model->materials.material[i].X, 1, 1, file); fread(&model->materials.material[i].Y, 1, 1, file); fread(&model->materials.material[i].TX, 1, 1, file); fread(&model->materials.material[i].TY, 1, 1, file); fread(&model->materials.material[i].alpha, 1, 1, file); fread(&model->materials.material[i].culling, 1, 1, file); fread(&model->materials.material[i].stringSize, 1, 1, file); model->materials.material[i].textureName = malloc(model->materials.material[i].stringSize); char * materialName = malloc(model->materials.material[i].stringSize);; fread(materialName, 1, model->materials.material[i].stringSize, file); model->materials.material[i].textureName = materialName; } fread(&model->materials.padding, 2, 1, file); } else iprintf(" no material section\n"); //Read vertex section fread(&model->vertices.vertSIGN, 4, 1, file); if(model->vertices.vertSIGN == 0x54524556) { model->vertices.vertex = malloc(model->header.vertexCount * sizeof(struct VERTEX)); int i = 0; while(i < model->header.vertexCount) { float xFloat; float yFloat; float zFloat; fread(&xFloat, 4, 1, file); fread(&yFloat, 4, 1, file); fread(&zFloat, 4, 1, file); model->vertices.vertex[i].x = floattov16(xFloat); model->vertices.vertex[i].y = floattov16(yFloat); model->vertices.vertex[i].z = floattov16(zFloat); i++; } } else iprintf(" no vertex section\n\n"); //read the coordinate section fread(&model->coordinates.tecoSIGN, 4, 1, file); if(model->coordinates.tecoSIGN == 0x4F434554) { model->coordinates.coordinate = malloc(model->header.coordinateCount * sizeof(struct COORDINATE)); int i = 0; while(i < model->header.coordinateCount) { float u, v; fread(&u, 4, 1, file); fread(&v, 4, 1, file); model->coordinates.coordinate[i].u = floattot16(u); model->coordinates.coordinate[i].v = floattot16(v); i++; } } else iprintf(" no coordinate section\n\n"); //read the normal section fread(&model->normals.nocoSIGN, 4, 1, file); if(model->normals.nocoSIGN == 0x4F434F4E) { model->normals.normal = malloc(model->header.normalCount * sizeof(struct NORMAL)); int i = 0; while(i < model->header.normalCount) { float x, y, z; fread(&x, 4, 1, file); fread(&y, 4, 1, file); fread(&z, 4, 1, file); model->normals.normal[i].x = floattov16(x); model->normals.normal[i].y = floattov16(y); model->normals.normal[i].z = floattov16(z); i++; } } else iprintf(" no normal section\n\n"); //read the color section fread(&model->colors.vecoSIGN, 4, 1, file); if(model->colors.vecoSIGN == 0x4F434556) { model->colors.color = malloc(model->header.colorCount * sizeof(struct COLOR)); int i = 0; while(i < model->header.colorCount) { char r, g, b; fread(&r, 1, 1, file); fread(&g, 1, 1, file); fread(&b, 1, 1, file); model->colors.color[i].r = r; model->colors.color[i].g = g; model->colors.color[i].b = b; i++; } } else iprintf(" no color section\n\n"); //read the face section fread(&model->faces.faceSIGN, 4, 1, file); if(model->faces.faceSIGN == 0x45434146) { model->faces.object = malloc(model->header.objectCount * sizeof(struct OBJECT)); int i = 0; while(i < model->header.objectCount) { fread(&model->faces.object[i].materialID, 1, 1, file); fread(&model->faces.object[i].triangleCount, 2, 1, file); model->faces.object[i].triangles = malloc(model->faces.object[i].triangleCount * sizeof(struct TRIANGLES)); int a = 0; while(a < model->faces.object[i].triangleCount) { if(model->header.flags == 1) { int b = 0; while(b < 3) { fread(&model->faces.object[i].triangles[a].triangle[b].v, 1, 1, file); fread(&model->faces.object[i].triangles[a].triangle[b].t, 1, 1, file); fread(&model->faces.object[i].triangles[a].triangle[b].n, 1, 1, file); fread(&model->faces.object[i].triangles[a].triangle[b].c, 1, 1, file); b++; } } else { int b = 0; while(b < 3) { fread(&model->faces.object[i].triangles[a].triangle[b].v, 2, 1, file); fread(&model->faces.object[i].triangles[a].triangle[b].t, 2, 1, file); fread(&model->faces.object[i].triangles[a].triangle[b].n, 2, 1, file); fread(&model->faces.object[i].triangles[a].triangle[b].c, 2, 1, file); b++; } } a++; } i++; } } else iprintf(" no face section\n\n"); } else iprintf(" Not correct file\n"); //load textures FILE * tex; model->textures = malloc(sizeof(int) * model->header.materialCount); glGenTextures(model->header.materialCount, &model->textures[0]); for(i = 0; i < model->header.materialCount; i++) { char * two = model->materials.material[i].textureName; char * thr = ".bin"; char str[80]; strcpy(str, "/data/texture/"); strcat(str, two); strcat(str, thr); tex = fopen(str, "rb"); if(tex != NULL) { int kenmerk = 0; int texType = 0; int sizeX = 0; int sizeY = 0; fread(&kenmerk, 4, 1, tex); fread(&sizeX, 4, 1, tex); fread(&sizeY, 4, 1, tex); if(kenmerk == 0x78424752) texType = 7; else texType = 8; int size = (sizeX * sizeY) * 2; char *data = malloc(size); fread(data, sizeof(char), size, tex); glBindTexture(0, model->textures[i]); int f = glTexImage2D(0, 0, texType, TEXTURE_SIZE_32, TEXTURE_SIZE_32, 0, TEXGEN_TEXCOORD | GL_TEXTURE_WRAP_S | GL_TEXTURE_WRAP_T, data); if(f == 0) printf(" texture load fail \n"); free(data); } else { printf(" texture NOT loaded \n"); model->textures[i] = 0; } fclose(tex); } printf(" \n"); return model; }