开发者

C++ OpenGL load image in GL_QUAD, glVertex2f

Using WIN32_FIND_DATA and FindFirstFile I'm searching for files in a directory an with fileName.find(".jpg") != std::string::npos I filter the jpg images out.

I'm using OpenGL for creating Boxes with a red color:

glBegin( GL_QUADS );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f );   glVertex2f( 0.35f, 0.7f );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f );   glVertex2f( -0.35f, 0.7f );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f );   glVertex2f( -0.35f, -0.3f );
glColor4f( 1.0f, 0.0f, 0.0f, 0.0f );   glVertex2f( 0.35f, -0.3f );

This is the box in the center with a red color.

My Question is how can I load the Images each in a Cube instead 开发者_如何转开发of the Red color (glColor4f)?

I think this is not the best way to make this, but this code is not my own Code, I'm trying to make this better for a friend.

Thank you!


You need to learn about texturing. See NeHe's tutorial on the subject as an example.

However, that tutorial is a bit old (as is your code, since you use glVertex(), so it might not matter to you right now... :).

Anyway, starting from OpenGL 3.1 and OpenGL ES 2.0, you should do it with using GLSL, fragment shaders and samplers instead. See another tutorial for that. It's actually simpler than learning all the fixed function stuff.


It's not really a good practice to use WinAPI together with OpenGL applications unless you really have reasons to - and loading textures from the disk is not a good reason.

Think this way: OpenGL is a platform-independent API, why to dimnish this advantage by using non-portable subroutines when portable alternatives exist and are more convenient to use in most cases?


For the loading textures, I recommend the SOIL library. This is likely to be much better a solution than what the NeHe tutorials recommend.

For finding files on the disk, you might want to use boost::filesystem if you want to get rid of the WinAPI dependency. But that's not a priority now.


When you have the texture loaded by SOIL (a GLuint value being the texture ID), you can do the following:

  • enable 2D texturing (glEnable(GL_TEXTURE_2D)),
  • bind the texture as active 2D texture (glBindTexture(GL_TEXTURE_2D,tex);),
  • set the active color to pure white so that the texture image will be full-bright,
  • draw the vertices as usual, but for each vertex you'll need to specify a texture coordinate (glTexCoord2f) instead of a color. (0,0) is upper left coord of the texture image, (1,1) is the lower right.

Note that the texture image must have dimensions being powers of two (like 16x16 or 256x512). If you want to use any texture size, switch to a newer OpenGL version which supports GL_TEXTURE_RECTANGLE.

Not really a lot of explaining, as far as the basics are concerned. :)

BTW- +1 for what Marcus said in his answer. You're learning an outdated OpenGL version right now; while you can do a lot of fun things with it, you can do more with at least OpenGL 2 and shaders... and it's usually easier with shaders too.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