How to fix my deprecated use of gluOrtho2D()? - macos

I've got existing code (OS X, Obj-C, NSOpenGLView) that calls:
gluOrtho2D(0.0, newSize.width, newSize.height, 0.0);
It works fine except that I get a deprecated function warning that urges me to use GLKMatrix4MakeOrtho() instead. OK – but how? I can't seem to even find the existence of that function; I'm including:
#import <OpenGL/gl.h>
#import <OpenGL/glu.h>
and Xcode does not know of a function by that name. My OpenGL reference manual does not have any mention of it, or indeed, of any functions with the prefix GLK; what's going on there? And then if I managed to find the function and include the right header and link against whatever I need to link against, what then – what would an equivalent call be for GLKMatrix4MakeOrtho() that would do the same thing as my gluOrtho2D() call? I tried Googling, and found many hits showing that other people are getting the same deprecation warning, but I couldn't find anybody saying how to fix it...

The include you need for GLKMatrix4MakeOrtho() is:
#include <GLKit/GLKMatrix4.h>
Then you call the function with the same arguments you would use with glOrtho(), e.g.:
GLKMatrix4 orthoMat = GLKMatrix4MakeOrtho(-1.0f, 1.0f, -1.0f, 1.0f, -1.0f, 1.0f);
How you use it depends on the kind of OpenGL you use. For fixed function with the legacy matrix stack, you can use for example:
glLoadMatrix(orthoMat.m);
With the programmable pipeline, you would typically load it as a uniform:
glUniformMatrix4fv(loc, 1, GL_FALSE, orthoMat.m);

Wow, that's a really unusual recommendation. GLKMatrix4MakeOrtho() is not a drop-in replacement for gluOrtho2D(); it's a function that would be used here if you were porting your application to OpenGL 3.2 or later. However, that port would be a much bigger task than just changing out this one call, as OpenGL 3.2 does not support any of the immediate mode APIs from earlier versions of OpenGL (e.g, glBegin()).
Bottom line is, so long as you continue to use OpenGL 1.x/2.x APIs, you will need to ignore this warning.

Here is one alternative to disable OpenGL deprecation warnings on macOS 10.15 and Xcode 11.2.1:
main.c
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <OpenGL/gl.h>
#include <GLUT/glut.h>
Another alternative would be via Build Settings:

Related

OpenCV C++ resize() not found

I installed opencv 2.4.9 for macOS and integrated it with Xcode. However, although it finds most functions, when calling the resize() function, I get the build error 'Use of undeclared identifier resize'.
Can anybody please tell me how to fix this?
You don't mention how you are calling it, but there are two resize functions: a member of Mat that changes the number of rows, and cv::resize() that interpolates to resize an image. For the latter you need imgproc.hpp.
#include <opencv2/imgproc/imgproc.hpp>
//...
cv::resize(src, dst, dst.size(), 0, 0, interpolation);

GL_INVALID_OPERATION when calling gluLookAt

I've been getting into OpenGL on OS X lately (tl;dr: I'm a OpenGL noob) and got some code working that draws a cube. However, I don't see any of the faces (besides the front) since my view isn't translated. I try to use the gluLookAt function to do this translation, but I get an GL_INVALID_OPERATION error. This is how I do my rendering:
// Activate and lock context
[_glView.openGLContext makeCurrentContext];
CGLLockContext(_glView.openGLContext.CGLContextObj);
// Update camera position
gluLookAt(0, 2.0, 0, 0, 0, 0, 0, 1, 0);
gl_GetError();
// Update viewport and render
glViewport(0, 0, _glView.frame.size.width, _glView.frame.size.height);
[_renderer doRender:time];
// Unlock and flush context
CGLUnlockContext(_glView.openGLContext.CGLContextObj);
[_glView.openGLContext flushBuffer];
This code works when I comment out the gluLookAt call, and from what I can gather from the docs, this error is caused by executing gluLookAt between glBegin and glEnd. I don't know if where these are getting called, as I do not call those myself, and wrapping the call to gluLookAt in glBegin and glEnd does not solve the issue.
If it makes a difference, I'm using OpenGL 3.2 Core Profile.
By the way, gluLookAt (...) (and GLU in general) is not a part of OpenGL. This is why you will not find documentation directly explaining the cause of this error.
The only reason it generates GL_INVALID_OPERATION is because behind the scenes it does this: glMultMatrixf (...) (which was a part of GL once upon a time). That is invalid in a core profile context, because there is no matrix stack anymore; anything that was deprecated in GL 3.0 or 3.1 is removed from GL 3.2 (core profile).
If you need gluLookAt / matrix stack functionality on OS X, GLKit provides a suitable collection of replacement utilities. Alternatively, you can use a much more portable (C++ based) library called GLM if you compile using Objective C++.
Now, the far simpler solution here is not to use a core profile context. If you are using things like gluLookAt (...) you are likely learning legacy OpenGL. You need a context that supports deprecated parts of OpenGL, and on OS X this means you need a 2.1 context.

OpenGL equivalent to GL_POINT_SIZE_ARRAY_OES?

