渲染SDK 0.1.2
载入中...
搜索中...
未找到
ADS材质示例

概述

本教程提供了Render SDK光照系数的使用教程,可通过该教程设置实体的环境光、漫反射光、镜面光系数。

前置条件

如果未初始化SDK,需要通过以下代码初始化

//创建sdk组件
auto mRenderComponent = AMCAXRender::CreateRenderComponent(this);
//创建实体Render
auto mRender = mRenderComponent->CreateBasicRender();
AMCAX_RENDER_API std::shared_ptr< IRenderComponent > CreateRenderComponent(QWidget *parent)
Create Render Component

示例代码

以下代码展示了创建平面视图模块的使用方法

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)
project(MaterialSample)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTOUIC ON)
add_definitions(-DUSE_AMCAX_KERNEL)
set(AMCAX_components AMCAXCommon AMCAXPart AMCAXStep AMCAXOCCTIO)
find_package(AMCAXCommon REQUIRED)
find_package(AMCAXStep REQUIRED)
find_package(AMCAXPart REQUIRED)
find_package(AMCAXOCCTIO REQUIRED)
list(APPEND CMAKE_PREFIX_PATH ${AMCAXRender_PATH})
find_package(AMCAXRender )
find_package(Qt6 COMPONENTS Widgets Core Gui REQUIRED)
list(APPEND CMAKE_PREFIX_PATH ${Json_PATH})
find_package(nlohmann_json 3.11.3 REQUIRED)
file(GLOB ALL_UI_FILES *.ui)
file(GLOB ALL_FILES *.cpp *.h)
add_executable(${PROJECT_NAME} ${ALL_UI_FILES} ${ALL_FILES})
target_link_libraries(${PROJECT_NAME} PRIVATE ${AMCAX_components})
target_link_libraries(${PROJECT_NAME} PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets)
target_link_libraries(${PROJECT_NAME} PRIVATE AMCAXRender::AMCAXRender)

main.cpp

#include <QApplication>
#include "Mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Mainwindow w;
w.show();
return a.exec();
}

Mainwindow.h

#pragma once
#include <QWidget>
#include "ui_Mainwindow.h"
#include <AMCAXRender.h>
class Mainwindow : public QWidget
{
Q_OBJECT
public:
Mainwindow(QWidget *parent = nullptr);
~Mainwindow();
private:
Ui::MainwindowClass ui;
std::shared_ptr<AMCAXRender::Material> material;
void onColorButtonClicked();
void onAmbientSliderChanged(int value);
void onDiffuseSliderChanged(int value);
void onSpecularSliderChanged(int value);
void onSpecularPowerSliderChanged(int value);
void handleOpenFile();
std::vector<AMCAXRender::EntityId> entityIds;
std::shared_ptr<AMCAXRender::IRenderComponent> mRenderComponent;
std::shared_ptr<AMCAXRender::CBasicRender> mRender;
};
Component Creation

Mainwindow.cpp

