OpenGL glOrtho and GL_DEPTH_TEST - opengl

I'm trying to draw a simple triangle and I've faced a problem combining glEnable(GL_DEPTH_TEST) and glOrtho. The triangle is displayed without depth test not depending on the nearVal argument but if the depth test is enabled and the nearVal is different from 0 then nothing is displayed. Here is the code.
#Override
public void init(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glEnable(GL2.GL_DEPTH_TEST);
gl.glDepthFunc(GL2.GL_LEQUAL);
gl.glClearColor(0, 0, 0, 1);
gl.glClearDepth(100.0);
gl.glMatrixMode(GL2.GL_PROJECTION);
gl.glLoadIdentity();
// the nearVal = 1 and the triangle is not displayed
// if we set the nearVal = 1 then the triangle is displayed
gl.glOrtho(-1, 1, -1, 1, 1, 10);
}
#Override
public void display(GLAutoDrawable drawable) {
GL2 gl = drawable.getGL().getGL2();
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
gl.glBegin(GL.GL_TRIANGLES);
gl.glColor3f(1, 0, 0);
gl.glVertex3f(0.0f, 0.0f, 0);
gl.glColor3f(0, 1, 0);
gl.glVertex3f(1, 0.0f, 0);
gl.glColor3f(0, 0, 1);
gl.glVertex3f(0f, 1f, 0);
gl.glEnd();
gl.glFlush();
}

Your glVertex3f calls define a triangle on the z=0 plane. If you set the nearPlane parameter of the glOrtho call to one, then your triangle will be clipped out.

Related

problem with QOpenGLWidget, QOpenGLWindow

