ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/libgender/material.C
Revision: 1.17
Committed: Fri Oct 29 16:05:21 2004 UTC (19 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.16: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.8 #include <cstdlib>
2     #include <cstring>
3    
4 root 1.9 #include <algorithm>
5    
6 root 1.13 #include "opengl.h"
7     #include "material.h"
8     #include "util.h"
9    
10 root 1.1 material::~material ()
11     {
12     }
13    
14 root 1.6 void
15 root 1.15 simple_material::enable ()
16 root 1.1 {
17 root 1.6 glMaterialfv (GL_FRONT, GL_DIFFUSE, (GLfloat *) & diffuse);
18     glMaterialfv (GL_FRONT, GL_SPECULAR, (GLfloat *) & specular);
19     glMaterialfv (GL_FRONT, GL_EMISSION, (GLfloat *) & emission);
20     glMaterialf (GL_FRONT, GL_SHININESS, shininess);
21 root 1.1 }
22    
23 root 1.6 void
24 root 1.15 simple_material::disable ()
25 root 1.1 {
26     }
27 root 1.6
28     GLuint
29     texture::load_texture (SDL_Surface * surface, GLfloat * tex2oord)
30 root 1.3 {
31 root 1.15 GLuint name;
32 root 1.3 int w, h;
33     SDL_Surface *image;
34     SDL_Rect area;
35     Uint32 saved_flags;
36     Uint8 saved_alpha;
37    
38     /* Use the surface width and height expanded to powers of 2 */
39     //w = power_of_two (surface->w);
40     //h = power_of_two (surface->h);
41     w = power_of_two (surface->w);
42     h = power_of_two (surface->h);
43 root 1.5 tex2oord[0] = 0.0f; /* Min X */
44     tex2oord[1] = 0.0f; /* Min Y */
45     tex2oord[2] = (GLfloat) surface->w / w; /* Max X */
46     tex2oord[3] = (GLfloat) surface->h / h; /* Max Y */
47 root 1.3
48     image = SDL_CreateRGBSurface (SDL_SWSURFACE, w, h, 32,
49     #if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
50     0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
51     #else
52     0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF
53     #endif
54     );
55 root 1.6
56 root 1.3 if (image == NULL)
57 root 1.6 return 0;
58 root 1.3
59     /* Save the alpha blending attributes */
60     saved_flags = surface->flags & (SDL_SRCALPHA | SDL_RLEACCELOK);
61     saved_alpha = surface->format->alpha;
62     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
63 root 1.6 SDL_SetAlpha (surface, 0, 0);
64 root 1.3
65     /* Copy the surface into the GL texture image */
66     area.x = 0;
67     area.y = 0;
68     area.w = surface->w;
69     area.h = surface->h;
70     SDL_BlitSurface (surface, &area, image, &area);
71    
72     /* Restore the alpha blending attributes */
73     if ((saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA)
74 root 1.6 SDL_SetAlpha (surface, saved_flags, saved_alpha);
75 root 1.3
76     /* Create an OpenGL texture for the image */
77 root 1.15 glGenTextures (1, &name);
78     glBindTexture (GL_TEXTURE_2D, name);
79 root 1.6 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
80     glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
81 root 1.15 glTexParameteri (GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);
82 root 1.3 glTexImage2D (GL_TEXTURE_2D,
83     0,
84     GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
85     SDL_FreeSurface (image); /* No longer needed */
86    
87 root 1.15 return name;
88     }
89    
90     test_material::test_material ()
91     : tex ("textures/osama.jpg"), texvar (tex.name)
92     {
93     using namespace shader;
94    
95     p.vsh->start ();
96    
97     temp_4f lightpos;
98     temp_4f wpos;
99    
100     //lightpos = shader::vec4 (0, 10, 0, 1);
101     vout.position = shader::gl.model_view_projection_matrix * vin.vertex;
102 root 1.17 vout.tex_coord[0] = vin.tex_coord[0];
103 root 1.15 //vout.tex_coord[1] = normalize (lightpos - wpos);
104     //vout.tex_coord[2] = normalize (wpos);
105     //vout.tex_coord[3] = normalize (xyz (gl.model_view_matrix_inverse_transpose) * vin.normal);
106     //vout.tex_coord[4] = normalize (xyz (gl.projection_matrix_inverse_transpose) - wpos);
107    
108     p.vsh->end ();
109     p.vsh->compile ();
110    
111     fragment_shader fsh;
112    
113     p.fsh->start ();
114    
115     xyz(fout.frag_color) = texture_2d (texvar, fin.tex_coord[0]);
116    
117     p.fsh->end ();
118     p.fsh->compile ();
119     p.link ();
120 root 1.3 }
121    
122 root 1.15 void test_material::enable ()
123     {
124     p.enable ();
125     texvar->enable ();
126     }
127 root 1.3
128 root 1.15 void test_material::disable ()
129 root 1.6 {
130 root 1.15 texvar->disable ();
131     p.disable ();
132 root 1.3 }
133 root 1.8