#include "Mainwindow.h"
#include <modeling/MakeBox.hpp>
#include <modeling/MakeCylinder.hpp>
#include <QColorDialog>
Mainwindow::Mainwindow(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
//初始化
mRenderComponent = AMCAXRender::CreateRenderComponent(this);
mRender = mRenderComponent->CreateBasicRender();
//窗口布局
ui.gridLayout_2->setColumnStretch(0, 1);
ui.gridLayout_2->setColumnMinimumWidth(1,200);
ui.gridLayout->addWidget(mRender->widget);
material = mRender->materialManager->CreateMaterial();
connect(ui.colorButton, &QPushButton::clicked, this, &Mainwindow::onColorButtonClicked);
connect(ui.importButton, &QPushButton::clicked, this, &Mainwindow::handleOpenFile);
connect(ui.ambientSlider, &QSlider::valueChanged, this, &Mainwindow::onAmbientSliderChanged);
connect(ui.diffuseSlider, &QSlider::valueChanged, this, &Mainwindow::onDiffuseSliderChanged);
connect(ui.specularSlider, &QSlider::valueChanged, this, &Mainwindow::onSpecularSliderChanged);
connect(ui.specularPowerSlider, &QSlider::valueChanged, this, &Mainwindow::onSpecularPowerSliderChanged);
}
Mainwindow::~Mainwindow()
{
}
void Mainwindow::onColorButtonClicked()
{
QColor color = QColorDialog::getColor();
if (color.isValid()) {
for (auto& id : entityIds)
{
double c[3] = { color.redF(), color.greenF(), color.blueF() };
mRender->entityManage->SetEntityColor(id, c);
}
}
mRender->styleManage->SetRenderMode(AMCAXRender::RenderMode::Shaded);
mRender->entityManage->DoRepaint();
}
void Mainwindow::handleOpenFile()
{
auto shape = AMCAX::MakeCylinder(5,15).Shape();
auto entity = mRender->entityFactory->FromShape(shape);
mRender->entityManage->AddEntity(entity);
entityIds.push_back(entity->GetEntityId());
mRender->cameraManage->ResetCamera();
mRender->entityManage->DoRepaint();
}
void Mainwindow::onAmbientSliderChanged(int value)
{
QSlider* slider = ui.ambientSlider;
double mappedValue = static_cast<double>(value - slider->minimum()) /
(slider->maximum() - slider->minimum());
material->SetAmbient(mappedValue);
for (auto& id : entityIds)
{
mRender->entityManage->SetMaterial(id, material);
}
mRender->entityManage->DoRepaint();
}
void Mainwindow::onDiffuseSliderChanged(int value)
{
QSlider* slider = ui.diffuseSlider;
double mappedValue = static_cast<double>(value - slider->minimum()) /
(slider->maximum() - slider->minimum());
material->SetDiffuse(mappedValue);
for (auto& id : entityIds)
{
mRender->entityManage->SetMaterial(id, material);
}
mRender->entityManage->DoRepaint();
}
void Mainwindow::onSpecularSliderChanged(int value)
{
QSlider* slider = ui.specularSlider;
double mappedValue = static_cast<double>(value - slider->minimum()) /
(slider->maximum() - slider->minimum());
material->SetSpecular(mappedValue);
for (auto& id : entityIds)
{
mRender->entityManage->SetMaterial(id, material);
}
mRender->entityManage->DoRepaint();
}
void Mainwindow::onSpecularPowerSliderChanged(int value)
{
QSlider* slider = ui.specularPowerSlider;
double mappedValue = static_cast<double>(value - slider->minimum()) /
(slider->maximum() - slider->minimum());
material->SetSpecularPower(mappedValue*128);
for (auto& id : entityIds)
{
mRender->entityManage->SetMaterial(id, material);
}
mRender->entityManage->DoRepaint();
}
@ Shaded
Shading Mode
定义 Constants.h:289

Mainwindow.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainwindowClass</class>
<widget class="QWidget" name="MainwindowClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>600</width>
<height>400</height>
</rect>
</property>
<property name="windowTitle">
<string>Mainwindow</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="1">
<layout class="QVBoxLayout" name="controlsLayout">
<property name="spacing">
<number>6</number>
</property>
<item>
<widget class="QPushButton" name="importButton">
<property name="text">
<string>Create Entity</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="colorButton">
<property name="text">
<string>Select Color</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="ambientLabel">
<property name="text">
<string>Ambient</string>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="ambientSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="diffuseLabel">
<property name="text">
<string>Diffuse</string>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="diffuseSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="specularLabel">
<property name="text">
<string>Specular</string>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="specularSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="specularPowerLabel">
<property name="text">
<string>SpecularPower</string>
</property>
</widget>
</item>
<item>
<widget class="QSlider" name="specularPowerSlider">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="0">
<layout class="QGridLayout" name="gridLayout"/>
</item>
</layout>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>

ADS示例预览

更多

更多设置请参考 IRenderComponent.h