Can anyone tell me why the following code in my paintGL does not draw when using the DrawArrays function? I'm using Qt 5.14.2 in Qt Creator on the last Windows 10.
A few comments;
I'm experimenting to try to understand the differences between implementations of OpenGL in Qt using the following declarations. In time, I will write my app using the implementation that I like best.
class OpenGLWindow : public QWindow, public QOpenGLFunctions
This works fine when I place the following code in the render() function, no issues at all, very nice!!
class myOpenGLWidget : public QOpenGLWidget, public QOpenGLFunctions
I place the code in the paintGL function. I can color the background, but glDrawArrays() does nothing. However, I can draw a triangle, the code between the glBegin and glEnd statements is successful. I'm not getting any errors. I test for the result of the m_program->bind() call and it comes back true.
class myQOpenGLWindow : public QOpenGLWindow, protected QOpenGLFunctions
Same as #2 except that I place the code in the render() function. I can color the background, but glDrawArrays() does nothing. However, I can draw a triangle, the code between the glBegin and glEnd statements is successful. I test for the result of the m_program->bind() call and it comes back true.
If anybody needs to ask, I'm doing it this way after perusing dozens of different tutorials, it's the best information that I've been able to find.
thank you!!
{
// Draw the scene:
QOpenGLFunctions *f = QOpenGLContext::currentContext()->functions();
const qreal retinaScale = devicePixelRatio();
f->glViewport(0, 0, width() * retinaScale, height() * retinaScale);
f->glClearColor(red, green, blue, 1.0f);
f->glClear(GL_COLOR_BUFFER_BIT);
QMatrix4x4 matrix;
matrix.perspective(60.0f, 4.0f / 3.0f, 0.1f, 100.0f);
matrix.translate(0, 0, 0);
matrix.rotate(0, 1, 0, 0);
m_program->setUniformValue(m_matrixUniform, matrix);
glBegin(GL_TRIANGLES);
glColor3f(1.0, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0);
glColor3f(0.0, 1.0, 0.0);
glVertex3f( 0.5, -0.5, 0);
glColor3f(0.0, 0.0, 1.0);
glVertex3f( 0.0, 0.5, 0);
glEnd();
//f->glBindTexture(GL_TEXTURE_2D, 0);
bound = m_program->bind();
GLfloat line_vertices[2160];
for (int v=0;v<360;v++)
{
line_vertices[(6*v)]=-3.5+float(v)/25;
line_vertices[(6*v)+1]=1.1+qSin(5*2*v*(M_PI)/180);
line_vertices[(6*v)+2]=-5;
line_vertices[(6*v)+3]=-3.5+float(v+1)/25;
line_vertices[(6*v)+4]=1.1+qSin(5*2*(v+1)*(M_PI)/180);;
line_vertices[(6*v)+5]=-5;
}
GLfloat line_colors[2160];
for (int v=0;v<360;v++)
{
line_colors[(6*v)]=1.0;
line_colors[(6*v)+1]=0;
line_colors[(6*v)+2]=0;
line_colors[(6*v)+3]=1.0;
line_colors[(6*v)+4]=0;
line_colors[(6*v)+5]=0;
}
f->glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, line_vertices);
f->glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, line_colors);
f->glEnableVertexAttribArray(m_posAttr);
f->glEnableVertexAttribArray(m_colAttr);
f->glDrawArrays(GL_LINES, 0, 360);
f->glDisableVertexAttribArray(m_colAttr);
f->glDisableVertexAttribArray(m_posAttr);
m_program->release();
}
GL_VERSION = 4.6.0
Here is the section of code where I define the position and color vertices, the VBO, various setting up of position and color buffers, and then the glDrawArrays function. I have tried this code in both the render() and in the paintgl() functions. I get no errors, but I get no pretty lines either. The triangle vertices defined between begin() and end() do show up though.
GLfloat line_vertices[2160];
for (int v=0;v<360;v++)
{
line_vertices[(6*v)]=-3.5+float(v)/25;
line_vertices[(6*v)+1]=1.1+qSin(5*2*v*(M_PI)/180);
line_vertices[(6*v)+2]=-5;
line_vertices[(6*v)+3]=-3.5+float(v+1)/25;
line_vertices[(6*v)+4]=1.1+qSin(5*2*(v+1)*(M_PI)/180);;
line_vertices[(6*v)+5]=-5;
}
GLfloat line_colors[2160];
for (int v=0;v<360;v++)
{
line_colors[(6*v)]=1.0;
line_colors[(6*v)+1]=0;
line_colors[(6*v)+2]=0;
line_colors[(6*v)+3]=1.0;
line_colors[(6*v)+4]=0;
line_colors[(6*v)+5]=0;
}
QOpenGLBuffer m_vertexBufferCube;
QOpenGLBuffer m_colorBufferCube;
m_program->setUniformValue(m_matrixUniform, matrix);
m_vertexBufferCube.create();
m_vertexBufferCube.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_vertexBufferCube.allocate(line_vertices, 3 * 360 * sizeof(float));
m_colorBufferCube.create();
m_colorBufferCube.setUsagePattern(QOpenGLBuffer::StaticDraw);
m_colorBufferCube.allocate(line_colors, 3 * 360 * sizeof(float));
bound = m_vertexBufferCube.bind();
//m_program->setAttributeBuffer("vertexPosition", GL_FLOAT,0,3);
m_program->setAttributeBuffer(m_posAttr, GL_FLOAT,0,3);
m_colorBufferCube.bind();
//m_program->setAttributeBuffer("colorPosition", GL_FLOAT,0,3);
m_program->setAttributeBuffer(m_colAttr, GL_FLOAT,0,3);
//glVertexAttribPointer(m_posAttr, 3, GL_FLOAT, GL_FALSE, 0, line_vertices);
//glVertexAttribPointer(m_colAttr, 3, GL_FLOAT, GL_FALSE, 0, line_colors);
//glEnableVertexAttribArray(m_posAttr);
//glEnableVertexAttribArray(m_colAttr);
glDrawArrays(GL_LINES, 0, 360);
I believe this is what you are looking for when you ask to see how I've set up my VAO, please correct me if I'm wrong.
// Create Shader (Do not release until VAO is created)
m_program = new QOpenGLShaderProgram(this);
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, ogl_vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, ogl_fragmentShaderSource);
m_program->link();
m_posAttr = m_program->attributeLocation("posAttr");
m_colAttr = m_program->attributeLocation("colAttr");
m_matrixUniform = m_program->attributeLocation("matrix");
m_program->release();

