lee*_*mes 13 c++ opengl qml qt5 qt3d
我目前正在使用Qt3D模块在C++/Qt5中编写游戏.
我可以在QGLView上渲染场景(QGLSceneNodes),但现在我不得不用一些GUI元素重复绘制场景.我还没有决定是否使用QML或C++来定义界面的外观,所以我愿意接受两者的解决方案.(注意,QML模块称为QtQuick3D,C++模块称为Qt3D,它们都是Qt5的一部分.)
我非常喜欢基于QML的解决方案.
我该如何做一些过度涂色?
必须具备以下条件:
我认为这只是使用另一个QGLSceneNode用于2D GUI部分.但是我认为定位和定向一些场景节点以便在渲染期间(使用顶点着色器)重新定位和重新定位没有意义,引入数值误差并且看起来效率低下.
是否正确使用另一个"GUI"顶点着色器?
(如何)我可以一起制作后期渲染效果和重复绘画?
如果我可以执行以下后期渲染效果,那将是非常好的:
当我使用特殊的着色器程序实现重绘时,我发现了一个问题:我无法访问GUI后面的像素来应用高斯模糊等效果,使GUI看起来像玻璃.我必须将场景渲染到另一个缓冲区而不是QGLView.这是我的主要问题......
我在通过 qglwidget 制作 opengl 游戏时也遇到过类似的问题,我想出的解决方案是使用正交投影来渲染临时构建的控件。在主绘制函数的末尾,我调用一个单独的函数来设置视图,然后绘制 2d 位。至于鼠标事件,我捕获发送到主小部件的事件,然后结合使用命中测试、递归坐标偏移和允许嵌套控件的伪回调。在我的实现中,我一次将命令转发到顶层控件(主要是滚动框),但如果需要动态添加控件,则可以轻松添加链接列表结构。我目前仅使用四边形将部件渲染为彩色面板,但添加纹理甚至使它们成为响应动态光照的 3D 组件应该很简单。代码很多,所以我将把相关的部分分成几部分:
void GLWidget::paintGL() {
if (isPaused) return;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLightfv(GL_LIGHT0, GL_POSITION, FV0001);
gluLookAt(...);
// Typical 3d stuff
glCallList(Body::glGrid);
glPopMatrix();
//non3d bits
drawOverlay(); // <---call the 2d rendering
fpsCount++;
}
Run Code Online (Sandbox Code Playgroud)
以下是为 2d 渲染设置模型视图/投影矩阵的代码:
void GLWidget::drawOverlay() {
glClear(GL_DEPTH_BUFFER_BIT);
glPushMatrix(); //reset
glLoadIdentity(); //modelview
glMatrixMode(GL_PROJECTION);//set ortho camera
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0,width()-2*padX,height()-2*padY,0); // <--critical; padx/y is just the letterboxing I use
glMatrixMode(GL_MODELVIEW);
glDisable(GL_DEPTH_TEST); // <-- necessary if you want to draw things in the order you call them
glDisable( GL_LIGHTING ); // <-- lighting can cause weird artifacts
drawHUD(); //<-- Actually does the drawing
glEnable( GL_LIGHTING );
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION); glPopMatrix();
glMatrixMode(GL_MODELVIEW); glPopMatrix();
}
Run Code Online (Sandbox Code Playgroud)
这是处理 HUD 内容的主要绘图函数;大多数绘图功能类似于大“背景面板”块。您基本上推送模型视图矩阵,像在 3d 中一样使用平移/旋转/缩放,绘制控件,然后弹出矩阵:
void GLWidget::drawHUD() {
float gridcol[] = { 0.5, 0.1, 0.5, 0.9 };
float gridcolB[] = { 0.3, 0.0, 0.3, 0.9 };
//string caption
QString tmpStr = QString::number(FPS);
drawText(tmpStr.toAscii(),120+padX,20+padY);
//Markers
tGroup* tmpGroup = curGroup->firstChild;
while (tmpGroup != 0) {
drawMarker(tmpGroup->scrXYZ);
tmpGroup = tmpGroup->nextItem;
}
//Background panel
glEnable(GL_BLEND);
glTranslatef(0,height()*0.8,0);
glBegin(GL_QUADS);
glColor4fv(gridcol);
glVertex2f(0.0f, 0.0);
glVertex2f(width(), 0);
glColor4fv(gridcolB);
glVertex2f(width(), height()*0.2);
glVertex2f(0.0f, height()*0.2);
glEnd();
glDisable(GL_BLEND);
glLoadIdentity(); //nec b/c of translate ^^
glTranslatef(-padX,-padY,0);
//Controls
cargoBox->Draw();
sideBox->Draw();
econBox->Draw();
}
Run Code Online (Sandbox Code Playgroud)
现在谈谈控件本身。我的所有控件(如按钮、滑块、标签等)都继承自具有空虚拟函数的公共基类,这些函数允许它们以类型无关的方式相互交互。基础有高度、坐标等典型的东西,以及一些处理回调和文本渲染的指针。还包括通用的 hittest(x,y) 函数,用于判断是否已被单击。如果您想要椭圆控制,您可以将其设为虚拟。这是基类、命中测试和一个简单按钮的代码:
class glBase {
public:
glBase(float tx, float ty, float tw, float th);
virtual void Draw() {return;}
virtual glBase* mouseDown(float xIn, float yIn) {return this;}
virtual void mouseUp(float xIn, float yIn) {return;}
virtual void mouseMove(float xIn, float yIn) {return;}
virtual void childCallBack(float vIn, void* caller) {return;}
bool HitTest(float xIn, float yIn);
float xOff, yOff;
float width, height;
glBase* parent;
static GLWidget* renderer;
protected:
glBase* callFwd;
};
bool glBase::HitTest(float xIn, float yIn) { //input should be relative to parents space
xIn -= xOff; yIn -= yOff;
if (yIn >= 0 && xIn >= 0) {
if (xIn <= width && yIn <= height) return true;
}
return false;
}
class glButton : public glBase {
public:
glButton(float tx, float ty, float tw, float th, char rChar);
void Draw();
glBase* mouseDown(float xIn, float yIn);
void mouseUp(float xIn, float yIn);
private:
bool isPressed;
char renderChar;
};
glButton::glButton(float tx, float ty, float tw, float th, char rChar) :
glBase(tx, ty, tw, th), isPressed(false), renderChar(rChar) {}
void glButton::Draw() {
float gridcolA[] = { 0.5, 0.6, 0.5, 1.0 };//up
float gridcolB[] = { 0.2, 0.3, 0.2, 1.0 };
float gridcolC[] = { 1.0, 0.2, 0.1, 1.0 };//dn
float gridcolD[] = { 1.0, 0.5, 0.3, 1.0 };
float* upState;
float* upStateB;
if (isPressed) {
upState = gridcolC;
upStateB = gridcolD;
} else {
upState = gridcolA;
upStateB = gridcolB;
}
glPushMatrix();
glTranslatef(xOff,yOff,0);
glBegin(GL_QUADS);
glColor4fv(upState);
glVertex2f(0, 0);
glVertex2f(width, 0);
glColor4fv(upStateB);
glVertex2f(width, height);
glVertex2f(0, height);
glEnd();
if (renderChar != 0) //center
renderer->drawChar(renderChar, (width - 12)/2, (height - 16)/2);
glPopMatrix();
}
glBase* glButton::mouseDown(float xIn, float yIn) {
isPressed = true;
return this;
}
void glButton::mouseUp(float xIn, float yIn) {
isPressed = false;
if (parent != 0) {
if (HitTest(xIn, yIn)) parent->childCallBack(0, this);
}
}
Run Code Online (Sandbox Code Playgroud)
由于像滑块这样的复合控件有多个按钮,而滚动框有磁贴和滑块,因此对于绘图/鼠标事件来说,所有这些都需要递归。每个控件还有一个通用回调函数,用于传递一个值及其自己的地址;这允许父级测试每个子级以查看谁在呼叫,并做出适当的响应(例如向上/向下按钮)。请注意,子控件是通过 c/dtors 中的 new/delete 添加的。此外,子控件的draw()函数在父控件自己的draw()调用期间被调用,这使得所有内容都是独立的。以下是中级滑动条的相关代码,其中包含 3 个子按钮:
glBase* glSlider::mouseDown(float xIn, float yIn) {
xIn -= xOff; yIn -= yOff;//move from parent to local coords
if (slider->HitTest(xIn,yIn-width)) { //clicked slider
yIn -= width; //localize to field area
callFwd = slider->mouseDown(xIn, yIn);
dragMode = true;
dragY = yIn - slider->yOff;
} else if (upButton->HitTest(xIn,yIn)) {
callFwd = upButton->mouseDown(xIn, yIn);
} else if (downButton->HitTest(xIn,yIn)) {
callFwd = downButton->mouseDown(xIn, yIn);
} else {
//clicked in field, but not on slider
}
return this;
}//TO BE CHECKED (esp slider hit)
void glSlider::mouseUp(float xIn, float yIn) {
xIn -= xOff; yIn -= yOff;
if (callFwd != 0) {
callFwd->mouseUp(xIn, yIn); //ok to not translate b/c slider doesn't callback
callFwd = 0;
dragMode = false;
} else {
//clicked in field, not any of the 3 buttons
}
}
void glSlider::childCallBack(float vIn, void* caller) { //can use value blending for smooth
float tDelta = (maxVal - minVal)/(10);
if (caller == upButton) {//only gets callbacks from ud buttons
setVal(Value - tDelta);
} else {
setVal(Value + tDelta);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们已经有了可以渲染到 opengl 上下文中的自包含控件,所需要的只是来自顶级控件的接口,例如来自 glWidget 的鼠标事件。
void GLWidget::mousePressEvent(QMouseEvent* event) {
if (cargoBox->HitTest(event->x()-padX, event->y()-padY)) { //if clicked first scrollbox
callFwd = cargoBox->mouseDown(event->x()-padX, event->y()-padY); //forward the click, noting which last got
return;
} else if (sideBox->HitTest(event->x()-padX, event->y()-padY)) {
callFwd = sideBox->mouseDown(event->x()-padX, event->y()-padY);
return;
} else if (econBox->HitTest(event->x()-padX, event->y()-padY)) {
callFwd = econBox->mouseDown(event->x()-padX, event->y()-padY);
return;
}
lastPos = event->pos(); //for dragging
}
void GLWidget::mouseReleaseEvent(QMouseEvent *event) {
if (callFwd != 0) { //Did user mousedown on something?
callFwd->mouseUp(event->x()-padX, event->y()-padY);
callFwd = 0; //^^^ Allows you to drag off a button before releasing w/o triggering its callback
return;
} else { //local
tBase* tmpPick = pickObject(event->x(), event->y());
//normal clicking, game stuff...
}
}
Run Code Online (Sandbox Code Playgroud)
总的来说,这个系统有点老套,我没有添加一些功能,如键盘响应或调整大小,但这些应该很容易添加,可能需要回调中的“事件类型”(即鼠标或键盘)。您甚至可以将 glBase 子类化为最顶层的小部件,它甚至允许它接收父->子回调。虽然工作量很大,但这个系统只需要 vanilla c++/opengl,并且它使用的 cpu/内存资源比原生 Qt 少得多,可能少一两个数量级。如果您需要更多信息,请询问。
| 归档时间: |
|
| 查看次数: |
2862 次 |
| 最近记录: |