Advertisement

Question about " glBindTexture(GL_TEXTURE_2D, texture[1]);"

Started by October 31, 2003 08:02 PM
9 comments, last by eduardor2k 21 years, 4 months ago
Hi to everyone. I would like to know how it works this function of OpenGL more precicely, what kind of data has to be texture[1], I Supose that he only accepts "int" (i don't know if in english it's integer). //****************// glBindTexture(GL_TEXTURE_2D, texture[1]); //****************// Because i would like to change texture[1] to texture[tex] for example, i would like to add the number of a variable called "tex", but when i execute the program it gives me debugs errors. Thank you for reading this post, but if you can answer it better. [edited by - eduardor2k on October 31, 2003 9:08:04 PM]
Thank you for reading this post, but if you can answer it better. :)
Just declare :

GLuint tex

========================
Leyder Dylan (dylan.leyder@slug-production.be.tf
http://users.skynet.be/fa550206/Slug-Production/Index.htm/
========================Leyder Dylan (dylan.leyder@slug-production.be.tf http://users.skynet.be/fa550206/Slug-Production/Index.htm/
Advertisement
Hi, thanks for GLuint,
But i what i want to do is the following.

the variable "tex" contains the nuber of the texture that has to be applied to every triangle.

What i know about this fonction is the following:
glBindTexture(GL_TEXTURE_2D, texture[1]);

Where texture[n], n is the texture who is going to be applied.

I would like to do is something like that:
glBindTexture(GL_TEXTURE_2D, texture[tex]);



Thank you for reading this post, but if you can answer it better.
Thank you for reading this post, but if you can answer it better. :)
CLuint *texture = new CLuint[30];// Reserve entriesglGenTextures(30, texture);// Load and create your textures here// ...// Bind texture based on [tex]for( int tex = 0; tex < 30; tex++ ) {   glBindTexture(GL_TEXTURE_2D, texture[tex]);   RenderStuff(tex);} 


You can also drop the glGenTextures and the texture array altogether, and address the texture directly by a positive integer larger or equal to one. Download and read the OpenGL redbook.
quote:
Original post by eduardor2k
I would like to do is something like that:
glBindTexture(GL_TEXTURE_2D, texture[tex]);



Try this, define texture as a GLuint with (number_of_textures-1);
Then you can bind into this array with glBindTexture.

Eg

GLuint texture[9]; //Space for 10 textures. (May help to define as a global variable)

//Load your texture here

//For this example we will fill all 10 texture slots with the same texture.
for(int texNum=0; texNum=<9; texNum++){
glGenTextures(1, &texture[texNum]);
//Other texture generation parameters here.
}

//You only need to call bind texture before drawing
int desiredTexture = 4;
glBindTexture(GL_TEXTURE_2D, texture[desiredTexture]);


This seems to be working for me, with one proviso, texture 0 always seems to be white, but I think thats a bug in my code more than likely.

Hope this helps.


-- Damn it took me 20 mins to write this post!! (Well I did get pizza in the middle...)

[edited by - MrChris on October 31, 2003 10:27:34 PM]
MrChris, if you want 10 textures you need to declare texture[10], not texture[9]
But besides that I''m assuming your question is answered
Advertisement
Hi Thanks to all for answering this post.
I'm using lesson 10 from nehe's as basecode.
If you saw the code, the fonction "StupWorld()" extracts the world data from a file.
Now it look like this:

//***** CODE *********//
void SetupWorld()
{

float x, y, z, u, v, tex;
int numtriangles, numtexturas;
FILE *filein;
char oneline[255];
filein = fopen("data/world.txt", "rt"); // File To Load World Data From
logg.Print("\n-- [LECTURA DEL NIVEL] --\n");

// Se grava en el Log el registro de la lectura de archivos.
logg.Print("Carga del Archivo: world.txt -- OK",filein);

// Leemos la cantidad de texturas que hay en el nivel.
readstr(filein,oneline);
sscanf(oneline, "TEXTURAS: %d\n", &numtexturas);
// Guardamos el resultado en el Log.
logg.Print("Nº de Texturas: %d",numtexturas);

readstr(filein,oneline);
sscanf(oneline, "NUMPOLLIES: %d\n", &numtriangles);
logg.Print("Estracción de datos -- OK\n");

sector1.triangle = new TRIANGLE[numtriangles];
sector1.numtriangles = numtriangles;
for (int loop = 0; loop < numtriangles; loop++)
{
for (int vert = 0; vert < 3; vert++)
{
readstr(filein,oneline);
sscanf(oneline, "%f %f %f %f %f %f", &x, &y, &z, &u, &v, &tex);
sector1.triangle[loop].vertex[vert].x = x;
sector1.triangle[loop].vertex[vert].y = y;
sector1.triangle[loop].vertex[vert].z = z;
sector1.triangle[loop].vertex[vert].u = u;
sector1.triangle[loop].vertex[vert].v = v;
logg.Print("Datos de las texturas: %f",tex);
logg.Print("-- Siguiente ciclo --");
}
}
fclose(filein);
return;
}
//********* CODE ********//

After that came the fonction who changes the coordinates to triangle strip and map the poligons.

//********* code *********//

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear The Screen And The Depth Buffer
glLoadIdentity(); // Reset The View

// VARIABLES QUE AFECTAN A OPENGL
GLfloat x_m, y_m, z_m, u_m, v_m;
GLfloat xtrans = -xpos;
GLfloat ztrans = -zpos;
GLfloat ytrans = -walkbias-0.25f;
GLfloat sceneroty = 360.0f - yrot;

int numtriangles;

glRotatef(lookupdown,1.0f,0,0);
glRotatef(sceneroty,0,1.0f,0);

glTranslatef(xtrans, ytrans, ztrans);

//***************** SELECCIÓN DE TEXTURA A CARGAR ************************//
// cÓDIGO oRIGINAL: glBindTexture(GL_TEXTURE_2D, texture[filter]);
glBindTexture(GL_TEXTURE_2D, texture[1]);

numtriangles = sector1.numtriangles;

// Process Each Triangle
for (int loop_m = 0; loop_m < numtriangles; loop_m++)
{
glBegin(GL_TRIANGLES);
glNormal3f( 0.0f, 0.0f, 1.0f);
x_m = sector1.triangle[loop_m].vertex[0].x;
y_m = sector1.triangle[loop_m].vertex[0].y;
z_m = sector1.triangle[loop_m].vertex[0].z;
u_m = sector1.triangle[loop_m].vertex[0].u;
v_m = sector1.triangle[loop_m].vertex[0].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

x_m = sector1.triangle[loop_m].vertex[1].x;
y_m = sector1.triangle[loop_m].vertex[1].y;
z_m = sector1.triangle[loop_m].vertex[1].z;
u_m = sector1.triangle[loop_m].vertex[1].u;
v_m = sector1.triangle[loop_m].vertex[1].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);

x_m = sector1.triangle[loop_m].vertex[2].x;
y_m = sector1.triangle[loop_m].vertex[2].y;
z_m = sector1.triangle[loop_m].vertex[2].z;
u_m = sector1.triangle[loop_m].vertex[2].u;
v_m = sector1.triangle[loop_m].vertex[2].v;
glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);
glEnd();
}