I'm trying to draw point sprites in a small Mac app. I want each sprite to have its own size, and I know that OpenGL ES has the client state "GL_POINT_SIZE_ARRAY_OES".
I did some googling and discovered that there is a similar value "GL_POINT_SIZE_ARRAY_APPLE" which (you'd think) should do the same thing. For some reason, though, it doesn't seem to. Here's my drawing code:
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_POINT_SIZE_ARRAY_APPLE);
glVertexPointer(2, GL_FLOAT, sizeof(SpriteData), spriteVertices);
glPointSizePointerAPPLE(GL_FLOAT, sizeof(SpriteData), spriteVertices + sizeof(LocationF));
glDrawArrays(GL_POINTS, 0, spriteCount);
glDisableClientState(GL_POINT_SIZE_ARRAY_APPLE);
glDisableClientState(GL_VERTEX_ARRAY);
SpriteData is a struct containing the vertex/size data of each sprite. spriteVertices is just an interleaved array of that struct.
The vertex pointer is working fine; it's drawing the sprites, but seems to be ignoring their size values. It instead defaults to the value set by glPointSize().
Despite the fact that this code compiles with no warnings, it seems very suspicious to me that googling "GL_POINT_SIZE_ARRAY_APPLE" brings up almost no results. Is this a useless parameter? If so, how else can I achieve what I want?
There is no official OpenGL extension which exposes a GL_POINT_SIZE_ARRAY_APPLE extension. This may be some detritus in Apple's headers, but you shouldn't use it. Just use a generic vertex array and use the value you pass as a point size.
If you want cross-platform code, you should avoid system-dependent headers. Instead, use a proper OpenGL loader, which comes with cross-platform headers that won't have system-dependent, non-standard detritus in them.

New equiavalent to D3DXCOLOR Structure in Directx 11.1?

I am having trouble finding the D3DXCOLOR struct, it seems it is no longer there in DirectX 11.1 (d3d11_1.h).
I have searched for an equal of it but to no luck. Can someone help me with it? Also could you tell me how to use it if it has changed a lot?
I'm a total noob to this and i too had the problem with a sample tutorial where they used it.
So after many hours searching the net.
drumroll
Tada....
Here is how I've converted it in my code
old code
// clear the back buffer to a deep blue
devcon->ClearRenderTargetView(backbuffer, D3DXCOLOR{ 0.0f, 0.2f, 0.4f, 1.0f };
new code
// clear the back buffer to a deep blue
float color[4] = { 0.0f, 0.2f, 0.4f, 1.0f };
devcon->ClearRenderTargetView(backbuffer, color);
Use XMCOLOR. It is located in DirectXPackedVector.h
How about declaring once
using RGBA = float[4]; //c++11
or
typedef float RGBA[4]; //pre-c++11
and then using wherever needed like this
devcon->ClearRenderTargetView(backbuffer, RGBA{0.0f, 0.2f, 0.4f, 1.0f});
D3DX or the DirectX Utility library has been removed, instead Microsoft wants you to use the XNA Math Library counterparts (that use SSEs & stuff, but in my case it kind of crashes sometimes so I stopped using it)

OpenCV: Drawing on an image

I am working on a program using the OpenCV library (though I am quite a noob on it). One of the things I need to do is to draw on the image. I looked at the OpenCV drawing functions and they all seem pretty simple (Circle, Line, etc), however the program won't compile! It says this to be exact: error C3861: 'Line': identifier not found.
Is there something I haven't installed? I used the tutorial on http://opencv.willowgarage.com/wiki/VisualC%2B%2B_VS2008 to install OpenCV on Visual Studio 2008 and so far this is the only real problem I have.
Please help me! I need this program working as soon as possible!
The function to draw a line in the OpenCV C API is named cvLine, not Line.
I think you have fallen victim of the following common mistake:
C includes are in #include <opencv/core.h> etc, whereas
C++ includes are:
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <oppencv2/highgui/highgui.hpp>
Include these for drawing and showing the image. Use using namespace cv; then
you don't have to write cv::line just line and everything will be working fine.
I had to battle with the very same problem when I began. ;)
(And btw use cv::Mat for c++.)
You can now easily paint on OpenCV images. For this you need to call the setMouseCallback(‘window_name’,image_name) function on opencv. After that you can easily handle the Mouse Callback Function upon your images. Then you need to detect the cv2.EVENT_LBUTTONDOWN, cv2.EVENT_MOUSEMOVE and cv2.EVENT_LBUTTONUP events. By checking the proper boolean condition you need to decide how you like to interact with the OpenCV images.
def paint_draw(event,former_x,former_y,flags,param):
global current_former_x,current_former_y,drawing, mode
if event==cv2.EVENT_LBUTTONDOWN:
drawing=True
current_former_x,current_former_y=former_x,former_y
elif event==cv2.EVENT_MOUSEMOVE:
if drawing==True:
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
elif event==cv2.EVENT_LBUTTONUP:
drawing=False
if mode==True:
cv2.line(image,(current_former_x,current_former_y),(former_x,former_y),(0,0,255),5)
current_former_x = former_x
current_former_y = former_y
return former_x,former_y
For details you can see link: How to Paint on OpenCV Images and Save the Image
Output:

Resources