annotate Renderer/test_render/texture.cpp @ 503:516d5c91d5b4 draft double-linked-task-list

comment
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 11 Oct 2009 19:35:33 +0900
parents 15bfacccde99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
283
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
1 #include <iostream>
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
2 #include <SDL.h>
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
3 #include <SDL_opengl.h>
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
4 #include <SDL_image.h>
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
5 #include <libxml/parser.h>
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
6 using namespace std;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
7
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
8 int power_of_two(int input)
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
9 {
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
10 int value = 1;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
11
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
12 while ( value < input )
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
13 {
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
14 value <<= 1;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
15 }
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
16 return value;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
17 }
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
18
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
19 GLuint SDL_GL_LoadTexture(SDL_Surface *surface, GLfloat *texcoord)
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
20 {
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
21 GLuint texture;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
22 int w, h;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
23 SDL_Surface *image;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
24 SDL_Rect area;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
25 Uint32 saved_flags;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
26 Uint8 saved_alpha;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
27
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
28 /* Use the surface width and height expanded to powers of 2 */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
29 w = power_of_two(surface->w);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
30 h = power_of_two(surface->h);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
31 texcoord[0] = 0.0f; /* Min X */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
32 texcoord[1] = 0.0f; /* Min Y */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
33 texcoord[2] = (GLfloat)surface->w / w; /* Max X */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
34 texcoord[3] = (GLfloat)surface->h / h; /* Max Y */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
35
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
36 image = SDL_CreateRGBSurface(
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
37 SDL_SWSURFACE,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
38 w, h,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
39 32,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
40 #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
41 0x000000FF,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
42 0x0000FF00,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
43 0x00FF0000,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
44 0xFF000000
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
45 #else
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
46 0xFF000000,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
47 0x00FF0000,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
48 0x0000FF00,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
49 0x000000FF
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
50 #endif
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
51 );
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
52 if ( image == NULL )
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
53 {
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
54 return 0;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
55 }
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
56
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
57 /* Save the alpha blending attributes */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
58 saved_flags = surface->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
59 saved_alpha = surface->format->alpha;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
60 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
61 {
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
62 SDL_SetAlpha(surface, 0, 0);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
63 }
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
64 /* Copy the surface into the GL texture image */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
65 area.x = 0;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
66 area.y = 0;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
67 area.w = surface->w;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
68 area.h = surface->h;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
69 SDL_BlitSurface(surface, &area, image, &area);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
70
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
71 /* Restore the alpha blending attributes */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
72 if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
73 {
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
74 SDL_SetAlpha(surface, saved_flags, saved_alpha);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
75 }
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
76
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
77 /* Create an OpenGL texture for the image */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
78 glGenTextures(1, &texture);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
79 glBindTexture(GL_TEXTURE_2D, texture);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
80 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
81 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
82 glTexImage2D(GL_TEXTURE_2D,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
83 0,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
84 GL_RGBA,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
85 w, h,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
86 0,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
87 GL_RGBA,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
88 GL_UNSIGNED_BYTE,
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
89 image->pixels);
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
90 SDL_FreeSurface(image); /* No longer needed */
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
91
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
92 return texture;
15bfacccde99 fix test_render
e065746@localhost.localdomain
parents:
diff changeset
93 }