Hello everyone,
I'm having a slight problem with glScissors, I'm trying to make a basis for my GUI for a template project I'm trying to make.
I'm using glScissors to clip the child GUI nodes to fit in their parents.
This is result of the code below:
This is how it looks like without glScissors enabled:
And this is how it should look like:
What you see here is that the red rectangle is the root node (for testing I've made it smaller, but normally this would be screen sized and with the colour: 0,0,0,0).
The green quad is the first child of the root it has the same dimensions (0.5:0.05) and is offsetted the same as the root (except that it is relative to the root instead of the screen).
The blue quad is the grand child of root, it has the same size, but it is offsetted by -0.15:-0.15 so it should disappear partly at the left and top.
Last but not least, here is my Node render source:
void GUINode::Render()
{
// Draw current GUI
glTranslated(x,y,0);
glBegin(GL_QUADS);
glColor4d(r,g,b,a);
glVertex2d(w,0);
glVertex2d(0,0);
glVertex2d(0,h);
glVertex2d(w,h);
glEnd();
// Retrieve viewport and scissor size
GLint* viewport = new GLint[4];
GLint* scissor = new GLint[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glGetIntegerv(GL_SCISSOR_BOX, scissor);
// Calculate new scissor size (if it would be the root node)
GLint nX,nY,nW,nH;
nX = (GLint)(scalar(viewport[2])*x)+scissor[0];
nY = (GLint)(scalar(viewport[3])*y)+scissor[1];
nW = (GLint)(scalar(viewport[2])*w);
nH = (GLint)(scalar(viewport[3])*h);
std::cout<<"Requested:"<<nX<<":"<<nY<<":"<<nW<<":"<<nH<<std::endl;
std::cout<<"Got: "<<(scissor[0] > nX ? scissor[0] : nX)<<":"<<
(scissor[1] > nY ? scissor[1] : nY)<<":"<<
(scissor[2] < nW ? scissor[2] : nW)<<":"<<
(scissor[3] < nH ? scissor[3] : nH)<<std::endl;
// Make the scissor size always smaller
/* This means that the X and Y values can only get bigger
* And that the W and H values can only get smaller*/
glScissor((scissor[0] > nX ? scissor[0] : nX),
(scissor[1] > nY ? scissor[1] : nY),
(scissor[2] < nW ? scissor[2] : nW),
(scissor[3] < nH ? scissor[3] : nH));
// Go through the child nodes and render them into the visible space of this node
BOOST_FOREACH(boost::shared_ptr<GUINode>& node, children)
node->Render();
// Reset translation for the siblings
glTranslated(-x,-y,0);
// Reset scissor for the siblings
glScissor(scissor[0],scissor[1],
scissor[2],scissor[3]);
delete[] viewport;
delete[] scissor;
}
This is the code that starts the rendering:
void GUIManager::Render()
{
glPushAttrib(GL_ALL_ATTRIB_BITS);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f,1.0f,1.0f,0.0f,-1.0f,1.0f);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_SCISSOR_TEST);
GLint* viewport = new GLint[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glScissor(viewport[0],viewport[1],
viewport[2],viewport[3]);
delete[] viewport;
std::cout<<"Starting"<<std::endl;
root->Render();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glPopAttrib();
}
Here is root a GUINode to which all the other nodes are attached to.
I'm staring the problem in the face and I can't find it...
I just hope that a fresh pair of eyes will spot the error.
Thank you for your time.