All figures outside of z-axis: (1;-1) range get clipped. Here is some code:
void MainWindow::initializeGL()
{
glDepthRange(-2,2);
glEnable(GL_TEXTURE_2D);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
//glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(1, 1, 0, 0);
glDisable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LESS); // The Type Of Depth Test To Do
glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void MainWindow::paintGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
// glEnable(GL_TEXTURE_2D);
// glBindTexture(GL_TEXTURE_2D,texture);
// glTexSubImage2D(GL_TEXTURE_2D, 0, 0,0 , image.width(), image.height(), 0, 0, image.bits() );
//glMatrixMode(GL_MODELVIEW);
//glEnable(GL_DEPTH_TEST);
glTranslated(0.0, 0.0, 1.9);
qglColor(Qt::black);
glBegin(GL_TRIANGLES);
glVertex3d(-0.1,0.1,1);
glVertex3d(-0.1,-0.1,-1);
glVertex3d(0.1,-0.1,0);
glEnd();
}
Any idea why does it happen?
This is actually completely normal behavior.
When you use identity modelview and projection matrices, your coordinates are in clip-space. The default W value for a 3D vertex in OpenGL is 1.0 (vertices are always 4D), and clip-space -> NDC works by dividing each component of a vertex by its W component and then clipping anything with a coordinate outside the range [-1,1].
I think what is confusing you is the glDepthRange (...) call. That does not affect clipping. Depth range is part of the viewport transformation, which happens after clipping.
Related
I create a 3D scene using Qt3D, there is an orthographic camera and a plane in the scene, I want the camera to capture the plane just right. The problem is, when I call QTransform::setScale3D() to scale up the plane along with Z-axis, increasing from 1, when the value acoss 2, part of the image goes black suddenly. After a lot of testing, I ensure that the position and rotation of the plane and the camera all not changed.
Some codes are as follows:
rootEntity = new Qt3DCore::QEntity;
// create a plane
plane = new Qt3DCore::QEntity(rootEntity);
// mesh
auto planeMesh = new Qt3DExtras::QPlaneMesh;
planeMesh->setWidth(1.0f);
planeMesh->setHeight(1.0f);
// transform
transform = new Qt3DCore::QTransform;
transform->setTranslation(QVector3D(0, 0, 0));
transform->setRotation(QQuaternion::fromEulerAngles(QVector3D(0, 0, 0)));
transform->setScale3D(QVector3D(1, 1, 2));
//transform->setScale3D(QVector3D(m_planeWidth, 1, m_planeHeight));
// material
//auto mat = new Qt3DExtras::QPhongMaterial;
texMat = new Qt3DExtras::QTextureMaterial;
// add component
plane->addComponent(planeMesh);
plane->addComponent(transform);
plane->addComponent(texMat);
// configure camera
camera()->setPosition(QVector3D(0.0f, 1.0f, 0.0f));
//camera()->rotate(QQuaternion::fromEulerAngles(QVector3D(-90, 0, 0)));
camera()->transform()->setRotation(QQuaternion::fromEulerAngles(QVector3D(-90, 0, 0)));
//camera()->setViewCenter(QVector3D(0.0f, 0.0f, 0.0f));
camera()->lens()->setOrthographicProjection(
m_planeWidth / 2, // left
m_planeWidth / 2, // right
m_planeHeight / 2, // bottom
m_planeHeight / 2, // top
0.3f, // nearPlane
1000.0f); // farPlane
Screenshots:
When the plane's scale-Z is 1:
When the plane's scale-Z goes to 2:
Any helpful discussion is welcome. Thanks in advance.
OK, I set the parameter left/right/bottom/top of the orthographic camera incorrectly. The left and bottom should be negative, but I set them all positive. I found my mistake when I printed the default values of those 6 properties of the orthographic camera, they are -0.5, 0.5, -0.5, 0.5. It's a little embarrassed.
I'm trying to use glBlendFunc in QOpenGLWidget (in paintGL), but objects do not mix (alpha is works).
My code:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(blenFunc, GL_ONE);
m_world.setToIdentity();
m_world.rotate((m_xRot / 16.0f), 1, 0, 0);
m_world.rotate(m_yRot / 16.0f, 0, 1, 0);
m_world.rotate(m_zRot / 16.0f, 0, 0, 1);
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_program->bind();
m_tex->bind();
fillYoffsetLightning();
const GLfloat scaleFactor = 0.05f;
m_world.scale(scaleFactor, scaleFactor, 0.0f);
m_world.translate(0.f, 0.0f, 0.0f);
const GLfloat fact = 1 / scaleFactor;
const uint8_t X = 0, Y = 1;
for(int i = 0; i < maxElem; ++i) {
const GLfloat offX = m_ELECT[i][X] * fact;
const GLfloat offY = m_ELECT[i][Y] * fact;
m_world.translate(offX, offY);
m_program->setUniformValue(m_projMatrixLoc, m_proj);
m_program->setUniformValue(m_mvMatrixLoc, m_camera * m_world);
QMatrix3x3 normalMatrix = m_world.normalMatrix();
m_program->setUniformValue(m_normalMatrixLoc, normalMatrix);
glDrawArrays(GL_TRIANGLE_FAN, 0, m_logo.vertexCount());
update();
m_world.translate(-offX, -offY);
}
m_program->release();
shaders are simple:
// vertex
"attribute highp vec4 color;\n"
"varying highp vec4 colorVertex;\n"
//......... main:
"colorVertex = color;\n"
// fragment
"varying highp vec4 colorVertex;\n"
//......... main:
"gl_FragColor = colorVertex;\n"
Color is:
a pentagon with a gradient from white from center to blue to the edges is drawn (center color is 1,1,1, edges is 0,0,0.5)
screenshoot
Why is this happening?
If you want to achieve a blending effect, the you have to disable the depth test:
glDisable(GL_DEPTH_TEST);
Note, the default depth test function is GL_LESS. If a fragment is draw on a place of a previous fragment, then it is discarded by the depth test, because this condition is not full filled.
If the depth test is disabled, then the fragments are "blended" by the blending function (glBlendFunc) and equation (glBlendEquation).
I recommend to use the following blending function:
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
In my case (Qt 5.15.2) I found that using a color call with no alpha component (eg. glColor3f(1,0,0) ) causes the blending to be disabled for any subsequent rendering. To my surprise I could not even recover it by re-issuing these commands:
glEnable(GL_BLEND); // wtf has no effect
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
Blending simply remained disabled until the next paint begins. This did not happen with the original QGLWidget class. It only happens with QOpenGLWidget and only on Windows (Mac + Linux are fine).
The good-enough solution for me was to replace any non-alpha color calls with alpha equivalents, at least for cases where you need to use blending later in the render. Eg.
glColor3f(1,0,0); // before
glColor4f(1,0,0,1); // after
Another issue that might come up is if you use QPainter along with direct rendering, because the QPainter will trash your OpenGL state. See the mention of 'beginNativePainting' in the docs:
https://doc.qt.io/qt-5/qopenglwidget.html#painting-techniques
EDIT: I'll add this here because my comment on Rabbid's answer was deleted for some reason - the depth test does NOT need to be disabled to use blending. Rabbid might be thinking of disabling depth buffer writes which is sometimes done to allow drawing all translucent objects without having to sort them in order of furthest to nearest:
Why we disable Z-write in blending
I'm trying to figure out how does glOrtho work. I always have (0, 0) point in (w/2, h/2) means it the center of my opengl widget, however I changed this behaviour with glOrtho:
void Widget::initializeGL() {
glClearColor(0.1, 0.1, 0.1, 0);
}
void Widget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(1, 0, 0);
glBegin(GL_QUADS);
glVertex2f(0.0, 0.0);
glVertex2f(1.0, 1.0);
glVertex2f(0.5, 0.5);
glVertex2f(0.2, 0.7);
glEnd();
}
void Widget::resizeGL(int w, int h) {
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 0.0);
}
So I always get the same picture
I want the coodinates were starting from left bottom or top left corner, but not from the center.
You don't reset the matrix mode to model-view after setting the projection matrix. So the call to glLoadIdentity() in paintGL() will reset any projection matrix you have set up.
void Widget::resizeGL(int w, int h) {
...
glMatrixMode(GL_MODELVIEW);
}
Keep in mind that this will draw something that is less than a pixel large. So you may want to change your geometry, too.
Btw, if you are just starting, I would recommend not to learn the deprecated OpenGL matrix stack or glVertex2f. It might be easier on first sight, but you will hit a wall in the long run.
I'm trying to figure out how does glOrtho work. I always have (0, 0)
point in (w/2, h/2) means it the center of my opengl widget, however I
changed this behaviour with glOrtho:
glOrtho(0.0, (GLdouble) w, (GLdouble) h, 0.0, 0.0, 0.0);
No, you did not change anything with glOrtho - you are not using glOrtho at all. Your glOrtho call will just generate a GL_INVALID_VALUE error and do nothing else, because it is invalid to cal it with near == far. As a result, you are using an identity matrix as projection, and this means that (0,0) is the center of the screen, (1,1) to top right corner, and so on.
So even if you fix your code to not overwrite the projection matrix with an identy matrix, as #Nico Schertler suggested,it won't change a thing.
I also second Mr. Schertler's recommendation: don't use this old crap. Fixed function pipeline is deprecated for almost a decade, and immediate mode (glBegin/glEnd) is out of fashion since 20 years.
With Qt and opengl, I would like draw in a QGlFrameBufferObject ?
I try this :
QGLFrameBufferObject * fbo = new QGLFramebufferObject(200, 200, QGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA32F);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, fbo->size().width(), fbo->size().height(), 0.0f, 0.0f, 1.0f);
fbo->bind();
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
fbo->release();
fbo->toImage().save("test.jpg"));
But I don't get a red image.
OTHER QUESTION
And if I want draw with :
glBegin(GL_QUAD);
glColor3d(0.2, 0.5, 1.0);
glVertex2i( 10, 20);
glColor3d(0.7, 0.5, 1.0);
glVertex2i( 15, 20);
glColor3d(0.8, 0.4, 1.0);
glVertex2i( 15, 25);
glColor3d(0.1, 0.9, 1.0);
glVertex2i( 10, 25);
glEnd();
Do I need also glClear() ?
You never actually clear the framebuffer. glClearColor() only sets the color used for clearing, but does not clear anything. You will need to add the second line here:
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
The glClear() call will clear the color buffer with the clear color you specified in the first call. The framebuffer content is initially undefined, so you should always clear the framebuffer with a glClear() call before you start drawing. The only exception is if you're certain that the primitives you render will cover every pixel of the drawing surface. Even then, on some architectures it can actually be better for performance to still call glClear() first.
It shouldn't matter yet as long as you only clear the buffer. But once you want to start drawing, you will also need to set the viewport:
glViewport(0, 0, 200, 200);
This question already has answers here:
Opengl: 2d HUD over 3D
(3 answers)
Closed 6 years ago.
I have a subclass "GLWidget" of QGLWidget where I render a 3D scene. I can do rotations and zoom on it.
I call with a QTimer the following function :
void GLWidget::processCurrent()
{
draw();
printStats();
glFlush();
swapBuffers();
}
and the main OpenGL render function "draw()" is :
void GLWidget::draw()
{
if (isDisplayFirst)
{
isDisplayFirst = false;
glViewport(0, 0, w_width, w_height);
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrixi
gluPerspective(45.0f, (float)w_width / w_height, g_nearPlane, g_farPlane);
gluLookAt (0.0, 0.0, 3.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(0.03f, 0.03f, 0.03f);
}
rotateScene();
glClearColor(0.0 ,0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
glEnable(GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE);
GLuint vbo_disk = 0;
glBindBuffer(GL_ARRAY_BUFFER, vbo_disk);
glVertexPointer(4, GL_DOUBLE, 4*sizeof(double), Galaxy->pos);
glEnableClientState(GL_VERTEX_ARRAY);
glColor4f(1.0f, 1.0f, 1.0f, 0.2f);
glDrawArrays(GL_POINTS, 0, Galaxy->getNumParticles_disk());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDisableClientState(GL_VERTEX_ARRAY);
glDisable(GL_BLEND);
glDisable(GL_POINT_SPRITE);
}
I have already rendered 2D text (statistics) on this GLWidget with "printStats()" function. I would like now to draw a "2D scale line" on this 3D scene.
I succeed in draw this line by doing into printStats():
glDisable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
glLineWidth(3.0f);
glBegin(GL_LINES);
glVertex2d(5, 40);
glVertex2d(20, 40);
glEnd();
glEnable(GL_TEXTURE_2D);
but my problem is that when I do rotations or zoom, this line moves with the 3D scene.
What is the way to lock this 2D line while being able to do operations on the 3D object ?
I want to avoid to use "overpainting" with overriding paintEvent.
I also try to use glPush/Pop Matrix but there are conflicts between 2D and 3D.
Could you give me some advice to get it ?
You are trying to implement HUD functionality, in principle what you need to to draw your 3D scene normaly, then backup all the OpenGL states that you want to reuse, and set up a new projection matrix with gluOrtho2D or glOrtho. Additionally you want to turn off the depth check.
After the call to the ortho function you will most likely draw in screen scale, so you will have to calculate the size of your line accordingly. Once you are done with the hud, you can pop the changes off the stack so you can draw again.
There are a couple of questions on stackoverflow that deal with drawing an OpenGl HUD
OpenGl 2D HUD over 3D
OpenGl 2d HUD in 3D Application
and a pretty decent discussion on the gamedev forum
http://www.gamedev.net/topic/388298-opengl-hud/