PiP in OpenGL causing flickering - opengl

I am trying to set up a Picture in picture style "map" display for a graphics program that displays a car. (Just shows the view from top again in a smaller view port.) However, the second viewport seems to flicker. I thought I was doing this correctly, but I may be not conceptualizing this correctly.
void display(void) {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
// Set Perspective
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0, 0, 500, 500);
// Lighting follows Camera if inserted here.
//Set Camera
calculateCamera();
gluLookAt(eyeX + carPosX, eyeY + carPosY, eyeZ + carPosZ, cX + carPosX,
cY + carPosY, cZ + carPosZ, 0, 1, 0);
displayEnvironment();
glClear(GL_DEPTH_BUFFER_BIT);
// Set Perspective
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(fov, aspect, near, far);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glViewport(0,0,150,150);
gluLookAt(0, 140, 0,0, 0, 0, 1, 0, 0);
displayEnvironment();
}

Where/when are you swapping buffers? Maybe you're not waiting for the render to finish?

Related

OpenGL gluLookAt

I tried to draw a teapot and view it in 3D but when I ran the program, nothing showed up. There is nothing in the window. I know it has something to do with my gluLookAt() function but I am not sure how to fix it.
// helloteapot.cc
//#include <GLUT/gl.h>
#include <GLUT/glut.h>
#include "GL/glui.h"
void display () {
/* clear window */
glClear(GL_COLOR_BUFFER_BIT);
/* draw scene */
glColor3f(1.0, 0.0, 0.0);
glTranslatef(0.0, 0.5, 0.0);
glutSolidTeapot(0.15);
/* flush drawing routines to the window */
glFlush();
}
int main ( int argc, char * argv[] ) {
glutInit(&argc,argv);
/* setup the size, position, and display mode for new windows */
glutInitWindowSize(800,600);
glutInitWindowPosition(0,0);
glutInitDisplayMode(GLUT_RGB);
/* create and set up a window */
glutCreateWindow("hello, teapot!");
glutDisplayFunc(display);
/*GLfloat width = 800;
GLfloat height = 600;
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);*/
// Set the aspect ratio of the clipping volume to match the
viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection
matrix
glLoadIdentity(); // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
//luPerspective(45.0f, aspect, -100.0f, 100.0f);
gluLookAt(0, 0, 0, 0, 0.5, 0, 0, -1, 0);
/* tell GLUT to wait for events */
glutMainLoop();
}
You have asked GL to position the camera at the origin, aim the camera upwards towards the point (0, 0.5, 0), and have also specified (incorrectly) the up vector to be 0, -1, 0. The problem is that the camera's forward direction is (0, 1, 0) [as specified by your eye and aim positions], and this direction conflicts with the up vector or (0, -1, 0). Try using a vector at right angles to the forward direction instead! (e.g. [1, 0, 0], [0, 0, 1])
gluLookAt(
0, 0, 0, //< camera location
0, 0.5, 0, //< looking towards point
1, 0, 0); //< which direction is up?

How to replace 'gluOrtho2d' in a Qt app

I am working on bring back to life a program I wrote seven years ago. It is all written in Qt and uses some OpenGL to draw some frame lines on an image that the application is displaying. The problem is that 'gluOrtho2D' is used but is no longer found. I am wondering how I can get around the problem. Here is the code:
void MSContext::ResizePaint(int width, int height)
{
// setup viewport, projection etc.:
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) width, 0.0, (GLdouble) height);
glMatrixMode(GL_MODELVIEW);
glClearColor(0.5, 0.5, 0.5, 1.0);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
CreatePrintCropMarks();
}
void MSContext::CreatePrintCropMarks()
{
GLuint print45CropId = openGLListMgr()->print45CropId();
glNewList( print45CropId, GL_COMPILE);
{
qreal lineSize = 4.0;
// Shrink down the canvas by 4 pixels so that the crop will show up
const QRectF& viewPort = primaryCanvas()->imageView()->viewRectangle();
QRectF shrunkingCanvas = viewPort.adjusted(lineSize, lineSize, -lineSize, -lineSize);
QRectF print45Crop = GetCropRect(shrunkingCanvas, 5, 4);
QRectF print57Crop = GetCropRect(print45Crop, 7, 5);
glLineWidth(lineSize / 2);
glLineStipple(1,0xFF00);
DrawBox(print57Crop);
glLineWidth(lineSize);
glLineStipple(1,0xFFFF);
DrawBox(print45Crop);
}
glEndList();
}
At the back of my head, I was wondering why I'd never heard of this function... turns out there is a fine alternative in (old-school) OpenGL. From the SGI GLU implementation:
void GLAPIENTRY
gluOrtho2D(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top)
{
glOrtho(left, right, bottom, top, -1, 1);
}
So you can write:
glOrtho(0.0, (GLdouble) width, 0.0, (GLdouble) height, -1, 1);

