void Surface::Set(const void* pixels, unsigned int width, unsigned int height, unsigned char bytes_per_pixel, bool amask0) { FreeSurface(*this); switch(bytes_per_pixel) { case 1: CreateSurface(width, height, 8, false); LoadPalette(); Lock(); std::memcpy(surface->pixels, pixels, width * height); Unlock(); break; default: { u32 rmask, gmask, bmask, amask; GetRGBAMask(bytes_per_pixel * 8, rmask, gmask, bmask, amask); surface = SDL_CreateRGBSurfaceFrom(const_cast<void *>(pixels), width, height, 8 * bytes_per_pixel, width * bytes_per_pixel, rmask, gmask, bmask, (amask0 ? amask : 0)); } break; } if(!surface) Error::Except(__FUNCTION__, SDL_GetError()); }
/* bpp: 8, 16, 24, 32 */ void Surface::Set(u16 sw, u16 sh, u8 bpp, bool amask0) { FreeSurface(*this); CreateSurface(sw, sh, bpp, amask0); if(8 == bpp) LoadPalette(); SetDefaultColorKey(); }
// ------------------------------------------------------------------------------------------------ Map :: ~Map() { for(int i = 0; i < NUM_OF_TILE_TYPES; i++) { if(tileSurfaces[i] != 0) { FreeSurface(tileSurfaces[i]); } } } // ----------------------------------------------------------------------------------------------
bool Surface::Load(const char* fn) { FreeSurface(*this); #ifdef WITH_IMAGE if(fn) surface = IMG_Load(fn); #else if(fn) surface = SDL_LoadBMP(fn); #endif return surface; }
void Surface::Set(u32 sw, u32 sh, const SurfaceFormat & fm) { FreeSurface(*this); #if SDL_VERSION_ATLEAST(2, 0, 0) surface = SDL_CreateRGBSurface(0, sw, sh, fm.depth, fm.rmask, fm.gmask, fm.bmask, fm.amask); #else surface = SDL_CreateRGBSurface(SDL_SWSURFACE, sw, sh, fm.depth, fm.rmask, fm.gmask, fm.bmask, fm.amask); #endif if(!surface) Error::Except(__FUNCTION__, SDL_GetError()); if(8 == depth()) { SetPalette(); Fill(fm.ckey); SetColorKey(fm.ckey); } else if(amask()) { #if SDL_VERSION_ATLEAST(2, 0, 0) Fill(RGBA(0, 0, 0, 0)); // no color key only amask #else Fill(RGBA(fm.ckey.r(), fm.ckey.g(), fm.ckey.b(), 0)); SetColorKey(fm.ckey); #endif } else if(fm.ckey.pack()) { Fill(fm.ckey); SetColorKey(fm.ckey); } if(amask()) { #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_BLEND); #else SDL_SetAlpha(surface, SDL_SRCALPHA, 255); #endif } else { #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE); #else SDL_SetAlpha(surface, 0, 0); #endif } }
/* ==================== idRenderModelOverlay::~idRenderModelOverlay ==================== */ idRenderModelOverlay::~idRenderModelOverlay() { int i, k; for ( k = 0; k < materials.Num(); k++ ) { for ( i = 0; i < materials[k]->surfaces.Num(); i++ ) { FreeSurface( materials[k]->surfaces[i] ); } materials[k]->surfaces.Clear(); delete materials[k]; } materials.Clear(); }
/* ============= FreeLeafSurfs ============= */ void FreeLeafSurfs (node_t *leaf) { surface_t *surf, *snext; face_t *f, *fnext; for (surf = leaf->surfaces ; surf ; surf=snext) { snext = surf->next; for (f=surf->faces ; f ; f=fnext) { fnext = f->next; FreeFace (f); } FreeSurface (surf); } leaf->surfaces = NULL; }
bool Surface::Load(const std::string & fn) { FreeSurface(*this); #ifdef WITH_IMAGE surface = IMG_Load(fn.c_str()); #if SDL_VERSION_ATLEAST(2, 0, 0) SDL_SetSurfaceBlendMode(surface, SDL_BLENDMODE_NONE); #endif #else surface = SDL_LoadBMP(fn.c_str()); #endif if(!surface) ERROR(SDL_GetError()); return surface; }
void Surface::Set(const Surface & bs, bool refcopy) { FreeSurface(*this); if(bs.isValid()) { if(refcopy) { surface = bs.surface; if(surface) surface->refcount += 1; } else { surface = SDL_ConvertSurface(bs.surface, bs.surface->format, bs.surface->flags); if(!surface) Error::Except(__FUNCTION__, SDL_GetError()); } } }
void Surface::Set(const Surface & bs, bool refcopy) /* copy surface */ { FreeSurface(*this); if(bs.isValid()) { if(refcopy || 1 < bs.RefCount()) { surface = bs.surface; if(surface) surface->refcount += 1; } else { if(8 == bs.depth()) Set(bs.surface->pixels, bs.w(), bs.h(), 1, false); else surface = SDL_ConvertSurface(bs.surface, bs.surface->format, bs.surface->flags); if(!surface) Error::Except(__FUNCTION__, SDL_GetError()); } } }
void Surface::Set(SDL_Surface* sf) { FreeSurface(*this); surface = sf ? sf : NULL; }
Surface::~Surface() { if(! isDisplay()) FreeSurface(*this); }
/* ==================== idRenderModelOverlay::AddOverlaySurfacesToModel ==================== */ void idRenderModelOverlay::AddOverlaySurfacesToModel( idRenderModel *baseModel ) { int i, j, k, numVerts, numIndexes, surfaceNum; const modelSurface_t *baseSurf; idRenderModelStatic *staticModel; overlaySurface_t *surf; srfTriangles_t *newTri; modelSurface_t *newSurf; if ( baseModel == NULL || baseModel->IsDefaultModel() ) { return; } // md5 models won't have any surfaces when r_showSkel is set if ( !baseModel->NumSurfaces() ) { return; } if ( baseModel->IsDynamicModel() != DM_STATIC ) { common->Error( "idRenderModelOverlay::AddOverlaySurfacesToModel: baseModel is not a static model" ); } assert( dynamic_cast<idRenderModelStatic *>(baseModel) != NULL ); staticModel = static_cast<idRenderModelStatic *>(baseModel); staticModel->overlaysAdded = 0; if ( !materials.Num() ) { staticModel->DeleteSurfacesWithNegativeId(); return; } for ( k = 0; k < materials.Num(); k++ ) { numVerts = numIndexes = 0; for ( i = 0; i < materials[k]->surfaces.Num(); i++ ) { numVerts += materials[k]->surfaces[i]->numVerts; numIndexes += materials[k]->surfaces[i]->numIndexes; } if ( staticModel->FindSurfaceWithId( -1 - k, surfaceNum ) ) { newSurf = &staticModel->surfaces[surfaceNum]; } else { newSurf = &staticModel->surfaces.Alloc(); newSurf->geometry = NULL; newSurf->shader = materials[k]->material; newSurf->id = -1 - k; } if ( newSurf->geometry == NULL || newSurf->geometry->numVerts < numVerts || newSurf->geometry->numIndexes < numIndexes ) { R_FreeStaticTriSurf( newSurf->geometry ); newSurf->geometry = R_AllocStaticTriSurf(); R_AllocStaticTriSurfVerts( newSurf->geometry, numVerts ); R_AllocStaticTriSurfIndexes( newSurf->geometry, numIndexes ); SIMDProcessor->Memset( newSurf->geometry->verts, 0, numVerts * sizeof( newTri->verts[0] ) ); } else { R_FreeStaticTriSurfVertexCaches( newSurf->geometry ); } newTri = newSurf->geometry; numVerts = numIndexes = 0; for ( i = 0; i < materials[k]->surfaces.Num(); i++ ) { surf = materials[k]->surfaces[i]; // get the model surface for this overlay surface if ( surf->surfaceNum < staticModel->NumSurfaces() ) { baseSurf = staticModel->Surface( surf->surfaceNum ); } else { baseSurf = NULL; } // if the surface ids no longer match if ( !baseSurf || baseSurf->id != surf->surfaceId ) { // find the surface with the correct id if ( staticModel->FindSurfaceWithId( surf->surfaceId, surf->surfaceNum ) ) { baseSurf = staticModel->Surface( surf->surfaceNum ); } else { // the surface with this id no longer exists FreeSurface( surf ); materials[k]->surfaces.RemoveIndex( i ); i--; continue; } } // copy indexes; for ( j = 0; j < surf->numIndexes; j++ ) { newTri->indexes[numIndexes + j] = numVerts + surf->indexes[j]; } numIndexes += surf->numIndexes; // copy vertices for ( j = 0; j < surf->numVerts; j++ ) { overlayVertex_t *overlayVert = &surf->verts[j]; newTri->verts[numVerts].st[0] = overlayVert->st[0]; newTri->verts[numVerts].st[1] = overlayVert->st[1]; if ( overlayVert->vertexNum >= baseSurf->geometry->numVerts ) { // This can happen when playing a demofile and a model has been changed since it was recorded, so just issue a warning and go on. common->Warning( "idRenderModelOverlay::AddOverlaySurfacesToModel: overlay vertex out of range. Model has probably changed since generating the overlay." ); FreeSurface( surf ); materials[k]->surfaces.RemoveIndex( i ); staticModel->DeleteSurfaceWithId( newSurf->id ); return; } newTri->verts[numVerts].xyz = baseSurf->geometry->verts[overlayVert->vertexNum].xyz; numVerts++; } } newTri->numVerts = numVerts; newTri->numIndexes = numIndexes; R_BoundTriSurf( newTri ); staticModel->overlaysAdded++; // so we don't create an overlay on an overlay surface } }
Button :: ~Button() { FreeSurface(upSurface); FreeSurface(downSurface); FreeSurface(overSurface); }
void Surface::Reset(void) { FreeSurface(*this); surface = NULL; /* hard set: for ref copy */ }
int main(int argc, char **argv) { RANDOM_Initialize(); uint start = 0; uint end = 0; printf(" STARTING FRAME : "); fflush(stdin); scanf("%d", &start); printf(" ENDING FRAME : "); fflush(stdin); scanf("%d", &end); fflush(stdin); if (start == end || start > end) return 0; SURFACE* marble_d = NULL; SURFACE* marble_n = NULL; SURFACE* marble_s = NULL; ImportBMPFile("data/marble_d.bmp", &marble_d); ImportBMPFile("data/marble_n.bmp", &marble_n); ImportBMPFile("data/marble_s.bmp", &marble_s); printf("\n"); printf("INITIALIZING SCENE\n\n"); VEC4 CameraPosition = VEC4(1.2f, 0.8f, -2.4f, 1.0); MainCamera = new Camera; MainCamera->position = CameraPosition; MainCamera->dimensions = VEC2(ViewWidth, ViewWidth * 0.75f); MainCamera->focalLength = 16.0f; MainCamera->sampleSize = Samples; Entity* diamond1 = new Entity; Entities.add(diamond1); diamond1->initialize(); ImportOBJFile("data/topDiamond.obj", &diamond1->mesh); diamond1->material->specular = 0.4f; diamond1->material->overlay = COLOR(0.05f, 0.4f, 1.0f, 1.0); diamond1->material->reflection = 1.0f; diamond1->material->refIndex = 1.333f; Entity* diamond2 = new Entity; Entities.add(diamond2); diamond2->initialize(); ImportOBJFile("data/bottomDiamond.obj", &diamond2->mesh); diamond2->material->specular = 0.4f; diamond2->material->overlay = COLOR(0.2f, 1.0f, 0.9f, 1.0); diamond2->material->reflection = 1.0f; diamond2->material->refIndex = 1.333f; Entity* background = new Entity; Entities.add(background); background->initialize(); ImportOBJFile("data/box.obj", &background->mesh); background->material->overlay = COLOR(0.7f, 1.0f, 0.9f, 1.0); background->material->reflection = 1.0f; background->material->specular = 0.4f; Light* light1 = new Light(VEC4(0.8f, 0.2f, -2.4f, 0.0f), VEC3(0.001f, 0.04f, 0.12f), COLOR(1.0f, 1.0f, 1.0f, 1.0f)); Lights.add(light1); light1->shadowIntensity = 0.8f; printf("START RENDERING\n\n"); SURFACE* render = NULL; CreateSurface(&render, "", Width, Height, PIXELFORMAT_RGB); char* filename = new char[128]; uint t = (uint)time(0); for (uint i = start; i < end; i++) { printf("RENDERING FRAME : %03d ", i); ClearBitmap(render, 0x333333); memset(filename, 0, sizeof(char) * 128); sprintf(filename, "RaySequence04_720p/RayTrace.%03d.bmp", i); Theta = float(i) * TurnSpeed; MainCamera->position = VEC4( (CameraPosition.x * cos(Theta)) - (CameraPosition.z * sin(Theta)), CameraPosition.y, (CameraPosition.x * sin(Theta)) + (CameraPosition.z * cos(Theta)), 1.0f); #if 0 light1->position = VEC4( (light1->position.x * cos(-Theta)) - (light1->position.z * sin(-Theta)), light1->position.y, (light1->position.x * sin(-Theta)) + (light1->position.z * cos(-Theta)), 1.0f); #endif //MainCamera->castRays(render); MainCamera->castRays(render); ExportBMPFile(filename, render); printf("DONE\n"); } delete [] filename; FreeSurface(&render); printf("\n"); //printf("FINISHED RENDERING\n\n"); return 0; }
void Surface::Set(SDL_Surface* sf) { FreeSurface(*this); surface = sf; }
/* ===================== idRenderModelOverlay::CreateOverlay This projects on both front and back sides to avoid seams The material should be clamped, because entire triangles are added, some of which may extend well past the 0.0 to 1.0 texture range ===================== */ void idRenderModelOverlay::CreateOverlay( const idRenderModel *model, const idPlane localTextureAxis[2], const idMaterial *mtr ) { int i, maxVerts, maxIndexes, surfNum; idRenderModelOverlay *overlay = NULL; // count up the maximum possible vertices and indexes per surface maxVerts = 0; maxIndexes = 0; for ( surfNum = 0; surfNum < model->NumSurfaces(); surfNum++ ) { const modelSurface_t *surf = model->Surface( surfNum ); if ( surf->geometry->numVerts > maxVerts ) { maxVerts = surf->geometry->numVerts; } if ( surf->geometry->numIndexes > maxIndexes ) { maxIndexes = surf->geometry->numIndexes; } } // make temporary buffers for the building process overlayVertex_t *overlayVerts = (overlayVertex_t *)_alloca( maxVerts * sizeof( *overlayVerts ) ); glIndex_t *overlayIndexes = (glIndex_t *)_alloca16( maxIndexes * sizeof( *overlayIndexes ) ); // pull out the triangles we need from the base surfaces for ( surfNum = 0; surfNum < model->NumBaseSurfaces(); surfNum++ ) { const modelSurface_t *surf = model->Surface( surfNum ); float d; if ( !surf->geometry || !surf->shader ) { continue; } // some surfaces can explicitly disallow overlays if ( !surf->shader->AllowOverlays() ) { continue; } const srfTriangles_t *stri = surf->geometry; // try to cull the whole surface along the first texture axis d = stri->bounds.PlaneDistance( localTextureAxis[0] ); if ( d < 0.0f || d > 1.0f ) { continue; } // try to cull the whole surface along the second texture axis d = stri->bounds.PlaneDistance( localTextureAxis[1] ); if ( d < 0.0f || d > 1.0f ) { continue; } byte *cullBits = (byte *)_alloca16( stri->numVerts * sizeof( cullBits[0] ) ); idVec2 *texCoords = (idVec2 *)_alloca16( stri->numVerts * sizeof( texCoords[0] ) ); SIMDProcessor->OverlayPointCull( cullBits, texCoords, localTextureAxis, stri->verts, stri->numVerts ); glIndex_t *vertexRemap = (glIndex_t *)_alloca16( sizeof( vertexRemap[0] ) * stri->numVerts ); SIMDProcessor->Memset( vertexRemap, -1, sizeof( vertexRemap[0] ) * stri->numVerts ); // find triangles that need the overlay int numVerts = 0; int numIndexes = 0; int triNum = 0; for ( int index = 0; index < stri->numIndexes; index += 3, triNum++ ) { int v1 = stri->indexes[index+0]; int v2 = stri->indexes[index+1]; int v3 = stri->indexes[index+2]; // skip triangles completely off one side if ( cullBits[v1] & cullBits[v2] & cullBits[v3] ) { continue; } // we could do more precise triangle culling, like the light interaction does, if desired // keep this triangle for ( int vnum = 0; vnum < 3; vnum++ ) { int ind = stri->indexes[index+vnum]; if ( vertexRemap[ind] == (glIndex_t)-1 ) { vertexRemap[ind] = numVerts; overlayVerts[numVerts].vertexNum = ind; overlayVerts[numVerts].st[0] = texCoords[ind][0]; overlayVerts[numVerts].st[1] = texCoords[ind][1]; numVerts++; } overlayIndexes[numIndexes++] = vertexRemap[ind]; } } if ( !numIndexes ) { continue; } overlaySurface_t *s = (overlaySurface_t *) Mem_Alloc( sizeof( overlaySurface_t ) ); s->surfaceNum = surfNum; s->surfaceId = surf->id; s->verts = (overlayVertex_t *)Mem_Alloc( numVerts * sizeof( s->verts[0] ) ); memcpy( s->verts, overlayVerts, numVerts * sizeof( s->verts[0] ) ); s->numVerts = numVerts; s->indexes = (glIndex_t *)Mem_Alloc( numIndexes * sizeof( s->indexes[0] ) ); memcpy( s->indexes, overlayIndexes, numIndexes * sizeof( s->indexes[0] ) ); s->numIndexes = numIndexes; for ( i = 0; i < materials.Num(); i++ ) { if ( materials[i]->material == mtr ) { break; } } if ( i < materials.Num() ) { materials[i]->surfaces.Append( s ); } else { overlayMaterial_t *mat = new overlayMaterial_t; mat->material = mtr; mat->surfaces.Append( s ); materials.Append( mat ); } } // remove the oldest overlay surfaces if there are too many per material for ( i = 0; i < materials.Num(); i++ ) { while( materials[i]->surfaces.Num() > MAX_OVERLAY_SURFACES ) { FreeSurface( materials[i]->surfaces[0] ); materials[i]->surfaces.RemoveIndex( 0 ); } } }
/* ================== LinkConvexFaces Determines the contents of the leaf and creates the final list of original faces that have some fragment inside this leaf ================== */ void LinkConvexFaces (surface_t *planelist, node_t *leafnode) { face_t *f, *next; surface_t *surf, *pnext; int i, count; leafnode->faces = NULL; leafnode->contents = 0; leafnode->planenum = -1; count = 0; for ( surf = planelist ; surf ; surf = surf->next) { for (f = surf->faces ; f ; f=f->next) { count++; if (!leafnode->contents) leafnode->contents = f->contents[0]; else if (leafnode->contents != f->contents[0]) Error ("LinkConvexFaces: Mixed face contents in leafnode"); } } if (!leafnode->contents) leafnode->contents = CONTENTS_SOLID; switch (leafnode->contents) { case CONTENTS_EMPTY: c_empty++; break; case CONTENTS_SOLID: c_solid++; break; case CONTENTS_WATER: case CONTENTS_SLIME: case CONTENTS_LAVA: case CONTENTS_SKY: c_water++; break; default: Error ("LinkConvexFaces: bad contents number"); } // // write the list of faces, and free the originals // leaffaces += count; leafnode->markfaces = malloc(sizeof(face_t *)*(count+1)); i = 0; for ( surf = planelist ; surf ; surf = pnext) { pnext = surf->next; for (f = surf->faces ; f ; f=next) { next = f->next; leafnode->markfaces[i] = f->original; i++; FreeFace (f); } FreeSurface (surf); } leafnode->markfaces[i] = NULL; // sentinal }