I need to add an earth texture to a glutSolidSphere. The problem is that I cannot figure out how to make the texture stretch over the entire sphere and still be able to rotate.
We're enabling the textures with.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE,GL_OBJECT_LINEAR);
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
//drawcode...
using GL_SPHERE_MAP in param instead of GL_OBJECT_LINEAR makes the textures look proper, but they cannot rotate.
The parameters I use for the texture are
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);
I understand that GL_REPEAT tiles the texture, while using GL_CLAMP instead gives me the texture once on the object, but I cannot get it to stretch over the whole sphere.
Does anyone know how to properly texture a glutSolidSphere?
glutSolidSphere doesn't provide proper texture coordinates, and OpenGL built in texture generation allows only for linear mappings from vertex position to vertex texture coordinate, which essentially means that you can not use them to texture a 3-sphere with a 2-flat, bounded texture (for a mathematical explanation look up the topics of topology of manifolds and map theory).
So what can you do? There are a number of possible solutions:
Don't use glutSolidSphere, but some other geometry generator that does provide proper texture coordinates (though texturing a sphere with just a single bounded 2D texture is a difficult topic, there are several mappings, each with their problems)
Use a texture with the same topology as a sphere, a cube map, then you can use the GL_NORMAL_MAP for the texture gen mode, i.e.
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_NORMAL_MAP);
Look up tutorials about cube mapping. But in a essence a cube map consists of 6 texture faces, arranged in a cube about the origin and texture coordinates are not points on the cube itself, but a direction from the origin and the addressed texel is the one, where the direction ray intersects with the cube.
Use a vertex shader, generating texture coordinates from vertex positions. Since a vertex shader is freely programmable, the mapping isn't required to be linear. Of course will run into the peculiarities of mapping a 3-sphere with a bounded 2-flat again.
Related
I'm programming a simple graphics engine in C++ that should be capable of representing the Earth. The Earth is modelled as an icosphere, and I planned to map its texture using the normals of each vertex. To do so, I'm essentially trying to get vertical and horizontal coordinates (φ and θ, respectively) (i.e. latitude and longitude) from the vertices' normals.
The following is the (edited) function that implements my approach (called when new vertices are generated). It should return texture coordinates from (0,0) to (1,1) when the parameter n is a normal vector:
glm::vec2 Icosphere::getTexCoord(glm::vec3 n)
{
float theta = (atan2f(n.x, n.z) / PI) / 2.f + 0.5f;
float phi = (asinf(-n.y) / (PI / 2.f)) / 2.f + 0.5f;
return glm::vec2(theta, phi);
}
However, this is yielding unexpected results. With this test image, my icosphere looks like this:
Small white and red triangles are located at (x,y,z) = (1,0,0). In the first image, I generated the icosphere with less triangular faces (320), whilst in the latter the icosphere is generated with ~82k triangles.
I'm suspecting that what causes those texture "glitches" is the fact that coordinates (0,*) and (1,*) are not considered adjacent. Is there a proper way of doing this?
NB: These are my texture parameters:
/* Configure texture parameters: */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
I finally managed to overcome this ugly texture mapping. The problem is indeed caused by the characteristic non-uniform orientation of triangles in an icosphere. In an icosphere (as well as in an icosahedron) it is impossible to find a plane in the origin (0,0,0) that does not intersect any triangle. If we think of the Earth, this means that all its meridians intersect some triangles. It also means that when mapping a texture, the triangles located in the texture vertical edges (i.e. with tex. coord. (1,y) and (0,y)) will have some of their vertices in either of the sides. When this happens, the texture is interpolated from x≈1 to x≈0, producing the effect showed in my images.
In order to solve this, I set the texture wrapping to GL_REPEAT and simply forced some vertices to be away from the texture coordinate space (i.e. x > 1). This allowed the texture edges to be adjacent and mapped the texture perfectly:
/* Configure texture parameters: */
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Not GL_CLAMP_TO_EDGE.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Not GL_CLAMP_TO_EDGE.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
Results:
After modifying texture coordinates of triangles in the edges (with GL_CLAMP_TO_EDGE):
Changing texture parameters to GL_REPEAT:
I want to know how to loop a texture on the surface of an object.
For a practical example, I'm trying to handle rendering world geometry, so that when I resize a world object twice the size of its texture, the texture will then appear twice - not double in size.
Is there any proper way to handle this? The only thing I can think of would be to physically create a brand new texture which is of the right size and manually copied X amount of times, but that sounds like it would eat up a lot of memory really quickly.
Here's a picture showing what I'm looking for, when resizing a plane on the XAxis:
Along with many other variables, I pass an object's UVMap to my shader as the "texture coordinate", and then I perform the following for rendering an object's texture prior to the draw arrays call:
QImage image;
image = worldObject->getQImage(i); //Within a for loop, grabs the right texture if the object has differing textures per face of object
glBindTexture(GL_TEXTURE_2D, spot);
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, image.width(), image.height(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image.bits());
This is very easy to do. Where you currently use texture coordinates in the range [0.0, 1.0], you simply use a bigger range. For example, to repeat the texture twice, as shown in your example, you specify texture coordinates in the range [0.0, 2.0].
This works in combination with using GL_REPEAT for the texture wrap mode:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
These values are actually the default, so there's no need to set them if you did not set them to a different value earlier.
This specifies that the texture should be repeated for texture coordinates outside the [0.0, 1.0] range. For example, texture coordinates between 1.0 and 2.0 will be equivalent to the coordinates between 0.0 and 1.0, repeating the whole texture.
I'm trying to blend two partially overlapping textures in GLSL and am wondering if I'm misunderstanding the concept of multi-texturing. Is it required that the textures fully overlap or can you have two offset textures that blend only where they overlap?
I have two images similar to the following (minus grid lines and text):
Example image
Ideally, the overlapping sections of the image would blend together nicely so that the final result would look like one smooth image that combines the two together. Overlapping orange pixels, for example, would blend together or take the higher intensity.
I'm new to GLSL and have been using this article GLSL Shader Article which uses a fragment shader to blend the textures (fairly standard).
Following the article, I#m setting up each texture like so:
glUseProgramObjectARB( m_hProgramObject );
GLint nParamObj = glGetUniformLocationARB( m_hProgramObject, pParamName_i );
...
glActiveTexture(GL_TEXTURE0 + nTextureID_i );
glBindTexture(GL_TEXTURE_2D, nTextureID_i);
glUniform1iARB( nParamObj, nTextureID_i );
I then bind each texture and draw triangle strips. My textures are created as:
glBindTexture( GL_TEXTURE_2D, m_nTextureID );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
glPixelStorei(GL_UNPACK_SKIP_ROWS, 0);
glPixelStorei(GL_UNPACK_SKIP_PIXELS, 0);
glTexImage2D(GL_TEXTURE_2D, 0, 4, nWidth, nHeight, 0, GL_RGBA,
GL_UNSIGNED_BYTE, pbyData);
Does that process seem reasonable or am I misunderstanding the concept? Any tips or advice on how to achieve this?
That process certainly seems adequate. The advantage of using a fragment shader is you get complete control over how the textures are combined. For the offset, you may want two sets of texture coordinates - one for each image - or you could generate them implicitly. Figuring out what you want and writing the fragment shader will probably be the difficult bit. Unfortunately if you want to blend many different textures, the fragment shader used in this way can get quite expensive or just wont work with too many textures bound.
Your example image doesn't look like any blending has occurred at all - the images are just positioned over each other. In this case, it's easier just to draw separate bits of geometry with mapped textures.
Blending is typically done by the fixed pipeline blending stage. For example using the following calls...
glEnable(GL_BLEND)
glBlendFunc(src_scale, dest_scale)
One of the most common configuration is alpha blending with the over operator: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) in which the amount blended is given by the alpha value of the colour your drawing - possibly influenced by the A component in your GL_RGBA texture. You can further manipulate the blend equations if needed. See Blending.
I have recently implemented Awesomium into a OpenGL application.
When I load Awesomium in to a texture OpenGL includes it in its shading process regardless of whether I draw the texture onto a surface or not.
I am trying to trace down the line of code that is processing the texture into the shaders, is there a specific function OpenGL uses to access all textures or a way to tell OpenGL to ignore the texture?
Update texture block
glBindTexture(GL_TEXTURE_2D, SkypeHUD);
glTexImage2D(GL_TEXTURE_2D, 0, 4, AwesomiumW, AwesomiumH, 0, GL_BGRA, GL_UNSIGNED_BYTE, surface->buffer());
Create texture block
glBindTexture(GL_TEXTURE_2D, SkypeHUD);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glBindTexture(GL_TEXTURE_2D, 0);
Drawing the scene without the texture being loaded: http://puu.sh/2bVTV
Drawing the scene after I have loaded the texture: http://puu.sh/2bVUb
You can see it blending the google texture in over the others.
Texture enable/disable should be controlled by the shader code, not some client binding state. Anyway, you most likely use several texture units (glActiveTexture); the texture binding is individual to each unit, so you'll have to do some leg work and unbind textures from each unit if you want to go this route.
I'm sure this is a relatively simple question, it's just one thing I've always had trouble wrapping my mind around.
I have a 512x512 background I'd like to tile "infinitely." I've searched around and can't seem to find a whole lot, so I figured I'd come here. Anyway, here it is:
background http://dl.dropbox.com/u/5003139/hud/stars_far.png
So, there you have it. I have a ship sprite that can move anywhere on a 2D plane, and this is a top-down game. How would I render this background so that it covers every pixel of an arbitrarily sized window?
With GL_REPEAT texture clamping/wrapping mode, texture coordinates outside the range [0,1] will wrap around, repeating the texture. So you can draw a screen filling quad, but use larger texture coordinates. For example using the texture coordinates (0,0) to (10,10) will repeat the texture 10 times in each direction. Repeating mode is enabled for the currently bound 2D texture with
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);