Sign in to follow this  
prophete

mapping 2 different textures

Recommended Posts

When you want to change texture to use. For example, in the display function.

glBindTexture(GL_TEXTURE_2D, firstTexture);
glBegin(GL_TRIANGLES);
... vertices for first object goes here
glEnd();

glBindTexture(GL_TEXTURE_2D, secondTexture);
glBegin(GL_TRIANGLES);
... vertices for second object goes here
glEnd();

Or however you deraw your objects. Replace the glBegin/glEnd pairs with whatever you use to draw the objects.

Share this post


Link to post
Share on other sites
sorry.. here's the code


#include <iostream>
#include <GL/glut.h>
#include "READ_BMP.h"


float zoom = 15.0f;
float rotx = 0;
float roty = 0.001f;
float tx = 0;
float ty = 0;
int lastx=0;
int lasty=0;
unsigned char Buttons[3] = {0};
BYTE * grass;
BYTE * wood;
BYTE * sky;
int image_width, image_height, width, height;
float stool_width=1.1, stool_foot_height=0.6, stool_foot_width=0.2, stool_height = 2;
GLuint texGrass, texSky, texSnow, texWood;

//-------------------------------------------------------------------------------
/// Initialises the openGL scene
void Init()
{
glEnable(GL_DEPTH_TEST);
}

//-------------------------------------------------------------------------------
/// Draws the scene
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//used for transformation of scene
glLoadIdentity();
glTranslatef(0,0,-zoom);
glTranslatef(tx,ty,0);
glRotatef(rotx,1,0,0);
glRotatef(roty,0,1,0);

// draw plane
glBindTexture (GL_TEXTURE_2D, texGrass);
glBegin(GL_QUADS);
glTexCoord2f (0, 0);
glVertex3f(-10,0,-10);

glTexCoord2f (0, 1);
glVertex3f(-10,0,10);

glTexCoord2f (1, 1);
glVertex3f(10,0,10);

glTexCoord2f (1, 0);
glVertex3f(10,0,-10);
glEnd();

glBindTexture(GL_TEXTURE_2D, texWood);

glBegin(GL_QUADS); //stool foot 1
glVertex3f(0.5, 0, 0);

glVertex3f (0.5 + stool_foot_width, 0, 0);

glVertex3f (0.5 + stool_foot_width, 0 + stool_foot_height, 0);

glVertex3f (0.5, 0 + stool_foot_height, 0);
glEnd();


glBegin(GL_QUADS); //stool seat
glTexCoord2f (0, 0);
glVertex3f(0.5, stool_foot_height, 0);

glTexCoord2f (0, 1);
glVertex3f(0.5 + stool_width, stool_foot_height, 0);

glTexCoord2f (1, 1);
glVertex3f(0.5 + stool_width, stool_foot_height, stool_width);

glTexCoord2f (1, 0);
glVertex3f(0.5, stool_foot_height, stool_width);
glEnd();


glBegin(GL_QUADS); //stool foot 2
glTexCoord2f (0, 0);
glVertex3f (0.5 + stool_width, 0, 0);

glTexCoord2f (0, 1);
glVertex3f (0.5 + stool_width - stool_foot_width, 0, 0);

glTexCoord2f (1, 1);
glVertex3f (0.5 + stool_width - stool_foot_width, stool_foot_height, 0);

glTexCoord2f (1, 0);
glVertex3f (0.5 + stool_width, stool_foot_height, 0);
glEnd();


glBegin(GL_QUADS); //stool foot 3
glTexCoord2f (0, 0);
glVertex3f (0.5 + stool_width, 0, stool_width);

glTexCoord2f (0, 1);
glVertex3f (0.5 + stool_width - stool_foot_width, 0, stool_width);

glTexCoord2f (1, 1);
glVertex3f (0.5 + stool_width - stool_foot_width, stool_foot_height, stool_width);

glTexCoord2f (1, 0);
glVertex3f (0.5 + stool_width, stool_foot_height, stool_width);
glEnd();


glBegin(GL_QUADS); //stool foot 4
glTexCoord2f (0, 0);
glVertex3f(0.5, 0, stool_width);

glTexCoord2f (0, 1);
glVertex3f (0.5 + stool_foot_width, 0, stool_width);

glTexCoord2f (1, 1);
glVertex3f (0.5 + stool_foot_width, 0 + stool_foot_height, stool_width);

glTexCoord2f (1, 0);
glVertex3f (0.5, 0 + stool_foot_height, stool_width);
glEnd();


glBegin(GL_QUADS); //stool back
glTexCoord2f (0, 0);
glVertex3f (0.5, stool_height, 0);

glTexCoord2f (0, 1);
glVertex3f (0.5 + stool_width, stool_height, 0);

glTexCoord2f (1, 1);
glVertex3f (0.5 + stool_width, stool_foot_height, 0);

glTexCoord2f (1, 0);
glVertex3f (0.5, stool_foot_height, 0);
glEnd();





glFlush();
glutSwapBuffers();
}

