Advertisement
Guest User

GLES 1.1 minimal code

a guest
Aug 5th, 2011
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <stdlib.h>
  2.  
  3. #define WINDOW_W 200
  4. #define WINDOW_H 300
  5.  
  6. #ifdef __ANDROID__
  7.     #include <android/log.h>
  8.     #include <GLES/gl.h>
  9.     #define DIR_SEP "/"
  10.     #define DIR_CUR "/sdcard/"
  11.     #define glOrtho(a,b,c,d,e,f) glOrthof(a,b,c,d,e,f) ///FIXME
  12. #else
  13.     #include <stdio.h>
  14.     #include <GL/gl.h>
  15.     #include <GL/glu.h>
  16.     #define DIR_CUR ""
  17.     #define DIR_SEP "/"
  18. #endif
  19.  
  20. #define PATH(filename) DIR_CUR "data" DIR_SEP filename
  21. #define ISPWR2(n) !(n&(n-1))
  22.  
  23. #include "SDL.h"
  24. #include "SDL_image.h"
  25.  
  26.  
  27. static SDL_Window* window;
  28. static SDL_GLContext context;
  29. static GLuint texture;
  30. static SDL_Event event;
  31. static bool running = true;
  32. static int angle;
  33.  
  34. int startup();
  35. int loadTexture(const char* filepath);
  36. void draw();
  37. int shutdown();
  38.  
  39.  
  40. int main(int argc, char** argv)
  41. {
  42.     // Initialise SDL and OpenGL
  43.     if(!startup())
  44.         return EXIT_FAILURE;
  45.  
  46.     // Main loop
  47.     while(running)
  48.     {
  49.         // Draw the texture
  50.         draw();
  51.  
  52.         // Wait
  53.         SDL_Delay(50);
  54.  
  55.         // Spin image
  56.         angle = (angle+2) % 360;
  57.  
  58.         // Check for exit
  59.         while (SDL_PollEvent(&event))
  60.             if(event.type == SDL_QUIT)
  61.                 running = false;
  62.     }
  63.  
  64.     // Clean up
  65.     if(!shutdown())
  66.         return EXIT_FAILURE;
  67.  
  68.     // Success !
  69.     return EXIT_SUCCESS;
  70. }
  71.  
  72. int startup()
  73. {
  74.     // Initialize SDL
  75.     if(SDL_Init(SDL_INIT_VIDEO) < 0)
  76.         return 0;
  77.  
  78.     // Open the window where we will draw
  79.     window = SDL_CreateWindow("OpenGL Test",
  80.                    SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  81.                    WINDOW_W, WINDOW_H, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN);
  82.  
  83.     // Create the OpenGL context for the window we just opened
  84.     context = SDL_GL_CreateContext(window);
  85.     SDL_GL_MakeCurrent(window, context);
  86.  
  87.     // Configure SDL for OpenGL or GLES 1.1
  88.     SDL_GL_SetSwapInterval(1); // 1 = use vertical synchronisation (vsync)
  89.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
  90.     SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
  91.     SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  92.     SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
  93.  
  94.     // Configure OpenGL propre
  95.     glClearColor(0, 0, 255, 255);
  96.     glEnable(GL_TEXTURE_2D);
  97.     glViewport(0, 0, WINDOW_W, WINDOW_H);
  98.     glMatrixMode(GL_PROJECTION);
  99.     glOrtho(0, WINDOW_W, WINDOW_H, 0, -1, 1);
  100.     glMatrixMode(GL_MODELVIEW);
  101.     glLoadIdentity();
  102.  
  103.     // Load the texture to draw
  104.     if(!loadTexture(PATH("image.png")))
  105.         return 0;
  106.  
  107.     // Success
  108.     return 1;
  109. }
  110.  
  111. int loadTexture(const char* filepath)
  112. {
  113.     // Local variables for extracting properties about the image
  114.     GLuint n_colours = 0;
  115.     GLenum format = (GLenum) NULL;
  116.  
  117.     // Load the image using SDL_image
  118.     SDL_Surface* surface = IMG_Load(filepath);
  119.     if(!surface)
  120.         return 0;
  121.  
  122.     // Make sure the image length and width are powers of 2
  123.     if (!ISPWR2(surface->w) || !ISPWR2(surface->h))
  124.         return 0;
  125.  
  126.     //get number of channels in the SDL surface
  127.     n_colours = surface->format->BytesPerPixel;
  128.     switch(n_colours)
  129.     {
  130.         case 3: format = GL_RGB; break;
  131.         case 4: format = GL_RGBA; break;
  132.         default: return 0; break;
  133.     }
  134.  
  135.     // Request an OpenGL texture handle
  136.     glGenTextures(1, &texture);
  137.  
  138.     // Bind the texture object to the current block
  139.     glBindTexture(GL_TEXTURE_2D, texture);
  140.  
  141.     // Set the texture’s properties
  142.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  143.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  144.     glTexImage2D(GL_TEXTURE_2D, 0, n_colours, surface->w, surface->h, 0,
  145.                     format, GL_UNSIGNED_BYTE, surface->pixels);
  146.  
  147.     // The original bitmap is no longer needed
  148.     SDL_FreeSurface(surface);
  149.  
  150.     // Success!
  151.     return 1;
  152. }
  153.  
  154. void draw()
  155. {
  156.     // Clear the entire screen
  157.     glClear(GL_COLOR_BUFFER_BIT);
  158.  
  159.     // Set up position, rotation, colour
  160.     glTranslatef(WINDOW_W/2, WINDOW_H/2, 0.0);
  161.     glRotatef(angle, 0.0, 0.0, 1.0);
  162.  
  163.     // Bind the texture to which subsequent calls refer to
  164.     glBindTexture(GL_TEXTURE_2D, texture);
  165.  
  166.     // Start drawing texture
  167.     glEnableClientState(GL_VERTEX_ARRAY);
  168.     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  169.     GLfloat size[9], coords[9];
  170.     glVertexPointer(3, GL_FLOAT, 0, size);
  171.     glTexCoordPointer(2, GL_FLOAT, 0, coords);
  172.  
  173.     // Top-left triangle
  174.     size = {-16,-16,0, 16,-16,0, 16,16,0}; ///FIXME
  175.     coords = {0,0, 1,0, 1,1}; ///FIXME
  176.     glDrawArrays(GL_TRIANGLES, 0, 3);
  177.  
  178.     // Bottom-right triangle
  179.     size = {-16,-16,0, -16,16,0, 16,16,0}; ///FIXME
  180.     coords = {0,0, 0,1, 1,1}; ///FIXME
  181.     glDrawArrays(GL_TRIANGLES, 0, 3);
  182.  
  183.     // Stop drawing texture
  184.     glDisableClientState(GL_VERTEX_ARRAY);
  185.     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
  186.  
  187.     // Reset back to the origin
  188.     glLoadIdentity();
  189.  
  190.     // Flip the buffers to update the screen
  191.     SDL_GL_SwapWindow(window);
  192. }
  193.  
  194. int shutdown()
  195. {
  196.     // Free ressources
  197.     glDeleteTextures(1, &texture);
  198.  
  199.     // Destory application display and control structures
  200.     SDL_GL_MakeCurrent(NULL, NULL);
  201.     SDL_GL_DeleteContext(context);
  202.     SDL_DestroyWindow(window);
  203.  
  204.     // Shut down SDL
  205.     SDL_Quit();
  206.  
  207.     // Success!
  208.     return EXIT_SUCCESS;
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement