@JoeCFD Try the following
Qt3DCore::QEntity* createTorusWithGradient(Qt3DCore::QEntity* rootEntity)
{
Qt3DCore::QEntity* torusEntity = new Qt3DCore::QEntity(rootEntity);
// Torus mesh
Qt3DExtras::QTorusMesh* torusMesh = new Qt3DExtras::QTorusMesh(torusEntity);
torusMesh->setRadius(5.0f);
torusMesh->setMinorRadius(1.0f);
torusMesh->setRings(100);
torusMesh->setSlices(20);
torusEntity->addComponent(torusMesh);
// Transform
Qt3DCore::QTransform* transform = new Qt3DCore::QTransform(torusEntity);
transform->setScale3D(QVector3D(1.5f, 1.5f, 1.5f)); // Fixed typo
transform->setRotation(QQuaternion::fromAxisAndAngle(QVector3D(1, 0, 0), 60.0f));
torusEntity->addComponent(transform);
// Custom material setup
Qt3DRender::QMaterial* material = new Qt3DRender::QMaterial(torusEntity);
// Effect, technique, render pass, and shader
Qt3DRender::QEffect* effect = new Qt3DRender::QEffect();
Qt3DRender::QTechnique* gl3Technique = new Qt3DRender::QTechnique();
Qt3DRender::QRenderPass* gl3Pass = new Qt3DRender::QRenderPass();
Qt3DRender::QShaderProgram* glShader = new Qt3DRender::QShaderProgram();
// Simple vertex shader
const QByteArray vertexShaderCode = R"(
#version 330 core
in vec3 vertexPosition;
in vec3 vertexNormal;
uniform mat4 modelViewProjection;
out vec3 fragPosition;
void main() {
fragPosition = vertexPosition;
gl_Position = modelViewProjection * vec4(vertexPosition, 1.0);
}
)";
// Simple fragment shader with color parameter
const QByteArray fragmentShaderCode = R"(
#version 330 core
in vec3 fragPosition;
out vec4 fragColor;
uniform vec4 color;
void main() {
fragColor = color; // Use the color parameter directly
}
)";
// Set shader code
glShader->setVertexShaderCode(vertexShaderCode);
glShader->setFragmentShaderCode(fragmentShaderCode);
// Connect shader program to render pass
gl3Pass->setShaderProgram(glShader);
gl3Technique->addRenderPass(gl3Pass);
// Set OpenGL 3.3 Core profile
gl3Technique->graphicsApiFilter()->setApi(Qt3DRender::QGraphicsApiFilter::OpenGL);
gl3Technique->graphicsApiFilter()->setMajorVersion(3);
gl3Technique->graphicsApiFilter()->setMinorVersion(3);
gl3Technique->graphicsApiFilter()->setProfile(Qt3DRender::QGraphicsApiFilter::CoreProfile);
// Add technique to effect
effect->addTechnique(gl3Technique);
material->setEffect(effect);
// Add a color parameter
material->addParameter(new Qt3DRender::QParameter("color", QColor::fromRgbF(0.0f, 1.0f, 0.0f, 1.0f)));
// Add material to entity
torusEntity->addComponent(material);
// Debug shader compilation errors
QObject::connect(glShader, &Qt3DRender::QShaderProgram::statusChanged,
[=](Qt3DRender::QShaderProgram::Status status) {
if (status == Qt3DRender::QShaderProgram::Error) {
qDebug() << "Shader Error:" << glShader->log();
}
});
return torusEntity;
}