//-------------------------------------------------------------------------------
/// Called when the screen gets resized
/// \param w - the new width
/// \param h - the new height
///
void reshape(int w, int h)
{
// prevent divide by 0 error when minimised
if(w==0)
h = 1;

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,(float)w/h,0.1,100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


//-------------------------------------------------------------------------------
//
void Motion(int x,int y)
{
int diffx=x-lastx;
int diffy=y-lasty;
lastx=x;
lasty=y;

if( Buttons[0] && Buttons[1] )
{
zoom -= (float) 0.05f * diffx;
}
else
if( Buttons[0] )
{
rotx += (float) 0.5f * diffy;
roty += (float) 0.5f * diffx;
}
else
if( Buttons[1] )
{
tx += (float) 0.05f * diffx;
ty -= (float) 0.05f * diffy;
}
glutPostRedisplay();
}

//-------------------------------------------------------------------------------
//
void Mouse(int b,int s,int x,int y)
{
lastx=x;
lasty=y;
switch(b)
{
case GLUT_LEFT_BUTTON:
Buttons[0] = ((GLUT_DOWN==s)?1:0);
break;
case GLUT_MIDDLE_BUTTON:
Buttons[1] = ((GLUT_DOWN==s)?1:0);
break;
case GLUT_RIGHT_BUTTON:
Buttons[2] = ((GLUT_DOWN==s)?1:0);
break;
default:
break;
}
glutPostRedisplay();
}

/*void tex (BYTE * texture, char* pic)
{

BMP_Read(pic, &texture, image_width, image_height);

width = image_width;
height = image_height;

while (width > 1)
width = width/2;
while (height > 1)
height = height/2;
if (height!=1 || width != 1)
gluScaleImage ( GL_TEXTURE_2D, image_width, image_height, GL_UNSIGNED_BYTE, &wood, image_width, image_height, GL_UNSIGNED_BYTE, &wood);

glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB,image_width, image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture);
;
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, texture);

glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glEnable(GL_TEXTURE_2D);



}*/


//-------------------------------------------------------------------------------
///
void main(int argc,char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(100,100);
glutCreateWindow("3D Scene");

Init();

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(Mouse);
glutMotionFunc(Motion);


BMP_Read("pics/Grass1.bmp", &grass, image_width, image_height);

width = image_width;
height = image_height;

while (width > 1)
width = width/2;
while (height > 1)
height = height/2;
if (height!=1 || width != 1)
gluScaleImage ( GL_TEXTURE_2D, image_width, image_height, GL_UNSIGNED_BYTE, &grass, image_width, image_height, GL_UNSIGNED_BYTE, &grass);

glBindTexture (GL_TEXTURE_2D, texGrass);

glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB,image_width, image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, grass);


gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, grass);

glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

// tex(sky, "pics/Sky.bmp");


BMP_Read("pics/wood.bmp", &wood, image_width, image_height);

width = image_width;
height = image_height;

while (width > 1)
width = width/2;
while (height > 1)
height = height/2;
if (height!=1 || width != 1)
gluScaleImage ( GL_TEXTURE_2D, image_width, image_height, GL_UNSIGNED_BYTE, &wood, image_width, image_height, GL_UNSIGNED_BYTE, &wood);

glBindTexture (GL_TEXTURE_2D, texWood);
glTexImage2D ( GL_TEXTURE_2D, 0, GL_RGB,image_width, image_height, 0, GL_RGB, GL_UNSIGNED_BYTE, wood);


gluBuild2DMipmaps(GL_TEXTURE_2D, 4, image_width, image_height, GL_RGB, GL_UNSIGNED_BYTE, wood);

glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

glEnable(GL_TEXTURE_2D);



glutMainLoop();
}



[Edited by - prophete on September 8, 2006 12:57:20 PM]

Share this post


Link to post
Share on other sites
Don't just dump code in a post like that. Formatting is screwed up among other things. Edit your code and put the code inside the source-tags. See the FAQ on how to make them. I will look at the code once you edited your post and posted the code inside those tags.

Share this post


Link to post
Share on other sites
Good. A few things though. You never initialize texGrass and texWood. Technically you could let them be uninitialized, as long as you can guarantee they will both will contain different "random" values, but you cannot guarantee that. Call glGenTextures to generate unique and unused texture names.

And whatever you try to do in the two nested while-loops doesn't make any sense. Looks like it's an attempt to scale the base level into the different mipmap levels, but it's wrong. First you scale the image along the Y-axis, all the way down to height=1. Then you scale the X-axis once the image is 1 pixel tall. But then again, the actual scaling you do is a no-op; the resulting image is as large as the original image.

And finally yyou're calling both glTexImage and gluBuild2DMipmaps. Choose one, don't call both. If you need mipmaps, use gluBuild2DMipmaps.

edit: Actually, ignore that about scaling the image in Y and then X. Misread it, but still, it makes no sense to me what it's for.

Share this post


Link to post
Share on other sites
I don't know why. I don't know what the code looks like after the changes you made. But the only reason I can see why you would get the same texture on both objects is if the two variables have the same value. Not saying they do, but saying that's the only reason I see for that behaviour.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this