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

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.

Related

Creating a 3d card using DirectXTK with Dynamic texture on one side

I am trying to create a card game. I want to have a deck of cards where the back of the card is a fixed texture but the front is dynamic, i.e. it has some text fields on it as well as a picture. I have created a box sized 3x2x0.16 to represent my card. I can get the fixed texture to load but I cannot find any code examples on the web that show me how to load a fixed texture on one side of the box and a dynamic one on the other. Can anyone point me to some examples please. I'm using DirectXTK mainly, but can probably fathom it out from any DirectX code too.
DirectX11 is version of DirectX I am using.
Any recommendations on how to do this would also be welcome.
Thanks
Easiest method for generating your cards, depending on how many there are and how large you want them, is to generate the faces at startup by using render to texture. Effectively, draw your dynamic card faces exactly like you would draw them in the world, but use an orthographic projection matrix and a blank 2D texture object as the render target. Once you have that, cache these "dynamic" textures in an std::map and bind them when drawing a specific card.
If your faces are relatively small, or you want to save on texture memory, you can stitch multiple card faces together into a large sheet of textures, then use some shader scaling logic to reference a subsection of the sheet for rendering a specific texture. With this, you can assemble "decks" of cards that only contain the faces in use in that particular game, allowing you to evict the others from GPU RAM.

What's the best way to render a lot of text with OpenGL

I work on a software who need to print a lot of texts on the screen, around 200 to 400 strings and a lot unique character (some objects are represented by a character), the software is already drawing a lot of things using OpenGL.
I already worked around with text rendering and I'm able to render text, but drawing more than 200 string using 200 draw call lead in a performance issue.
The software draw only in 2D.
It's important to notice that I the software run on 32bit computer with old graphics card, I can't use a more recent version of OpenGL than 2.0.
What would be the best options from your opinion?
Render everything in one draw call per font, using one big buffer with all information.
Render each string in a texture and then make one draw call for every string. (the text don't change a lot, every seconds max)
Any other idea?
While I don't think my way of rendering the text is anywhere close to standard, I prerendered the font into one giant glyph map, and stored the metadata (the location of glyphs) into a ssbo buffer (that stores an array of glyphs data except the bitmap and the advance). After that, when I need to render a bunch of text, I converted the charcodes to an array of glyph IDs. Then by looping over it and only passing over a vec2 position and a hint glyph Id per char, I turned points to a rectangle in the geometry shader by reading data in the ssbo, passing along the texture coordinate to fragment shaders.
This method is so fast that it can easily render 10000s of text. It also allows fast switching of font sets, and able to support any unicode char as long as the font has the glyph.
From what I learned, the standard approach would be to make a font atlas texture. This is the place where you can rasterize the glyphs to, and save the uv coordinates of the rasterized glyphs into a lookup table. Then batch the vertices and draw all of the text in one draw call by using a single texture (your atlas) and having one buffer for all vertex positions.
All of the fonts and character sizes can be written to the same atlas, provided that you make a large enough texture. You could also probably use a second atlas texture for the unique characters.
Since OpenGl does not have any support to render fonts on screen , it is left to the programmer to define a system.
Freetype is a good Library to render fonts.
https://www.freetype.org/
You can render Font Texture to Quads or Make Font Atlas and than render them to Quads
Generally 200 strings should not be challenging but you can check techniques like signed distance fields.
This is a good tutorial to render fonts in opengl.
https://learnopengl.com/In-Practice/Text-Rendering

OpenGL create trees

I am using PyOpenGL with PyGame (although I am also trying to copy the game into c++ as well), and I would like to draw some low-poly trees in my game, something like the one in the picture below.
But at the moment I only know how to draw simple flat surfaces and put textures on them (by creating an array of x,y,z coordinates and texture coordinates and using glDrawArrays). Is there a way to make something like the tree below using only opengl (would it involve 3d texture coordinates?) or do I need an external graphics engine?
If I do need a graphics designer, does anyone have any recommendations, and then am I right that I would need to pass the vertices to an array in python and then use that in glDrawElements?
After some point, you cannot handle complex objects by just defining 3D vertexes in Opengl. Instead you need object model that you can include it to your project. Most of the objects models are come with their texture files and texture coordinates included so you don't need to worry about texturing them.
For loading objects into your scene, I suggest you to use assimp library. And after you setup your environment, only thing you have to do is search for free low poly tree models. Here is a webpage that you can find free low poly trees : http://www.loopix-project.com/

A couple of textures into one in Blender

I have some model in Blender. I'd like to:
Connect a few different textures into one and save it as bitmap
Make UV mapping for these connected textures
I need to solve this problem for textured models in OpenGL. I have data structure which giving me possibility to bind one texture into one model, so I'd like to have one texture per one model. I'm aware of fact that I can use Texture GL_TEXTURE_xD_ARRAY, but I don't want to complicate my project. I know how to do simple UV mapping in Blender.
My questions:
Can I do 1. and 2. phases exclusively in Blender?
Is Blender Bake technique is what I'm searching for?
Is there some tutorials shows how to do it? (for this one specific problem)
Maybe somebody advise me another Blender technique (or OpenGL
solution)
Connect a few different textures into one and save it as bitmap
Make UV mapping for these connected textures
You mean generating a texture atlas?
Can I do 1. and 2. phases exclusively in Blender?
No. But it would be surely a well received add-in.
Is Blender Bake technique is what I'm searching for?
No. Blender Bake generates texture contents using the rendering process. For example you might have a texture on a static object into which you bake global illumination; then, instead of recalculating GI for each and every frame in a flythrough, the texture is used as source for the illumination terms (it acts like a cache). Other applications is generating textures for the game engine, from Blender's procedural materials.
Maybe somebody advise me another Blender technique (or OpenGL solution)
I think a texture array would be really the best solution, as it also won't make problems for wrapped/repeated textures.
Another possibility is to use projection painting. An object in blender can have multiple uvmaps, if importing it doesn't create each uvmap then you may need to align each one by hand. Then you create a new uvmap that lays the entire model onto one image.
In Texture painting mode you can use projection painting to use the material from one uvmap as the paint brush for painting onto the new image.

Drawing a simple anti-aliased stickman in OpenGL

I would like to draw a simple 2D stickman on the screen. I also want it to be anti-aliased.
The problem is that I want to use a bones system, which will be written after I would know how to draw the stickman itself based on the joints positions. This means I can't use sprites - I want my stickman to be fully controlable in the code.
It would be great if it will be possible to draw curves too.
Drawing a 3D stickman using a model would also be great if not better. The camera will be positioned like it's 2D, but I would still have depth. The problem is that I only have experience in Maya, and exporting and vertex weighting of the model in OpenGL seems like a mess...
I tried to find libraries for 2D anti-aliased drawing or enable multi-sampling and draw normally, but I had no luck. I also tried to use OpenGL's native anti-aliasing but it seems deprecated and the line joins are bad...
I don't want it to be too complicated because, well, it shouldn't be - it's just the first part of my program, and it's drawing a stickman...
I hope you guys can help me, I'm sure you know better than me :)
You could enable GL_SMOOTH. To check if you device supports your required line width for smooth lines, you can use glGet(GL_SMOOTH_LINE_WIDTH_RANGE);
If you want your code to be generic, you can also use antialiased textures.
Take a look at this link
http://www.opengl.org/resources/code/samples/advanced/advanced97/notes/node62.html
The only way to get antialiasing is use GL library which knows how to get antialiased GL context, for example, SDL. As of stickman, you can draw him with colored polygons.