// Posición de las letras en pantalla.
glRasterPos2f(0.0f, 1.0f);
glPrint("R2K ENGINE");

return TRUE; // Everything Went OK
}

//******* END CODE ********//

What i want to do in drawglscene(); is when every triangle is procesed aplied the texture who's stored in the variable "tex".

One question how do nehe's to pass variables between fonctions?


Thank you for reading this post, but if you can answer it better.

[edited by - eduardor2k on November 1, 2003 7:57:17 AM]
Thank you for reading this post, but if you can answer it better. :)
change that last part to
//***************** SELECCIÓN DE TEXTURA A CARGAR ************************//// cÓDIGO oRIGINAL: glBindTexture(GL_TEXTURE_2D, texture[filter]);numtriangles = sector1.numtriangles;// Process Each Trianglefor (int loop_m = 0; loop_m < numtriangles; loop_m++){glBindTexture(GL_TEXTURE_2D, texture[loop_m]);glBegin(GL_TRIANGLES);glNormal3f( 0.0f, 0.0f, 1.0f);x_m = sector1.triangle[loop_m].vertex[0].x;y_m = sector1.triangle[loop_m].vertex[0].y;z_m = sector1.triangle[loop_m].vertex[0].z;u_m = sector1.triangle[loop_m].vertex[0].u;v_m = sector1.triangle[loop_m].vertex[0].v;glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);x_m = sector1.triangle[loop_m].vertex[1].x;y_m = sector1.triangle[loop_m].vertex[1].y;z_m = sector1.triangle[loop_m].vertex[1].z;u_m = sector1.triangle[loop_m].vertex[1].u;v_m = sector1.triangle[loop_m].vertex[1].v;glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);x_m = sector1.triangle[loop_m].vertex[2].x;y_m = sector1.triangle[loop_m].vertex[2].y;z_m = sector1.triangle[loop_m].vertex[2].z;u_m = sector1.triangle[loop_m].vertex[2].u;v_m = sector1.triangle[loop_m].vertex[2].v;glTexCoord2f(u_m,v_m); glVertex3f(x_m,y_m,z_m);glEnd();}// Posición de las letras en pantalla.glRasterPos2f(0.0f, 1.0f);glPrint("R2K ENGINE");return TRUE; // Everything Went OK} 


