• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
synthetix

How to squeeze rectangular bitmap into GL_TEXTURE_2D?

7 posts in this topic

I have a program working with images using GL_TEXTURE_RECTANGLE_ARB and a fragment shader with both sampler2DRect and texture2DRect to grab texture coordinates. However, I need to implement a shader that accesses the images using normalized values (0-1.0). sampler2DRect and texture2DRect only work with integers.

I know a big difference between GL_TEXTURE_RECTANGLE_ARB and GL_TEXTURE_2D is the way texture dimensions work. Of course, my images are all over the place size-wise, so I decided I would just take the longest dimension of an image and pass a square texture to glTexImage2D. I'd then work out the cropping, etc. in the shader:

[code]
/*
"orig_data" is value returned from image reader, etc. It is the
actual pixel data as a grayscale image (only one color plane)
"orig_length" is the length of the actual data in bytes
*/

int width = 1024;
int height = 768;

glEnable(GL_TEXTURE_2D);

/* make a bigger-than-normal buffer to hold the image plus padding needed to make texture square */
GLubyte *new_data = (GLubyte*)malloc(height*height);

/* copy existing data into bigger buffer */
memcpy(new_data,orig_data,orig_length);

glTexImage2D(GL_TEXTURE_2D,0,1,width,width,0,GL_LUMINANCE,GL_UNSIGNED_BYTE,(GLubyte*)new_data);[/code]

However, this doesn't work even when I use sampler2D and texture2D in my shader. What I usually get is either garbled data or black.

Any idea why this isn't working as expected? Is it something to do with my padding of the image data?
0

Share this post


Link to post
Share on other sites
memcpy(new_data,orig_data,orig_length);

This is not going to put a square texture into a rectangle if that is what you are trying to do. Your newData is a rectangle, your old data is....?(assume a square). This is going to basically create garbage. On nvidia cards (never used amd but those probably work as well, maybe not intel though), you can texImage a rectangle and not have to do anything different between a rectangle and square texture. The texture coords are still the same as well (0 to 1). It will help what you are doing, I see that your destination is rectangle, then your source in square? Why put a square texture into a rectangle?

[quote]and pass a square texture to glTexImage2D[/quote]
It sounds like you want to make a rectangle and pack it into a square texture the next biggest size. Right? Because that is not what you are doing here. You passed a texture of (1024x768) to texImage2D. And you could always open up mspaint/photoshop etc and just stretch a rectangular texture into the next highest square size. Then you don't have to program the stuff you are doing.
0

Share this post


Link to post
Share on other sites
[quote name='dpadam450' timestamp='1321156720' post='4883382']
On nvidia cards (never used amd but those probably work as well, maybe not intel though), you can texImage a rectangle and not have to do anything different between a rectangle and square texture. The texture coords are still the same as well (0 to 1). It will help what you are doing
[/quote]
Well, the documentation says that the texture size for GL_TEXTURE_2D must be power of 2. That's why I passed "width" as both the width and height. Is it possible to pass rectangular dimensions when the texture target is GL_TEXTURE_2D on some systems? I'd been using GL_TEXTURE_RECTANGLE_ARB because I thought I had to with non-power of 2 textures. But then I have the problem that the texture coords aren't normalized.

[quote]
I see that your destination is rectangle, then your source in square? Why put a square texture into a rectangle?
[/quote]
Other way around. I have an image (1024x768) that I need to put into a square texture. I thought padding the pixel data to make it square would work, but as you pointed out it didn't work.
1

Share this post


Link to post
Share on other sites
[quote]Other way around. I have an image (1024x768) that I need to put into a square texture.[/quote]
In your example though you made a texture of widthxheight = 1024x768 to glTexImage2D(). So you made your texture not square. Anyway on certain cards you don't need to do anything just make a non power of two texture and load it in. I've done it before, dont do all that memcpy() stuff and try it first.

