1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/* pyramid.c
*
* Kamil Kaminski
* kkaminsk.com
*
* Textured Pyramid,
* running on a homebrew framework
*
*
*/
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <sys/time.h>
#include "client.h"
#include "event.h"
#include "gldraw.h"
#include "glframe.h"
#include "gltools.h"
#include "math3d.h"
#include "platform.h"
#include "sdltools.h"
#include "window.h"
/* sane defaults, these are overwritable by config.lua */
int program_running = 1;
unsigned int xres_w = 640;
unsigned int yres_w = 480;
unsigned int bpp_w = 32;
unsigned int af_w = 8;
const char *window_caption = "Textured Pyramid";
const char *window_icon_path = "tux.png";
const unsigned int sdl_video_flags = SDL_OPENGL | SDL_RESIZABLE;
unsigned int maxfps_w = 60;
/* function prototypes */
static int client_init(struct client *);
static int client_destroy(struct client *);
static inline void process_events(void);
static void render(struct platform *);
static int client_init(struct client *p)
{
p->textures[0] = gltLoadTGATexture("stone.tga");
p->textures[1] = gltLoadTGATexture("grass.tga");
/* display list, precompile commands */
p->ground_list = glGenLists(3);
p->triangle_list = p->ground_list + 1;
p->figures_list = p->ground_list + 2;
/* a triangle with a texture */
glNewList(p->triangle_list, GL_COMPILE);
glBindTexture(GL_TEXTURE_2D, p->textures[0]);
/* glBindTexture(GL_TEXTURE_2D, p->textures[1]); */
glColor3f(1.0f, 1.0f, 1.0f);
glDrawTriangle();
glEndList();
/* a ground drawn using magneta lines */
glNewList(p->ground_list, GL_COMPILE);
glColor3ub(255, 0, 255);
glDrawGround();
glEndList();
/* figures */
glNewList(p->figures_list, GL_COMPILE);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glDrawFigures();
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glEndList();
return 0;
}
static int client_destroy(struct client *p)
{
glDeleteTextures(2, p->textures);
glDeleteLists(p->ground_list, 3);
return 0;
}
static inline void process_events(void)
{
sdl_process_events();
}
static void render(struct platform *p)
{
/* clear the window with current clearing color */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/* save the matrix state and do the rotations */
glPushMatrix();
/* apply camera transform, and draw the ground */
glframe_apply_camera_transform(&p->camera, 0);
glCallList(p->c->ground_list);
glPushMatrix();
/* move object back and do in place rotation */
glTranslatef(0.0f, 0.2f, -3.5f);
glRotatef(p->c->xrot, 1.0f, 0.0f, 0.0f);
glRotatef(p->c->yrot, 0.0f, 1.0f, 0.0f);
/* draw the pyramid */
glCallList(p->c->triangle_list);
glPopMatrix();
glPushMatrix();
/* draw a snowman */
glDisable(GL_TEXTURE_2D);
int i;
for (i = 0; i < 3; i++)
{
glTranslatef(0.0f, 0.0f, -6.0f);
glDrawSnowman();
}
glEnable(GL_TEXTURE_2D);
glPopMatrix();
glPushMatrix();
/* draw figures */
glTranslatef(0.0f, 4.0f, 0.0f);
glCallList(p->c->figures_list);
glPopMatrix();
/* restore the matrix state */
glPopMatrix();
/* buffer swap */
SDL_GL_SwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
struct platform p;
memset(&p, 0, sizeof(p));
/* setup client platform */
struct client c;
memset(&c, 0, sizeof(c));
p.c = &c;
p.client_init = &client_init;
p.client_destroy = &client_destroy;
/* initialize host platform */
platform_init(&p);
gltErrorCheck();
gltOpenGLInfo();
unsigned int startclock;
while (program_running)
{
startclock = SDL_GetTicks();
process_events();
render(&p);
sdlFrameControl(startclock);
}
platform_destroy(&p);
puts("bye!");
return 0;
}
|