Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [c++][OpenGL]Obsługa spritów i wyświetlanie fragmenu bitmapy
Forum PHP.pl > Inne > Hydepark
qba10
Witam
Szukam jakiegoś tutka, kodu, dokumentacji, czegokolwiek w którym wykorzystuje się wyświetlanie textur za pomocą fragmentów bitmap.
wiewiorek
A patrzyłeś na nehe ?
qba10
Taa, ale nie mogę znaleźć tam tego co potrzebuje.
Wiem jak nałożyć całą wczytaną bitmape na czworokąt, ale nie wiem porostu jak nałożyć tylko fragment tej bitmapy;
wiewiorek
A co konkretnie chcesz zrobić ? W jakim celu chcesz nałożyć fragment bitmapy ?
qba10
Mam set sprite ( nie wiem czy to tak sie nazywa - mnóstwo textur w jednym pliku graficznym) i po prosotu chce wczytać w danym polu tylko jedną texture. To pozostałość jeszcze po tym jak mapę rysowałem za pomocą SDL a teraz właśnie przerzucam się na rysowanie za pomocą OpenGL - oczywiście mówię o 2Dl
Theqos
Która wersja OpenGL? W skrócie musisz każdemu wierzchołkowi przypisać współrzędne tekstury. Poczytaj o UV Mapping.
qba10
Nie wiem jakiej używam wersji OpenGL tongue.gif