Drawing GLBatch multiple times at different locations

I am using GLBatch from the GLTools library to draw a tetrahedron. I want to draw the tetrahedron multiple times at different locations to draw a Sierpinski-Tetrahedron. Creating the geometry and drawing the cone works fine for one instance of the tetrahedron, but I want to draw the tetrahedron multiple times at different locations. Calling geometry.Draw() multiple times (with different model view matrices) does not work. The cone is still drawn only once.
How can I draw the GLBatch multiple times?
Here is my code:
GLBatch geometry;
// This is called once on initialization.
void CreateTetrahedron()
{
geometry.Begin(GL_TRIANGLE_STRIP, 6);
geometry.Color4f(1, 0, 0, 1);
geometry.Vertex3f(0, 1, 0);
geometry.Color4f(1, 0, 0, 1);
geometry.Vertex3f(-0.5f, 0, 0.5f);
geometry.Color4f(1, 0, 0, 1);
geometry.Vertex3f(0.5f, 0, 0.5f);
if (recursionLevel < 3)
{
geometry.Color4f(0, 1, 0, 1);
geometry.Vertex3f(0, 0, -0.7f);
geometry.Color4f(0, 0, 1, 1);
geometry.Vertex3f(0, 1, 0);
geometry.Color4f(1, 1, 1, 1);
geometry.Vertex3f(-0.5f, 0, 0.5f);
}
geometry.End();
}
// This is where I want to draw it. The function is recursive, and thus the geometry should be drawn multiple times with different matrices.
void DrawSierpinskiTetrahedron(UINT32 level)
{
if (level == 0)
{
geometry.Draw();
}
else
{
modelViewMatrix.PushMatrix();
modelViewMatrix.Scale(0.5f, 0.5f, 0.5f);
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0, 1, 0);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(-0.5f, 0, 0.5f);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0.5f, 0, 0.5f);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0, 0, -0.7f);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PopMatrix();
}
}
void RenderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
modelViewMatrix.PushMatrix();
// Camera Translation
modelViewMatrix.Translate(xTrans, yTrans, zTrans);
// Model Translation, Rotatation und Scale
glm::mat4 trans = glm::translate(glm::mat4(), translation);
modelViewMatrix.MultMatrix(glm::value_ptr(trans));
glm::mat4 rot = glm::mat4_cast(rotation);
modelViewMatrix.MultMatrix(glm::value_ptr(rot));
glm::mat4 sc = glm::scale(glm::mat4(), scale);
modelViewMatrix.MultMatrix(glm::value_ptr(sc));
// Set the shader for rendering
shaderManager.UseStockShader(GLT_SHADER_FLAT_ATTRIBUTES, transformPipeline.GetModelViewProjectionMatrix());
if (recursionLevel < 0)
{
recursionLevel = 0;
}
if (recursionLevel > 5)
{
recursionLevel = 5;
}
DrawSierpinskiTetrahedron(recursionLevel);
gltCheckErrors(0);
modelViewMatrix.PopMatrix();
TwDraw();
glutSwapBuffers();
glutPostRedisplay();
}
// Initialize rendering context
void SetupRC()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glFrontFace(GL_CW);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
shaderManager.InitializeStockShaders();
transformPipeline.SetMatrixStacks(modelViewMatrix,projectionMatrix);
// Create the geometry
CreateTetrahedron();
InitGUI();
}
Update:
Doing the following does not scale the tetrahedron.
modelViewMatrix.PushMatrix();
modelViewMatrix.Scale(1000, 1000, 1000);
geometry.Draw();
modelViewMatrix.PopMatrix();
GLMatrixStack is a class which provides a matrix stack and operations to the top matrix of the stack.
The class GLShaderManager can load a shader and handles setting of the uniform variables of the shader.
The shader program transforms the vertex coordinates of the mesh by the model view matrix. This causes that the mseh can be rendered to different places an with different orientations in the scene.
If the modelview matrix is changed, it has to be set to the corresponding uniform variable in the shader program, to make the change work.
This can be done by calling GLShaderManager::UseStockShader before drawing the mesh.
Adapt your code like this:
void DrawSierpinskiTetrahedron(UINT32 level)
{
if (level == 0)
{
shaderManager.UseStockShader(GLT_SHADER_FLAT_ATTRIBUTES, transformPipeline.GetModelViewProjectionMatrix());
geometry.Draw();
}
else
{
modelViewMatrix.PushMatrix();
modelViewMatrix.Scale(0.5f, 0.5f, 0.5f);
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0, 1, 0);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(-0.5f, 0, 0.5f);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0.5f, 0, 0.5f);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PushMatrix();
modelViewMatrix.Translate(0, 0, -0.7f);
DrawSierpinskiTetrahedron(level - 1);
modelViewMatrix.PopMatrix();
modelViewMatrix.PopMatrix();
}
}

