PDA

View Full Version : Texturing doesn't work



konrad509
12-10-2011, 06:33 AM
I did everything what was written in pdf from my teacher, but texturing doesn't work. I don't know what's wrong. Program is in zip file. In the code I marked with "//***********" the parts that concern texturing so it was easier to find them. When you run program you'll see "houses". Texture should be placed on brown side.

BionicBytes
12-10-2011, 04:43 PM
Can't you just post the bits of code concerning texture generation and main rendering loop. Much easier to answer post when we don't have to mess about unzipping and going through endless lines of unrelated code.

konrad509
12-11-2011, 03:59 AM
If you think it's enough, then ok.

variables declared at very beginning:

GLuint _textureId,_textureId2;

GLuint loadTexture(Image* image)
{
GLuint textureId;
glGenTextures (1,&textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
image->width, image->height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
image->pixels);
return textureId;
}
This is whole display function:

display(void)
{

glEnable(GL_TEXTURE_2D);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _NEAREST);
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
const double a = t*90.0;
if (deltaRuch) PlaskiRuch(deltaRuch);
if (deltakat)
{
kat += deltakat;
zorientujMnie(kat);
}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int p=100;
glBegin(GL_QUADS);
glColor3f(red,green,blue);
glVertex3f(p,0,-p);
glColor3f(red,green,blue);
glVertex3f(-p,0,-p);
glColor3f(red,green,blue);
glVertex3f(-p,0,p);
glColor3f(red,green,blue);
glVertex3f(p,0,p);
glEnd();

int num=8;
for (int i=-num;i<num;i++)
{
for (int j=-num;j<num;j++)
{
glPushMatrix();
glTranslated(0,1,0);
glTranslated(i*sp*3.2,0,j*sp*3.2);
if (fig == 0) domek(2);
else if (fig == 1) {
sp=1.7;
if (dpth<0) dpth=0;
sierp(dpth);
}
else if (fig == 2)
{
sp=1.6;
fig3();
}
glPopMatrix();
}
}
glutSwapBuffers();
}
This is part of function called "domek":

glBindTexture(GL_TEXTURE_2D, _textureId);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _NEAREST);
sp=r;
glScaled(r,r,r);
glTranslated(0,1-1/r,0);
glBegin(GL_QUADS);
//glColor3f(0.0f,0.3f,0.0f);
glTexCoord2f(0.0f,0.0f);
glVertex3f(0,0,0);
// glColor3f(0.0f,0.3f,0.0f);
glTexCoord2f(1.0f,0.0f);
glVertex3f(-1,0,0);
// glColor3f(1.0f,1.0f,0.0f);
glTexCoord2f(1.0f,1.0f);
glVertex3f(-1,-1,0);
// glColor3f(1.0f,1.0f,0.0f);
glTexCoord2f(0.0f,1.0f);
glVertex3f(0,-1,0);
glEnd();
This is in main:

Image*obrazek = loadBMP("xxx.bmp");
_textureId=loadTexture(obrazek);
delete obrazek;


I also have included imageloader.h by
#include "imageloader.h" and imageloader.cpp added just by adding file to project.

mobeen
12-11-2011, 04:28 AM
Hi,
A few issues here.

1) You cannot call a GL function before the GL context is created. Call it after the window is created .



glutInit(&amp;argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
//****************************
//cannot call gl functions here since there is no GL context
//****************************
glutCreateWindow("FreeGLUT Shapes");

glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glutIgnoreKeyRepeat(1);
glutSpecialFunc(pressKey);
glutSpecialUpFunc(releaseKey);
glutMotionFunc(mysz);
glutMouseFunc(mysz1);
createMenus();

Image* obrazek = loadBMP("xxx.bmp");
_textureId=loadTexture(obrazek);
delete obrazek;

glClearColor(0,0,0,1);
//...


2) You cannot use GL_NEAREST with GL_TEXTURE_WRAP_S and GL_TEXTURE_WRAP_T, they accept GL_CLAMP/GL_REPEAT etc modes.
GL_NEAREST and GL_LINEAR are for the magnification/minification filters.

