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);
Related
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?
I'm trying to look at the square from the other side using the gluLookAt() function.
After using the function, nothing changes, although I expected that the corners of the square will change.
I set the camera point to the rightmost part of the world and look at its center, where the square is located.
He had to stretch out to the sides. Why hasn't anything changed?
Code:
#include "includes.h"
using namespace std;
constexpr auto FPS_RATE = 60;
int windowHeight = 600, windowWidth = 600, windowDepth = 600;
void init();
void idleFunction();
void displayFunction();
double getTime();
double getTime()
{
using Duration = std::chrono::duration<double>;
return std::chrono::duration_cast<Duration>(
std::chrono::high_resolution_clock::now().time_since_epoch()
).count();
}
const double frame_delay = 1.0 / FPS_RATE;
double last_render = 0;
void init()
{
glutDisplayFunc(displayFunction);
glutIdleFunc(idleFunction);
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-windowWidth / 2, windowWidth / 2, -windowHeight / 2, windowHeight / 2, -windowDepth / 2, windowDepth / 2);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void idleFunction()
{
const double current_time = getTime();
if ((current_time - last_render) > frame_delay)
{
last_render = current_time;
glutPostRedisplay();
}
}
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_POLYGON);
gluLookAt(-300, 0, 0,
0, 0, 0,
0, 1, 0);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition((GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2, (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2);
glutCreateWindow("Window");
init();
glutMainLoop();
return 0;
}
The issue is caused because gluLookAt() is call with in a glBegin/glEnd sequence. This is not allowed. You've to call gluLookAt before glBegin.
Once drawing of primitives was started by glBegin it is only allowed to specify vertex coordinates (glVertex) and change attributes (e.g. glColor, glTexCoord ...), till the drawn is ended (glEnd).
All other instruction will be ignored and cause a GL_INVALID_OPERATION error (error code 1282).
Further note, that glLookAt doesn't set a the current matrix. It defines a matrix and multiplies the current matrix by the new matrix. Set the matrix mode (glMatrixMode) and set Identity matrix by glLoadIdentity before gluLookAt.
With the view matrix
gluLookAt(-300, 0, 0, 0, 0, 0, 0, 1, 0);
you want "see" anything, because with that matrix the line of sight is set along the x-axis and you look at the 2 dimensional polygon from the side.
Note, the polygon is a 2D object. The size of the object appears different if you look at it from the front, from the side (then it is a line and not visible) or from an direction in between. The first 3 parameters of gluLookAt define the point of view the next 3 parameters define the point you look at. The vector from the point of view to the point you look at is the line of sight.
Probably yo want look along the z-axis:
void displayFunction()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, -300, 0, 0, 0, 0, 1, 0);
glBegin(GL_POLYGON);
glColor3f(1, 1, 1);
glVertex3i(-150, 150, 0);
glVertex3i(150, 150, 0);
glVertex3i(150, -150, 0);
glVertex3i(-150, -150, 0);
glEnd();
glutSwapBuffers();
}
You use Orthographic (parallel) projection. If you would use Perspective projection, then the projected size of the object would decrease, when the distance to the point of view increases. Perspective projection can be set by gluPerspective. e.g.:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (double)windowWidth / windowHeight, 0.1, 600.0);
I'm really pulling my hair out with this problem. I'm trying to create a simple game where the player rolls a ball around a playing area.
I'm using WinAPI for window management and input handling.
I tried to render some simple quads too, instead of the GLU sphere, but that didn't work either.
I've separated the code across different classes. I present the relevant code below. This code is in my WinMain:
while (running) {
PeekMessage(&msg, hwnd, NULL, NULL, PM_REMOVE);
if (msg.message == WM_QUIT)
running = false;
else {
// handle key presses
// update
gameWorld->update(getDirections());
// render
gameWorld->render(deviceContext);
// I added this block of code for testing, still does not work
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glVertex3f(10, 10, 0);
glVertex3f(10, -10, 0);
glVertex3f(-10, -10, 0);
glVertex3f(-10, 10, 0);
glEnd();
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Here's GameWorld.cpp:
GameWorld::GameWorld()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
this->ball = new Ball(1, 10, 10);
this->camera = new Camera(ball);
}
GameWorld::~GameWorld()
{
delete this->ball;
}
void GameWorld::render(HDC deviceContext) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
this->ball->draw();
SwapBuffers(deviceContext);
}
void GameWorld::update(Directions dirs) {
glLoadIdentity();
this->ball->handleInput(dirs);
this->ball->update();
this->camera->update();
}
Here's Camera update method:
void Camera::update() {
GLdouble ballX = ball->getLocation()->getX();
GLdouble ballY = ball->getLocation()->getY();
GLdouble ballZ = ball->getLocation()->getZ();
GLdouble x = ballX + cos(90) * this->distanceFromBall;
GLdouble y = ballY + cos(90) * this->distanceFromBall;
GLdouble z = ballZ + cos(90) * this->distanceFromBall;
gluLookAt(
x, y, z,
ballX, ballY, ballZ,
0, 1, 0
);
}
Here's the Ball draw method:
void Ball::draw() {
glPushMatrix();
this->quadric = gluNewQuadric();
glTranslated(this->location->getX(), this->location->getY(), this->location->getZ());
gluQuadricDrawStyle(this->quadric, GLU_FILL);
glColor4f(1, 1, 1, 1);
gluSphere(this->quadric, this->radius, this->slices, this->stacks);
gluDeleteQuadric(this->quadric);
glPopMatrix();
}
What the #!#% is wrong with this code? I should get this thing done in a week, so I really could use some help...
I had to use the gluPerspective() function to make this work. My GameWorld constructor now looks like this:
GameWorld::GameWorld()
{
glViewport(0, 0, WIDTH, HEIGHT); // reset the viewport to new dimensions
glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
glLoadIdentity(); // reset projection matrix
// calculate aspect ratio of window
gluPerspective(54.0f, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1.0f, 1000.0f);
glMatrixMode(GL_MODELVIEW); // set modelview matrix
glLoadIdentity();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
this->ball = new Ball(1, 20, 20);
this->camera = new Camera(ball);
}
The code is copied from the sample code of Dave Astle's book "OpenGL Game Programming".
I am still trying to figure out, why i see only typical "black screen". I render just one rectangle, but nothing happened.
#include "expwidget.h"
#include <iostream>
ExpWidget::ExpWidget(QObject *parent) :
QGLWidget(QGLFormat(QGL::DoubleBuffer), (QWidget *) parent)
{
QGLFormat fmt = this->format();
fmt.setDepth(true);
this->setFormat(fmt);
}
void ExpWidget::initializeGL() {
QGLWidget::initializeGL();
std::cout << "inicializace...\n";
glClearColor(0.0f,0.0f,0.0f,0.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
}
void ExpWidget::paintGL() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1, 0, .5);
glBegin(GL_QUADS);
glVertex3f(-1.0f,-1.0f,-5.0f);
glVertex3f(1.0f,-1.0f,-5.0f);
glVertex3f(1.0f,1.0f,-5.0f);
glVertex3f(-1.0f,1.0f,-5.0f);
glEnd();
glFlush();
}
void ExpWidget::resizeGL(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-2.0, 2.0, -2.0, 2.0, 0.0, -30.0);
glMatrixMode(GL_MODELVIEW);
}
I've faced a similar problem in the past, but I don't claim to be an expert.
Calls to glFrustum are in the form glFrustum(left, right, bottom, top, near, far)
near and far must both be positive and non-zero. (http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml)
so in your case I would recommend changing your call to:
glFrustum(-2.0, 2.0, -2.0, 2.0, 1.0, 30.0);
Also, your coordinates should have negative Z to render in this view.
http://www.opengl.org/sdk/docs/man2/xhtml/glFrustum.xml:
nearVal, farVal
Specify the distances to the near and far depth clipping planes. Both distances must be positive.
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?