LWJGL OpenGL texturing not correct

I'm trying to make a simple button in OpenGL/LWJGL,
I can render my 2D QUAD correctly, but when i implement the texture, only about 3/4 parts of the whole quad gets textured, like this: https://dl.dropboxusercontent.com/u/60223805/glerror1.png
and if i remove the texture coords i get this: https://dl.dropboxusercontent.com/u/60223805/glerror2.png
none.bind();
co.Enable2D_GUI();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(co.width/2-200, co.height/2);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(co.width/2+none.getTextureWidth(),co.height/2);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(co.width/2+none.getTextureWidth(), co.height/2+none.getTextureHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(co.width/2-200, co.height/2+none.getTextureHeight());
GL11.glEnd();
co.Disable2D_GUI();
where none is an Texture (from slick-util library) and the functions Enable2D_GUI and Disable2D_GUI just enables and disable ortho and stuff.
What can be wrong? I'm very new to OpenGL so im sorry if my question is a bit nooby
This is my Enable2D_GUI and Disable2D_GUI functions:
public void Enable2D_GUI() {
GL11.glMatrixMode (GL11.GL_PROJECTION);
GL11.glPushMatrix();
GL11.glLoadIdentity ();
GL11.glOrtho (0, width, height, 0, 1, -1);
GL11.glDisable(GL11.GL_DEPTH_TEST);
GL11.glMatrixMode (GL11.GL_MODELVIEW);
GL11.glPushMatrix();
GL11.glLoadIdentity();
}
public void Disable2D_GUI() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glPopMatrix();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glPopMatrix();
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
}
Now when I test it with a 3D QUAD it doesnt work either, same result. This is my OpenGL init code:
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL11.GL_SMOOTH);
GL11.glClearColor(0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(
45.0f,
(float)width/(float)height,
0.5f,
50.0f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
You're misusing the glPushMatrix and glPopMatrix and also not adding or performing state switches in the correct order.
In you init code you need to change it to the following.
GL11.glViewport(0, 0, width, height);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GLU.gluPerspective(45.0f, (float) width / (float) height, 0.5f, 50.0f);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glShadeModel(GL_SMOOTH);
GL11.glClearColor(0f, 0.0f, 0.0f, 0.0f);
GL11.glClearDepth(1.0);
GL11.glEnable(GL11.GL_DEPTH_TEST);
GL11.glDepthFunc(GL11.GL_LEQUAL);
You don't have to perform state switches after the glLoadMatrix and glLoadIdentity though it would be better doing so. 1 The code is more readable. 2 Some things gets reset after calling glLoadIdentity though just the stuff about and within the Matrix itself.
Then you also need to fix your Enable2D_GUI and Disable2D_GUI to the following.
Enable2D_GUI
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, height, 0f, 1f, -1f);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_DEPTH_TEST);
Disable2D_GUI
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();
GL11.glDisable(GL11.GL_BLEND);
GL11.glEnable(GL11.GL_DEPTH_TEST);
Aleast of what I know you can not use the glPushMatrix and glPopMatrix in between glMatrixMode and glLoadIdentity calls. I could be mistaking Then I also added the glLoadIdentity calls instead to reset the Matrices.

OpenGL won't draw, why?

