# OpenGL glCopyTexSubImage2D squishing images? [resolved]

## Recommended Posts

The Demo (Requires windows.h) I'm having this odd problem with glCopyTexSubImage2D where it seems as though the function is squishing the captured framebuffer image towards the bottom of the texture it is storing to. Here's an image to illustrate what I mean: This is a simply paint program. The cursor is a white diffuse sphere image and when you left click, the program copies everything from the framebuffer to a constantly updated texture, then renders the updated texture as a screen-aligned quad. That big line in the middle of the screen is the result of simply holding down "left click" while keeping the pointer stationary. As you can see, glCopyTexSubImage2D is grabbing the old image and stretching (or compressing, whichever you prefer) the image downward, resulting in the streak. Likewise, you can see near the bottome how a bunch of scribbles have been squished over time. Has anyone encountered anything similar to this before? I looked at parameters for glPixelStore and glPixelTransfer, but none of them seem like they would affect this at all. To create my initial texture, I call glCopyTexImage2D, and the width and height parameters are the same throughout the program, and are the same for both CopyTex calls, so it should be working, as far as I can tell. [Edited by - OpenGLAaron on November 25, 2004 1:26:34 AM]

##### Share on other sites
Post code - specifically, your render segment and the part containing the glCopyTexImage2D. I suspect something like not glLoadIdentity'ing in the right place (or not at all), but it's really hard to give you any suggestions without code.

Regards,

Hew T.

##### Share on other sites
///////////////////////////////////////////////////////////////////////////////// draw the screen aligned quad and cursor brush to the backbuffer///////////////////////////////////////////////////////////////////////////////{	glClearColor(0.0,0.0,0.0,0.0);	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	glOrtho(-1,1,-1,1,-1000,1000);	glMatrixMode( GL_MODELVIEW );	glLoadIdentity();	glDisable(GL_LIGHTING);	glPolygonMode(GL_FRONT, GL_FILL);	///////////////////////////////////////////////////////////////////////////////	// draw the screen-aligned quad	///////////////////////////////////////////////////////////////////////////////	glActiveTextureARB(GL_TEXTURE1_ARB);	glDisable(GL_TEXTURE_2D);	glActiveTextureARB(GL_TEXTURE2_ARB);	glDisable(GL_TEXTURE_2D);	if (firstRun)	{		glActiveTextureARB(GL_TEXTURE0_ARB);		glDisable(GL_TEXTURE_2D);		firstRun = false;	}	else	{		glActiveTextureARB(GL_TEXTURE0_ARB);		glEnable(GL_TEXTURE_2D);		glBindTexture(GL_TEXTURE_2D, updateTex);		glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);	}	glColor3f(0.0,0.0,0.0);	drawQuad();	///////////////////////////////////////////////////////////////////////////////	// this draws the actual brush	///////////////////////////////////////////////////////////////////////////////	float texSize = 64.0/512.0;	glTranslatef(mousePos3D.m_x,mousePos3D.m_y,5);	glScalef(texSize,texSize,texSize);	glActiveTextureARB(GL_TEXTURE0_ARB);	glDisable(GL_TEXTURE_2D);	glColor3f(1.0,1.0,1.0);	glutSolidSphere(0.4,16,3);	glEnable(GL_LIGHTING);	///////////////////////////////////////////////////////////////////////////////	// on a left mouse click, copy the "paint texture" over the existing texture	///////////////////////////////////////////////////////////////////////////////	if (leftClick)	{		glReadBuffer(GL_BACK);		glBindTexture(GL_TEXTURE_2D, updateTex);		glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, WIDTH, HEIGHT);	}}///////////////////////////////////////////////////////////////////////////////// draw the updated texture after the copytexsubimage call///////////////////////////////////////////////////////////////////////////////{	glClearColor(0.0,0.0,0.0,0.0);	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );	glMatrixMode(GL_PROJECTION);	glLoadIdentity();	glOrtho(-1,1,-1,1,-1000,1000);	glMatrixMode( GL_MODELVIEW );	glLoadIdentity();	glDisable(GL_LIGHTING);	glPolygonMode(GL_FRONT, GL_FILL);	//draw the updated texture	glActiveTextureARB(GL_TEXTURE2_ARB);	glDisable(GL_TEXTURE_2D);	glActiveTextureARB(GL_TEXTURE1_ARB);	glDisable(GL_TEXTURE_2D);	glActiveTextureARB(GL_TEXTURE0_ARB);	glEnable(GL_TEXTURE_2D);	glBindTexture(GL_TEXTURE_2D,updateTex);	drawQuad();	glEnable(GL_LIGHTING);}

This is my drawing function. The two segments of curly braces are actually nested within an encompassing pair, I just include them to clarify code.
Thanks for taking a look.

Woops, forgot the glCopyTexImage2D part.

	glReadBuffer(GL_BACK);	glGenTextures(1, &updateTex);	glBindTexture(GL_TEXTURE_2D, updateTex);	glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGB,0,0,WIDTH,HEIGHT,0);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

And I just have that last segment in my initialization function.

[Edited by - OpenGLAaron on November 24, 2004 10:24:46 PM]

##### Share on other sites
Firstly, using the "source" tags when posting code is good. The FAQ can tell you how.

First look - can't see anything obviously wrong. I'd like to know what happens in your "drawQuad" routine - if the routine is not drawing to the entire screen properly, it could result in the effect you're experiencing.

Regards,

Hew T.

