• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By kanageddaamen
      Hello all,
      I am currently working on a game engine for use with my game development that I would like to be as flexible as possible.  As such the exact requirements for how things should work can't be nailed down to a specific implementation and I am looking for, at least now, a default good average case scenario design.
      Here is what I have implemented:
      Deferred rendering using OpenGL Arbitrary number of lights and shadow mapping Each rendered object, as defined by a set of geometry, textures, animation data, and a model matrix is rendered with its own draw call Skeletal animations implemented on the GPU.   Model matrix transformation implemented on the GPU Frustum and octree culling for optimization Here are my questions and concerns:
      Doing the skeletal animation on the GPU, currently, requires doing the skinning for each object multiple times per frame: once for the initial geometry rendering and once for the shadow map rendering for each light for which it is not culled.  This seems very inefficient.  Is there a way to do skeletal animation on the GPU only once across these render calls? Without doing the model matrix transformation on the CPU, I fail to see how I can easily batch objects with the same textures and shaders in a single draw call without passing a ton of matrix data to the GPU (an array of model matrices then an index for each vertex into that array for transformation purposes?) If I do the matrix transformations on the CPU, It seems I can't really do the skinning on the GPU as the pre-transformed vertexes will wreck havoc with the calculations, so this seems not viable unless I am missing something Overall it seems like simplest solution is to just do all of the vertex manipulation on the CPU and pass the pre-transformed data to the GPU, using vertex shaders that do basically nothing.  This doesn't seem the most efficient use of the graphics hardware, but could potentially reduce the number of draw calls needed.

      Really, I am looking for some advice on how to proceed with this, how something like this is typically handled.  Are the multiple draw calls and skinning calculations not a huge deal?  I would LIKE to save as much of the CPU's time per frame so it can be tasked with other things, as to keep CPU resources open to the implementation of the engine.  However, that becomes a moot point if the GPU becomes a bottleneck.
    • By DiligentDev
      I would like to introduce Diligent Engine, a project that I've been recently working on. Diligent Engine is a light-weight cross-platform abstraction layer between the application and the platform-specific graphics API. Its main goal is to take advantages of the next-generation APIs such as Direct3D12 and Vulkan, but at the same time provide support for older platforms via Direct3D11, OpenGL and OpenGLES. Diligent Engine exposes common front-end for all supported platforms and provides interoperability with underlying native API. Shader source code converter allows shaders authored in HLSL to be translated to GLSL and used on all platforms. Diligent Engine supports integration with Unity and is designed to be used as a graphics subsystem in a standalone game engine, Unity native plugin or any other 3D application. It is distributed under Apache 2.0 license and is free to use. Full source code is available for download on GitHub.
      True cross-platform Exact same client code for all supported platforms and rendering backends No #if defined(_WIN32) ... #elif defined(LINUX) ... #elif defined(ANDROID) ... No #if defined(D3D11) ... #elif defined(D3D12) ... #elif defined(OPENGL) ... Exact same HLSL shaders run on all platforms and all backends Modular design Components are clearly separated logically and physically and can be used as needed Only take what you need for your project (do not want to keep samples and tutorials in your codebase? Simply remove Samples submodule. Only need core functionality? Use only Core submodule) No 15000 lines-of-code files Clear object-based interface No global states Key graphics features: Automatic shader resource binding designed to leverage the next-generation rendering APIs Multithreaded command buffer generation 50,000 draw calls at 300 fps with D3D12 backend Descriptor, memory and resource state management Modern c++ features to make code fast and reliable The following platforms and low-level APIs are currently supported:
      Windows Desktop: Direct3D11, Direct3D12, OpenGL Universal Windows: Direct3D11, Direct3D12 Linux: OpenGL Android: OpenGLES MacOS: OpenGL iOS: OpenGLES API Basics
      The engine can perform initialization of the API or attach to already existing D3D11/D3D12 device or OpenGL/GLES context. For instance, the following code shows how the engine can be initialized in D3D12 mode:
      #include "RenderDeviceFactoryD3D12.h" using namespace Diligent; // ...  GetEngineFactoryD3D12Type GetEngineFactoryD3D12 = nullptr; // Load the dll and import GetEngineFactoryD3D12() function LoadGraphicsEngineD3D12(GetEngineFactoryD3D12); auto *pFactoryD3D11 = GetEngineFactoryD3D12(); EngineD3D12Attribs EngD3D12Attribs; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[0] = 1024; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[1] = 32; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[2] = 16; EngD3D12Attribs.CPUDescriptorHeapAllocationSize[3] = 16; EngD3D12Attribs.NumCommandsToFlushCmdList = 64; RefCntAutoPtr<IRenderDevice> pRenderDevice; RefCntAutoPtr<IDeviceContext> pImmediateContext; SwapChainDesc SwapChainDesc; RefCntAutoPtr<ISwapChain> pSwapChain; pFactoryD3D11->CreateDeviceAndContextsD3D12( EngD3D12Attribs, &pRenderDevice, &pImmediateContext, 0 ); pFactoryD3D11->CreateSwapChainD3D12( pRenderDevice, pImmediateContext, SwapChainDesc, hWnd, &pSwapChain ); Creating Resources
      Device resources are created by the render device. The two main resource types are buffers, which represent linear memory, and textures, which use memory layouts optimized for fast filtering. To create a buffer, you need to populate BufferDesc structure and call IRenderDevice::CreateBuffer(). The following code creates a uniform (constant) buffer:
      BufferDesc BuffDesc; BufferDesc.Name = "Uniform buffer"; BuffDesc.BindFlags = BIND_UNIFORM_BUFFER; BuffDesc.Usage = USAGE_DYNAMIC; BuffDesc.uiSizeInBytes = sizeof(ShaderConstants); BuffDesc.CPUAccessFlags = CPU_ACCESS_WRITE; m_pDevice->CreateBuffer( BuffDesc, BufferData(), &m_pConstantBuffer ); Similar, to create a texture, populate TextureDesc structure and call IRenderDevice::CreateTexture() as in the following example:
      TextureDesc TexDesc; TexDesc.Name = "My texture 2D"; TexDesc.Type = TEXTURE_TYPE_2D; TexDesc.Width = 1024; TexDesc.Height = 1024; TexDesc.Format = TEX_FORMAT_RGBA8_UNORM; TexDesc.Usage = USAGE_DEFAULT; TexDesc.BindFlags = BIND_SHADER_RESOURCE | BIND_RENDER_TARGET | BIND_UNORDERED_ACCESS; TexDesc.Name = "Sample 2D Texture"; m_pRenderDevice->CreateTexture( TexDesc, TextureData(), &m_pTestTex ); Initializing Pipeline State
      Diligent Engine follows Direct3D12 style to configure the graphics/compute pipeline. One big Pipelines State Object (PSO) encompasses all required states (all shader stages, input layout description, depth stencil, rasterizer and blend state descriptions etc.)
      Creating Shaders
      To create a shader, populate ShaderCreationAttribs structure. An important member is ShaderCreationAttribs::SourceLanguage. The following are valid values for this member:
      SHADER_SOURCE_LANGUAGE_DEFAULT  - The shader source format matches the underlying graphics API: HLSL for D3D11 or D3D12 mode, and GLSL for OpenGL and OpenGLES modes. SHADER_SOURCE_LANGUAGE_HLSL  - The shader source is in HLSL. For OpenGL and OpenGLES modes, the source code will be converted to GLSL. See shader converter for details. SHADER_SOURCE_LANGUAGE_GLSL  - The shader source is in GLSL. There is currently no GLSL to HLSL converter. To allow grouping of resources based on the frequency of expected change, Diligent Engine introduces classification of shader variables:
      Static variables (SHADER_VARIABLE_TYPE_STATIC) are variables that are expected to be set only once. They may not be changed once a resource is bound to the variable. Such variables are intended to hold global constants such as camera attributes or global light attributes constant buffers. Mutable variables (SHADER_VARIABLE_TYPE_MUTABLE) define resources that are expected to change on a per-material frequency. Examples may include diffuse textures, normal maps etc. Dynamic variables (SHADER_VARIABLE_TYPE_DYNAMIC) are expected to change frequently and randomly. This post describes the resource binding model in Diligent Engine.
      The following is an example of shader initialization:
      ShaderCreationAttribs Attrs; Attrs.Desc.Name = "MyPixelShader"; Attrs.FilePath = "MyShaderFile.fx"; Attrs.SearchDirectories = "shaders;shaders\\inc;"; Attrs.EntryPoint = "MyPixelShader"; Attrs.Desc.ShaderType = SHADER_TYPE_PIXEL; Attrs.SourceLanguage = SHADER_SOURCE_LANGUAGE_HLSL; BasicShaderSourceStreamFactory BasicSSSFactory(Attrs.SearchDirectories); Attrs.pShaderSourceStreamFactory = &BasicSSSFactory; ShaderVariableDesc ShaderVars[] =  {     {"g_StaticTexture", SHADER_VARIABLE_TYPE_STATIC},     {"g_MutableTexture", SHADER_VARIABLE_TYPE_MUTABLE},     {"g_DynamicTexture", SHADER_VARIABLE_TYPE_DYNAMIC} }; Attrs.Desc.VariableDesc = ShaderVars; Attrs.Desc.NumVariables = _countof(ShaderVars); Attrs.Desc.DefaultVariableType = SHADER_VARIABLE_TYPE_STATIC; StaticSamplerDesc StaticSampler; StaticSampler.Desc.MinFilter = FILTER_TYPE_LINEAR; StaticSampler.Desc.MagFilter = FILTER_TYPE_LINEAR; StaticSampler.Desc.MipFilter = FILTER_TYPE_LINEAR; StaticSampler.TextureName = "g_MutableTexture"; Attrs.Desc.NumStaticSamplers = 1; Attrs.Desc.StaticSamplers = &StaticSampler; ShaderMacroHelper Macros; Macros.AddShaderMacro("USE_SHADOWS", 1); Macros.AddShaderMacro("NUM_SHADOW_SAMPLES", 4); Macros.Finalize(); Attrs.Macros = Macros; RefCntAutoPtr<IShader> pShader; m_pDevice->CreateShader( Attrs, &pShader ); Creating the Pipeline State Object
      To create a pipeline state object, define instance of PipelineStateDesc structure. The structure defines the pipeline specifics such as if the pipeline is a compute pipeline, number and format of render targets as well as depth-stencil format:
      // This is a graphics pipeline PSODesc.IsComputePipeline = false; PSODesc.GraphicsPipeline.NumRenderTargets = 1; PSODesc.GraphicsPipeline.RTVFormats[0] = TEX_FORMAT_RGBA8_UNORM_SRGB; PSODesc.GraphicsPipeline.DSVFormat = TEX_FORMAT_D32_FLOAT; The structure also defines depth-stencil, rasterizer, blend state, input layout and other parameters. For instance, rasterizer state can be defined as in the code snippet below:
      // Init rasterizer state RasterizerStateDesc &RasterizerDesc = PSODesc.GraphicsPipeline.RasterizerDesc; RasterizerDesc.FillMode = FILL_MODE_SOLID; RasterizerDesc.CullMode = CULL_MODE_NONE; RasterizerDesc.FrontCounterClockwise = True; RasterizerDesc.ScissorEnable = True; //RSDesc.MultisampleEnable = false; // do not allow msaa (fonts would be degraded) RasterizerDesc.AntialiasedLineEnable = False; When all fields are populated, call IRenderDevice::CreatePipelineState() to create the PSO:
      m_pDev->CreatePipelineState(PSODesc, &m_pPSO); Binding Shader Resources
      Shader resource binding in Diligent Engine is based on grouping variables in 3 different groups (static, mutable and dynamic). Static variables are variables that are expected to be set only once. They may not be changed once a resource is bound to the variable. Such variables are intended to hold global constants such as camera attributes or global light attributes constant buffers. They are bound directly to the shader object:
      PixelShader->GetShaderVariable( "g_tex2DShadowMap" )->Set( pShadowMapSRV ); Mutable and dynamic variables are bound via a new object called Shader Resource Binding (SRB), which is created by the pipeline state:
      m_pPSO->CreateShaderResourceBinding(&m_pSRB); Dynamic and mutable resources are then bound through SRB object:
      m_pSRB->GetVariable(SHADER_TYPE_VERTEX, "tex2DDiffuse")->Set(pDiffuseTexSRV); m_pSRB->GetVariable(SHADER_TYPE_VERTEX, "cbRandomAttribs")->Set(pRandomAttrsCB); The difference between mutable and dynamic resources is that mutable ones can only be set once for every instance of a shader resource binding. Dynamic resources can be set multiple times. It is important to properly set the variable type as this may affect performance. Static variables are generally most efficient, followed by mutable. Dynamic variables are most expensive from performance point of view. This post explains shader resource binding in more details.
      Setting the Pipeline State and Invoking Draw Command
      Before any draw command can be invoked, all required vertex and index buffers as well as the pipeline state should be bound to the device context:
      // Clear render target const float zero[4] = {0, 0, 0, 0}; m_pContext->ClearRenderTarget(nullptr, zero); // Set vertex and index buffers IBuffer *buffer[] = {m_pVertexBuffer}; Uint32 offsets[] = {0}; Uint32 strides[] = {sizeof(MyVertex)}; m_pContext->SetVertexBuffers(0, 1, buffer, strides, offsets, SET_VERTEX_BUFFERS_FLAG_RESET); m_pContext->SetIndexBuffer(m_pIndexBuffer, 0); m_pContext->SetPipelineState(m_pPSO); Also, all shader resources must be committed to the device context:
      m_pContext->CommitShaderResources(m_pSRB, COMMIT_SHADER_RESOURCES_FLAG_TRANSITION_RESOURCES); When all required states and resources are bound, IDeviceContext::Draw() can be used to execute draw command or IDeviceContext::DispatchCompute() can be used to execute compute command. Note that for a draw command, graphics pipeline must be bound, and for dispatch command, compute pipeline must be bound. Draw() takes DrawAttribs structure as an argument. The structure members define all attributes required to perform the command (primitive topology, number of vertices or indices, if draw call is indexed or not, if draw call is instanced or not, if draw call is indirect or not, etc.). For example:
      DrawAttribs attrs; attrs.IsIndexed = true; attrs.IndexType = VT_UINT16; attrs.NumIndices = 36; attrs.Topology = PRIMITIVE_TOPOLOGY_TRIANGLE_LIST; pContext->Draw(attrs); Tutorials and Samples
      The GitHub repository contains a number of tutorials and sample applications that demonstrate the API usage.
      Tutorial 01 - Hello Triangle This tutorial shows how to render a simple triangle using Diligent Engine API.   Tutorial 02 - Cube This tutorial demonstrates how to render an actual 3D object, a cube. It shows how to load shaders from files, create and use vertex, index and uniform buffers.   Tutorial 03 - Texturing This tutorial demonstrates how to apply a texture to a 3D object. It shows how to load a texture from file, create shader resource binding object and how to sample a texture in the shader.   Tutorial 04 - Instancing This tutorial demonstrates how to use instancing to render multiple copies of one object using unique transformation matrix for every copy.   Tutorial 05 - Texture Array This tutorial demonstrates how to combine instancing with texture arrays to use unique texture for every instance.   Tutorial 06 - Multithreading This tutorial shows how to generate command lists in parallel from multiple threads.   Tutorial 07 - Geometry Shader This tutorial shows how to use geometry shader to render smooth wireframe.   Tutorial 08 - Tessellation This tutorial shows how to use hardware tessellation to implement simple adaptive terrain rendering algorithm.   Tutorial_09 - Quads This tutorial shows how to render multiple 2D quads, frequently swithcing textures and blend modes.
      AntTweakBar sample demonstrates how to use AntTweakBar library to create simple user interface.

      Atmospheric scattering sample is a more advanced example. It demonstrates how Diligent Engine can be used to implement various rendering tasks: loading textures from files, using complex shaders, rendering to textures, using compute shaders and unordered access views, etc. 

      The repository includes Asteroids performance benchmark based on this demo developed by Intel. It renders 50,000 unique textured asteroids and lets compare performance of D3D11 and D3D12 implementations. Every asteroid is a combination of one of 1000 unique meshes and one of 10 unique textures. 

      Integration with Unity
      Diligent Engine supports integration with Unity through Unity low-level native plugin interface. The engine relies on Native API Interoperability to attach to the graphics API initialized by Unity. After Diligent Engine device and context are created, they can be used us usual to create resources and issue rendering commands. GhostCubePlugin shows an example how Diligent Engine can be used to render a ghost cube only visible as a reflection in a mirror.

    • By Yxjmir
      I'm trying to load data from a .gltf file into a struct to use to load a .bin file. I don't think there is a problem with how the vertex positions are loaded, but with the indices. This is what I get when drawing with glDrawArrays(GL_LINES, ...):

      Also, using glDrawElements gives a similar result. Since it looks like its drawing triangles using the wrong vertices for each face, I'm assuming it needs an index buffer/element buffer. (I'm not sure why there is a line going through part of it, it doesn't look like it belongs to a side, re-exported it without texture coordinates checked, and its not there)
      I'm using jsoncpp to load the GLTF file, its format is based on JSON. Here is the gltf struct I'm using, and how I parse the file:
      #define GLTF_TARGET_ARRAY_BUFFER (34962) #define GLTF_TARGET_ELEMENT_ARRAY_BUFFER (34963) #define GLTF_COMPONENT_TYPE_BYTE (5120) #define GLTF_COMPONENT_TYPE_UNSIGNED_BYTE (5121) #define GLTF_COMPONENT_TYPE_SHORT (5122) #define GLTF_COMPONENT_TYPE_UNSIGNED_SHORT (5123) #define GLTF_COMPONENT_TYPE_INT (5124) #define GLTF_COMPONENT_TYPE_UNSIGNED_INT (5125) #define GLTF_COMPONENT_TYPE_FLOAT (5126) #define GLTF_COMPONENT_TYPE_DOUBLE (5127) #define GLTF_PARAMETER_TYPE_BYTE (5120) #define GLTF_PARAMETER_TYPE_UNSIGNED_BYTE (5121) #define GLTF_PARAMETER_TYPE_SHORT (5122) #define GLTF_PARAMETER_TYPE_UNSIGNED_SHORT (5123) #define GLTF_PARAMETER_TYPE_INT (5124) #define GLTF_PARAMETER_TYPE_UNSIGNED_INT (5125) #define GLTF_PARAMETER_TYPE_FLOAT (5126) #define GLTF_PARAMETER_TYPE_FLOAT_VEC2 (35664) #define GLTF_PARAMETER_TYPE_FLOAT_VEC3 (35665) #define GLTF_PARAMETER_TYPE_FLOAT_VEC4 (35666) struct GLTF { struct Accessor { USHORT bufferView; USHORT componentType; UINT count; vector<INT> max; vector<INT> min; string type; }; vector<Accessor> m_accessors; struct Asset { string copyright; string generator; string version; }m_asset; struct BufferView { UINT buffer; UINT byteLength; UINT byteOffset; UINT target; }; vector<BufferView> m_bufferViews; struct Buffer { UINT byteLength; string uri; }; vector<Buffer> m_buffers; vector<string> m_Images; struct Material { string name; string alphaMode; Vec4 baseColorFactor; UINT baseColorTexture; UINT normalTexture; float metallicFactor; }; vector<Material> m_materials; struct Meshes { string name; struct Primitive { vector<UINT> attributes_indices; UINT indices; UINT material; }; vector<Primitive> primitives; }; vector<Meshes> m_meshes; struct Nodes { int mesh; string name; Vec3 translation; }; vector<Nodes> m_nodes; struct Scenes { UINT index; string name; vector<UINT> nodes; }; vector<Scenes> m_scenes; vector<UINT> samplers; struct Textures { UINT sampler; UINT source; }; vector<Textures> m_textures; map<UINT, string> attributes_map; map<UINT, string> textures_map; }; GLTF m_gltf; // This is actually in the Mesh class bool Mesh::Load(string sFilename) { string sFileAsString; stringstream sStream; ifstream fin(sFilename); sStream << fin.rdbuf(); fin.close(); sFileAsString = sStream.str(); Json::Reader r; Json::Value root; if (!r.parse(sFileAsString, root)) { string errors = r.getFormatedErrorMessages(); if (errors != "") { // TODO: Log errors return false; } } if (root.isNull()) return false; Json::Value object; Json::Value value; // Load Accessors array, these are referenced by attributes with their index value object = root.get("accessors", Json::Value()); // store object with key "accessors", if not found it will default to Json::Value() if (!object.isNull()) { for (Json::ValueIterator it = object.begin(); it != object.end(); it++) { GLTF::Accessor accessor; value = (*it).get("bufferView", Json::Value()); if (!value.isNull()) accessor.bufferView = value.asUINT(); else return false; value = (*it).get("componentType", Json::Value()); if (!value.isNull()) accessor.componentType = value.asUINT(); else return false; value = (*it).get("count", Json::Value()); if (!value.isNull()) accessor.count = value.asUINT(); else return false; value = (*it).get("type", Json::Value()); if (!value.isNull()) accessor.type = value.asString(); else return false; m_gltf.accessors.push_back(accessor); } } else return false; object = root.get("bufferViews", Json::Value()); if(!object.isNull()) { for (Json::ValueIterator it = object.begin(); it != object.end(); it++) { GLTF::BufferView bufferView; value = (*it).get("buffer", Json::Value()); if(!value.isNull()) bufferView.buffer = value.asUInt(); else return false; value = (*it).get("byteLength", Json::Value()); if(!value.isNull()) bufferView.byteLength = value.asUInt(); else return false; value = (*it).get("byteOffset", Json::Value()); if(!value.isNull()) bufferView.byteOffset = value.asUInt(); else return false; value = (*it).get("target", Json::Value()); if(!value.isNull()) bufferView.target = value.asUInt(); else return false; m_gltf.m_bufferViews.push_back(bufferView); } } else return false; object = root.get("buffers", Json::Value()); if(!object.isNull()) { for (Json::ValueIterator it = object.begin(); it != object.end(); it++) { GLTF::Buffer buffer; value = (*it).get("byteLength", Json::Value()); if(!value.isNull()) buffer.byteLength = value.asUInt(); else return false; // Store the filename of the .bin file value = (*it).get("uri", Json::Value()); if(!value.isNull()) buffer.uri = value.asString(); else return false; } } else return false; object = root.get("meshes", Json::Value()); if(!object.isNull()) { for(Json::ValueIterator it = object.begin(); it != object.end(); it++) { GLTF::Meshes mesh; value = (*it).get("primitives", Json::Value()); for(Json::ValueIterator value_it = value.begin(); value_it != value.end(); value_it++) { GLTF::Meshes::Primitive primitive; Json::Value attributes; attributes = (*value_it).get("attributes", Json::Value()); vector<string> memberNames = attributes.getMemberNames(); for(size_t i = 0; i < memberNames.size(); i++) { Json::Value member; member = attributes.get(memeberNames[i], Json::Value()); if(!member.isNull()) { primitive.attributes_indices.push_back(member.asUInt()); m_gltf.attributes_map[member.asUInt()] = memberNames[i]; // Each of these referes to an accessor by indice, so each indice should be unique, and they are when loading a cube } else return false; } // Indice of the accessor used for indices Json::Value indices; indices = (*value_it).get("indices", Json::Value()); primitive.indices = indices.asUInt(); mesh.primitives.push_back(primitive); } m_gltf.m_meshes.push_back(mesh); } } vector<float> vertexData; vector<USHORT> indiceData; int vertexBufferSizeTotal = 0; int elementBufferSizeTotal = 0; GLTF::Meshes mesh = m_gltf.m_meshes[0]; vector<GLTF::Meshes::Primitive> primitives = mesh.primitives; // trying to make the code easier to read for (size_t p = 0; p < primitive.size(); p++) { vector<UINT> attributes = primitives[p].attributes_indices; for(size_t a = 0; a < attributes.size(); a++) { GLTF::Accessor accessor = m_gltf.m_accessors[attributes[a]]; GLTF::BufferView bufferView = m_gltf.m_bufferViews[accessor.bufferView]; UINT target = bufferView.target; if(target == GLTF_TARGET_ARRAY_BUFFER) vertexBufferSizeTotal += bufferView.byteLength; } UINT indice = primitives[p].indices; GLTF::BufferView bufferView = m_gltf.m_bufferViews[indice]; UINT target = bufferView.target; if(target == GLTF_TARGET_ELEMENT_ARRAY_BUFFER) elementBufferSizeTotal += bufferView.byteLength; } // These have already been generated glBindVertexArray(g_pGame->m_VAO); glBindBuffer(GL_ARRAY_BUFFER, g_pGame->m_VBO); glBufferData(GL_ARRAY_BUFFER, vertexBufferSizeTotal, nullptr, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_pGame->m_EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBufferSizeTotal, nullptr, GL_STATIC_DRAW); int offset = 0; int offset_indice = 0; for (size_t p = 0; p < primitive.size(); p++) { vector<UINT> attributes = primitives[p].attributes_indices; int pos = sFilename.find_last_of('\\') + 1; string sFolder = sFilename.substr(0, pos); for (size_t a = 0; a < attributes.size(); a++) { LoadBufferView(sFolder, attributes[a], data, offset); } UINT indice = primitives[p].indices; GLTF::BufferView bufferView_indice = m_gltf.m_bufferViews[indice]; UINT target_indice = bufferView_indice.target; bool result = LoadBufferView(sFolder, indice, data, offset_indice); if(!result) return false; } return true; } bool Mesh::LoadBufferView(string sFolder, UINT a, vector<float> &vertexData, vector<float> &indiceData, int &offset_indice) { ifstream fin; GLTF::Accessor accessor = m_gltf.m_accessors[a]; GLTF::BufferView bufferView = m_gltf.m_bufferViews[accessor.bufferView]; GLTF::Buffer buffer = m_gltf.m_buffers[bufferView.buffer]; const size_t count = accessor.count; UINT target = bufferView.target; int elementSize; int componentSize; int numComponents; string sFilename_bin = sFolder + buffer.uri; fin.open(sFilename_bin, ios::binary); if (fin.fail()) { return false; } fin.seekg(bufferView.byteOffset, ios::beg); switch (accessor.componentType) { case GLTF_COMPONENT_TYPE_BYTE: componentSize = sizeof(GLbyte); break; case GLTF_COMPONENT_TYPE_UNSIGNED_BYTE: componentSize = sizeof(GLubyte); break; case GLTF_COMPONENT_TYPE_SHORT: componentSize = sizeof(GLshort); break; case GLTF_COMPONENT_TYPE_UNSIGNED_SHORT: componentSize = sizeof(GLushort); break; case GLTF_COMPONENT_TYPE_INT: componentSize = sizeof(GLint); break; case GLTF_COMPONENT_TYPE_UNSIGNED_INT: componentSize = sizeof(GLuint); break; case GLTF_COMPONENT_TYPE_FLOAT: componentSize = sizeof(GLfloat); break; case GLTF_COMPONENT_TYPE_DOUBLE: componentSize = sizeof(GLfloat); break; default: componentSize = 0; break; } if (accessor.type == "SCALAR") numComponents = 1; else if (accessor.type == "VEC2") numComponents = 2; else if (accessor.type == "VEC3") numComponents = 3; else if (accessor.type == "VEC4") numComponents = 4; else if (accessor.type == "MAT2") numComponents = 4; else if (accessor.type == "MAT3") numComponents = 9; else if (accessor.type == "MAT4") numComponents = 16; else return false; vector<float> fSubdata; // I'm pretty sure this is one of the problems, or related to it. If I use vector<USHORT> only half of the vector if filled, if I use GLubyte, the entire vector is filled, but the data might not be right vector<GLubyte> nSubdata; elementSize = (componentSize) * (numComponents); // Only fill the vector I'm using if (accessor.type == "SCALAR") { nSubdata.resize(count * numComponents); fin.read(reinterpret_cast<char*>(&nSubdata[0]), count/* * elementSize*/); // I commented this out since I'm not sure which size the .bin is storing the indice values, and I kept getting runtime errors, no matter what type I used for nSubdata } else { fSubdata.resize(count * numComponents); fin.read(reinterpret_cast<char*>(&fSubdata[0]), count * elementSize); } switch (target) { case GLTF_TARGET_ARRAY_BUFFER: { vertexData.insert(vertexData.end(), fSubdata.begin(), fSubdata.end()); glBindBuffer(GL_ARRAY_BUFFER, g_pGame->m_VBO); glBufferSubData(GL_ARRAY_BUFFER, offset, fSubdata.size() * componentSize, &fSubdata[0]); int attribute_index = 0; // I'm only loading vertex positions, the only attribute stored in the files for now glEnableVertexAttribArray(attribute_index); glVertexAttribPointer(0, numComponents, GL_FLOAT, GL_FALSE, componentSize * numComponents, (void*)(offset)); }break; case GLTF_TARGET_ELEMENT_ARRAY_BUFFER: { indiceData.insert(indiceData.end(), nSubdata.begin(), nSubdata.end()); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_pGame->m_EBO); // This is another area where I'm not sure of the correct values, but if componentSize is the correct size for the type being used it should be correct glBufferSubData is expecting the size in bytes, right? glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, offset, nSubdata.size() * componentSize, &nSubdata[0]); }break; default: return false; } if (accessor.type == "SCALAR") offset += nSubdata.size() * componentSize; else offset += fSubdata.size() * componentSize; fin.close(); return true; } these are the draw calls, I only use one at a time, but neither is currently display properly, g_pGame->m_indices is the same as indiceData vector, and vertexCount contains the correct vertex count, but I forgot to copy the lines of code containing where I set them, which is at the end of Mesh::Load(), I double checked the values to make sure.
      glDrawElements(GL_LINES, g_pGame->m_indices.size(), GL_UNSIGNED_BYTE, (void*)0); // Only shows with GL_UNSIGNED_BYTE
      glDrawArrays(GL_LINES, 0, g_pGame->m_vertexCount);
      So, I'm asking what type should I use for the indices? it doesn't seem to be unsigned short, which is what I selected with the Khronos Group Exporter for blender. Also, am I reading part or all of the .bin file wrong?
    • By ritzmax72
      That means how do I use base DirectX or OpenGL api's to make a physics based destruction simulation? 
      Will it be just smart rendering or something else is required?
    • By jsquare89
      I am somewhat new to game development and trying to create a basic 3d engine. I have managed to set up a first person camera and it seems to be working fine for the most part. While I am able to look up, down, left and right just fine the camera is constrained to the mouse movement in the window (i.e when the mouse reaches edges of the window it discontinues camera rotation and mouse is out of window bounds. I tried to use SDL_WarpMouseInWindow(window, center.x,center.y) but when I do this then it messes up the camera and the camera is stuck, even though there is some slight movement of the camera, it keeps going back to the center.
      void Camera::UpdateViewByMouse(SDL_Window &window, glm::vec2 mousePosition)
      float xDistanceFromWindowCenter = mousePosition.x - ((float)1024 / 2) ;
      float yDistanceFromWindowCenter = ((float)720 / 2) - mousePosition.y;
      yaw = xDistanceFromWindowCenter * cameraRotationSpeed;
      pitch = yDistanceFromWindowCenter * cameraRotationSpeed;
      SDL_WarpMouseInWindow(&window, 1024 / 2, 768 / 2); }
      i’ve been stuck on this for far too long. any help would be much appreciated
      i have also tried relative mouse movement,  and .xrel and .yrel to avail. polling mouse state with sdl_event. I do also know that SDL_WarpMouseInWindow makes change to event and have tried also ignore and reenabling to no avail
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Well,how to Load .3ds file in openGL?

