Patch::Patch(Shader shader) { _transform = Transform(); _transform.position = glm::vec3(); _transform.rotation = glm::quat(); _transform.scale = glm::vec3(1.0f, 1.0f, 1.0f); _transform.angularVelocity = glm::quat(); _transform.linearVelocity = glm::vec3(); _transform.rotationOrigin = glm::vec3(); _transform.scaleOrigin = glm::vec3(); GLfloat data = 0.0f; GLint elements = 0; glGenVertexArrays(1, &_vao); glBindVertexArray(_vao); glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * NUM_VERTS_STORED, &data, GL_DYNAMIC_DRAW); glGenBuffers(1, &_ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint) * NUM_ELEMENTS, &elements, GL_DYNAMIC_DRAW); // Bind buffer data to shader values GLint posAttrib = glGetAttribLocation(shader.shaderPointer, "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), 0); GLint normAttrib = glGetAttribLocation(shader.shaderPointer, "normal"); glEnableVertexAttribArray(normAttrib); glVertexAttribPointer(normAttrib, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (void*)(3 * sizeof(GLfloat))); _curve = new RenderShape(_vao, NUM_ELEMENTS, GL_TRIANGLES, shader, glm::vec4(0.6f, 0.6f, 0.6f, 1.0f)); _curve->transform().parent = &_transform; RenderManager::AddShape(_curve); GeneratePlane(); }
bool initialize(WinMemViewerPtr viewer) { float w = (float)viewer->width(); float h = (float)viewer->height(); texlist_.push_back(GridMipmap()); itex_ = 0; nspace_ = 0; GeneratePlane(output_.prims); proj_ = Matrix44f::Perspective(kGSPI * 0.6f, w / h, 1.0f, 500.0f);//投影变换 output_.uniforms.texture = texlist_[itex_];//纹理 output_.uniforms.ambient.set(0.4f, 0.4f, 0.4f);//环境光 output_.uniforms.diffuse.set(0.587609f, 0.587609f, 0.587609f);//漫反射 output_.uniforms.specular.set(0.071744f, 0.071744f, 0.071744f);//镜面反射 alpha_ = 0.0f; pos_ = 3.5f; sample_cat_ = 0; viewer_ = viewer; return true; }
int main(int argc, char **argv) { // generate the plane std::vector<int> pl = GeneratePlane(PLANE_SIZE); int tri_count = 2 * (PLANE_SIZE-1) * (PLANE_SIZE-1); printf("Mesh triangle count: %d\n", tri_count); VertexCache vertex_cache; int misses = vertex_cache.GetCacheMissCount(&pl[0], tri_count); printf("*** Before optimization ***\n"); printf("Cache misses\t: %d\n", misses); printf("ACMR\t\t: %f\n", (float)misses / (float)tri_count); VertexCacheOptimizer vco; printf("Optimizing ... \n"); unsigned int time = GetMSec(); VertexCacheOptimizer::Result res = vco.Optimize(&pl[0], tri_count); time = GetMSec() - time; if (res) { printf("Error\n"); return 0; } printf("Optimized in %f seconds (%d ns/triangle)\n", time / 1000.0f, (time * 1000) / tri_count); misses = vertex_cache.GetCacheMissCount(&pl[0], tri_count); printf("*** After optimization ***\n"); printf("Cache misses\t: %d\n", misses); printf("ACMR\t\t: %f\n", (float)misses / (float)tri_count); return 0; }
SubMesh* Mesh::BuildSubMesh(const Primitive& primitive, const MeshParams& params) { #if NAZARA_UTILITY_SAFE if (!m_impl) { NazaraError("Mesh not created"); return nullptr; } if (m_impl->animationType != AnimationType_Static) { NazaraError("Mesh must be static"); return nullptr; } if (!params.IsValid()) { NazaraError("Parameters must be valid"); return nullptr; } #endif Boxf aabb; IndexBufferRef indexBuffer; VertexBufferRef vertexBuffer; Matrix4f matrix(primitive.matrix); matrix.ApplyScale(params.scale); VertexDeclaration* declaration = VertexDeclaration::Get(VertexLayout_XYZ_Normal_UV_Tangent); switch (primitive.type) { case PrimitiveType_Box: { unsigned int indexCount; unsigned int vertexCount; ComputeBoxIndexVertexCount(primitive.box.subdivision, &indexCount, &vertexCount); indexBuffer = IndexBuffer::New(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.storage, BufferUsage_Static); vertexBuffer = VertexBuffer::New(declaration, vertexCount, params.storage, BufferUsage_Static); VertexMapper vertexMapper(vertexBuffer, BufferAccess_WriteOnly); VertexPointers pointers; pointers.normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal); pointers.positionPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position); pointers.tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent); pointers.uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord); IndexMapper indexMapper(indexBuffer, BufferAccess_WriteOnly); GenerateBox(primitive.box.lengths, primitive.box.subdivision, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb); break; } case PrimitiveType_Cone: { unsigned int indexCount; unsigned int vertexCount; ComputeConeIndexVertexCount(primitive.cone.subdivision, &indexCount, &vertexCount); indexBuffer = IndexBuffer::New(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.storage, BufferUsage_Static); vertexBuffer = VertexBuffer::New(declaration, vertexCount, params.storage, BufferUsage_Static); VertexMapper vertexMapper(vertexBuffer, BufferAccess_WriteOnly); VertexPointers pointers; pointers.normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal); pointers.positionPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position); pointers.tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent); pointers.uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord); IndexMapper indexMapper(indexBuffer, BufferAccess_WriteOnly); GenerateCone(primitive.cone.length, primitive.cone.radius, primitive.cone.subdivision, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb); break; } case PrimitiveType_Plane: { unsigned int indexCount; unsigned int vertexCount; ComputePlaneIndexVertexCount(primitive.plane.subdivision, &indexCount, &vertexCount); indexBuffer = IndexBuffer::New(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.storage, BufferUsage_Static); vertexBuffer = VertexBuffer::New(declaration, vertexCount, params.storage, BufferUsage_Static); VertexMapper vertexMapper(vertexBuffer, BufferAccess_WriteOnly); VertexPointers pointers; pointers.normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal); pointers.positionPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position); pointers.tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent); pointers.uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord); IndexMapper indexMapper(indexBuffer, BufferAccess_WriteOnly); GeneratePlane(primitive.plane.subdivision, primitive.plane.size, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb); break; } case PrimitiveType_Sphere: { switch (primitive.sphere.type) { case SphereType_Cubic: { unsigned int indexCount; unsigned int vertexCount; ComputeCubicSphereIndexVertexCount(primitive.sphere.cubic.subdivision, &indexCount, &vertexCount); indexBuffer = IndexBuffer::New(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.storage, BufferUsage_Static); vertexBuffer = VertexBuffer::New(declaration, vertexCount, params.storage, BufferUsage_Static); VertexMapper vertexMapper(vertexBuffer, BufferAccess_ReadWrite); VertexPointers pointers; pointers.normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal); pointers.positionPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position); pointers.tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent); pointers.uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord); IndexMapper indexMapper(indexBuffer, BufferAccess_WriteOnly); GenerateCubicSphere(primitive.sphere.size, primitive.sphere.cubic.subdivision, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb); break; } case SphereType_Ico: { unsigned int indexCount; unsigned int vertexCount; ComputeIcoSphereIndexVertexCount(primitive.sphere.ico.recursionLevel, &indexCount, &vertexCount); indexBuffer = IndexBuffer::New(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.storage, BufferUsage_Static); vertexBuffer = VertexBuffer::New(declaration, vertexCount, params.storage, BufferUsage_Static); VertexMapper vertexMapper(vertexBuffer, BufferAccess_WriteOnly); VertexPointers pointers; pointers.normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal); pointers.positionPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position); pointers.tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent); pointers.uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord); IndexMapper indexMapper(indexBuffer, BufferAccess_WriteOnly); GenerateIcoSphere(primitive.sphere.size, primitive.sphere.ico.recursionLevel, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb); break; } case SphereType_UV: { unsigned int indexCount; unsigned int vertexCount; ComputeUvSphereIndexVertexCount(primitive.sphere.uv.sliceCount, primitive.sphere.uv.stackCount, &indexCount, &vertexCount); indexBuffer = IndexBuffer::New(vertexCount > std::numeric_limits<UInt16>::max(), indexCount, params.storage, BufferUsage_Static); vertexBuffer = VertexBuffer::New(declaration, vertexCount, params.storage, BufferUsage_Static); VertexMapper vertexMapper(vertexBuffer, BufferAccess_WriteOnly); VertexPointers pointers; pointers.normalPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Normal); pointers.positionPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Position); pointers.tangentPtr = vertexMapper.GetComponentPtr<Vector3f>(VertexComponent_Tangent); pointers.uvPtr = vertexMapper.GetComponentPtr<Vector2f>(VertexComponent_TexCoord); IndexMapper indexMapper(indexBuffer, BufferAccess_WriteOnly); GenerateUvSphere(primitive.sphere.size, primitive.sphere.uv.sliceCount, primitive.sphere.uv.stackCount, matrix, primitive.textureCoords, pointers, indexMapper.begin(), &aabb); break; } } break; } } StaticMeshRef subMesh = StaticMesh::New(this); if (!subMesh->Create(vertexBuffer)) { NazaraError("Failed to create StaticMesh"); return nullptr; } if (params.optimizeIndexBuffers) indexBuffer->Optimize(); subMesh->SetAABB(aabb); subMesh->SetIndexBuffer(indexBuffer); AddSubMesh(subMesh); return subMesh; }
Patch::Patch(RenderShape& markerTemplate, RenderShape& slopeLineTemplate) { _transform = Transform(); _transform.position = glm::vec3(); _transform.rotation = glm::quat(); _transform.scale = glm::vec3(1.0f, 1.0f, 1.0f); _transform.angularVelocity = glm::quat(); _transform.linearVelocity = glm::vec3(); _transform.rotationOrigin = glm::vec3(); _transform.scaleOrigin = glm::vec3(); GLfloat data = 0.0f; GLint elements = 0; glGenVertexArrays(1, &_vao); glBindVertexArray(_vao); glGenBuffers(1, &_vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat), &data, GL_DYNAMIC_DRAW); glGenBuffers(1, &_ebo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLint), &elements, GL_DYNAMIC_DRAW); // Bind buffer data to shader values GLint posAttrib = glGetAttribLocation(slopeLineTemplate.shader().shaderPointer, "position"); glEnableVertexAttribArray(posAttrib); glVertexAttribPointer(posAttrib, 3, GL_FLOAT, GL_FALSE, 0, 0); GLint uTransform = glGetUniformLocation(slopeLineTemplate.shader().shaderPointer, "transform"); GLint uColor = glGetUniformLocation(slopeLineTemplate.shader().shaderPointer, "color"); _curve = new RenderShape(_vao, NUM_ELEMENTS, GL_TRIANGLES, slopeLineTemplate.shader(), glm::vec4(0.6f, 0.6f, 0.6f, 1.0f)); _curve->transform().parent = &_transform; RenderManager::AddShape(_curve); GeneratePlane(); for (int i = 0; i < 16; ++i) { _controlPointMarkers[i] = new RenderShape(markerTemplate); _controlPointMarkers[i]->transform().scale = glm::vec3(0.01f, 0.01f, 0.01f); _controlPointMarkers[i]->transform().position = _controlPoints[i]; _controlPointMarkers[i]->transform().parent = &_curve->transform(); RenderManager::AddShape(_controlPointMarkers[i]); } for (int i = 0; i < 8; ++i) { _slopeLines[i] = new RenderShape(slopeLineTemplate); _slopeLines[i]->transform().parent = &_curve->transform(); RenderManager::AddShape(_slopeLines[i]); } _controlPointsActive = true; }