##### Share on other sites
I was using source tags... Sorta. I'm used to using [ code ] tags, and I assumed it was the same here. If you looked closely, you would have seen that the formatting was slightly different between my [ code ] tags. Nevertheless, I fixed it to what it should have been from the start. Sorry. :P

Here's my drawQuad function. Pretty straightforward, really:
void drawQuad(){	glBegin(GL_QUADS);	{		glNormal3f(0,0,1);		glMultiTexCoord2fARB(GL_TEXTURE0, 0,0);		glMultiTexCoord2fARB(GL_TEXTURE1, 0,0);		glMultiTexCoord2fARB(GL_TEXTURE2, 0,0);		glVertex3f(-1,-1,0);		glMultiTexCoord2fARB(GL_TEXTURE0, 1,0);		glMultiTexCoord2fARB(GL_TEXTURE1, 1,0);		glMultiTexCoord2fARB(GL_TEXTURE2, 1,0);		glVertex3f(1,-1,0);		glMultiTexCoord2fARB(GL_TEXTURE0, 1,1);		glMultiTexCoord2fARB(GL_TEXTURE1, 1,1);		glMultiTexCoord2fARB(GL_TEXTURE2, 1,1);		glVertex3f(1,1,0);		glMultiTexCoord2fARB(GL_TEXTURE0, 0,1);		glMultiTexCoord2fARB(GL_TEXTURE1, 0,1);		glMultiTexCoord2fARB(GL_TEXTURE2, 0,1);		glVertex3f(-1,1,0);	}	glEnd();}

 man, I'm a typo monster.
[edit 2] Just updated the code a bit, removed some things I don't need anymore.

##### Share on other sites
I can't understand the exact problem from the screenshot, can you post a screenshot (or drawing) on how it is supposed to look?
Anyway, unless your screen size is a power of 2, then you might have some strange results. How big is the screen (x,y) and how big is the texture?

##### Share on other sites
The screen size is 512 x 512 and the texture size is the same. Maybe I could send you the executable and then you could get a better idea of what's wrong. Actually, I could just post it on my website and anyone could try it out. Give me a few...

Okay, here it is:

##### Share on other sites
What happens if you remove:
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);	glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);

##### Share on other sites
When I remove those, the screen is just blank, but white.
I've had problems before where not defining the minification and magnification resulted in the texture not working at all.
Hmm. I tried leaving just the minification and magnification settings in, but it does the same thing.

##### Share on other sites
I tried to run your application, but it needs glut, and I don't have it (and I am too tired right now to go and find it). Anyway, try this: Remove (comment out) all the code in the program except for the code that copies the image to the texture and then make a simple code that displays that texture, but without any fancy things such as multitexxturing, polygon offset, etc. See where the problem is.

##### Share on other sites
Alright, I updated the .zip file on my webpage. I simplified the project to about as bare as I can possibly make it, but it's still doing that weird thing.
For the project, I removed the explicit inclusion of glut.h, and as far as I can tell, glfw doesn't require the use of glut. So, maybe give it another shot. I can't promise that it will work, though.

 I get the same problem if I replace the glTexSubImage2D call with a glTexImage2D call.

[Edited by - OpenGLAaron on November 25, 2004 12:16:02 AM]

##### Share on other sites
Not sure if you just uploaded a different version, or something... but it appears to work for me. It's currently 'painting' a white square over the black background. If I resize the window it makes, however, things start getting crazy. For example - if I reduce the vertical size of the window slightly (one or two pixels), it displays what I believe is the 'squishing' problem you have.

I suspect there is a mismatch between the 'height' and/or 'width' variables you're using to grab the image, and the actual client-space height/width of the window.

Regards,

Hew T.

##### Share on other sites
Yeah, for the new version I "disabled" the resizing code. Improperly, mind you, since I simply don't do anything if you resize it.
But you say it works fine otherwise? I wonder if it's a driver issue for me? Mind telling me what hardware you are using, driver, etc?

 when I resize the window, I'm getting all kinds of crazy stuff; all very similar to the "squishing" problem I have by default. Odd.

[edit 2] You are absolutely correct.
GLFW isn't actually setting my window size to 512,512 like I'm requesting; instead, it's setting it to 512,506.
Oh well, thanks for all the help, everyone! I was going to switch over to GLUT tomorrow so I could easily use GLUI anyway...

[Edited by - OpenGLAaron on November 25, 2004 1:09:13 AM]

##### Share on other sites
I don't think it's a driver issue. For the hell of it, I decided to do a screengrab (good ol' PrintScreen!), and dump the grab into Photoshop.

Photoshop tells me the black 'interior' section of your GL window is actually 511 * 511 in size, not 512*512. Herein lies the problem, methinks. I'm not sure what method you're using to create your window, but if the window is smaller than you want, the framebuffer will also be so... and I think glCopyTexSubImage2D will subsequently get a smaller number of pixels... and so on.

Worth a look, anyways.

Regards,

Hew T.

Edit : Beat my reply. Best of luck with GLUT / GLUI.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628284
• Total Posts
2981829
• ### Similar Content

• By mellinoe
Hi all,
First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
• By aejt
I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
I have these classes:
For GPU resources:
Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).
And my plan is to define everything into these XML documents to abstract away files:
Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
Factory classes for resources:
For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
Factory classes for assets:
Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).

Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
Thanks!
• By nedondev
I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
Thanks.

• So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!

• Hi,
I'm trying to learn OpenGL through a website and have proceeded until this page of it. The output is a simple triangle. The problem is the complexity.
I have read that page several times and tried to analyse the code but I haven't understood the code properly and completely yet. This is the code: