Applying Texture on Custom Mesh
Unsolved
Game Development
-
I want to render texture image (.png) on my material faces consist of custom mesh. Structure is rendering fine but without texture. And I want to render the same image on all faces of Tetrahedron structure. I am very new to openGl so may be its very basic question that I want to know that How engine maps the vertex co-ordinates to texture coordinates (Qt3D c++).
Window window;// (inherits QWindow) Qt3DCore::QAspectEngine engine; engine.registerAspect(new Qt3DRender::QRenderAspect()); Qt3DInput::QInputAspect *input = new Qt3DInput::QInputAspect; engine.registerAspect(input); QVariantMap data; data.insert(QStringLiteral("surface"), QVariant::fromValue(static_cast<QSurface *>(&window))); data.insert(QStringLiteral("eventSource"), QVariant::fromValue(&window)); engine.setData(data); // Root entity Qt3DCore::QEntity *rootEntity = new Qt3DCore::QEntity(); // Camera Qt3DCore::QCamera *cameraEntity = new Qt3DCore::QCamera(rootEntity); cameraEntity->lens()->setPerspectiveProjection(45.0f, 16.0f/9.0f, 0.1f, 1000.0f); cameraEntity->setProjectionType(Qt3DCore::QCameraLens::PerspectiveProjection); cameraEntity->setAspectRatio(window.width() / window.height()); cameraEntity->setUpVector(QVector3D(0.0f, 1.0f, -100.0f)); cameraEntity->setViewCenter(QVector3D(0.0f, 0.5f, 0.0f)); cameraEntity->setPosition(QVector3D(0.0f, 60.5f, 15.0f)); input->setCamera(cameraEntity); // FrameGraph Qt3DRender::QFrameGraph *frameGraph = new Qt3DRender::QFrameGraph(); Qt3DRender::QForwardRenderer *forwardRenderer = new Qt3DRender::QForwardRenderer(); forwardRenderer->setClearColor(QColor::fromRgbF(0.1, 0.8, 0.8, 1.0)); forwardRenderer->setCamera(cameraEntity); frameGraph->setActiveFrameGraph(forwardRenderer); rootEntity->addComponent(frameGraph); // Torus LiRenderableEntity *customMeshEntity = new LiRenderableEntity(rootEntity); //setting material Qt3DRender::QNormalDiffuseSpecularMapMaterial *material = new Qt3DRender::QNormalDiffuseSpecularMapMaterial(); Qt3DRender::QTextureImage *textureDfImage = new Qt3DRender::QTextureImage(); textureDfImage->setSource(QUrl(QStringLiteral("qrc:/Glass/metalbarrel/normal_middle_bumps.webp"))); material->diffuse()->addTextureImage(textureDfImage); material->diffuse()->setSize(5,5); customMeshEntity->addComponent(material); // Custom Mesh (TetraHedron) Qt3DRender::QGeometryRenderer *customMeshRenderer = new Qt3DRender::QGeometryRenderer; Qt3DRender::QGeometry *customGeometry = new Qt3DRender::QGeometry(customMeshRenderer); Qt3DRender::QBuffer *vertexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::VertexBuffer, customGeometry); Qt3DRender::QBuffer *indexDataBuffer = new Qt3DRender::QBuffer(Qt3DRender::QBuffer::IndexBuffer, customGeometry); // 4 distinct vertices QByteArray vertexBufferData; vertexBufferData.resize(4 * (3 + 3 + 2) * sizeof(float)); // Vertices QVector3D v0(-1.0f, 0.9f, -1.0f); QVector3D v1(1.0f, 0.9f, -1.0f); QVector3D v2(0.0f, 1.9f, 0.0f); QVector3D v3(0.0f, 0.9f, 1.0f); QVector2D v01(0,0); QVector2D v02(0,1); QVector2D v03(1,1); QVector2D v04(1,0); // Faces Normals QVector3D n023 = QVector3D::normal(v0, v2, v3); QVector3D n012 = QVector3D::normal(v0, v1, v2); QVector3D n310 = QVector3D::normal(v3, v1, v0); QVector3D n132 = QVector3D::normal(v1, v3, v2); // Vector Normals QVector3D n0 = QVector3D(n023 + n012 + n310).normalized(); QVector3D n1 = QVector3D(n132 + n012 + n310).normalized(); QVector3D n2 = QVector3D(n132 + n012 + n023).normalized(); QVector3D n3 = QVector3D(n132 + n310 + n023).normalized(); QVector<QVector3D> vertices = QVector<QVector3D>() << v0 << v1 << v2 << v3 << n023 << n012 << n310 << n132 ; QVector<QVector2D> vertices2d = QVector<QVector2D>() << v01 << v02 << v03 << v04; float *rawVertexArray = reinterpret_cast<float *>(vertexBufferData.data()); int idx = 0; Q_FOREACH (const QVector3D &v, vertices) { rawVertexArray[idx++] = v.x(); rawVertexArray[idx++] = v.y(); rawVertexArray[idx++] = v.z(); } Q_FOREACH (const QVector2D &v, vertices2d) { rawVertexArray[idx++] = v.x(); rawVertexArray[idx++] = v.y(); } // Indices (12) QByteArray indexBufferData; indexBufferData.resize(4 * 3 * sizeof(ushort)); ushort *rawIndexArray = reinterpret_cast<ushort *>(indexBufferData.data()); // Front rawIndexArray[0] = 0; rawIndexArray[1] = 1; rawIndexArray[2] = 2; // Bottom rawIndexArray[3] = 3; rawIndexArray[4] = 1; rawIndexArray[5] = 0; // Left rawIndexArray[6] = 0; rawIndexArray[7] = 2; rawIndexArray[8] = 3; // Right rawIndexArray[9] = 1; rawIndexArray[10] = 3; rawIndexArray[11] = 2; vertexDataBuffer->setData(vertexBufferData); indexDataBuffer->setData(indexBufferData); // Attributes Qt3DRender::QAttribute *positionAttribute = new Qt3DRender::QAttribute(); positionAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); positionAttribute->setBuffer(vertexDataBuffer); positionAttribute->setDataType(Qt3DRender::QAttribute::Float); positionAttribute->setDataSize(3); positionAttribute->setByteOffset(0); positionAttribute->setByteStride(3 * sizeof(float)); positionAttribute->setCount(4); positionAttribute->setName(Qt3DRender::QAttribute::defaultPositionAttributeName()); Qt3DRender::QAttribute *normalAttribute = new Qt3DRender::QAttribute(); normalAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); normalAttribute->setBuffer(vertexDataBuffer); normalAttribute->setDataType(Qt3DRender::QAttribute::Float); normalAttribute->setDataSize(3); normalAttribute->setByteOffset(12 * sizeof(float)); normalAttribute->setByteStride(3 * sizeof(float)); normalAttribute->setCount(4); normalAttribute->setName(Qt3DRender::QAttribute::defaultNormalAttributeName()); Qt3DRender::QAttribute *indexAttribute = new Qt3DRender::QAttribute(); indexAttribute->setAttributeType(Qt3DRender::QAttribute::IndexAttribute); indexAttribute->setBuffer(indexDataBuffer); indexAttribute->setDataType(Qt3DRender::QAttribute::UnsignedShort); indexAttribute->setDataSize(1); indexAttribute->setByteOffset(0); indexAttribute->setByteStride(0); indexAttribute->setCount(12); Qt3DRender::QAttribute *textureCoordinateAttribute = new Qt3DRender::QAttribute(); textureCoordinateAttribute->setAttributeType(Qt3DRender::QAttribute::VertexAttribute); textureCoordinateAttribute->setBuffer(vertexDataBuffer); textureCoordinateAttribute->setDataType(Qt3DRender::QAttribute::Float); textureCoordinateAttribute->setDataSize(2); textureCoordinateAttribute->setByteOffset(24 * sizeof(float)); textureCoordinateAttribute->setByteStride(2 * sizeof(float)); textureCoordinateAttribute->setCount(4); textureCoordinateAttribute->setName(Qt3DRender::QAttribute::defaultTextureCoordinateAttributeName()); customGeometry->addAttribute(positionAttribute); customGeometry->addAttribute(normalAttribute); customGeometry->addAttribute(indexAttribute); customGeometry->addAttribute(textureCoordinateAttribute); customMeshRenderer->setInstanceCount(1); customMeshRenderer->setBaseVertex(0); customMeshRenderer->setBaseInstance(0); customMeshRenderer->setPrimitiveType(Qt3DRender::QGeometryRenderer::Triangles); customMeshRenderer->setGeometry(customGeometry); // 4 faces of 3 points customMeshRenderer->setPrimitiveCount(12); customMeshEntity->addComponent(customMeshRenderer); customMeshEntity->transform()->setScale(10.0f); engine.setRootEntity(rootEntity); window.showNormal();