[quote]I thought padding the pixel data to make it square would work, but as you pointed out it didn't work.[/quote]It didnt work because you didnt pad it, you just raw memcpy()'d it which means you continued writing the first row, with the second rows data.

Anyway, all you need to do is fix your texture as an artist would, resize in photoshop, which would stretch your rectangle to a square.
or
Make a square image, and copy/paste your rectangle into it (obviously it wont fit the exact size and will have padding). You will need to recompute UV coords then to display it without the blank padding.
0

Share this post


Link to post
Share on other sites
[quote name='dpadam450' timestamp='1321165467' post='4883407']
In your example though you made a texture of widthxheight = 1024x768 to glTexImage2D(). So you made your texture not square.[/quote]
Have another look. I actually passed "width" twice (not w x h), so I could keep all the resolution of the input bitmap and satisfy the power of 2 requirement.

[quote]
Anyway on certain cards you don't need to do anything just make a non power of two texture and load it in. I've done it before, dont do all that memcpy() stuff and try it first.[/quote]
Ok, I'll give it a shot!

[quote]Anyway, all you need to do is fix your texture as an artist would, resize in photoshop, which would stretch your rectangle to a square.
or
Make a square image, and copy/paste your rectangle into it (obviously it wont fit the exact size and will have padding). You will need to recompute UV coords then to display it without the blank padding.
[/quote]
These are actually dynamically loaded images, so I won't know the dimensions beforehand. I guess this will be OK if the GL implementation accepts the non-power of 2 dimensions.
0

Share this post


Link to post
Share on other sites
[quote name='Synthetix' timestamp='1321164571' post='4883403']
Well, the documentation says that the texture size for GL_TEXTURE_2D must be power of 2. That's why I passed "width" as both the width and height. Is it possible to pass rectangular dimensions when the texture target is GL_TEXTURE_2D on some systems? I'd been using GL_TEXTURE_RECTANGLE_ARB because I thought I had to with non-power of 2 textures. But then I have the problem that the texture coords aren't normalized.
[/quote]

You've a misunderstanding there. The power of two restriction applies to each dimension separately, and textures are not required to be square. You can have a width of 1024 and a height of 64 if you wish, and the texture will work (with GL_TEXTURE_2D) on any hardware (provided the hardware supports a texture size of 1024 or higher, of course).

Only older hardware has the power of two restriction, so if you're happy to drop support for this older hardware then you can quite happily use any size you want. In general, any graphics hardware from the last 7 years or so will no longer have this restriction, but it still exists with some slightly more recent Intel chips (so if you need to run on Intel graphics it becomes last 4 or 5 years). This is where you need to research your target market and make a decision on what the minimum hardware spec you'll require is. You have a risk of putting a lot of work into supporting old hardware that none of your end users actually has, and while supporting that kind of hardware can seem like the "right thing" to do, in the end you need to decide if it's time and effort that would have been better invested elsewhere. Only you can make that decision, but you need to be sure that the decision is supported by actual facts.
0

Share this post


Link to post
Share on other sites
Well, oh boy. The reason it wasn't working was because I was still using glTexCoord2i instead of glTexCoord2f! Oops!

It seems to be working fine now with GL_TEXTURE_2D. Thanks for your help!

[quote][color=#1C2837][size=2]You've a misunderstanding there. The power of two restriction applies to each dimension separately, and textures are not required to be square.[/quote][/size][/color]
[color=#1C2837][size=2]Ah. That helps. Yes, I didn't get that. Thanks again.[/size][/color]
0

Share this post


Link to post
Share on other sites
There is a bunch of things wrong here. You don't need to use integer coordinates to access GL_TEXTURE_RECTANGLE_ARB. You need non-normalized coordinates.

As for GL_TEXTURE_2D and the requirement of being power of 2, that was for GL 1.1
With GL 2.0, that restriction is removed. Make sure you video card supports at least GL 2.0.
Since you are using GLSL, I'm assuming that is so.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0