// Our GL Specific Initializations. Returns true On Success, false On Fail. bool init(void) { char *chr = (char*)glGetString(GL_VERSION); printf("%s\n", chr); free(chr); // 在此处初始化绘制场景 glClearColor(0.2f, 0.2f, 0.2f, 0.5f); // 清屏为黑色 glClearDepth(1.0f); // 设置深度缓存 glDepthFunc(GL_LEQUAL); // 选择深度测试方式 glEnable(GL_DEPTH_TEST); // 开启深度测试 glShadeModel(GL_SMOOTH); // 阴暗处理采用平滑方式 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // 最精细的透视计算 BuildTexture((char*)"Data/Reflection.bmp", pyramidTex); int ret; ret = ConeCreate(&sCone); assert(ret != 0); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); return TRUE; }
// 载入3ds文件 BOOL C3DSModel::Load(char *strFileName) { char strMessage[128] = {0}; tChunk chunk = {0}; // 打开文件 m_FilePtr = fopen(strFileName,"rb"); // 如果文件打开失败 if (!m_FilePtr) { sprintf(strMessage, "3DS文件 %s 不存在!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } // 读取3ds文件的第一个Chunk ReadChunk(&chunk); // 检查是否是3ds文件 if (chunk.ID != PRIMARY) { sprintf(strMessage, "读取文件 %s 失败!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); fclose(m_FilePtr); return false; } // 开始读取3ds文件 ReadPrimary(chunk.length-6); // 计算每个顶点的法线量 ComputeNormals(); // 关闭打开的文件 fclose(m_FilePtr); m_FilePtr = NULL; // 对有纹理的材质载入该纹理 for (int i=0; i<m_3DModel.numOfMaterials; i++) { if (m_3DModel.pMaterials[i].isTexMat) { if (!BuildTexture(m_3DModel.pMaterials[i].mapName.string, m_3DModel.pMaterials[i].texureId)) { // 纹理载入失败 sprintf(strMessage, "3DS纹理文件载入失败: %s !", m_3DModel.pMaterials[i].mapName.string); MessageBox(NULL, strMessage, "Error", MB_OK); } } } return true; }
RenderTexture::RenderTexture(ID3D10Device* device, UINT width, UINT height, DXGI_FORMAT colorFormat) :m_device(device), m_width(width), m_height(height), m_format(colorFormat), m_srv(0),m_rtv(0),m_tex(0) { BuildTexture(); m_vp.Width = m_width; m_vp.Height = m_height; m_vp.MaxDepth = 1; m_vp.MinDepth = 0; m_vp.TopLeftX = 0; m_vp.TopLeftY = 0; }
SkyBox::SkyBox(char const *texturedir) { // Set up the texture glEnable(GL_TEXTURE_2D); BuildTexture("./data/skybox1/FT.png", &textures[0], GL_CLAMP, false); glBindTexture(GL_TEXTURE_2D, textures[0]); BuildTexture("./data/skybox1/LT.png", &textures[1], GL_CLAMP, false); glBindTexture(GL_TEXTURE_2D, textures[1]); BuildTexture("./data/skybox1/BK.png", &textures[2], GL_CLAMP, false); glBindTexture(GL_TEXTURE_2D, textures[2]); BuildTexture("./data/skybox1/RT.png", &textures[3], GL_CLAMP, false); glBindTexture(GL_TEXTURE_2D, textures[3]); BuildTexture("./data/skybox1/UP.png", &textures[4], GL_CLAMP, false); glBindTexture(GL_TEXTURE_2D, textures[4]); BuildTexture("./data/skybox1/DN.png", &textures[5], GL_CLAMP, false); glBindTexture(GL_TEXTURE_2D, textures[5]); }
Model::Model(const char *textureName) { loaded = false; BuildTexture(textureName, &texture, GL_CLAMP_TO_EDGE, true); glBindTexture(GL_TEXTURE_2D, texture); }
int SceneBuilder::BuildObjectLOD(const Fmatrix& parent, CEditableObject* E, int sector_num) { if (!E->m_objectFlags.is(CEditableObject::eoUsingLOD)) return -1; xr_string lod_name = E->GetLODTextureName(); b_material mtl; mtl.surfidx = (u16)BuildTexture (LEVEL_LODS_TEX_NAME); mtl.shader = (u16)BuildShader (E->GetLODShaderName()); mtl.sector = (u16)sector_num; mtl.shader_xrlc = -1; if ((u16(-1)==mtl.surfidx)||(u16(-1)==mtl.shader)) return -2; int mtl_idx = FindInMaterials(&mtl); if (mtl_idx<0){ l_materials.push_back(mtl); mtl_idx = l_materials.size()-1; } l_lods.push_back(e_b_lod()); e_b_lod& b = l_lods.back(); Fvector p[4]; Fvector2 t[4]; for (int frame=0; frame<LOD_SAMPLE_COUNT; frame++){ E->GetLODFrame(frame,p,t,&parent); for (int k=0; k<4; k++){ b.lod.faces[frame].v[k].set(p[k]); b.lod.faces[frame].t[k].set(t[k]); } } b.lod.dwMaterial= mtl_idx; b.lod_name = lod_name.c_str(); xr_string l_name= lod_name.c_str(); u32 w,h; int age; if (!ImageLib.LoadTextureData(l_name.c_str(),b.data,w,h,&age)){ Msg("!Can't find LOD texture: '%s'",l_name.c_str()); return -2; } /* if (age!=E->Version()){ Msg("!Invalid LOD texture version: '%s'",l_name.c_str()); return -2; }*/ l_name += "_nm"; if (!ImageLib.LoadTextureData(l_name.c_str(),b.ndata,w,h,&age)){ Msg("!Can't find LOD texture: '%s'",l_name.c_str()); return -2; } /* if (age!=E->Version()){ Msg("!Invalid LOD texture version: '%s'",l_name.c_str()); return -2; }*/ // b.data /* // make lod Fbox bb = E->GetBox(); E->m_Flags.set (CEditableObject::eoUsingLOD,FALSE); object_for_render = E; ImageLib.CreateLODTexture (bb, b.data, LOD_IMAGE_SIZE,LOD_IMAGE_SIZE,LOD_SAMPLE_COUNT); E->m_Flags.set (CEditableObject::eoUsingLOD,TRUE); // build lod normals b.ndata.resize (LOD_IMAGE_SIZE*LOD_IMAGE_SIZE*LOD_SAMPLE_COUNT); U8Vec hemi_temp (LOD_IMAGE_SIZE*LOD_IMAGE_SIZE*LOD_SAMPLE_COUNT,0); Fvector o_center,o_size; Fmatrix M,Mi; bb.getradius (o_size); bb.getcenter (o_center); SPBItem* PB = UI->ProgressStart(LOD_SAMPLE_COUNT*LOD_IMAGE_SIZE,lod_name.c_str()); float dW = _max(o_size.x,o_size.z)/(LOD_IMAGE_SIZE/2); float dH = o_size.y/(LOD_IMAGE_SIZE/2); float dR = bb.getradius(); float d2R = dR*2.f; XRC.ray_options (CDB::OPT_CULL); ETOOLS::ray_options (CDB::OPT_CULL); float tN=0.f,tH=0.f,tT=0.f,tR=0.f; float LOD_CALC_SAMPLES = Scene->m_LevelOp.m_LOD_Quality; s32 LOD_CALC_SAMPLES_LIM= LOD_CALC_SAMPLES/2; // preload textures for (SurfaceIt surf_it=E->Surfaces().begin(); surf_it!=E->Surfaces().end(); surf_it++){ CSurface* surf = *surf_it; Shader_xrLC* c_sh = EDevice.ShaderXRLC.Get(surf->_ShaderXRLCName()); if (!c_sh->flags.bRendering) continue; if (0==surf->m_ImageData)surf->CreateImageData(); } // preload CF Model for (EditMeshIt mesh_it=E->Meshes().begin(); mesh_it!=E->Meshes().end(); mesh_it++) (*mesh_it)->GenerateCFModel(); // calculate for (u32 sample_idx=0; sample_idx<LOD_SAMPLE_COUNT; sample_idx++){ float angle = sample_idx*(PI_MUL_2/LOD_SAMPLE_COUNT); M.setXYZ (0,angle,0); M.translate_over (o_center); Mi.invert (M); for (s32 iH=0; iH<LOD_IMAGE_SIZE; iH++){ PB->Inc (); float Y = (iH-(LOD_IMAGE_SIZE-1)/2)*dH; for (s32 iW=0; iW<LOD_IMAGE_SIZE; iW++){ float X = (iW-LOD_IMAGE_SIZE/2)*dW; u32 pixel = (LOD_IMAGE_SIZE-iH-1)*LOD_SAMPLE_COUNT*LOD_IMAGE_SIZE+LOD_IMAGE_SIZE*sample_idx+iW; u32& tgt_n = b.ndata[pixel]; u8& tgt_h = hemi_temp[pixel]; u8 src_a = color_get_A (b.data[pixel]); if (0==src_a) continue; FvectorVec n_vec; Fvector4Vec sample_pt_vec;//(iFloor(LOD_CALC_SAMPLES*LOD_CALC_SAMPLES)); //. FvectorVec c_vec; Fvector start; CTimer TT,TT1; TT.Start(); SPickQuery PQ; for (s32 iiH=-LOD_CALC_SAMPLES_LIM; iiH<=LOD_CALC_SAMPLES_LIM; iiH++){ float dY = iiH*(dH/LOD_CALC_SAMPLES); for (s32 iiW=-LOD_CALC_SAMPLES_LIM; iiW<=LOD_CALC_SAMPLES_LIM; iiW++){ float dX= iiW*(dW/LOD_CALC_SAMPLES); start.set (X+dX,Y+dY,0); M.transform_tiny(start); start.mad (M.k,-dR); PQ.prepare_rq (start,M.k,d2R,CDB::OPT_CULL); E->RayQuery (PQ); if (PQ.r_count()){ PQ.r_sort(); Fvector N = {0,0,0}; for (s32 k=PQ.r_count()-1; k>=0; k--){ SPickQuery::SResult* R = PQ.r_begin()+k; u32 uA; if (!GetPointColor(R,uA)) continue; float fA = float(uA)/255.f; if (uA){ Fvector pt; pt.mad(PQ.m_Start,PQ.m_Direction,R->range-EPS_L); Fvector4 ptt; ptt.set(pt.x,pt.y,pt.z,fA); sample_pt_vec.push_back(ptt); } // normal Fvector Nn; Nn.mknormal (R->verts[0],R->verts[1],R->verts[2]); Nn.mul (fA); N.mul (1.f-fA); N.add (Nn); } float n_mag = N.magnitude(); if (!fis_zero(n_mag,EPS)) n_vec.push_back (N.div(n_mag)); } } } tN+=TT.Stop(); TT.Start(); // light points float res_transp = 0.f; for (Fvector4It pt_it=sample_pt_vec.begin(); pt_it!=sample_pt_vec.end(); pt_it++){ float avg_transp = 0.f; for (BLIt it=simple_hemi.begin(); it!=simple_hemi.end(); it++){ TT1.Start(); Fvector start; start.mad (Fvector().set(pt_it->x,pt_it->y,pt_it->z),it->light.direction,-dR); PQ.prepare_rq (start,it->light.direction,dR,CDB::OPT_CULL); E->RayQuery (PQ); tR+=TT1.Stop(); float ray_transp= 1.f; if (PQ.r_count()){ for (s32 k=0; k<PQ.r_count(); k++){ u32 a; TT1.Start(); if (!GetPointColor(PQ.r_begin()+k,a)) continue; tT+=TT1.Stop(); ray_transp *= (1.f - float(a)/255.f); if (fis_zero(ray_transp,EPS_L)) break; } } avg_transp += ray_transp; } avg_transp /= simple_hemi.size(); res_transp = res_transp*(1.f-pt_it->w)+avg_transp*pt_it->w; } tH+=TT.Stop(); tgt_h = iFloor (res_transp*255.f); Fvector N={0,0,0}; if (!n_vec.empty()){ for (FvectorIt it=n_vec.begin(); it!=n_vec.end(); it++) N.add(*it); N.div (n_vec.size()); N.normalize_safe(); Mi.transform_dir(N); } N.mul (0.5f); N.add (0.5f); N.mul (255.f); tgt_n = color_rgba(iFloor(N.x),iFloor(N.y),iFloor(N.z),src_a); } } } Msg("Normal: %3.2fsec, Hemi: %3.2f, PC: %3.2f, RP: %3.2f",tN,tH,tT,tR); ImageLib.ApplyBorders (b.ndata, LOD_IMAGE_SIZE*LOD_SAMPLE_COUNT,LOD_IMAGE_SIZE); // fill alpha to N-channel for (int px_idx=0; px_idx<int(b.ndata.size()); px_idx++) b.ndata[px_idx] = subst_alpha(b.ndata[px_idx],hemi_temp[px_idx]); UI->ProgressEnd(PB); */ return l_lods.size()-1; }
Texture3D::Texture3D(unsigned int width, unsigned int height, unsigned int depth, const TextureFiltering &filtering, const TextureFormat &format) : _Width(width), _Height(height), _Depth(depth), _Filtering (filtering), _Format(format), ID(0) { glGenTextures (1, &ID); BuildTexture(width,height,depth,filtering,format); }