3) Specify the texture params just once when you load the texture.

4) You should atleast specify the GL_TEXTURE_MIN_FILTER filter because the default is mipmap filter and if u dont provide mipmaps, it would not work.
So the loadtexture function becomes this,


GLuint loadTexture(Image* image)
{
GLuint textureId;
glEnable(GL_TEXTURE_2D);
glGenTextures (1,&amp;textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
image->width, image->height,
0,
GL_RGB,
GL_UNSIGNED_BYTE,
image->pixels);
return textureId;
}



See if this helps.

konrad509
12-11-2011, 04:51 AM
It's working :D Not exactly how I would like it to, but I guess I can handle it now. If not, I'll be asking again ;D Thanks for help.

konrad509
12-12-2011, 06:54 AM
So, I have a problem again :D I need to texture "ground" by repeating texture, not stretching. Certainly I'm doing something wrong because it's not working :P

Here's part of display function:

display(void)
{
.
.
.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int p=128;
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, mode);
if (f4 !=true) glDisable(GL_TEXTURE_2D);
else glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texID3);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,filter2);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
glBegin(GL_QUADS);
glColor3f(red,green,blue);
glTexCoord2f(0.0f,0.0f);
glVertex3f(p,0,-p);
glColor3f(red,green,blue);
glTexCoord2f(1.0f,0.0f);
glVertex3f(-p,0,-p);
glColor3f(red,green,blue);
glTexCoord2f(1.0f,1.0f);
glVertex3f(-p,0,p);
glColor3f(red,green,blue);
glTexCoord2f(0.0f,1.0f);
glVertex3f(p,0,p);
glEnd();
glDisable(GL_TEXTURE_2D);
.
.
.
}

mobeen
12-12-2011, 07:32 AM
So, I have a problem again :D I need to texture "ground" by repeating texture, not stretching. Certainly I'm doing something wrong because it's not working :P
Well just scale the texture coordinate from 0-1 range to 0-N range where N is the number of tiles u want.
So for repeating the texture in 10x10 tiles you would do this way


glTexCoord2f(0,0); glVertex2f(...);
glTexCoord2f(10,0); glVertex2f(...);
glTexCoord2f(10,10); glVertex2f(...);
glTexCoord2f(0,10); glVertex2f(...);

See if this helps.

konrad509
12-12-2011, 07:51 AM
I was thinking about this but I thought it's not too elegant :P
What about GL_REPEAT? Why didn't it work? I guess I don't understand use of this option.

mobeen
12-12-2011, 08:03 AM
I was thinking about this but I thought it's not too elegant :P
What about GL_REPEAT? Why didn't it work? I guess I don't understand use of this option.
This tells me that you do not understand how texturing works. The texture wrapping modes are giving hints to opengl on what to do if there are out of range texture coordinates. In our case, we changed the texture coordinates from 0-1 range to 0-10 range. Now for every point out of 0-1 range the repeat hint will tell opengl to repeat the texture again. So it is something like how the remainder operator works.

Had we specified clamping mode (GL_CLAMP), the color in the last column of current row (for s) and the color in the last row of current column will be used (for t). So this will give u the smear of solid or pattern (depending on the texture) in both s and t for out of range values.

konrad509
12-12-2011, 08:09 AM
I guess I understand now. But when I remove these lines

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
nothing changes. Does it mean that if mode isn't specified, it's GL_REPEAT as default? Or I don't understand something again?

mobeen
12-12-2011, 08:17 AM
I guess I understand now. But when I remove these lines

glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL _REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL _REPEAT);
nothing changes. Does it mean that if mode isn't specified, it's GL_REPEAT as default? Or I don't understand something again?
Yes even if u dont specify the default is GL_REPEAT.

konrad509
12-12-2011, 08:22 AM
Thanks a lot then ;)