Posted By

Desoxena on 04/10/12


Tagged

loader bmp texture


Versions (?)

BMP Texture Loader


 / Published in: C++
 

This is a really simple texture loader. It takes bitmaps only.

  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <gl\gl.h>
  4. #include <gl\glu.h>
  5. #include <GL\glut.h>
  6. #include "SOIL.h"
  7. bool* keyStates = new bool[256];
  8. GLuint texture[1];
  9. int LoadGLTextures()
  10. {
  11. texture[0] = SOIL_load_OGL_texture("game_textures/cobblestone.bmp", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_INVERT_Y);
  12. if(texture[0] == 0) return false;
  13. glBindTexture(GL_TEXTURE_2D, texture[0]);
  14. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
  15. glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
  16. return true;
  17. }
  18. void resize(int height, int width) {
  19. const float ar = (float) width / (float) height;
  20. glViewport(0, 10, width, height);
  21. glMatrixMode(GL_PROJECTION);
  22. glLoadIdentity();
  23. glFrustum(-ar, ar, -1.0, 1.0, 2.0, 90.0);
  24. //gluLookAt(0, 2, 0, -1, 1, -3, 0, 1, 0);
  25. glMatrixMode(GL_MODELVIEW);
  26. glLoadIdentity() ;
  27. }
  28. void keyOperations (void) {
  29. if (!keyStates['a']) {}
  30. }
  31. static void Draw(void)
  32. {
  33. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  34. glLoadIdentity();
  35. glTranslatef(0.0f,0.0f,-5.0f);
  36. LoadGLTextures();
  37. glBindTexture(GL_TEXTURE_2D, texture[0]);
  38. glBegin(GL_QUADS);
  39. glTexCoord2f(0.0f, 0.0f); glVertex3f(-.25f, -.25f, 0.0f);
  40. glTexCoord2f(1.0f, 0.0f); glVertex3f( .25f, -.25f, 0.0f);
  41. glTexCoord2f(1.0f, 1.0f); glVertex3f( .25f, .25f, 0.0f);
  42. glTexCoord2f(0.0f, 1.0f); glVertex3f(-.25f, .25f, 0.0f);
  43. glEnd();
  44. glutSwapBuffers();
  45. }
  46. void keyPressed (unsigned char key, int x, int y) {
  47. keyStates[key] = false;
  48. }
  49. void keyUp (unsigned char key, int x, int y) {
  50. keyStates[key] = true;
  51. }
  52. int main(int argc, char **argv)
  53. {
  54. glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  55. glutInit(&argc, argv);
  56. glutInitWindowSize(600, 600);
  57. glutCreateWindow("texture_prog");
  58. glutReshapeFunc(resize);
  59. glutDisplayFunc(Draw);
  60. glutKeyboardFunc(keyPressed);
  61. glutKeyboardUpFunc(keyUp);
  62. ////////////////////////////////////
  63. glEnable(GL_TEXTURE_2D);
  64. glShadeModel(GL_SMOOTH);
  65. glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
  66. glClearDepth(1.0f);
  67. glEnable(GL_DEPTH_TEST);
  68. glDepthFunc(GL_LEQUAL);
  69. glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  70. /////////////////////////////////////
  71. glutMainLoop();
  72. }

Report this snippet  

You need to login to post a comment.