Jump to content
  • Advertisement
Sign in to follow this  
dreamslayerx

OpenGL Making multiple texture objects with OpenGL and SDL

This topic is 2644 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi guys. I am having a little trouble using opengl to read in multiple textures. The issue is that I am trying to display multiple objects and assign one UV map to that texture. I can read in on model and texture find, but for 2 models it seems to only load up the last texture id for both models. So basically I want to display a house and a cube. I have both uv maps but only the final texture is loading. Can anyone let me know how to load up multiple textures without overwriting others, something like a texture array or somthing?

Here is an example of what I am getting:

[attachment=5520:Texture multiple issue.jpg]


If you notice the texture for the cube is correct, while the texture for the house was over written, since the cube is the last texture to be read in.
Here is the code below:
The code shows me loading in the texture, and the makeGeometry function is just use to create display list and state which file and texture to load:

[/code start]

[color="#008000"][color="#008000"]//*********CURRENTLY: WORKING ON DISPLAYING MULTIPLE TEXTURES

//Texture counter variable for texture id, Tid

[color="#0000ff"][color="#0000ff"]int Tid=1;

[color="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]int loadTexture([color="#0000ff"][color="#0000ff"]const [color="#0000ff"][color="#0000ff"]char* filenameTex)

{

[color="#008000"][color="#008000"]//Here just trying to load in two different textures

SDL_Surface* img =SDL_LoadBMP(filenameTex); [color="#008000"][color="#008000"]//figure out why color isn't coming in

[color="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]int id[3]; [color="#008000"][color="#008000"]//assigning an id for glGenTextures

glGenTextures(2, &id[Tid-1]); [color="#008000"][color="#008000"]//idea assigned (how many textures to generate, &texture id)

glBindTexture(GL_TEXTURE_2D,id[Tid-1]); [color="#008000"][color="#008000"]//binding texture to the id

Tid++;

glTexImage2D(GL_TEXTURE_2D,

0,

GL_RGB,

img->w, [color="#008000"][color="#008000"]//width of image

img->h, [color="#008000"][color="#008000"]//height of image

0,

GL_BGR, [color="#008000"][color="#008000"]//format Note: sometimes the color scheme can be reversed to BGR or not reversed it is RGB

GL_UNSIGNED_BYTE, [color="#008000"][color="#008000"]//what format is the image stored

img->pixels); [color="#008000"][color="#008000"]//pixel array, pixels



[color="#008000"][color="#008000"]//Setting Parameters

glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

[color="#008000"][color="#008000"]//Warping Parameters (how texture will be applied to the object)

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); [color="#008000"][color="#008000"]//Many different parameters can be set her to manipulate the textures

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

[color="#008000"][color="#008000"]//Filtering Parameters

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); [color="#008000"][color="#008000"]//2d texture with a minimum filter, and linear interpolation (averaging pixlels)

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

SDL_FreeSurface(img); [color="#008000"][color="#008000"]//deletes the texture image out of the texture memory
[color="#0000ff"][color="#0000ff"]return id[Tid-1];

}

[color="#008000"][color="#008000"]//***********************Display List Section******************************************************

[color="#0000ff"][color="#0000ff"]void makeGeometry() [color="#008000"][color="#008000"]//Function that uses display list to build the geometry

