openGL into png - c++

I'm trying to convert an openGL [edit: "card that I drew"(?):) thx unwind]containing a lot of textures (nothing moving) into one PNG file that I can use in another part of the framework I'm working with. Is there a C++ library that does that?
thanks!

If you simply mean "take a scene rendered by OpenGL and save it as an image," then it is fairly straightforward. You need to read the scene with glReadPixels(), and then convert that data to an image format such as PNG (http://www.opengl.org/resources/faq/technical/miscellaneous.htm).
There are also more efficient ways of achieving this, such as using FBOs. Instead of rendering the scene directly into the framebuffer, you can render it to a texture via an FBO, then render that texture as a full-screen quad. You can then take this texture and save it to a file (using glGetTexImage, for example).

What is an "OpenGL file"? OpenGL is a graphics API, it doesn't specify any file formats. Do you mean a DDS file, or something?

There are better ways to make a compose texture than drawing them with the graphics card. This is really something you would want to do before hand on the cpu, store and then use as and when you need it with opengl

Related

How to Convert Existing OpenGL Texture to Metal Texture

I am working on developing some FxPlug plugins for Motion and FCP X. Ultimately, I'd like to have them render in Metal as Apple is deprecating OpenGL.
I'm currently using CoreImage, and while I've been able to use the CoreImage functionality to do Metal processing outside of the FxPlug SDK, FxPlug only provides me the frame as an OpenGL texture. I've tried just passing this into the CoreImage filter, but I end up getting this error:
Cannot render image (with an input GL texture) using a metal-DG context.
After a bit of research, I found that I can supposedly use CVPixelBuffers to share textures between the two, but after trying to write code utilizing this method for a while, I've come to the belief that this was intended as a way to WRITE (as in, create from scratch) to a shared buffer, but not convert between. While this may be incorrect, I cannot find a way to get the existing GL texture to exist in a CVPixelBuffer.
TL;DR: I've found ways to get a resulting Metal or OpenGL texture FROM a CVPixelBuffer, but I cannot find a way to create a CVPixelBuffer from an existing OpenGL texture. My heart is not set on this method, as my ultimate goal is to simply convert from OpenGL to Metal, then back to OpenGL (ideally in an efficient way).
Has anyone else found a way to work with FxPlug with Metal? Is there a good way to convert from an OpenGL texture to Metal/CVPixelBuffer?
I have written an FxPlug that uses both OpenGL textures and Metal textures. The thing you're looking for is an IOSurface. They are textures that can be used with either Metal or OpenGL, though they have some limitations. As such, if you already have a Metal or OpenGL texture, you must copy it into an IOSurface to use it with the other system.
To create an IOSurface you can either use CVPixelBuffers (by including the kCVPixelBufferIOSurfacePropertiesKey) or you can directly create one using the IOSurface class defined in <IOSurface/IOSurfaceObjC.h>.
Once you have an IOSurface, you can copy your OpenGL texture into it by getting an OpenGL texture from the IOSurface via CGLTexImageIOSurface2D() (defined in <OpenGL/CGLIOSurface.h>). You then take that texture and use it as the backing texture for an FBO. You can, for example, draw a textured quad into it using the input FxTexture as the texture. Be sure the call glFlush() when done!
Next take the IOSurface and create a MTLTexture from it via -[MTLDevice newTextureWithDescriptor:ioSurface:plane:] (described here). You'll want to create an output IOSurface to draw into and also create a MTLTexture from it. Do your Metal rendering into the output MTLTexture. Next, take the output IOSurface and create an OpenGL texture out of it via CGLTexImageIOSurface2D(). Now copy that OpenGL texture into the output FxTexture either by using it as the backing of a texture-backed FBO or whatever other method you prefer.
As you can see, the downside of this is that each render requires 2 copies - 1 of the input into an IOSurface and 1 of the output IOSurface into the output texture the app gives you. The other downside is that this is probably all moot, as with Apple having announced publicly that they're ending support for OpenGL, they're probably working on a Metal-based solution already. It may be extra work to do it all yourself. (Though the upside is that you can use that same code in other host applications that only support OpenGL.)

How to draw texts on a 3D objects (such as sphere)

I learn OpenGL under Linux platform. Recently, I try to use texts created by glutBitmapCharacter() as the texture of some quadrics objects provided by glu or glut. However, glutBitmapCharacter() does not return a pointer so that I can't feed it to the glTexImage2D(). I had google it for quite a while, but all I found is some topic related to Android SDK which I have no experience to it.
All I can think of is to render texts and read it form buffer using glReadPixels(), then save it to a file. Next, read the pixels back from the file and refer it to a pointer. Finally, draw 3D objects with these texts as the texture (i.e. feed the pointer to the glTexImage2D()).
However, it's kind of silly. What I want to ask is: Are there some other alternative way to this?
Applying text on top of a 3D surface is not trivial with pure OpenGL. GLUT does not provide any tools for that. One possible option would be for you to implement your own text rendering methods, possibly loading glyphs using Freetype then create a texture with the glyphs and apply that texture to the polygons. Freetype-GL is a tiny helper library that would facilitate a lot if you were to do that.
Another option would be to again load the text glyphs into a texture and then apply them as decals over the geometry. That way you could still simulate a 2D text drawing in a flat surface (the decal) and then apply that on top of a 3D object.

Import PNG texture to memory but not framebuffer- using C++ and OpenGL

I am investigating a possible method of collision detection for a simple game I am developing but I am now stuck.
I am trying to load a texture into memory but not into the frame buffer and read pixels (specifically, the colour) from it using coordinates...I can read the buffer contents easily and get the color of pixels at coordinates but I cannot work out how to do this on a texture, is it even possible?
Any help/guidance/what to research or possible functions would be much appreciated.
Note: I am using OpenGL 2.0
OpenGL is not a image manipulation, nor image access library. It's a drawing API and as such it should be treated. Reading back (whole) textures is possible with OpenGL (though not very performant). On OpenGL-ES there's no direct way to read texture data.
You already have the whole texture image in a regular buffer? Good, because that's what you want to operate on anyway. Reading back single pixels for their color is just stupid, because it clogs up the CPU with function call overhead.
decode png to regular buffer.
create texture by glGenTextures;
bind texture by glBindTexture;
load image to texture by glTexImage2D.

Render text to an opengl texture in a standalone program

I'm using opengl+glut and wondering if there is a library out there that can render text to a buffer in a format consistent with opengl textures.
Check out FTGL. It uses FreeType to render text to textures, and can also create polygons if you feel like it. The tutorial shows the basic usage nicely.

OpenGL - Using glTexImage2d to fill the entire screen with a texture

Two questions -
What is the best way to use a texture in OpenGL to fill the entire window?
I want to use glTexImage2D to take in an array of ints containing colour data, how would I go about doing this? (I've found a couple of pages of reference on glTexImage2D but tutorial on using it would be great)
Clarification:
I have done texturing before. I simply need help on these two specific parts.
glTexImage2D just uploads texture data, nothing more. When you have your texture, draw a texture mapped quad the size of the screen and you will draw your texture pixels to the screen.
A ortographic projection is usually used for this.
NeHe provides tutorials for almost any OpenGL topic.
The first lesson on using textures is #6.
Also you could just upload the pixels with glDrawPixels if you don't need to update to much.
There is a nice example from Nehe on how to use textures here: