当前位置: 动力学知识库 > 问答 > 编程问答 >

opengl - VBO texturing does not work

问题描述:

The problem is that this code display nothing. I have problem with my loads of textures. If I comment the code lines about the textures treatments it works perfectly. It displays a 4 colored square. Can you help me ?

#define OFFSET_BUFFER(bytes) ((GLfloat *)NULL + bytes)

GLfloat vertices[12] =

{

-1.0f, -1.0f, 1.0f,

-1.0f, 1.0f, 1.0f,

1.0f, 1.0f, 1.0f,

1.0f, -1.0f, 1.0f,

};

GLfloat colors[12] =

{

1.0f, 0.0f, 0.0f,

0.0f, 1.0f, 0.0f,

0.0f, 0.0f, 1.0f,

1.0f, 0.0f, 1.0f,

};

GLfloat texture[8] =

{

0.0f, 0.0f,

1.0f, 0.0f,

1.0f, 1.0f,

0.0f, 1.0f

};

int main(int argc, char *argv[])

{

SDL_Init(SDL_INIT_VIDEO);

SDL_WM_SetCaption("Texture Mapping",NULL);

SDL_SetVideoMode(500, 500, 32, SDL_OPENGL);

bool continuer = true;

SDL_Event event;

GLuint buf[2];

GLuint texture1;

glClearDepth(1.0f);

glClearColor(0.1f, 0.1f, 0.1f, 0.1f);

glEnable(GL_DEPTH_TEST);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

gluPerspective(70.0f, (float)500.0f / (float)500.0f, 1.0f, 3000.0f);

glewInit();

texture1 = loadTexture("caisse.jpg");

glBindTexture(GL_TEXTURE_2D, texture1);

glGenBuffers(2, &buf[0]);

glBindBuffer(GL_ARRAY_BUFFER, buf[0]);

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, buf[1]);

glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);

glBindBuffer(GL_ARRAY_BUFFER, texture1);

glBufferData(GL_ARRAY_BUFFER, sizeof(texture), texture, GL_STATIC_DRAW);

while (continuer)

{

SDL_WaitEvent(&event);

switch(event.type)

{

case SDL_QUIT:

continuer = false;

}

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

glBindBuffer(GL_ARRAY_BUFFER, buf[0]);

glVertexPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));

glBindBuffer(GL_ARRAY_BUFFER, buf[1]);

glColorPointer(3, GL_FLOAT, 0, OFFSET_BUFFER(0));

glBindBuffer(GL_ARRAY_BUFFER, texture1);

glTexCoordPointer(2, GL_FLOAT, 0, OFFSET_BUFFER(0));

glEnableClientState(GL_VERTEX_ARRAY);

glEnableClientState(GL_COLOR_ARRAY);

glEnableClientState(GL_TEXTURE_COORD_ARRAY);

glDrawArrays(GL_QUADS, 0, 4);

glDisableClientState(GL_TEXTURE_COORD_ARRAY);

glDisableClientState(GL_COLOR_ARRAY);

glDisableClientState(GL_VERTEX_ARRAY);

glDisable(GL_TEXTURE_2D);

glFlush();

SDL_GL_SwapBuffers();

}

SDL_Quit();

return 0;

}

网友答案:

Firstly, you need to start putting glGetError() in your code for debugging, as you're certainly generating some errors here.

You're binding your texture texture1 with glBindBuffer, which is not correct to do so. Texture coordinates are not the same as textures. You should be generating 3 buffers instead of 2, and loading the texture[8] texcoords into the third buffer.

分享给朋友:
您可能感兴趣的文章:
随机阅读: