int CALL RadMatApl(int* n, int Obj, int Mat) { ApplyMaterial(Obj, Mat); *n = ioBuffer.OutInt(); return ioBuffer.OutErrorStatus(); }
void UberShader::ApplyMaterial(const glm::vec4 &light_ambient, const glm::vec4 &light_diffuse, const glm::vec4 &light_specular) { Device *dev = Device::GetInstance(); RenderState *render_dev = &dev->render_state(); Material material = render_dev->LastMaterial(); for(int i = 0 ; i < 4 ; i++) { material.ambient[i] *= light_ambient[i]; material.diffuse[i] *= light_diffuse[i]; material.specular[i] *= light_specular[i]; } ApplyMaterial(material); }
void MeshLoader::RenderMesh(const aiNode* node) { aiMatrix4x4 Mx = mAnimator->GetLocalTransform(node); Mx.Transpose(); glPushMatrix(); glMultMatrixf((float*)&Mx); for (unsigned int i = 0; i < node->mNumMeshes; i++) { const aiMesh* mesh = m_Scene->mMeshes[node->mMeshes[i]]; std::vector<aiVector3D> CachedPosition(mesh->mNumVertices); std::vector<aiVector3D> CachedNormal(mesh->mNumVertices); if (mesh->HasBones()) { const std::vector<aiMatrix4x4>& boneMatrices = mAnimator->GetBoneMatrices(node, i); for (unsigned int a = 0; a < mesh->mNumBones; a++) { const aiBone *bone = mesh->mBones[a]; const aiMatrix4x4& posTrafo = boneMatrices[a]; aiMatrix3x3 normTrafo = aiMatrix3x3(posTrafo); for (unsigned int b = 0; b < bone->mNumWeights; b++) { const aiVertexWeight& weight = bone->mWeights[b]; unsigned int vertexId = weight.mVertexId; const aiVector3D& srcPos = mesh->mVertices[vertexId]; const aiVector3D& srcNorm = mesh->mNormals[vertexId]; CachedPosition[vertexId] += weight.mWeight * (posTrafo * srcPos); CachedNormal[vertexId] += weight.mWeight * (normTrafo * srcNorm); } } } ApplyMaterial(m_Scene->mMaterials[mesh->mMaterialIndex]); for (unsigned int j = 0; j < mesh->mNumFaces; ++j) { const aiFace* face = &mesh->mFaces[j]; glBegin(GL_TRIANGLES); for (unsigned int k = 0; k < face->mNumIndices; k++) { int v_index = face->mIndices[k]; if (mesh->mColors[0] != NULL) glColor4fv((GLfloat*)&mesh->mColors[0][v_index]); if (mesh->mNormals != NULL) glNormal3fv(&CachedNormal[v_index].x); glVertex3fv(&CachedPosition[v_index].x); } glEnd(); } } for (unsigned int i = 0; i < node->mNumChildren; i++) { RenderMesh(node->mChildren[i]); } glPopMatrix(); }
bool RendererLegacy::DrawTriangles(const VertexArray *v, const Material *m, PrimitiveType t) { if (!v || v->position.size() < 3) return false; ApplyMaterial(m); EnableClientStates(v); glDrawArrays(t, 0, v->GetNumVerts()); UnApplyMaterial(m); DisableClientStates(); return true; }
bool RendererLegacy::DrawSurface(const Surface *s) { if (!s || !s->GetVertices() || s->GetNumIndices() < 3) return false; const Material *m = s->GetMaterial().Get(); const VertexArray *v = s->GetVertices(); ApplyMaterial(m); EnableClientStates(v); glDrawElements(s->GetPrimtiveType(), s->GetNumIndices(), GL_UNSIGNED_SHORT, s->GetIndexPointer()); UnApplyMaterial(m); DisableClientStates(); return true; }
bool RendererLegacy::DrawStaticMesh(StaticMesh *t) { if (!t) return false; //Approach: //on first render, buffer vertices from all surfaces to a vbo //since surfaces can have different materials (but they should have the same vertex format?) //bind buffer, set pointers and then draw each surface //(save buffer offsets in surfaces' render info) // prepare the buffer on first run if (!t->cached) { if (!BufferStaticMesh(t)) return false; } MeshRenderInfo *meshInfo = static_cast<MeshRenderInfo*>(t->GetRenderInfo()); //draw each surface meshInfo->vbuf->Bind(); if (meshInfo->ibuf) { meshInfo->ibuf->Bind(); } for (StaticMesh::SurfaceIterator surface = t->SurfacesBegin(); surface != t->SurfacesEnd(); ++surface) { SurfaceRenderInfo *surfaceInfo = static_cast<SurfaceRenderInfo*>((*surface)->GetRenderInfo()); ApplyMaterial((*surface)->GetMaterial().Get()); if (meshInfo->ibuf) { meshInfo->vbuf->DrawIndexed(t->GetPrimtiveType(), surfaceInfo->glOffset, surfaceInfo->glAmount); } else { //draw unindexed per surface meshInfo->vbuf->Draw(t->GetPrimtiveType(), surfaceInfo->glOffset, surfaceInfo->glAmount); } UnApplyMaterial((*surface)->GetMaterial().Get()); } if (meshInfo->ibuf) meshInfo->ibuf->Unbind(); meshInfo->vbuf->Unbind(); return true; }
/*virtual*/ void D3D9Renderer::RenderBucket( Bucket* pBucket, const ViewportPass* const pViewportPass) { XTRACE_FUNCTION; PROFILE_FUNCTION; if (pBucket->m_RenderTarget) { SetRenderTarget(pBucket->m_RenderTarget); if (pViewportPass) { SetViewport(pViewportPass); } } if (pBucket->m_ClearFlags != CLEAR_NONE) { Clear(pBucket->m_ClearFlags, pBucket->m_ClearColor, pBucket->m_ClearDepth); } if (pBucket->m_View) { View* const pView = pViewportPass ? pViewportPass->GetView(pBucket->m_View) : pBucket->m_View; m_View = *pView; m_View.ApplyToRenderer(*this); } if (pBucket->m_Flags & MAT_ALPHA) { pBucket->Sort(m_View); } else if (m_DoMaterialSort && pBucket->m_SortByMaterial) { pBucket->SortByMaterials(); } Mesh* pMesh = NULL; D3D9VertexBuffer* VertexBuffer = NULL; D3D9IndexBuffer* IndexBuffer = NULL; IVertexDeclaration* pVertexDeclaration = NULL; uint NumMeshes = pBucket->m_Meshes.Size(); Frustum ViewFrustum(GetViewMatrix() * GetProjectionMatrix()); #if BUILD_DEBUG if (pBucket->m_DEBUGUseFrustum) { pBucket->m_DEBUGFrustumView.ApplyToFrustum(ViewFrustum); } #endif for (uint MeshIndex = 0; MeshIndex < NumMeshes; ++MeshIndex) { XTRACE_NAMED(RenderBucketMesh); pMesh = pBucket->m_Meshes[MeshIndex]; DEVASSERT(pMesh); DEVASSERT(pMesh->m_VertexBuffer->GetNumVertices() > 0); const uint MaterialFlags = pMesh->GetMaterialFlags(); // Frustum culling--I can't do this earlier, when a mesh is added, because // it might be visible in one view (e.g., shadow map depth) and not to // player. if (!m_DoFrustumCulling || pBucket->m_Flags & MAT_HUD || pBucket->m_Flags & MAT_INWORLDHUD || #if BUILD_DEV pBucket->m_Flags & MAT_DEBUG_ALWAYS || MaterialFlags & MAT_DEBUG_ALWAYS || #endif pBucket->m_Flags & MAT_ALWAYS || MaterialFlags & MAT_ALWAYS || ViewFrustum.Intersects(pMesh->m_AABB)) { VertexBuffer = (D3D9VertexBuffer*)pMesh->m_VertexBuffer; IndexBuffer = (D3D9IndexBuffer*)pMesh->m_IndexBuffer; // RENDERTODO: It might be useful to adapt this for material overrides in // the future // Shader = (D3D9Shader*)( pBucket->m_OverrideShader ? // pBucket->m_OverrideShader : pMesh->m_OLDMaterial.m_Shader ); if (pMesh->m_VertexDeclaration != pVertexDeclaration) { XTRACE_NAMED(SetVertexDeclaration); m_D3DDevice->SetVertexDeclaration( (IDirect3DVertexDeclaration9*) pMesh->m_VertexDeclaration->GetDeclaration()); } pVertexDeclaration = pMesh->m_VertexDeclaration; DEVASSERT(VertexBuffer); DEVASSERT(pVertexDeclaration); DEVASSERT(IndexBuffer); SetWorldMatrix(pMesh->GetConcatenatedTransforms()); { XTRACE_NAMED(SetStreams); uint VertexSignature = pVertexDeclaration->GetSignature(); uint Index = 0; #define SETSTREAM(STREAM, SIGNATURE, TYPE) \ if (SIGNATURE == (VertexSignature & SIGNATURE)) { \ IDirect3DVertexBuffer9* const pBuffer = \ static_cast<IDirect3DVertexBuffer9*>(VertexBuffer->Get##STREAM()); \ DEVASSERT(pBuffer); \ m_D3DDevice->SetStreamSource(Index++, pBuffer, 0, sizeof(TYPE)); \ } SETSTREAM(Positions, VD_POSITIONS, Vector); SETSTREAM(Colors, VD_COLORS, uint); #if USE_HDR SETSTREAM(FloatColors1, VD_FLOATCOLORS, Vector4); SETSTREAM(FloatColors2, VD_BASISCOLORS, Vector4); SETSTREAM(FloatColors3, VD_BASISCOLORS, Vector4); // For SM2 cards, an alternative way to do HDR colors SETSTREAM(FloatColors1, VD_FLOATCOLORS_SM2, Vector4); SETSTREAM(FloatColors2, VD_BASISCOLORS_SM2, Vector4); SETSTREAM(FloatColors3, VD_BASISCOLORS_SM2, Vector4); #endif SETSTREAM(UVs, VD_UVS, Vector2); SETSTREAM(Normals, VD_NORMALS, Vector); SETSTREAM(Tangents, VD_TANGENTS, Vector4); SETSTREAM(BoneIndices, VD_BONEINDICES, SBoneData); SETSTREAM(BoneWeights, VD_BONEWEIGHTS, SBoneData); #undef SETSTREAM m_D3DDevice->SetIndices( (IDirect3DIndexBuffer9*)IndexBuffer->GetIndices()); } ApplyMaterial(pMesh->m_Material, pMesh, m_View); if (VertexBuffer->GetNumVertices() > 0) { XTRACE_NAMED(DrawIndexedPrimitive); m_D3DDevice->DrawIndexedPrimitive(IndexBuffer->GetPrimitiveType(), 0, 0, VertexBuffer->GetNumVertices(), 0, IndexBuffer->GetNumPrimitives()); } #if BUILD_DEBUG ++m_DEBUGRenderStats.NumMeshes; m_DEBUGRenderStats.NumPrimitives += IndexBuffer->GetNumPrimitives(); #endif } #if BUILD_DEV // WARNING: This assumes the mesh is only in one bucket, which could be bad if (pMesh->m_IsDebugMesh) { m_DeferredDeleteDebugMeshes.PushBackUnique(pMesh); } #endif } }
void UberShader::ApplyMaterial() { Device *dev = Device::GetInstance(); RenderState *render_dev = &dev->render_state(); const Material &material = render_dev->LastMaterial(); ApplyMaterial(material); }
void GLMesh::DrawCanonicalObject() { if (m_pScene) { glPushName(m_iMeshID); glColor4f(m_meshcolor.r, m_meshcolor.g, m_meshcolor.b, m_meshcolor.a); for (unsigned int i = 0; i < m_Meshes.size(); i++) { glBindBuffer(GL_ARRAY_BUFFER, m_Meshes[i].m_uVB); glVertexPointer(3, GL_FLOAT, sizeof(Vertex), 0); glEnableClientState(GL_VERTEX_ARRAY); size_t uNormalOffset = 3 * sizeof(float); glNormalPointer(GL_FLOAT, sizeof(Vertex), (GLvoid*)uNormalOffset); glEnableClientState(GL_NORMAL_ARRAY); size_t uTexOffset = 6 * sizeof(float); glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), (GLvoid*)uTexOffset); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Meshes[i].m_uIB); const unsigned int MaterialIndex = m_Meshes[i].m_uMaterialIndex; const aiMaterial* pMaterial = m_pScene->mMaterials[MaterialIndex]; ApplyMaterial(pMaterial); // WARNING: A relatively arbitrary list of texture types to // try and load. In the future these should be dealt with properly const size_t numTexTypesToLoad = 4; static aiTextureType texTypesToLoad[numTexTypesToLoad] = { aiTextureType_DIFFUSE, aiTextureType_AMBIENT, aiTextureType_EMISSIVE, aiTextureType_LIGHTMAP}; int totaltex = 0; for (size_t tti = 0; tti < numTexTypesToLoad; ++tti) { const aiTextureType tt = texTypesToLoad[tti]; const unsigned int numTex = pMaterial->GetTextureCount(tt); totaltex += numTex; for (unsigned int dt = 0; dt < numTex; ++dt) { aiString path; if (pMaterial->GetTexture(tt, dt, &path) == AI_SUCCESS) { std::map<std::string, GLuint>::iterator ix = m_mapPathToGLTex.find(path.data); if (ix != m_mapPathToGLTex.end()) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, ix->second); // Only bind first one for now. goto endoftextures; } } } } endoftextures: aiMatrix4x4 m = m_Meshes[i].m_Transformation; aiTransposeMatrix4(&m); glPushMatrix(); glMultMatrixf(&(m.a1)); glDrawElements(GL_TRIANGLES, m_Meshes[i].m_uNumIndices, GL_UNSIGNED_INT, 0); glPopMatrix(); if (totaltex > 0) { glDisable(GL_TEXTURE_2D); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glPopName(); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } }
void NaDbSurface::DefineDisplay() { GLfloat bgcol[4]; glGetFloatv(GL_COLOR_CLEAR_VALUE, bgcol); GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f }; // Enable Depth Testing glEnable(GL_DEPTH_TEST); glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE); // Enable lighting glEnable(GL_LIGHTING); // Enable color tracking glEnable(GL_COLOR_MATERIAL); // Set Material properties to follow glColor values glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR,specref); glMaterialf(GL_FRONT_AND_BACK,GL_SHININESS,128); glShadeModel(GL_SMOOTH); glEnable(GL_AUTO_NORMAL); glEnable(GL_NORMALIZE); if(displayMode == GLSHADED) { glPushAttrib(GL_LIGHTING_BIT); ApplyMaterial(); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(8, 8); DrawShaded(); glDisable(GL_LIGHTING); glDisable(GL_COLOR_MATERIAL); glColor3ub(25, 25, 25); DrawEdges(); glDisable(GL_POLYGON_OFFSET_FILL); glPopAttrib(); } else if(displayMode == GLWIREFRAME) { glPushAttrib(GL_LIGHTING_BIT); glDisable(GL_LIGHTING); glDisable(GL_COLOR_MATERIAL); glDisable(GL_DEPTH_TEST); glColor3ub(itsRed, itsGreen, itsBlue); DrawWired(); glPopAttrib(); } else if(displayMode == GLHLREMOVED) { glPushAttrib(GL_LIGHTING_BIT); glDisable(GL_LIGHTING); glDisable(GL_COLOR_MATERIAL); glEnable(GL_DEPTH_TEST); glColor3f(bgcol[0], bgcol[1], bgcol[2]); glEnable(GL_POLYGON_OFFSET_FILL); glPolygonOffset(5, 5); DrawShaded(); DrawWired(); glDisable(GL_POLYGON_OFFSET_FILL); glPopAttrib(); } }