This topic is 3537 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

oh,i'm really puzzled,i find many stuff from the Internet, but i don't see anyting loading from .3ds file in my program. here is a example i found,who could help find some error? ///////////////////////////////////////////////////////////////////////// 前些天在网上搜这个问题时几乎没找到肯定的明确的方法,偶尔在space里看到一篇解决方法,没试过验证一下,放到网上先,共享一下。 opengl----使用3dmax建模后怎样把模型导入 ////////// importmodel.h///////////////////////////////////////////// #include <math.h> #include <vector> #include <windows.h> // Header File For Windows #include <stdio.h> // Header File For Standard Input/Output #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include <gl\glaux.h> // Header File For The Glaux Library #include <math.h> #define PRIMARY 0x4D4D #define OBJECTINFO 0x3D3D #define VERSION 0x0002 #define EDITKEYFRAME 0xB000 #define MATERIAL 0xAFFF #define OBJECT 0x4000 #define MATNAME 0xA000 #define MATDIFFUSE 0xA020 #define MATMAP 0xA200 #define MATMAPFILE 0xA300 #define OBJ_MESH 0x4100 #define MAX_TEXTURES 100 #define OBJ_VERTICES 0x4110 #define OBJ_FACES 0x4120 #define OBJ_MATERIAL 0x4130 #define OBJ_UV 0x4140 #define MAP_W 32 // size of map along x-axis 32 #define MAP_SCALE 24.0f // the scale of the terrain map #define MAP MAP_W*MAP_SCALE/2 #define KEY_DOWN(vk_code)((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0) #define RAND_COORD(x) ((float)rand()/RAND_MAX * (x)) #define FRAND (((float)rand()-(float)rand())/RAND_MAX) using namespace std; class CVector3 {public: float x, y, z; }; class CVector2 {public: float x, y; }; struct tFace { int vertIndex[3]; int coordIndex[3]; }; struct tMatInfo { char strName[255]; char strFile[255]; BYTE color[3]; int texureId; float uTile; float vTile; float uOffset; float vOffset; } ; struct t3DObject { int numOfVerts; int numOfFaces; int numTexVertex; int materialID; bool bHasTexture; char strName[255]; CVector3 *pVerts; CVector3 *pNormals; CVector2 *pTexVerts; tFace *pFaces; }; struct t3DModel //模型信息结构体 { int numOfObjects; // 模型中对象的数目 int numOfMaterials; // 模型中材质的数目 vector<tMatInfo>pMaterials; // 材质链表信息 vector<t3DObject> pObject; // 模型中对象链表信息 }; struct tChunk //保存块信息的结构 { unsigned short int ID; // 块的ID unsigned int length; // 块的长度 unsigned int bytesRead; // 需要读的块数据的字节数 }; class CLoad3DS// CLoad3DS类处理所有的装入代码 { public: CLoad3DS(); // 初始化数据成员 virtual ~CLoad3DS(); void show3ds(int j0,float tx,float ty,float tz,float size);//显示3ds模型 void Init(char *filename,int j); private: bool Import3DS(t3DModel *pModel, char *strFileName);// 装入3ds文件到模型结构中 void CreateTexture(UINT textureArray[],LPSTR strFileName,int textureID);// 从文件中创建纹理 int GetString(char *); // 读一个字符串 void ReadChunk(tChunk *); // 读下一个块 void ReadNextChunk(t3DModel *pModel, tChunk *); // 读下一个块 void ReadNextObjChunk(t3DModel *pModel,t3DObject *pObject,tChunk *);// 读下一个对象块 void ReadNextMatChunk(t3DModel *pModel, tChunk *); // 读下一个材质块 void ReadColor(tMatInfo *pMaterial, tChunk *pChunk);// 读对象颜色的RGB值 void ReadVertices(t3DObject *pObject, tChunk *); // 读对象的顶点 void ReadVertexIndices(t3DObject *pObject,tChunk *);// 读对象的面信息 void ReadUVCoordinates(t3DObject *pObject,tChunk *);// 读对象的纹理坐标 void ReadObjMat(t3DModel *pModel,t3DObject *pObject,tChunk *pPreChunk);// 读赋予对象的材质名称 void ComputeNormals(t3DModel *pModel); // 计算对象顶点的法向量 void CleanUp(); // 关闭文件,释放内存空间 FILE *m_FilePointer; // 文件指针 tChunk *m_CurrentChunk; tChunk *m_TempChunk; }; ////////////////////////////////////////////////////////////////////////////////// ///////ImportModel.cpp #include "StdAfx.h" //#include "Set3ds.h" #include "ImportModel.h" #include <windows.h> // Header File For Windows #include <stdio.h> // Header File For Standard Input/Output #include <gl\gl.h> // Header File For The OpenGL32 Library #include <gl\glu.h> // Header File For The GLu32 Library #include <gl\glaux.h> // Header File For The Glaux Library #include <math.h> UINT g_Texture[10][MAX_TEXTURES] = {0}; t3DModel g_3DModel[10]; int g_ViewMode = GL_TRIANGLES; bool g_bLighting = true; CLoad3DS::CLoad3DS()// 构造函数的功能是初始化tChunk数据 { m_CurrentChunk = new tChunk; // 初始化并为当前的块分配空间 m_TempChunk = new tChunk; // 初始化一个临时块并分配空间 } CLoad3DS::~CLoad3DS() { CleanUp();// 释放内存空间 for(int j = 0; j <10;j++) for(int i = 0; i < g_3DModel[j].numOfObjects; i++) { delete [] g_3DModel[j].pObject.pFaces;// 删除所有的变量 delete [] g_3DModel[j].pObject.pNormals; delete [] g_3DModel[j].pObject.pVerts; delete [] g_3DModel[j].pObject.pTexVerts; } } //////////////////////////////////////////////////////////////////////// void CLoad3DS::Init(char *filename,int j)// { Import3DS(&g_3DModel[j], filename); // 将3ds文件装入到模型结构体中 for(int i =0; i<g_3DModel[j].numOfMaterials;i++) {if(strlen(g_3DModel[j].pMaterials.strFile)>0)// 判断是否是一个文件名 CreateTexture(g_Texture[j], g_3DModel[j].pMaterials.strFile, i);//使用纹理文件名称来装入位图 g_3DModel[j].pMaterials.texureId = i;// 设置材质的纹理ID } } // 从文件中创建纹理 void CLoad3DS::CreateTexture(UINT textureArray[], LPSTR strFileName, int textureID) { AUX_RGBImageRec *pBitmap = NULL; if(!strFileName) return; // 如果无此文件,则直接返回 pBitmap = auxDIBImageLoad(strFileName); // 装入位图,并保存数据 if(pBitmap == NULL) exit(0); // 如果装入位图失败,则退出 // 生成纹理 glGenTextures(1, &textureArray[textureID]); // 设置像素对齐格式 glPixelStorei (GL_UNPACK_ALIGNMENT, 1); glBindTexture(GL_TEXTURE_2D, textureArray[textureID]); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pBitmap->sizeX, pBitmap->sizeY, GL_RGB, GL_UNSIGNED_BYTE, pBitmap->data); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); if (pBitmap) // 释放位图占用的资源 { if (pBitmap->data) free(pBitmap->data); free(pBitmap); } } void CLoad3DS::show3ds(int j0,float tx,float ty,float tz,float size) //显示3ds模型 { glPushAttrib(GL_CURRENT_BIT);//保存现有颜色属实性 glPushMatrix(); glDisable(GL_TEXTURE_2D); ::glTranslatef( tx, ty, tz); ::glScaled(size,size,size); glRotatef(90, 0, 1.0f, 0); // 遍历模型中所有的对象 for(int i = 0; i < g_3DModel[j0].numOfObjects; i++) {if(g_3DModel[j0].pObject.size() <= 0) break;// 如果对象的大小小于0,则退出 t3DObject *pObject = &g_3DModel[j0].pObject;// 获得当前显示的对象 if(pObject->bHasTexture)// 判断该对象是否有纹理映射 { glEnable(GL_TEXTURE_2D);// 打开纹理映射 glBindTexture(GL_TEXTURE_2D, g_Texture[j0][pObject->materialID]); } else glDisable(GL_TEXTURE_2D);// 关闭纹理映射 //这里原来有错,不行正确调用模型的贴图,g_Texture应该为2维数组 glColor3ub(255, 255, 255); glBegin(g_ViewMode);//开始以g_ViewMode模式绘制 for(int j = 0; j < pObject->numOfFaces; j++) // 遍历所有的面 {for(int tex = 0; tex < 3; tex++) // 遍历三角形的所有点 {int index = pObject->pFaces[j].vertIndex[tex]; // 获得面对每个点的索引 glNormal3f(pObject->pNormals[index].x,pObject->pNormals[index].y, pObject->pNormals[index].z); // 给出法向量 if(pObject->bHasTexture) // 如果对象具有纹理 { if(pObject->pTexVerts) // 确定是否有UVW纹理坐标 glTexCoord2f(pObject->pTexVerts[index].x,pObject->pTexVerts[index].y); } else { if(g_3DModel[j0].pMaterials.size() && pObject->materialID>= 0) { BYTE *pColor = g_3DModel[j0].pMaterials[pObject->materialID].color; glColor3ub(pColor[0],pColor[1],pColor[2]); } } glVertex3f(pObject->pVerts[index].x,pObject->pVerts[index].y,pObject->pVerts[index].z); } } glEnd();// 绘制结束 } glEnable(GL_TEXTURE_2D); glPopMatrix(); glPopAttrib();//恢复前一属性 } ////////////////////////////////////////////////////////////////// // 打开一个3ds文件,读出其中的内容,并释放内存 bool CLoad3DS::Import3DS(t3DModel *pModel, char *strFileName) { char strMessage[255] = {0}; // 打开一个3ds文件 m_FilePointer = fopen(strFileName, "rb"); // 确保所获得的文件指针合法 if(!m_FilePointer) { sprintf(strMessage, "Unable to find the file: %s!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } // 当文件打开之后,首先应该将文件最开始的数据块读出以判断是否是一个3ds文件 // 如果是3ds文件的话,第一个块ID应该是PRIMARY // 将文件的第一块读出并判断是否是3ds文件 ReadChunk(m_CurrentChunk); // 确保是3ds文件 if (m_CurrentChunk->ID != PRIMARY) { sprintf(strMessage, "Unable to load PRIMARY chuck from file: %s!", strFileName); MessageBox(NULL, strMessage, "Error", MB_OK); return false; } // 现在开始读入数据,ReadNextChunk()是一个递归函数 // 通过调用下面的递归函数,将对象读出 ReadNextChunk(pModel, m_CurrentChunk); // 在读完整个3ds文件之后,计算顶点的法线 ComputeNormals(pModel); // 释放内存空间 // CleanUp(); return true; } // 下面的函数释放所有的内存空间,并关闭文件 void CLoad3DS::CleanUp() { // 遍历场景中所有的对象 fclose(m_FilePointer); // 关闭当前的文件指针 delete m_CurrentChunk; // 释放当前块 delete m_TempChunk; // 释放临时块 } // 下面的函数读出3ds文件的主要部分 void CLoad3DS::ReadNextChunk(t3DModel *pModel, tChunk *pPreChunk) { t3DObject newObject = {0}; // 用来添加到对象链表 tMatInfo newTexture = {0}; // 用来添加到材质链表 unsigned int version = 0; // 保存文件版本 int buffer[50000] = {0}; // 用来跳过不需要的数据 m_CurrentChunk = new tChunk; // 为新的块分配空间 // 下面每读一个新块,都要判断一下块的ID,如果该块是需要的读入的,则继续进行 // 如果是不需要读入的块,则略过 // 继续读入子块,直到达到预定的长度 while (pPreChunk->bytesRead < pPreChunk->length) { // 读入下一个块 ReadChunk(m_CurrentChunk); // 判断块的ID号 switch (m_CurrentChunk->ID) { case VERSION: // 文件版本号 // 在该块中有一个无符号短整型数保存了文件的版本 // 读入文件的版本号,并将字节数添加到bytesRead变量中 m_CurrentChunk->bytesRead += fread(&version, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer); // 如果文件版本号大于3,给出一个警告信息 if (version > 0x03) MessageBox(NULL, "This 3DS file is over version 3 so it may load incorrectly", "Warning", MB_OK); break; case OBJECTINFO: // 网格版本信息 // 读入下一个块 ReadChunk(m_TempChunk); // 获得网格的版本号 m_TempChunk->bytesRead += fread(&version, 1, m_TempChunk->length - m_TempChunk->bytesRead, m_FilePointer); // 增加读入的字节数 m_CurrentChunk->bytesRead += m_TempChunk->bytesRead; // 进入下一个块 ReadNextChunk(pModel, m_CurrentChunk); break; case MATERIAL: // 材质信息 // 材质的数目递增 pModel->numOfMaterials++; // 在纹理链表中添加一个空白纹理结构 pModel->pMaterials.push_back(newTexture); // 进入材质装入函数 ReadNextMatChunk(pModel, m_CurrentChunk); break; case OBJECT: // 对象的名称 // 该块是对象信息块的头部,保存了对象了名称 // 对象数递增 pModel->numOfObjects++; // 添加一个新的tObject节点到对象链表中 pModel->pObject.push_back(newObject); // 初始化对象和它的所有数据成员 memset(&(pModel->pObject[pModel->numOfObjects - 1]), 0, sizeof(t3DObject)); // 获得并保存对象的名称,然后增加读入的字节数 m_CurrentChunk->bytesRead += GetString(pModel->pObject[pModel->numOfObjects - 1].strName); // 进入其余的对象信息的读入 ReadNextObjChunk(pModel, &(pModel->pObject[pModel->numOfObjects - 1]), m_CurrentChunk); break; case EDITKEYFRAME: // 跳过关键帧块的读入,增加需要读入的字节数 m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer); break; default: // 跳过所有忽略的块的内容的读入,增加需要读入的字节数 m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer); break; } // 增加从最后块读入的字节数 pPreChunk->bytesRead += m_CurrentChunk->bytesRead; } // 释放当前块的内存空间 delete m_CurrentChunk; m_CurrentChunk = pPreChunk; } // 下面的函数处理所有的文件中对象的信息 void CLoad3DS::ReadNextObjChunk(t3DModel *pModel, t3DObject *pObject, tChunk *pPreChunk) { int buffer[50000] = {0}; // 用于读入不需要的数据 // 对新的块分配存储空间 m_CurrentChunk = new tChunk; // 继续读入块的内容直至本子块结束 while (pPreChunk->bytesRead < pPreChunk->length) { // 读入下一个块 ReadChunk(m_CurrentChunk); // 区别读入是哪种块 switch (m_CurrentChunk->ID) { case OBJ_MESH: // 正读入的是一个新块 // 使用递归函数调用,处理该新块 ReadNextObjChunk(pModel, pObject, m_CurrentChunk); break; case OBJ_VERTICES: // 读入是对象顶点 ReadVertices(pObject, m_CurrentChunk); break; case OBJ_FACES: // 读入的是对象的面 ReadVertexIndices(pObject, m_CurrentChunk); break; case OBJ_MATERIAL: // 读入的是对象的材质名称 // 该块保存了对象材质的名称,可能是一个颜色,也可能是一个纹理映射。同时在该块中也保存了 // 纹理对象所赋予的面 // 下面读入对象的材质名称 ReadObjMat(pModel, pObject, m_CurrentChunk); break; case OBJ_UV: // 读入对象的UV纹理坐标 // 读入对象的UV纹理坐标 ReadUVCoordinates(pObject, m_CurrentChunk); break; default: // 略过不需要读入的块 m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer); break; } // 添加从最后块中读入的字节数到前面的读入的字节中 pPreChunk->bytesRead += m_CurrentChunk->bytesRead; } // 释放当前块的内存空间,并把当前块设置为前面块 delete m_CurrentChunk; m_CurrentChunk = pPreChunk; } // 下面的函数处理所有的材质信息 void CLoad3DS::ReadNextMatChunk(t3DModel *pModel, tChunk *pPreChunk) { int buffer[50000] = {0}; // 用于读入不需要的数据 // 给当前块分配存储空间 m_CurrentChunk = new tChunk; // 继续读入这些块,知道该子块结束 while (pPreChunk->bytesRead < pPreChunk->length) { // 读入下一块 ReadChunk(m_CurrentChunk); // 判断读入的是什么块 switch (m_CurrentChunk->ID) { case MATNAME: // 材质的名称 // 读入材质的名称 m_CurrentChunk->bytesRead += fread(pModel->pMaterials[pModel->numOfMaterials - 1].strName, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer); break; case MATDIFFUSE: // 对象的R G B颜色 ReadColor(&(pModel->pMaterials[pModel->numOfMaterials - 1]), m_CurrentChunk); break; case MATMAP: // 纹理信息的头部 // 进入下一个材质块信息 ReadNextMatChunk(pModel, m_CurrentChunk); break; case MATMAPFILE: // 材质文件的名称 // 读入材质的文件名称 m_CurrentChunk->bytesRead += fread(pModel->pMaterials[pModel->numOfMaterials - 1].strFile, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer); break; default: // 掠过不需要读入的块 m_CurrentChunk->bytesRead += fread(buffer, 1, m_CurrentChunk->length - m_CurrentChunk->bytesRead, m_FilePointer); break; } // 添加从最后块中读入的字节数 pPreChunk->bytesRead += m_CurrentChunk->bytesRead; } // 删除当前块,并将当前块设置为前面的块 delete m_CurrentChunk; m_CurrentChunk = pPreChunk; } // 下面函数读入块的ID号和它的字节长度 void CLoad3DS::ReadChunk(tChunk *pChunk) { // 读入块的ID号,占用了2个字节。块的ID号象OBJECT或MATERIAL一样,说明了在块中所包含的内容 pChunk->bytesRead = fread(&pChunk->ID, 1, 2, m_FilePointer); // 然后读入块占用的长度,包含了四个字节 pChunk->bytesRead += fread(&pChunk->length, 1, 4, m_FilePointer); } // 下面的函数读入一个字符串 int CLoad3DS::GetString(char *pBuffer) { int index = 0; // 读入一个字节的数据 fread(pBuffer, 1, 1, m_FilePointer); // 直到结束 while (*(pBuffer + index++) != 0) { // 读入一个字符直到NULL fread(pBuffer + index, 1, 1, m_FilePointer); } // 返回字符串的长度 return strlen(pBuffer) + 1; } // 下面的函数读入RGB颜色 void CLoad3DS::ReadColor(tMatInfo *pMaterial, tChunk *pChunk) { // 读入颜色块信息 ReadChunk(m_TempChunk); // 读入RGB颜色 m_TempChunk->bytesRead += fread(pMaterial->color, 1, m_TempChunk->length - m_TempChunk->bytesRead, m_FilePointer); // 增加读入的字节数 pChunk->bytesRead += m_TempChunk->bytesRead; } // 下面的函数读入顶点索引 void CLoad3DS::ReadVertexIndices(t3DObject *pObject, tChunk *pPreChunk) { unsigned short index = 0; // 用于读入当前面的索引 // 读入该对象中面的数目 pPreChunk->bytesRead += fread(&pObject->numOfFaces, 1, 2, m_FilePointer); // 分配所有面的存储空间,并初始化结构 pObject->pFaces = new tFace [pObject->numOfFaces]; memset(pObject->pFaces, 0, sizeof(tFace) * pObject->numOfFaces); // 遍历对象中所有的面 for(int i = 0; i < pObject->numOfFaces; i++) { for(int j = 0; j < 4; j++) { // 读入当前面的第一个点 pPreChunk->bytesRead += fread(&index, 1, sizeof(index), m_FilePointer); if(j < 3) { // 将索引保存在面的结构中 pObject->pFaces.vertIndex[j] = index; } } } } // 下面的函数读入对象的UV坐标 void CLoad3DS::ReadUVCoordinates(t3DObject *pObject, tChunk *pPreChunk) { // 为了读入对象的UV坐标,首先需要读入UV坐标的数量,然后才读入具体的数据 // 读入UV坐标的数量 pPreChunk->bytesRead += fread(&pObject->numTexVertex, 1, 2, m_FilePointer); // 分配保存UV坐标的内存空间 pObject->pTexVerts = new CVector2 [pObject->numTexVertex]; // 读入纹理坐标 pPreChunk->bytesRead += fread(pObject->pTexVerts, 1, pPreChunk->length - pPreChunk->bytesRead, m_FilePointer); } // 读入对象的顶点 void CLoad3DS::ReadVertices(t3DObject *pObject, tChunk *pPreChunk) { // 在读入实际的顶点之前,首先必须确定需要读入多少个顶点。 // 读入顶点的数目 pPreChunk->bytesRead += fread(&(pObject->numOfVerts), 1, 2, m_FilePointer); // 分配顶点的存储空间,然后初始化结构体 pObject->pVerts = new CVector3 [pObject->numOfVerts]; memset(pObject->pVerts, 0, sizeof(CVector3) * pObject->numOfVerts); // 读入顶点序列 pPreChunk->bytesRead += fread(pObject->pVerts, 1, pPreChunk->length - pPreChunk->bytesRead, m_FilePointer); // 现在已经读入了所有的顶点。 // 因为3D Studio Max的模型的Z轴是指向上的,因此需要将y轴和z轴翻转过来。 // 具体的做法是将Y轴和Z轴交换,然后将Z轴反向。 // 遍历所有的顶点 for(int i = 0; i < pObject->numOfVerts; i++) { // 保存Y轴的值 float fTempY = pObject->pVerts.y; // 设置Y轴的值等于Z轴的值 pObject->pVerts.y = pObject->pVerts.z; // 设置Z轴的值等于-Y轴的值 pObject->pVerts.z = -fTempY; } } // 下面的函数读入对象的材质名称 void CLoad3DS::ReadObjMat(t3DModel *pModel, t3DObject *pObject, tChunk *pPreChunk) { char strMaterial[255] = {0}; // 用来保存对象的材质名称 int buffer[50000] = {0}; // 用来读入不需要的数据 // 材质或者是颜色,或者是对象的纹理,也可能保存了象明亮度、发光度等信息。 // 下面读入赋予当前对象的材质名称 pPreChunk->bytesRead += GetString(strMaterial); // 遍历所有的纹理 for(int i = 0; i < pModel->numOfMaterials; i++) { //如果读入的纹理与当前的纹理名称匹配 if(strcmp(strMaterial, pModel->pMaterials.strName) == 0) { // 设置材质ID pObject->materialID = i; // 判断是否是纹理映射,如果strFile是一个长度大于1的字符串,则是纹理 if(strlen(pModel->pMaterials.strFile) > 0) { // 设置对象的纹理映射标志 pObject->bHasTexture = true; } break; } else { // 如果该对象没有材质,则设置ID为-1 pObject->materialID = -1; } } pPreChunk->bytesRead += fread(buffer, 1, pPreChunk->length - pPreChunk->bytesRead, m_FilePointer); } // 下面的这些函数主要用来计算顶点的法向量,顶点的法向量主要用来计算光照 // 下面的宏定义计算一个矢量的长度 #define Mag(Normal) (sqrt(Normal.x*Normal.x + Normal.y*Normal.y + Normal.z*Normal.z)) // 下面的函数求两点决定的矢量 CVector3 Vector(CVector3 vPoint1, CVector3 vPoint2) { CVector3 vVector; vVector.x = vPoint1.x - vPoint2.x; vVector.y = vPoint1.y - vPoint2.y; vVector.z = vPoint1.z - vPoint2.z; return vVector; } // 下面的函数两个矢量相加 CVector3 AddVector(CVector3 vVector1, CVector3 vVector2) { CVector3 vResult; vResult.x = vVector2.x + vVector1.x; vResult.y = vVector2.y + vVector1.y; vResult.z = vVector2.z + vVector1.z; return vResult; } // 下面的函数处理矢量的缩放 CVector3 DivideVectorByScaler(CVector3 vVector1, float Scaler) { CVector3 vResult; vResult.x = vVector1.x / Scaler; vResult.y = vVector1.y / Scaler; vResult.z = vVector1.z / Scaler; return vResult; } // 下面的函数返回两个矢量的叉积 CVector3 Cross(CVector3 vVector1, CVector3 vVector2) { CVector3 vCross; vCross.x = ((vVector1.y * vVector2.z) - (vVector1.z * vVector2.y)); vCross.y = ((vVector1.z * vVector2.x) - (vVector1.x * vVector2.z)); vCross.z = ((vVector1.x * vVector2.y) - (vVector1.y * vVector2.x)); return vCross; } // 下面的函数规范化矢量 CVector3 Normalize(CVector3 vNormal) { double Magnitude; Magnitude = Mag(vNormal); // 获得矢量的长度 vNormal.x /= (float)Magnitude; vNormal.y /= (float)Magnitude; vNormal.z /= (float)Magnitude; return vNormal; } // 下面的函数用于计算对象的法向量 void CLoad3DS::ComputeNormals(t3DModel *pModel) { CVector3 vVector1, vVector2, vNormal, vPoly[3]; // 如果模型中没有对象,则返回 if(pModel->numOfObjects <= 0) return; // 遍历模型中所有的对象 for(int index = 0; index < pModel->numOfObjects; index++) { // 获得当前的对象 t3DObject *pObject = &(pModel->pObject[index]); // 分配需要的存储空间 CVector3 *pNormals = new CVector3 [pObject->numOfFaces]; CVector3 *pTempNormals = new CVector3 [pObject->numOfFaces]; pObject->pNormals = new CVector3 [pObject->numOfVerts]; // 遍历对象的所有面 for(int i=0; i < pObject->numOfFaces; i++) { vPoly[0] = pObject->pVerts[pObject->pFaces.vertIndex[0]]; vPoly[1] = pObject->pVerts[pObject->pFaces.vertIndex[1]]; vPoly[2] = pObject->pVerts[pObject->pFaces.vertIndex[2]]; // 计算面的法向量 vVector1 = Vector(vPoly[0], vPoly[2]); // 获得多边形的矢量 vVector2 = Vector(vPoly[2], vPoly[1]); // 获得多边形的第二个矢量 vNormal = Cross(vVector1, vVector2); // 获得两个矢量的叉积 pTempNormals = vNormal; // 保存非规范化法向量 vNormal = Normalize(vNormal); // 规范化获得的叉积 pNormals = vNormal; // 将法向量添加到法向量列表中 } // 下面求顶点法向量 CVector3 vSum = {0.0, 0.0, 0.0}; CVector3 vZero = vSum; int shared=0; // 遍历所有的顶点 for (i = 0; i < pObject->numOfVerts; i++) { for (int j = 0; j < pObject->numOfFaces; j++) // 遍历所有的三角形面 { // 判断该点是否与其它的面共享 if (pObject->pFaces[j].vertIndex[0] == i || pObject->pFaces[j].vertIndex[1] == i || pObject->pFaces[j].vertIndex[2] == i) { vSum = AddVector(vSum, pTempNormals[j]); shared++; } } pObject->pNormals = DivideVectorByScaler(vSum, float(-shared)); // 规范化最后的顶点法向 pObject->pNormals = Normalize(pObject->pNormals); vSum = vZero; shared = 0; } // 释放存储空间,开始下一个对象 delete [] pTempNormals; delete [] pNormals; } } ////////////////////////////////////// 使用模型: class basicpic { public: basicpic(); virtual ~basicpic(); GLUquadricObj *obj; CLoad3DS* m_3ds; void Scene(int obj,float size); }; basicpic::basicpic() { m_3ds=new CLoad3DS(); m_3ds->Init("ccc1.3DS",0); m_3ds->Init("art1.3DS",1); m_3ds->Init("art2.3DS",2); m_3ds->Init("art3.3DS",3); m_3ds->Init("art4.3DS",4); m_3ds->Init("mis1.3DS",5); m_3ds->Init("mis2.3DS",6); m_3ds->Init("mis3.3DS",7); m_3ds->Init("mis4.3DS",8); glEnable(GL_TEXTURE_2D); } void basicpic::Scene(int obj,float size) { m_3ds->show3ds(obj,0,0,0,size); }

Share this post

Link to post
Share on other sites
Perhaps you should use the source tags, tell us what error(s) you're getting and explain what effort you did to fix the problem yourself.


Share this post

Link to post
Share on other sites
There are many .3ds loaders for OpenGL, just look around google or find a librari like lib3ds. And please put all code in [sou.rce] code here
[/sou.rce] (without the periods), and lastly post the errors. If your just going to copy/paste code without understanding it, your not going to go anywhere fast, NEVER do that, understand everything you copy, why it's there, and many other things.

Share this post

Link to post
Share on other sites
oo,i image to learn about this,it is very useful to manage it in my program.And anybody who could give me the file structure of the *.3DS?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement