opengl, textures have always the same size - opengl

I'm trying to apply a texture to a vertex array whit the following code:
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glColor3f(1.0f, 1.0f, 1.0f);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, texcoords);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glDrawElements(GL_QUADS, 12, GL_UNSIGNED_BYTE, faceIndices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
glDisable(GL_TEXTURE_2D);
with this texture:
so i have this result:
Now I'm wondering how can I scale the floor texture, i've already tried to scale the texture with photoshop, but the result is the same but heavier.

I assume you mean you want the texture to tile less, or tile more. In which case, change your texture coordinates, not the texture (i.e. whatever data is in texcoords).
Also, your example texture is blue, but it's brown in the rendered image. You might be swapping the R+B channels when loading.

It depends on your texture coordinates how you want to map the texture.
Let take example,it cover the whole polygon
glTexCoord2f(0.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f);
Now if you want to repeat the texture five times then provide coordinates like `
glTexCoord2f(0.0f, 0.0f);
glTexCoord2f(5.0f, 0.0f);
glTexCoord2f(5.0f, 5.0f);
glTexCoord2f(0.0f, 5.0f);`
Like above example change the value how you want to map the texture.

Related

OpenGL: Can't get textures to display

I'm trying to add a texture to a simple square for more than 5h now but I still don't manage to do so.
Here's the code I'm using in paintGL():
glEnable(GL_TEXTURE_2D);
GLuint id;
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
float pixels[] = {
0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f
};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(0,0,0);
glTexCoord2f(1.0f, 0.0f); glVertex3f(1,0,0);
glTexCoord2f(1.0f, 1.0f); glVertex3f(1,1,0);
glTexCoord2f(0.0f, 1.0f); glVertex3f(0,1,0);
glEnd();
if (glGetError() != 0)
{
qDebug() << glGetError();
}
There is no errors nor OpenGL's ones. I initialized the clear color to grey.
When I try to change color with glColor3f(...), it works.
I read about all tutorials that I could find with Google but no one helped.
SOLUTION:
I never put
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
at the right place: just after the glTexImage2D! Code above is edited and now work like a charm.
You HAVE to specify filtering for your texture:
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
Because default filter uses mipmaps, but you don't generate them.
It looks to me like you're only using one quarter of your texture. It's only 4 pixels, and you've set the texture coordinates to be just the first pixel. If that pixel is white and your texture environment is set to multiply the quad's color by the texture, and you're set to use nearest neighbor sampling, then you'll get just the color. Try changing the texture coordinates to be (0,0) to (1,1) instead of (0,0) to (0.5,0.5) and see if that gives you the expected result. You can also try setting the various texture parameters and environments differently to see how that affects your drawing.

glTexSubImage2D does not work correctly

I've written a very simple OpenGL application. Its goal is to load a texture and draw it on a plane. If I use the function 'glTexSubImage2D' the plane is not textured and the function 'glGetError' returns the error '1281' (invalid value). However if I use the function 'glTexImage2D' my plane plane is textured correctly (and I have no error).
Here's a piece of my code :
void core::RootDevice::LoadTexture(char const *pFileName)
{
SDL_Surface *pSurface = IMG_Load(pFileName);
char *pPixels = reinterpret_cast<char*>(pSurface->pixels);
uint32_t bytePerPixel = pSurface->format->BitsPerPixel;
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
{
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboID);
{
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, pSurface->w, //NO ERROR : All is ok
//pSurface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, pPixels);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, pSurface->w, //ERROR : 1281
pSurface->h, GL_RGB, GL_UNSIGNED_BYTE, pPixels);
std::cout << glGetError() << std::endl;
getchar();
}
glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);
}
glBindTexture(GL_TEXTURE_2D, 0);
}
And the rendering code :
void core::RootDevice::Render(void)
{
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureId);
{
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
}
glBindTexture(GL_TEXTURE_2D, 0);
}
And the result is the followings:
Does anyone can help me?
glTexSubImage2D() is used to replace parts or all of a texture that already has image data allocated. You have to call glTexImage2D() on the texture at least once before you can use glTexSubImage2D() on it. Unlike glTexSubImage2D(), glTexImage2D() allocates image data. You can use NULL for the last (data) argument to glTexImage2D() if you only want to allocate image data, and later set the data with glTexSubImage2D().
Newer versions of OpenGL (4.4 and ES 3.0) have a new entry point glTexStorage2D() that can be used as an alternative to glTexImage2D() to allocate the image data for a texture without specifying the data. It is similar to calling glTexImage2D() with data = NULL, but also allows specifying ahead of time if space for mipmaps will be needed.

OpenGL texturing does not work as it should

I have 128 x 128 px 24 bit bmp file for texture. It's a green square with one vertical line. It loads OK with stbi_image. In initialization are enabled
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
and texture parameters are
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
I have an object which is made with cubes and cylinders (and some polygons with vertices). I bind texture to one like this to glut:
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, tex1_id);
glColor3f(0.0f, 1.0f, 0.0f);
glTranslatef(-0.5f, 0.2f, 0.05f);
glScalef(2.4f, 0.4f, 1.0f);
glutSolidCube(0.6f);
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
or to vertices square:
glPushMatrix();
glTranslatef(-0.03f, 1.26f, -0.35f);
glRotatef(94, 1.0f, 0, 0);
glBindTexture(GL_TEXTURE_2D, tex1);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-0.3f, 0.4f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-0.3f, 0.85f, 0.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex3f(0.3f, 0.85f, 0.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex3f(0.3f, 0.4f, 0.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
I know that glutSolidCube does not generete texture coordinates but apparently with upper glEnables it works just fine. So the texture shows up on the object. Everything looks good at this point.
Problem starts when I change view with lookAt or I translate and rotate object in some path. Then it looks like as texture does not move and rotate with objects but remains static.
First: initial view; Second: slightly rotated view (lookAt); Third: slightly translated object
Same thing happens if I will draw that object with vertices. I use OpenGL 2.1 in C++ with glut.
You are doing in wrong way. You have to define texture co-ordinate.
Draw cube by giving vertices and texture co-ordinates by yourself.
//first face of cube
glTexCoord2f(0.0, 0.0);
glVertex3f(x, y, z);
glTexCoord2f(1.0, 0.0);
glVertex3f(x, y, z);
glTexCoord2f(1.0, 1.0);
glVertex3f(x, y, z);
glTexCoord2f(0.0, 1.0);
glVertex3f(x, y, z);
//and so on for all six face of cube.
You must understand how texture co-ordinates works.
Now if you want to draw the cylinder then use gluCylinder function look here for details. You can map texture on cylinder rendered using gluCylinder by using gluQuadricTexture function like below.I assume you have loaded the texture.
//Create the quadric object to render cylinder
GLUquadric *quad;
quad = gluNewQuadric();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
gluQuadricTexture(quad,1);
gluCylinder(quad,2,2,3,20,20);
You can find the similar example here.

OpenGL blurry RAW file

I am mapping a very large background texture to a quad for a sidescroller. The texture's graphics do not have any aliasing, and therein lies the problem.
The texture ends up very blurry. The image size is 800 x 600. Do the dimensions have to be a power of 2? If so, Am I stuck making a larger image at 1024 x 1024 and leaving the excess offscreen?
I am doing everything in orthographic mode. Here is the application of the texture to quad.
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, *texture);
glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(pos_x, pos_y, depth);
glTexCoord2f(1.0f, 1.0f); glVertex3f(pos_x + size_x, pos_y, depth);
glTexCoord2f(1.0f, 0.0f); glVertex3f(pos_x + size_x, pos_y + size_y, depth);
glTexCoord2f(0.0f, 0.0f); glVertex3f(pos_x, pos_y + size_y, depth);
glEnd();
glDisable(GL_TEXTURE_2D);
glDisable(GL_COLOR_MATERIAL);
Maybe you need to disable mipmap filtering when creating your texture:
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

Lightmap with multitexturing, disabling lighting for one of the textures?

How i can disable lighting for only one of the textures in this multitexturing scheme? I tried to use glDisable(GL_LIGHTING) and glEnable(GL_LIGHTING) but it doesnt remember the settings when i render the quad.
Here is the code snippet:
glDisable(GL_LIGHTING);
glEnable(GL_BLEND);
glDisable(GL_TEXTURE_RECTANGLE_ARB);
glDisable(GL_TEXTURE_2D);
//------------------------
glActiveTextureARB(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, LIGHT_MAP); // texture with black/white
//------------------------
glActiveTextureARB(GL_TEXTURE1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, MY_TEXTURE); // normal texture
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_ADD);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE1_RGB, GL_TEXTURE);
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, GL_PREVIOUS);
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, GL_ADD);
glColor4f(1, 1, 1, 1);
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 0.0f);
glMultiTexCoord2fARB(GL_TEXTURE1, 0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 0.0f);
glMultiTexCoord2fARB(GL_TEXTURE0, 0.0f, 1.0f);
glMultiTexCoord2fARB(GL_TEXTURE1, 0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 1.0f);
glMultiTexCoord2fARB(GL_TEXTURE1, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glMultiTexCoord2fARB(GL_TEXTURE0, 1.0f, 0.0f);
glMultiTexCoord2fARB(GL_TEXTURE1, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();
glActiveTextureARB(GL_TEXTURE0);
glDisable(GL_TEXTURE_2D);
glActiveTextureARB(GL_TEXTURE1);
glDisable(GL_TEXTURE_2D);
Edit: the texture loading:
glGenTextures(1, &LIGHT_MAP);
glBindTexture(GL_TEXTURE_2D, LIGHT_MAP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
Weird thing is that when i enable lighting at the first line of code, i cant see the light_map texture rendered at all in the multitexture quad. Also the lighting doesnt seem to affect that quad at all, it works on other quads (which arent multitextured) in my app.
Any help appreciated.
Your first sentence does not make sense. GL Lighting happens at the vertex level. texturing, be it single texture or multitexture. happens way after. In more details:
The lighting that got computed per vertex is interpolated to each fragment, and that value is input to the texturing pipeline stage 0.
So... The lit color L comes in and gets combined with the texture on the texture stage 0 (your light map here), according to the texture environment of stage 0. You don't show how you set up the Texture environment for texture stage 0, so I can't tell you how it will pass down.
Then your output from stage 0 (computed from lighting and texture 0) get sent to the texture stage 1 input (where your "normal" texture is bound. That's called a diffuse texture). You use an environment of COMBINE with ADD on both RGB and ALPHA here, which is not typical of lightmapping: You're adding your lighting+lightmap to the diffuse color. Usually you want to multiply them (GL_TEXTURE_ENV_MODE=GL_MODULATE rather than GL_COMBINE).
Also, you did not set the SOURCE0_ALPHA and SOURCE1_ALPHA, so I'm not sure what exactly you add to generate the stage 1 ALPHA output.
All in all, I'd advise to write down exactly what you want as a result first (as math), and try to map that to the GL multitexturing pipeline.
Edit: Following your comments, if you don't specify stage 0 TexEnv, then you get the default:
GL_TEXTURE_ENV_MODE defaults to GL_MODULATE and GL_TEXTURE_ENV_COLOR
defaults to (0, 0, 0, 0).
So your color coming from the lighting is modulated with your lightmap (which may make sense depending on what you're trying to achieve). That gets added to your diffuse (where, as I said, it should probably get modulated instead).
None of these change what I said earlier: write down exactly what math you would like for your result. From that we can help find which texture environment you may need. But you did not say what result you want to achieve.