Soundbomber's picture

Single Texture Covering Many Primitives

Can a single large texture bitmap be used to cover many triangle primitives?
If I apply a texture to my shape, it applies and scales the texture per primitive (see attached). The effect I want to achieve is to use the bitmap over all the primitives.

Inline Images

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
objarni's picture

Just set something different from 0 or 1 using TexCoord:

GL.TexCoord(0.5, 0.2); // x-coordinate in middle of texture, y-coordinate at 20% of height of texture.

Also, there are really cool effects possible using the Texture Coordinate Functions. You can do all sorts of "cheap effects" like skyboxes, mirror-spheres and such. I don't know the names of the functions right now but someone else can fill in for me.

Soundbomber's picture

This doesnt appear to work :(
Just to clarify, I want the appearance of 'draping' a single bitmap 'sheet' over the entire shape.

objarni's picture

Yeah what you want is the Texture coordinates to be the same as the vertex coordinates in world system, possibly with some transform.

But you should definitely be able to set texture coordinates different from 0 and 1 in TexCoord manually. Screenshot?

objarni's picture

Here's a manual for a page about Generating Texture Coordinates (as opposed to specifying manually via TexCoord).

I found a more visually appealing description.

Soundbomber's picture

Yes I can change the TexCoords but this doesnt give the desired effect. The screenshot would be as the previous one, just with the images scaled slightly differently. Do you mean change the coords to any arbitary value as long as it is not 0 or 1? Do this for all three vertices that make up my triangle primitive?
The bitmap doesnt even have to be in any particular place as long as it covers the entire object. Isn't there some way to maybe overlap one texture? Or do I really have to go down the route of calculating the relative texture coords for each vertex?

objarni's picture

Please read about texture coordinate generation functions. That's what you are looking for.

Basically, ever vertex you specify in OpenGL has, in addition to a location vector, also a color and a texture coordinate*.

For example, if you draw a triangle with a texture like this:

GL.BindTexture2D(mytex);
GL.Begin(Triangles);
GL.TexCoord(0.3,0.4);
GL.Vertex(0,0);
GL.Vertex(100,0);
GL.Vertex(100,100);
GL.End();

.. all vertices in the triangle with have the same texture coordinate. In the above example, all three corners of the triangle use the same texel coordinate. Thus the triangle will be of a single color! The color of the "texel" at (.3, .4) in the texture bitmap.

* .. and more, like normals. Anyone know all state available to each vertex in OpenGL? I know they have a "secondary" color too, right?

objarni's picture

About Generating Texture Coordinates: I always liked the analogy with "sculpting in stone". Think of the texture in 3d coordinates, or rather how the u,v coordinates of the texture change as you move a point through the scene. Should the u coordinate decrease when x decreases? The v coordinate when y decreases? What about the z coordinate, should it affect u or v or both?

Soundbomber's picture

I have managed to clean up the coordinates a lot by using some simple maths, but this is only really effective on areas where I have simple primitives. On the areas where I have more detail like curved parts of my beam, I am struggling. Does anyone have any working code for automatic texture coordinate generation?

objarni's picture

Do you use TexGen?

Soundbomber's picture

No