I'm kind of new to OpenGL, so I tried to draw a textured 2D rect. I don't know why, but this didn't work:
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glViewport(0, 0, frameWidth, frameHeight);
GL11.glOrtho(0, 640, 0, 480, 0, 128);
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, renderer.getTexture("/textures/menu/title.png"));
GL11.glEnable(GL11.GL_ALPHA_TEST);
GL11.glAlphaFunc(GL11.GL_GREATER, 0.1F);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex3f(-frameWidth/2, -frameHeight/2, -1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex3f(+frameWidth/2, -frameHeight/2, -1);
GL11.glTexCoord2f(1, 1);
GL11.glVertex3f(+frameWidth/2, +frameHeight/2, -1);
GL11.glTexCoord2f(0, 1);
GL11.glVertex3f(-frameWidth/2, +frameWidth/2, -1);
GL11.glEnd();
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glCullFace(GL11.GL_BACK);
frameWidth is the width of the window, frameHeight is the height.
Because you didn't actually write what went wrong, I can only guess, but here, you specified orthogonal projection with z in range (0,128).
GL11.glOrtho(0, 640, 0, 480, 0, 128);
And then put your vertices with z = -1. Certainly it won't be drawn, as all the vertices will be clipped.
GL11.glVertex3f(-frameWidth/2, -frameHeight/2, -1);
Try changing glOrtho to gluOrtho2D (or hard-coded equivalent), and all the vertices to glVertex2f.
You should be calling glEnable(GL_CULL_FACE); glCullFace(GL_BACK) before the rendering. If you are using glOrtho for 2D, you should be doing glOrtho(..., ..., ..., ..., -1, 1); and using glVertex2f for your 2D vertices.

gluLookAt trouble

I have some code which draws a line along the x, y and z axes. My problem is that these lines are being clipped so that they are invisible near the origin:
This sounds like a far clipping plane issue, but I gave zFar=50 to gluPerspective, which should be plenty. Making it even larger doesn't seem to help. What else could be causing the clipping?
Here is my code:
import static org.lwjgl.opengl.GL11.*;
import org.lwjgl.opengl.*;
import org.lwjgl.util.glu.GLU;
public class Test {
static int width = 300, height = 200;
public static void main(String[] _) throws Exception {
Display.setDisplayMode(new DisplayMode(width, height));
Display.create();
glClear(GL_COLOR_BUFFER_BIT);
// projection matrix
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
GLU.gluPerspective(50, width / (float) height, .1f, 50);
// modelview matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLU.gluLookAt(
.8f, .8f, .8f,
0, 0, 0,
0, 1, 0);
// draw a line for each axis
glBegin(GL_LINES);
// x axis in red
glColor3f(1, 0, 0);
glVertex3i(0, 0, 0);
glVertex3i(10, 0, 0);
// y axis in green
glColor3f(0, 1, 0);
glVertex3i(0, 0, 0);
glVertex3i(0, 10, 0);
// z axis in blue
glColor3f(0, 0, 1);
glVertex3i(0, 0, 0);
glVertex3i(0, 0, 10);
glEnd();
Display.update();
// wait for a close event
while (!Display.isCloseRequested()) {
Thread.sleep(20);
Display.processMessages();
}
Display.destroy();
}
}
Update - Removing glLoadIdentity(); after glMatrixMode(GL_MODELVIEW); gives the desired result, but I don't understand why. Isn't the default modelview matrix the identity matrix?
Update - I wrote a C version of the same code and it works as desired. Why the difference?
Indeed, after testing it, it turns out that glMatrixMode(GL_PROJECTION_MATRIX); should be glMatrixMode(GL_PROJECTION); instead.
So it seems that the modelview was active by default and glLoadIdentity() cleared the results of GLU.gluPerspective(50, width / (float) height, .1f, 50);
edit: Btw. in case you wonder what GL_PROJECTION_MATRIX is for, it's to retrieve the current matrix from the top of the matrix stack with glGetFloatv(GL_PROJECTION_MATRIX,output); or glGetDoublev(GL_PROJECTION_MATRIX,output);