• 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
OmarShehata

Trouble using glGetTexImage

9 posts in this topic

I'm trying to create a function that takes an array of textures and compiles them all into one big texture.

I'll be doing this using [b]glTexSubImage2D[/b]. However to do that, I need to get the pixel data from the texture. So I'm doing that using [b]glGetTexImage[/b]. Here's my code so far:

[CODE]
GLuint CreateMegaTexture(vector<GLuint> texies ){
glBindTexture(GL_TEXTURE_2D,texies.at(0));

GLint textureWidth, textureHeight;
int bytes;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &textureWidth);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &textureHeight);
bytes = textureWidth*textureHeight;


float Pixels[bytes];

glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_UNSIGNED_BYTE,&Pixels);
//GLenum error = glGetError();//Get error
}
[/CODE]

For now, I'm just testing by trying to get the pixel data of the first element in the "texies" array. So first I get the size of the texture so that I know how large I need to make the array to hold the pixel data. Now there is the problem.

The documentation said the "Pixels" array would need to be of the same type that I chose, which is "GL_UNSIGNED_BYTE" in my case. However, if I do:

[i]GL_UNSIGNED_BYTE Pixels[bytes];[/i]

I get an error:

[i]"error: expected ';' before 'Pixels'[/i]

If I try GLfloat instead:

[i]GLfloat Pixels[bytes];
glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GLfloat,&Pixels);[/i]

I get an even stranger error:

[i]expected primary-expression before ',' token[/i]

I don't think it will work if I have the array as simply "float" (although it doesn't throw any errors by the compiler), so I thought I should fix this issue before moving on to glTexSubImage2D.

Any help would be appreciated, thanks!
0

Share this post


Link to post
Share on other sites
[code]
// GLubyte is the GL defined type for unsigned bytes
// make sure that "bytes" is large enough; since each texel has 4 components
// (r, g, b and a) "bytes = textureWidth * textureHeight;" is not sufficient.
// use "bytes = textureWidth * textureHeight * 4;" instead
// you probably don't want to declare this as a local either...
GLubyte Pixels[bytes];

// don't use &Pixels as that will get the address of the array. Just Pixels on
// it's own is what you need
glGetTexImage (GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, Pixels);[/code]
1

Share this post


Link to post
Share on other sites
Extra help. Example "Getting image data from texture":
[code]
GLint width,height,internalFormat;
glBindTexture(GL_TEXTURE_2D, your_texture_id);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &internalFormat); // get internal format type of GL texture
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &width); // get width of GL texture
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &height); // get height of GL texture
// GL_TEXTURE_COMPONENTS and GL_INTERNAL_FORMAT are the same.
// just work with RGB8 and RGBA8
GLint numBytes = 0;
switch(internalFormat) // determine what type GL texture has...
{
case GL_RGB:
numBytes = width * height * 3;
break;
case GL_RGBA:
numBytes = width * height * 4;
break;
default: // unsupported type (or you can put some code to support more formats if you need)
break;
}

if(numBytes)
{
unsigned char *pixels = (unsigned char*)malloc(numBytes); // allocate image data into RAM
glGetTexImage(GL_TEXTURE_2D, 0, internalFormat, GL_UNSIGNED_BYTE, pixels);
{
// TODO with pixels
}
free(pixels); // when you don't need 'pixels' anymore clean a memory page to avoid memory leak.
}

[/code]

Best wishes, FXACE.
2

Share this post


Link to post
Share on other sites
A final note here is that this isn't actually OpenGL-specific stuff; it's basic C/C++ - allocating sufficient memory, getting data types right, how to use arrays and pointers, etc. I'd advise to focus some effort in this area before diving into OpenGL itself.
0

Share this post


Link to post
Share on other sites
Thanks for the help everybody! I got it to work.

To mhagain,
I'm pretty sure I need to use the address of the Pixel array because according to the documentation, this function returns the pixel value *in* the Points array, so if I don't pass a reference it won't be able to update the value of Pixels array.

And the problem was me not knowing what the data type was called for an unsigned byte in openGL, not simply a general C++ problem.

To FXAXE, thanks again! I really really appreciate the detailed explanation and all your help so far with all my topics :P
0

Share this post


Link to post
Share on other sites
[quote name='tanzanite7' timestamp='1333102536' post='4926625']
Identifier for an array IS a reference to the array data.
[/quote]

Wait so if Points is an array, what's the difference between:

Points and &Points ?
0

Share this post


Link to post
Share on other sites
The difference is the type of the expression; [i]Points[/i] is a reference to an array (type is [i]GLubyte [N][/i], where [i]N[/i] is the number of elements in the array), and [i]&Points[/i] is a pointer to an array (type is [i]GLubyte (*)[N][/i]). They are different types, and have different properties when it comes to what other pointer types they decay to or can be implicitly cast into. But as long as they decay to or can be cast into another pointer type, they will likely have the same actual value which is a pointer to the first element of the array.
1

Share this post


Link to post
Share on other sites
Look at another example - different API, same principle. Good old-fashioned memcpy.

If you want to memcpy to an array you use:[code]type myarray[size];
memcpy (myarray, stuff, length);[/code]

If you want to memcpy to a pointer you use:[code]type *mypointer; // make sure this points somewhere valid!!!
memcpy (mypointer, stuff, length);[/code]
Again, this is basic C/C++ stuff and nothing to do with OpenGL.
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