我目前正在使用Qt3D模块在C ++ / Qt5中编写游戏。
我可以在QGLView上渲染场景(QGLSceneNodes),但我现在停留在用一些GUI元素重绘场景。我还没有决定是否使用QML或C ++来定义界面的外观,所以我愿意接受两者的解决方案。 (注意,QML模块称为QtQuick3D,C ++模块称为Qt3D,它们都是Qt5的一部分。)
我非常喜欢基于QML的解决方案。
我该如何做一些过度涂色?
必须具备以下条件:
我认为只使用另一个用于2D GUI部分的QGLSceneNode就可以实现这一切。但是我认为定位和定向一些场景节点以便在渲染过程中重新定位和重新定位(使用顶点着色器)没有意义,引入数值误差并且效率低下。
正在使用另一个“GUI”顶点着色器吗?
(怎么样)我可以一起制作后期渲染效果和过度绘画吗?
如果我可以执行以下后期渲染效果,那将非常好:
当我使用特殊的着色器程序实现重绘时,我发现了一个问题:我无法访问GUI后面的像素来应用高斯模糊等效果,使GUI看起来像玻璃。 我必须将场景渲染到另一个缓冲区而不是QGLView。这是我的主要问题......
答案 0 :(得分:1)
我也有类似的问题通过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++;
}
以下是为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();
}
这是处理hud东西的主要绘图功能;大多数绘制功能类似于大的“背景板”块。你基本上推模型视图矩阵,像在3d中一样使用translate / rotate / scale,绘制控件,然后使用popmatrix:
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();
}
现在为控件本身。我的所有控件,如按钮,滑块,标签等,都来自一个公共基类,它具有空的虚函数,允许它们与其他类型的东西进行交互。基础具有典型的东西,如高度,坐标等,以及一些处理回调和文本渲染的指针。还包括通用的hittest(x,y)函数,它告诉它是否已被点击。如果你想要椭圆控制,你可以使这个虚拟。这是基类,hittest和简单按钮的代码。:
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);
}
}
由于像滑块这样的复合控件有多个按钮,而滚动框有滚动和滑块,因此对于绘图/鼠标事件都需要递归。每个控件还有一个通用的回调函数,它传递一个值和它自己的地址;这允许父母测试每个孩子的孩子以查看谁在呼叫,并做出适当的反应(例如上/下按钮)。请注意,通过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);
}
}
现在我们有自包含的控件呈现为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...
}
}
总的来说这个系统有点hacky,我没有添加一些功能,如键盘响应或调整大小,但这些应该很容易添加,可能需要回调中的“事件类型”(即鼠标或键盘)。你甚至可以将glBase子类化到最顶层的小部件中,它甚至可以让它接收parent-&gt; childcallback。虽然这是一项很多工作,但这个系统只需要vanilla c ++ / opengl,它使用的本地Qt内容比使用本机Qt少得多,可能只需要一两个数量级。如果您还需要更多信息,请询问。
答案 1 :(得分:1)
resize和事件很容易实现 这是一个简短的视频,可以帮助您入门
https://www.youtube.com/watch?v=1nzHSkY4K18
关于绘制图像和文本qt有qpainter可以帮助你这样做, 使用QGLContext(http://qt-project.org/doc/qt-4.8/qglcontext.html)将它传递给当前上下文并在特定的cordinate中呈现你的标签/图像/小部件,但要确保这个函数调用是你的paint事件中的最后一个,否则它不会在顶部< / p>
对鼠标事件作出反应使你的类包含QMouseEvent标题然后你可以通过重载受保护事件mouseMoveEvent(QMouseEvent * event){event-&gt; pos()来获取窗口小部件中鼠标的坐标并将它们传递给opengl ;}