Sign in to follow this  
Zen721

Texturing

Recommended Posts

Zen721    157
I have a problem because i don't know have to load texture to the array of vertices this is my code (i have load a texture) /code void plane1() { static GLfloat TextureCoordData[] = { 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }; static GLfloat VertexData[] = { -0.494811f, 0.000115215f, -0.7f, -0.494811f, 0.0281828f, 0.7f, 0.500000f, 0.000115215f, -0.7f, 0.500000f, 0.0281828f, 0.7f }; static GLfloat NormalData[] = { 0.000000f, 0.999602f, -0.0282140f, 0.000000f, 0.999602f, -0.0282140f, 0.000000f, 0.999602f, -0.0282140f, 0.000000f, 0.999602f, -0.0282140f }; static GLint Indices[] = { 1, 3, 2, 1, 2, 0 }; glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, 0, NormalData); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, 0, VertexData); //glEnableClientState(GL_TEXTURE_COORD_ARRAY); //glBindTexture(GL_TEXTURE_2D, texture[0]); //glTexCoordPointer(2, GL_FLOAT, 0, TextureCoordData); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, &Indices[0]); glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, &Indices[3]); } // End of polygons for object 'pplane1'

Share this post


Link to post
Share on other sites
cignox1    735
I'm new to OGL too, but If I understood your question correctly, you're asking how to tell to opengl to use a specific texture for the mesh.
Well, give a look to the following functions in the manual, for creating a texture:

glGenTextures();
glBindTexture();
glTexImage2D();

With the first you ask OGL to create one (or more) unused texture name.
With glBindTexture() you set the passed texture as the current one, so that every following texture-related instruction will operate on it.
The last let you set the picture of the texture. In order to load the image from the disc you will need an external library (as SDL_Image or windows LoadBitmap()). Note that if you use bmp or tga formats you will need to flip the lines (because they are stored bottom to top, that is, the first line is stored last and the last as first) and use GL_BGR(A) extension because colors are placed in invered order.

When you want to set a texture for rendering, use the two functions:
glBindTexture();
glEnable(GL_TEXTURE_2D);

That should be all you need to start.

Share this post


Link to post
Share on other sites
Zen721    157
OK, good
But i have loaded texture but know how can i put it to the vertices in array, because everywhere when someone load texture it is usually looks like this:
<code>
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
<code>

And texture:
<code>
GLuint texture[1];



//Load a bitmap
AUX_RGBImageRec *LoadBMP(char *Filename)
{
FILE *File = NULL;

if(!Filename) return NULL;

File = fopen(Filename, "r");

if(File)
{
fclose(File);
return auxDIBImageLoad(Filename);
}

return NULL;
}

int LoadGLTextures()
{
int Status= FALSE;

AUX_RGBImageRec *TextureImage[1];

memset(TextureImage,0, sizeof(void *) * 1);

//Load the bitmap
if (TextureImage[0] = LoadBMP("Data/Images/ground01.bmp"))
{
Status = TRUE;

glGenTextures(1, &texture[0]);

glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0,
GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}

if (TextureImage[0])
{
if (TextureImage[0]->data)
{
free(TextureImage[0]->data);
}
free(TextureImage[0]);
}
return Status;
}
</code>

Share this post


Link to post
Share on other sites
MARS_999    1627
Zen you're referring to immediate mode rendering when you see


glTexCoord2f();glVertex();



this is slow and not adivisable unless you are just playing around and need to render a few objects and thats it. From what I see you are trying to use VBO's. You need to load the mesh data. I use a struct to hold all my vertex data. e.g.


struct Vertex
{
float x,y,z;
float nx, ny, nz;
float s, t;
};



now with Vertex you can allocate memory for it as


Vertex *mesh = new Vertex[width * height];



width * height could be whatever amount of vertices your mesh needs. Then you upload all your data into that struct format and upload that mesh Vertex struct to OpenGL's VBO with your VBO commands


unsigned int vertex_buffer;
glGenBuffers(1, &vertex_buffer);
//calculate the memory requirements for the VBO
unsigned int vboObjectSizeArray = sizeof(float) * (sizeof(Vertex) / sizeof(float)) * width * height;
//allocate memory for the vertex list.
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, vboObjectSizeArray, mesh, GL_STATIC_DRAW);



I have to run now, but if this is what you are looking for I can post more later. Or maybe someone else can fill in...

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