To jest kod (taki tam testowy do ogarnięcia wyświetlania textur) w którym wyświetlam textur.
  1.  
  2. #include <SDL/SDL.h>
  3. #include <SDL/SDL_image.h>
  4. #include <SDL/SDL_opengl.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. GLuint texture = NULL; //this is a handle to our texture object
  8. GLenum texture_format = NULL;
  9. GLint nofcolors;
  10.  
  11. SDL_Event event;
  12.  
  13. int loadImage() {
  14. SDL_Surface *surface; // this surface will tell us the details of the image
  15.  
  16. if ((surface = IMG_Load("/home/qba/cpp/opengl/Debug/spr.png"))) {
  17.  
  18. // Check that the image’s width is a power of 2
  19. if ( (surface->w & (surface->w - 1)) != 0 ) {
  20. printf("warning: image.bmp's width is not a power of 2\n");
  21. }
  22.  
  23. // Also check if the height is a power of 2
  24. if ( (surface->h & (surface->h - 1)) != 0 ) {
  25. printf("warning: image.bmp's width is not a power of 2\n");
  26. }
  27.  
  28. //get number of channels in the SDL surface
  29. nofcolors = surface->format->BytesPerPixel;
  30.  
  31. //contains an alpha channel
  32. if (nofcolors == 4) {
  33. if (surface->format->Rmask == 0x000000ff)
  34. texture_format = GL_RGBA;
  35. else
  36. texture_format = GL_BGRA;
  37. } else if (nofcolors == 3) //no alpha channel
  38. {
  39. if (surface->format->Rmask == 0x000000ff)
  40. texture_format = GL_RGB;
  41. else
  42. texture_format = GL_BGR;
  43. } else {
  44. printf("warning: the image is not truecolor…this will break ");
  45. }
  46.  
  47. // Have OpenGL generate a texture object handle for us
  48. glGenTextures(1, &texture);
  49.  
  50. // Bind the texture object
  51. glBindTexture(GL_TEXTURE_2D, texture);
  52.  
  53. // Set the texture’s stretching properties
  54. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  55. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  56.  
  57. glTexImage2D(GL_TEXTURE_2D, 0, nofcolors, surface->w, surface->h-50, 0,
  58. texture_format, GL_UNSIGNED_BYTE, surface->pixels);
  59. } else {
  60. printf("SDL could not load image.bmp: %s\n", SDL_GetError());
  61. SDL_Quit();
  62. return 1;
  63. }
  64.  
  65. // Free the SDL_Surface only if it was successfully created
  66. if (surface) {
  67. SDL_FreeSurface(surface);
  68. }
  69. }
  70.  
  71. void drawImage() {
  72.  
  73. // Clear the screen before drawing
  74. glClear(GL_COLOR_BUFFER_BIT);
  75.  
  76. // Bind the texture to which subsequent calls refer to
  77. glBindTexture(GL_TEXTURE_2D, texture);
  78.  
  79. glBegin(GL_QUADS);
  80. // Top-left vertex (corner)
  81. glTexCoord2i(0, 0);
  82. glVertex3f(0, 0, 0);
  83.  
  84. // Bottom-left vertex (corner)
  85. glTexCoord2i(1, 0);
  86. glVertex3f(0, 100, 0);
  87.  
  88. // Bottom-right vertex (corner)
  89. glTexCoord2i(1, 1);
  90. glVertex3f(100,100, 0);
  91.  
  92. // Top-right vertex (corner)
  93. glTexCoord2i(0, 1);
  94. glVertex3f(100, 0, 0);
  95. glEnd();
  96. glBegin(GL_QUADS);
  97. // Top-left vertex (corner)
  98. glTexCoord2i(0, 0);
  99. glVertex3f(100, 0, 0);
  100.  
  101. // Bottom-left vertex (corner)
  102. glTexCoord2i(1, 0);
  103. glVertex3f(100, 100, 0);
  104.  
  105. // Bottom-right vertex (corner)
  106. glTexCoord2i(1, 1);
  107. glVertex3f(200,100, 0);
  108.  
  109. // Top-right vertex (corner)
  110. glTexCoord2i(0, 1);
  111. glVertex3f(200, 0, 0);
  112. glEnd();
  113. glLoadIdentity();
  114. SDL_GL_SwapBuffers();
  115. }
  116.  
  117. int init() {
  118. SDL_Surface *screen;
  119.  
  120. // Slightly different SDL initialization
  121. if (SDL_Init(SDL_INIT_VIDEO) != 0) {
  122. printf("Unable to initialize SDL: %s\n", SDL_GetError());
  123. return 1;
  124. }
  125.  
  126. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  127.  
  128. screen = SDL_SetVideoMode(640, 480, 16, SDL_OPENGL | SDL_RESIZABLE);
  129. if (!screen) {
  130. printf("Unable to set video mode: %s\n", SDL_GetError());
  131. return 1;
  132. }
  133. }
  134.  
  135. void init_GL() {
  136. // Set the OpenGL state after creating the context with SDL_SetVideoMode
  137.  
  138. glClearColor(0, 0, 0, 0);
  139.  
  140. glEnable(GL_TEXTURE_2D); // Need this to display a texture
  141.  
  142. glViewport(0, 0, 640, 480);
  143.  
  144. glMatrixMode(GL_PROJECTION);
  145. glLoadIdentity();
  146.  
  147. glOrtho(0, 640, 480, 0, -1, 1);
  148.  
  149. glMatrixMode(GL_MODELVIEW);
  150. glLoadIdentity();
  151. }
  152.  
  153. void clean_up() {
  154. glDeleteTextures(1, &texture);
  155.  
  156. SDL_Quit();
  157. }
  158.  
  159. int main(int argc, char *argv[]) {
  160. //Make sure the program waits for a quit
  161. bool quit = false;
  162.  
  163. init();
  164. init_GL();
  165. loadImage();
  166. drawImage();
  167.  
  168. while (quit == false) {
  169. if (SDL_PollEvent(&event)) {
  170. //If a key was pressed
  171. if (event.type == SDL_KEYDOWN) {
  172. //Adjust the velocity
  173. switch (event.key.keysym.sym) {
  174. case SDLK_ESCAPE:
  175. quit = true;
  176. break;
  177. default:
  178. ;
  179. }
  180. }
  181.  
  182. }
  183. }
  184. clean_up();
  185. }
Theqos
W tym przypadku glTexCoord'em mapujesz teksturę na dany wierzchołek. Przy czy o ile twoja tekstura ma jakąś tam rozdzielczość typu 1024x1024, to tutaj używasz przedzialu [0,1]. Czyli jak chcesz wyświetlić tylko ćwiartkę tekstury to do boków twojego kwadrata mapujesz odpowiednio koordynaty tekstury (0, 0), (0, 0.5), (0.5, 0), (0.5, 0.5).

  1. // Top-left vertex (corner)
  2. glTexCoord2f(0.0f, 0.0f);
  3. glVertex3f(0, 0, 0);
  4.  
  5. // Bottom-left vertex (corner)
  6. glTexCoord2f(0.5f, 0.0f);
  7. glVertex3f(0, 100, 0);
  8.  
  9. // Bottom-right vertex (corner)
  10. glTexCoord2f(0.5f, 0.5f);
  11. glVertex3f(100,100, 0);
  12.  
  13. // Top-right vertex (corner)
  14. glTexCoord2f(0.0f, 0.5f);
  15. glVertex3f(100, 0, 0);
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.