That is assuming that each triangle has it''s own texture
Thanks m_e_my_self.

But in the code wou wrote, a think you are asigning the nº of the texture = loop_m,
glBindTexture(GL_TEXTURE_2D, texture[loop_m]);
triangle nº1 = texture 1
triangle nº2 = texture 2

What i want to do is nº of the texture = text (this one comes from the setuworld() function.

glBindTexture(GL_TEXTURE_2D, texture[tex]);
(But i doesn''t work when i do that)

That way i need only to change the values from the text file, to change the texture.



Thank you for reading this post, but if you can answer it better.
Thank you for reading this post, but if you can answer it better. :)
eduardor2k,

I'm posting the complete code to my basic image panelling class. Note this uses SDL and STD::string in some parts.
Note though that if you create an instance of this object and load a texture file into a slot you can then draw that texture by passing the slot number into the draw method.

--Note I'm primarily a Java programmer and use those conventions rather than C++ (why doesn't someone write a C++ for Java programmers book!!!)

Oh, the .h is in the second source block.

#include "ImagePanel.h"int MAX_TEXTURES = 255;GLuint *texture = new GLuint[MAX_TEXTURES];ImagePanel::ImagePanel(){    glGenTextures(MAX_TEXTURES, texture);	zrot=0;}ImagePanel::drawImage(int imageNumber){	glMatrixMode(GL_PROJECTION);						// Select The Projection Matrix	glLoadIdentity();									// Reset The Projection Matrix	// Calculate The Aspect Ratio Of The Window	gluPerspective(45.0f,(GLfloat)800/(GLfloat)600,0.1f,100.0f);	glMatrixMode(GL_MODELVIEW);							// Select The Modelview Matrix    	glEnable(GL_TEXTURE_2D);	glLoadIdentity();	glTranslatef(0.0,0.0,-5.0);	glRotatef(zrot,0.0,0.0,1.0);	glBindTexture(GL_TEXTURE_2D, texture[imageNumber]);	printf("Binding...");	glBegin(GL_QUADS);		glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, -1.0f,  1.0f);// Bottom Left Of The Texture and Quad		glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, -1.0f,  1.0f);// Bottom Right Of The Texture and Quad		glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);// Top Right Of The Texture and Quad		glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);// Top Left Of The Texture and Quad	glEnd();	zrot += 0.5;	glLoadIdentity();}ImagePanel::loadTexture(string textureName, int slotNumber){	SDL_Surface *TextureImage;	printf("loading image...");	TextureImage=IMG_Load(textureName.c_str());	imageTexture[slotNumber]=TextureImage;	glBindTexture(GL_TEXTURE_2D, texture[slotNumber]);    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureImage->w, TextureImage->h, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage->pixels);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);// Linear Filtering    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);// Linear Filtering	printf("texture set\n");}



#include "windows.h"#include "SDL.h"#include "SDL_mixer.h" #include "SDL_image.h"#include "smpeg.h" #include "GL/gl.h"#include "GL/glu.h"//Why do I have to do this?! Odd.#ifndef __STD_STRING#define __STD_STRING#include <string>using namespace std;#endifclass ImagePanel{public: 					ImagePanel();					drawImage(int imageNumber);					loadTexture(string textureName, int slotNumber);private:	SDL_Surface		*imageTexture[255];	GLuint			zrot;};


Hope this helps.

Oh and m_e_my_self, I'm having a bad array day ok! :D

[edited by - MrChris on November 1, 2003 1:55:58 PM]

This topic is closed to new replies.

Advertisement