-
Notifications
You must be signed in to change notification settings - Fork 79
/
sgl-test-glfw.c
216 lines (196 loc) · 7.04 KB
/
sgl-test-glfw.c
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//------------------------------------------------------------------------------
// sgl-test-glfw.c
// This doesn't have any sokol header code, it's a pure GL 1.2 program
// for testing whether sokol-gl behaves like a real GL.
//------------------------------------------------------------------------------
#define GLFW_INCLUDE_NONE
#include "GLFW/glfw3.h"
#include "flextgl12/flextGL.h"
#include <math.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif
#include "glu.h"
static void reset_state(void) {
glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
static void draw_triangle(void) {
reset_state();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(0.0f, 0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(-0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5f, -0.5f);
glEnd();
}
static float rad(float deg) {
return (deg * (float)M_PI) / 180.0f;
}
static void draw_quad(void) {
static float angle_deg = 0.0f;
float scale = 1.0f + sinf(rad(angle_deg)) * 0.5f;
angle_deg += 1.0f;
reset_state();
glRotatef(angle_deg, 0.0f, 0.0f, 1.0f);
glScalef(scale, scale, 1.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(-0.5f, -0.5f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(0.5f, -0.5f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.5f, 0.5f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(-0.5f, 0.5f);
glEnd();
}
// vertex specification for a cube with colored sides and texture coords
static void cube(void) {
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f( 1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f( 1.0f, -1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(-1.0f, -1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f); glVertex3f(-1.0, -1.0, 1.0);
glTexCoord2f( 1.0f, 1.0f); glVertex3f( 1.0, -1.0, 1.0);
glTexCoord2f( 1.0f, -1.0f); glVertex3f( 1.0, 1.0, 1.0);
glTexCoord2f(-1.0f, -1.0f); glVertex3f(-1.0, 1.0, 1.0);
glColor3f(0.0f, 0.0f, 1.0f);
glTexCoord2f(-1.0f, 1.0f); glVertex3f(-1.0, -1.0, 1.0);
glTexCoord2f( 1.0f, 1.0f); glVertex3f(-1.0, 1.0, 1.0);
glTexCoord2f( 1.0f, -1.0f); glVertex3f(-1.0, 1.0, -1.0);
glTexCoord2f(-1.0f, -1.0f); glVertex3f(-1.0, -1.0, -1.0);
glColor3f(1.0f, 0.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f); glVertex3f(1.0, -1.0, 1.0);
glTexCoord2f( 1.0f, 1.0f); glVertex3f(1.0, -1.0, -1.0);
glTexCoord2f( 1.0f, -1.0f); glVertex3f(1.0, 1.0, -1.0);
glTexCoord2f(-1.0f, -1.0f); glVertex3f(1.0, 1.0, 1.0);
glColor3f(0.0f, 0.5f, 1.0f);
glTexCoord2f(-1.0f, 1.0f); glVertex3f( 1.0, -1.0, -1.0);
glTexCoord2f( 1.0f, 1.0f); glVertex3f( 1.0, -1.0, 1.0);
glTexCoord2f( 1.0f, -1.0f); glVertex3f(-1.0, -1.0, 1.0);
glTexCoord2f(-1.0f, -1.0f); glVertex3f(-1.0, -1.0, -1.0);
glColor3f(1.0f, 0.0f, 0.5f);
glTexCoord2f(-1.0f, 1.0f); glVertex3f(-1.0, 1.0, -1.0);
glTexCoord2f( 1.0f, 1.0f); glVertex3f(-1.0, 1.0, 1.0);
glTexCoord2f( 1.0f, -1.0f); glVertex3f( 1.0, 1.0, 1.0);
glTexCoord2f(-1.0f, -1.0f); glVertex3f( 1.0, 1.0, -1.0);
glEnd();
}
static void draw_cubes(void) {
static float rot[2] = { 0.0f, 0.0f };
rot[0] += 1.0f;
rot[1] += 2.0f;
reset_state();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f, 1.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0f, 0.0f, -12.0f);
glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
cube();
glPushMatrix();
glTranslatef(0.0f, 0.0f, 3.0f);
glScalef(0.5f, 0.5f, 0.5f);
glRotatef(-2.0f * rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(-2.0f * rot[1], 0.0f, 1.0f, 0.0f);
cube();
glPushMatrix();
glTranslatef(0.0f, 0.0f, 3.0f);
glScalef(0.5f, 0.5f, 0.5f);
glRotatef(-3.0f * 2*rot[0], 1.0f, 0.0f, 0.0f);
glRotatef(3.0f * 2*rot[1], 0.0f, 0.0f, 1.0f);
cube();
glPopMatrix();
glPopMatrix();
}
static void draw_tex_cube(void) {
static float frame_count = 0.0f;
frame_count += 1.0f;
float a = (float) frame_count;
// texture matrix rotation and scale
float tex_rot = 0.5f * a;
const float tex_scale = 1.0f + sinf(rad(a)) * 0.5f;
// compute an orbiting eye-position for testing sgl_lookat()
float eye_x = sinf(rad(a)) * 6.0f;
float eye_z = cosf(rad(a)) * 6.0f;
float eye_y = sinf(rad(a)) * 3.0f;
reset_state();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glMatrixMode(GL_PROJECTION);
gluPerspective(45.0f, 1.0f, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
gluLookAt(eye_x, eye_y, eye_z, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glMatrixMode(GL_TEXTURE);
glRotatef(tex_rot, 0.0f, 0.0f, 1.0f);
glScalef(tex_scale, tex_scale, 1.0f);
cube();
}
int main() {
glfwInit();
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
GLFWwindow* w = glfwCreateWindow(512, 512, "SGL/GL Test GLFW", 0, 0);
glfwMakeContextCurrent(w);
glfwSwapInterval(1);
flextInit();
// a checkerboard texture
uint32_t pixels[8][8];
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
pixels[y][x] = ((y ^ x) & 1) ? 0xFFFFFFFF : 0xFF000000;
}
}
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 8, 8, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
while (!glfwWindowShouldClose(w)) {
int dw, dh;
glfwGetFramebufferSize(w, &dw, &dh);
const int ww = dh/2; // not a bug
const int hh = dh/2;
const int x0 = dw/2 - hh;
const int x1 = dw/2;
const int y0 = dh/2;
const int y1 = 0;
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glViewport(x0, y0, ww, hh);
draw_triangle();
glViewport(x1, y0, ww, hh);
draw_quad();
glViewport(x0, y1, ww, hh);
draw_cubes();
glViewport(x1, y1, ww, hh);
draw_tex_cube();
glViewport(0, 0, dw, dh);
glfwSwapBuffers(w);
glfwPollEvents();
}
glfwTerminate();
}