OpenGL translate not working

Okay, so my program opens a file, reads in xyz-points, then draws a line strip out of it. I originally had this program written in SharpGL (implemnted as WPF window) and it worked, but not well due to using immediate mode, so I have moved onto OpenGL in C++. I have (somewhat) figured out VBO's and I now I am trying to add mouse functionality now. My problem is I can't move the picture with my mouse, I want to be able to click and 'drag' the picture. My mouseClickFunc and mouseMotion work (my cout statements execute), however it seems like my translate call is never being executed (i.e. the picture starts partially 'clipped' in the scene and I would like the ability to drag it and center it). I know this is a shot in the dark but I am really not sure what to do.
MotionFunc:
void mouseMotion(int x, int y)
{
if (moveable)
{
xMove += xTransform(x) - xTransform(xDown);
yMove += yTransform(y) - yTransform(yDown);
xDown = x;
yDown = y;
cout << yMove << "---" << xMove << endl;
glutSwapBuffers();
glutPostRedisplay();
}
}
Display Function:
void RenderFunction(void)
{
++FrameCount;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
// Reset the modelview matrix.
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
I am sure there is more code that I need to show, this is just where I think the problem is. Any help would be greatly appreciated.
Here is a picture of a console output and the screen (OpenGL context) as I see it.
UPDATE: Updated my code. It looks like my coordinates are moving, but the picture is not if that makes sense. If you look at my output, if I keep 'dragging' the picture, you can see in the console that the variable xMove and yMove can get as large or small as they want, again translate is just never moving it.
You pop your matrix before drawing things, which resets the matrix to the state of last push matrix. Move glPopMatrix(); below draw call
You're popping the matrix before you call glDrawArrays(), so this naturally negates the effect of the translation. It also negates the glOrtho() call, but that should be issued on the projection matrix and not on the modelview matrix in the first place.
And, of course, the problem is in your code, and not in OpenGL.
In this code excerpt :
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
glPopMatrix();
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
you are :
setting the identity as the view matrix
push it into the queue
modify it by glTranslate
pop it of the stack
render the image
Therefore, your translation is ignored.
This is correct operation :
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
glPopMatrix();
You are multiplying projection before translation, remember to always read matrix transformation from bottome to top in OpenGL 1.1 which you should upgrade IMO. Another issue is that you are poping the matrix before drawing.
Correct code:
//glViewport((GLint)xMove*100, (GLint)-yMove*100, CurrentWidth, CurrentHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(xMin - 1, xMax + 1, yMin - 1, yMax + 1, -diameter * zScale, diameter * zScale);
// Reset the modelview matrix.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(xMove, -yMove, 0);
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDrawArrays(GL_LINE_STRIP, 0, 29000);
glPopMatrix();

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);

Why can't I display same object in multiple viewports?

I'm pretty new to OpenGL. I was playing around with some code but I can't figure out why the following will not produce two viewports with the same object view. Here's the code:
glViewport(0, windowHeight/2, windowWidth/2, windowHeight);
glScissor(0, windowHeight/2, windowWidth/2, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)(windowWidth/2)/(GLfloat)(windowHeight/2), 0.1f,
500.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
drawParticleView();
glViewport(windowWidth/2, 0, windowWidth, windowHeight/2);
glScissor(windowWidth/2, 0, windowWidth, windowHeight/2);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 45.0, (GLfloat)(windowWidth/2)/(GLfloat)(windowHeight/2), 0.1f,
500.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
drawParticleView();
drawParticleView() just draws an array of rectangles. The problem is that the second viewport is a squashed representation of the first. My window width is 1280 and height 960. I'm obviously doing something wrong but what? Thanks
The parameters to glViewport are the lower left corner of your viewport as x and y, then width and height.
For a window of 100 pixels square, your two viewports are specified as:
x1 = 0, y1 = 50, width1 = 50, height1 = 100.
x2 = 50, y2 = 0, width2 = 100, height2 = 50.
These placements and sizes put the first viewport in the upper left quadrant of your window, hanging half out the top of your window, and the second in the lower left quadrant of your window, hanging half out the side of your window.
For side by side viewports I think you want:
glViewport(0, 0, windowWidth/2, windowHeight);
// drawing code
glViewport(windowWidth/2, 0, windowWidth/2, windowHeight);
// repeat drawing code
Or top and bottom viewports I think you want:
glViewport(0, 0, windowWidth, windowHeight/2);
// drawing code
glViewport(0, windowHeight/2, windowWidth, windowHeight/2);
// repeat drawing code
The reason your second viewport is squashed is because it's aspect ratio is inverted, and therefore the parameter to gluPerspective is wrong. The aspect ratio parameter should be (windowWidth/2)/windowHeight for the first option above, and windowWidth/(windowHeigh/2) in the second option above.