Can I tile a texture on a RectangleShape? - c++

I have a rectangleshape that can change size in a program of mine (I won't copy it here as it is too large), and I have assigned a 64x64 pixel texture to it. The shape itself is much larger than the texture, but the texture just gets spread over the whole shape. Is there a way to change it so that the texture remains 64x64, but tiles across the rectangleshape?

Figured out how to do it, I just had to use the line
tex.setRepeated(true);
and the line
rect.setTextureRect(sf::IntRect(0, 0, xRect, yRect));
with xRect and yRect being the dimensions of the rectangleobject, tex bing the texture name and and rect being the rectangle object that the texture has been assigned to.

Related

Resizing an OpenGL texture without changing UV coordinates

I have a texture atlas that gets generated during gameplay. Textures are loaded and unloaded depending on which textures are visible when the player moves around.
The problem is when there are too many textures to fit in the atlas and I need to resize it, but if I simply resize the texture atlas by copying it to a larger texture, then I would need to re-render everything to update the UV coordinates of the quads.
Is it possible to somehow create an OpenGL texture and define its width and height in UV coordinates to be something other than 1?
One solution would be to use texel coordinates rather than normalized coordinates (assuming you are not going to scale or move the images around within the atlas when you make it bigger). If you can live with their limitations (no wrapping, no mipmaps, only linear filtering), you could just use Rectangle Textures and call it a day. Alternatively, you could simply apply scaling factors to the texture coordinates in your vertex shader, either by passing them as uniforms or directly querying the texture dimensions in the shader using the textureSize() GLSL function.
Alternatively, you might consider using an Array Texture to hold multiple atlases. Instead of enlarging an existing atlas to make room for more subimages, you could just start a new atlas in another array slice. That way, the texture coordinates of all existing subimages would stay the sameā€¦

OpenGL to OpenSceneGraph migration: Incorrect display in case of certain texture dimensions

I am replacing the OpenGL code of my app with code that uses OpenSceneGraph.
I am working with large images (resolution higher than 5000x5000px), therefore images are split into smaller tiles.
The OpenGL code to draw the tiles uses glTexImage2D(GL_TEXTURE_2D, ..., imageData), where imageData is the tile byte array.
With OpenSceneGraph, I create an osg::Image with the same imageData and use this osg::Image to texture a simple quad.
The problem is that I have an ugly display resulting for certain osg::Image dimensions.
For tiles likes 256x128, everything is OK.
That's how the original image loogs with OpenGL
But here's how it looks for 254x130 tile and osg::Image:
I would like to understand what the problem is. Since OpenSceneGraph is based on OpenGL, I guess the OpenSceneGraph code I wrote is equivalent to the old OpenGL one. Furthermore, I cannot change the tile size, so I really need to make it work with 254x130 tiles.
image creation code :
`osg::Image * image = new osg::Image();
//width, height, textFormat, pixelFormat, type and data
//are the ones that were used with glTexImage2D
image->setImage(width, height, 1, textFormat, pixelFormat, type, data, NO_DELETE);
osg::Texture2D * texture = new osg::Texture2D;
texture->setImage(image);
stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);`
I think it's most likely a mismatch between the pixel data and the format/type you pass to setImage().
For instance, if your image data is RGB with one byte per color, you should call
image->setImage(w, h, 1, GL_RGBA8, GL_RGB, GL_UNSIGNED_BYTE, data, osg::Image::NO_DELETE);
If your texture is flipped vertically, it's because openGL always consider the texture origin in the bottom left corner, so you either have to flip the image data before invoking setImage() (or invert the UV coordinates of your geometries).

Scaling a mesh object with texture

I have a mesh which have a tiled texture applied. When, I scaled that object in DirectX, the size of texture tiles also gets scaled.
Is there a way to keep texture size same and scale only the object. If, object size gets increased, reapply same texture on that part again with original tile size.

opengl - how to put texture on 3d irregular object

I have to create animation where gatling gun will be shoot (it doesn't have to be complex, cause it's just a practice). I drew basic version of my gun which looks like this:
Don't bother colors - i made them like that to be able to see where are egdes of particular parts of gun. Now i would like to make it look better by using some texture - moro or something like metalic color - example 1 or example2. I know how to load texture and how to use it for 2d objects, but i have no idea if there is a possiblity to use this texture for my whole drawing or do i have to use texture for every part separately? This is my code which corresponds to load texture from bmp file and make it able to use:
void initTexture(string fileName)
{
loadBmp(fileName.c_str());
textureId = 13;
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
tex.info.biWidth, tex.info.biHeight, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
tex.px); //The actual pixel data
}
loadBmp() is function to load bitmap file. I tried to search something in the Internet and stackoverflow, but all exmaples where about cubes or spheres, which doesn't help me. How can i put texture on this drawing?
Texture mapping requires to (manually) assign texture coordinates to each vertex. There are some approaches on automatic texture coordinate generation, or getting away without texture coordinates, by giving each face an individual pixel (Disney Animation pioneered the later method for their computer animated films).
Since you didn't specify which program you used to model I'll refer you to some tutorials on texture UV mapping for Blender
http://wiki.blender.org/index.php/Doc:2.6/Manual/Textures/Mapping/UV/Unwrapping
Please don't tell me you did "code" your gun, because this is wrong, wrong, wrong!
Once you got the texture coordinates you pass them to OpenGL as just another vertex attribute.

How to match texture size with window size?

I am trying to draw pixels on a given texture, when I move my mouse around. Something similar to drawing program, but with layers.
How do I expand my texture so that its dimension matches my window size (e.g. 1280 x 1080), this is mainly because I'd like to click anywhere on the window, and it will update the pixel.
In OpenGL textures get their size set upon creation. You can't change the size afterwards. But you can create a new texture and copy the contents of the old one to it. For that you bind the old texture as a framebuffer object (FBO) color attachment and render the old texture into it.