{

[color="#008000"][color="#008000"]//Test area



OpenFile([color="#a31515"][color="#a31515"]"house.obj", [color="#a31515"][color="#a31515"]"houseUV_Cube.bmp"); [color="#008000"][color="#008000"]//This is just passed to a function OpenFile(obj file, file texture.bmp)

[color="#008000"][color="#008000"]//holder.FName is the obj file, holder.FTexture is the texture read in for the file

tex = loadTexture(holder.FTexture); [color="#008000"][color="#008000"]//texture

readFileCount(holder.FName);

readFile(holder.FName); [color="#008000"][color="#008000"]//geometry file



[color="#008000"][color="#008000"]//End Test area

listIndex = glGenLists (1); [color="#008000"][color="#008000"]//creates display list name and size of 1 for the model, this may have to be adjusted for importing multiple geometries

glNewList(listIndex, GL_COMPILE);

[color="#008000"][color="#008000"]//Now start here to build the geometry of the model

[color="#008000"][color="#008000"]//This section will go into the display function

glBegin(GL_TRIANGLES);



[color="#008000"][color="#008000"]//Section will decide if normals are read in or not by the conditional if statement

[color="#0000ff"][color="#0000ff"]if(NormExist == 0)

{

[color="#0000ff"][color="#0000ff"]for([color="#0000ff"][color="#0000ff"]int i=0; i<countObj.F*3; i++)

{

glTexCoord2f(texPoint.u,1-texPoint.v); [color="#008000"][color="#008000"]//Here I had to put 1-y-coordinate since openGL starts reading textures from (0,0), bottom left

glVertex3f(makePoint.x, makePoint.y,makePoint.z);

}

}

[color="#0000ff"][color="#0000ff"]else

{

[color="#0000ff"][color="#0000ff"]for([color="#0000ff"][color="#0000ff"]int i=0; i<countObj.F*3; i++)

{

glNormal3f(VNormPoint.x,VNormPoint.y,VNormPoint.z);

glTexCoord2f(texPoint.u,1-texPoint.v); [color="#008000"][color="#008000"]//Here I had to put 1-y-coordinate since openGL starts reading textures from (0,0), bottom left

glVertex3f(makePoint.x, makePoint.y,makePoint.z);

}

}

glEnd();

glEndList(); [color="#008000"][color="#008000"]//glCallList(listIndex); is used to call the read in data

[color="#008000"][color="#008000"]//Test area****** To load in second model



OpenFile([color="#a31515"][color="#a31515"]"cube.obj", [color="#a31515"][color="#a31515"]"cube_Cube.bmp");

tex = loadTexture(holder.FTexture); [color="#008000"][color="#008000"]//load up texure file original code

readFileCount(holder.FName);

readFile(holder.FName);

listIndex2 = glGenLists (2); [color="#008000"][color="#008000"]//creates display list name and size of 1 for the model, this may have to be adjusted for importing multiple geometries

glNewList(listIndex2, GL_COMPILE);

[color="#008000"][color="#008000"]//Now start here to build the geometry of the model

[color="#008000"][color="#008000"]//This section will go into the display function

glBegin(GL_TRIANGLES);



[color="#008000"][color="#008000"]//Section will decide if normals are read in or not by the conditional if statement

[color="#0000ff"][color="#0000ff"]if(NormExist == 0)

{

[color="#0000ff"][color="#0000ff"]for([color="#0000ff"][color="#0000ff"]int i=0; i<countObj.F*3; i++)

{



glTexCoord2f(texPoint.u,1-texPoint.v); [color="#008000"][color="#008000"]//Here I had to put 1-y-coordinate since openGL starts reading textures from (0,0), bottom left

glVertex3f(makePoint.x, makePoint.y,makePoint.z);

}

}

[color="#0000ff"][color="#0000ff"]else

{

[color="#0000ff"][color="#0000ff"]for([color="#0000ff"][color="#0000ff"]int i=0; i<countObj.F*3; i++)

{

glNormal3f(VNormPoint.x,VNormPoint.y,VNormPoint.z);

glTexCoord2f(texPoint.u,1-texPoint.v); [color="#008000"][color="#008000"]//Here I had to put 1-y-coordinate since openGL starts reading textures from (0,0), bottom left

glVertex3f(makePoint.x, makePoint.y,makePoint.z);



}

}

glEnd();

glEndList(); [color="#008000"][color="#008000"]//glCallList(listIndex2); is used to call the read in data

//For some reason this seems to be reading in one texture, displaying the final texture and overwriting the previous

[color="#008000"][color="#008000"]//End Test area***

}



[/code end]]

The code works find. I just don't understand how to display multiple textures and the book that I have doesn't state it. Thanks guys.

Share this post


Link to post
Share on other sites
Advertisement

If you notice the texture for the cube is correct, while the texture for the house was over written, since the cube is the last texture to be read in.
[/quote]

I must be misunderstanding something, but the house and the cube look to be textured by two different textures. What is wrong with your picture exactly?

I don't understand that loadTexture function anyway. You pass in a single texture name, allocate two textures, create an array of 3 ids, and then generate the first texture and then return the second texture (after incrementing Tid?), with which you do nothing with? I think you need to think a little harder about what you're trying to accomplish with that function.

Share this post


Link to post
Share on other sites


If you notice the texture for the cube is correct, while the texture for the house was over written, since the cube is the last texture to be read in.


I must be misunderstanding something, but the house and the cube look to be textured by two different textures. What is wrong with your picture exactly?

I don't understand that loadTexture function anyway. You pass in a single texture name, allocate two textures, create an array of 3 ids, and then generate the first texture and then return the second texture (after incrementing Tid?), with which you do nothing with? I think you need to think a little harder about what you're trying to accomplish with that function.
[/quote]

Its not an issue loading the texture and putting the texture on a model. It is actually the fact that the texture for the house is not the correct texture. It is actually the texture of the cube that is place on the house, and the cube texture is the last texture loaded into the id. So it seems to not even show the texture of the house and only the cube.

This is what the house suppose to look like:
[attachment=5522:House texture.jpg]

This is what the cube should look like:

[attachment=5523:Cube texture.jpg]


This is what happening when I load up both textures with different calls to the texture function. Note the cube texture is loaded last. So why is the texture on the house object, and not the cube only.

[attachment=5524:Texture multiple issue.jpg]

Any advice on creating a texture array so that I can use the correct texture? Keep it simple I am new to this.

Share this post


Link to post
Share on other sites
I figured it out. I decided not to make a texture array and load up on texture at a time using the function for loading and didn't use an array for id. I used my OpenFile() function to decide which file and texture I opened. Turns out that I needed to read my load texture function right before I did glBegin. Its strange.

Here are my results:

[attachment=5525:Multi Texutre Fixed.jpg]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!