PDA

View Full Version : Generate 2D square texture to apply to a 3D cube



nenekira
05-27-2016, 02:19 AM
Hello,
I want to generate 2D textures (but not to display them) and then apply them to a 3D cube. But I have no idea how to it.
I seen that we can load files and use them as textures, but I want my program to self generate its own textures.
Can you help me ?

Edit : i'm using opengl 2.1 with SDL2 on Xubuntu.

GClements
05-27-2016, 05:10 AM
I seen that we can load files and use them as textures, but I want my program to self generate its own textures.

Edit : i'm using opengl 2.1 with SDL2 on Xubuntu.

If you have OpenGL 3.0 or the ARB_framebuffer_object extension, you can use OpenGL to render into a texture.

If you don't have either of those, you can use OpenGL to render to the default framebuffer then read the resulting pixels with glReadPixels(), copy them to a new texture with glCopyTexImage2D(), or copy them into an existing texture with glCopyTexSubImage2D().

If the default framebuffer is a window, any portion of the window which is off-screen or which is obscured may result in missing (not-rendered) pixels. You can avoid this by using a GLXPixmap or PBuffer, but support for those varies (and I don't know whether SDL supports using them, so you may need to use raw Xlib).

nenekira
05-27-2016, 05:27 AM
Thank you for the answer, I'm gonna try it right now. I'll keep you update if it worked or not.

nenekira
05-27-2016, 05:41 AM
Arf I have another question, I don't know how to draw 2D graphics on the window (in or off screen portion) while i'm using 2D graphics. I tried to use glVertex2*() after drawing my cube but it show nothing (for the test I tried to draw in an in-screen window portion). Can you please tell me how to do it ?

And I think of an alternative solution, can I draw in a SDL_Surface and then choose the Surface as the texture for the cube ? is it possible ? If yes which one is the easier solution ? The most efficient ?

GClements
05-28-2016, 01:00 AM
Arf I have another question, I don't know how to draw 2D graphics on the window (in or off screen portion) while i'm using 2D graphics. I tried to use glVertex2*() after drawing my cube but it show nothing (for the test I tried to draw in an in-screen window portion).

glVertex2* is equivalent to glVertex3* with Z=0. The coordinates are still affected by any transformation matrices, and operations such as clipping and (if enabled) depth testing will still be performed. So you need to ensure that the transformed geometry lies within the clip region and isn't obscured by any 3D geometry.



And I think of an alternative solution, can I draw in a SDL_Surface and then choose the Surface as the texture for the cube ? is it possible ? If yes which one is the easier solution ? The most efficient ?
You can use any method which ends up providing you with pixel data which you can pass to e.g. glTexImage2D().