PDA

View Full Version : Texturing Problem with C++



Gold_Dragon
10-06-2003, 01:59 PM
Does a texture just fill the object in with copy of the textures like bricks or does it stretch to from each end point?

Does the TexCoord2D, like the start of the texture and the end of the texture or does it have to the coordinates as the object? If it does, then does it have to work with the coordinates of the object? For example, do you start the first TexCoord when you start the first coordinate of the object and does it have to be in order?

Here is my code in full:



static unsigned int texWood;

void init()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Sets the background color
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST); // Enable Depth Test
glBindTexture(GL_TEXTURE_2D, texWood);
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texWood);
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_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, woodWidth, woodHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, wood);
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, texWood);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-0.5, -0.5, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-0.5, 0.5, 0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(0.0, 0.5, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -0.5, 0.0);
glEnd();
glDisable(GL_TEXTURE_2D);
//glutSwapBuffers();
glFlush();
}


I'm using single Buffering and I'm also using Visual C++ 6.0. I created the image in the RGBA mode. The image is 8x8 since any higher would take to long. I can display the image using DrawPixels OpenGL function. I can't apply the texture to an image. All the code I have looked at works fine but it doesn't seem to like me for some reason.

[This message has been edited by Gold_Dragon (edited 10-06-2003).]

mdog1234
10-06-2003, 08:11 PM
In your init function you will want to put your glBindTexture after glGenTextures. It works with draw pixels because that it isnt a texture that way

Gold_Dragon
10-06-2003, 09:47 PM
Hot digity! It works, oh thank god how it works. Thanks.

Now, how do you keep it from stretching to fit the object? I want it to just repeat the same texture, through it does look kind of good the way it is.

10-07-2003, 12:17 AM
Modify your texture coordinates. Your coordinates currently vary from 0 to 1, but if you tell them to GL_REPEAT like you have and change your coordinates to vary from 0 to 6 (for example) the texture will be tiled six times over the face.

10-07-2003, 12:19 AM
Clarification, before anyone gets picky: It'll tile six times in each direction, or 36 times in total. http://www.opengl.org/discussion_boards/ubb/tongue.gif

Leyder Dylan
10-07-2003, 05:57 AM
Before drawing your polygon, you need to restet the matrix.

Try this :




void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texWood);
// the following code ...
}


[This message has been edited by Leyder Dylan (edited 10-07-2003).]

10-07-2003, 09:41 AM
Why do I want to reset the matrix? What will it do?

Gold_Dragon
10-07-2003, 09:49 AM
I want as many textures to repeat as I can. There won't be much happening in the first place so speed isn't much of a problem.

Thanks. Tutorials aren't really clear on this when using textures. I think they want to just stretch it out to fit the object whatever it may be. When you do repeating, will it copy the same texture or just reuse the same texture?

10-07-2003, 10:39 AM
Don't worry it shouldn't have any real effect of speed, unless your drivers don't handle it very well. It doesn't create new textures or anything - the same texture data is used.

The reason why tutorials stretch textures to cover an object is that there's very little else they can do! The texture doesn't know the "size" of the surface it's being applied to, indeed there isn't really any concept of "size" as there are no pre-defined units in OpenGL. You have to make the decision as to how many times you want the texture to repeat.