-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexample.vds.h
202 lines (151 loc) · 6.82 KB
/
example.vds.h
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
#pragma once
#include "example.h"
#include <glwnd/tex2d.h>
#include <glwnd/vbo.h>
// Example VDS with 2 attributes : position + color
class GLViewExampleVDS2A : public GLView
{
public:
GLViewExampleVDS2A() : GLView() {}
virtual ~GLViewExampleVDS2A() {}
virtual void initial()
{
// define vds format and draw
static GLfloat data[] =
{
-1, -1, -1, 0, 0, 0, -1, -1, +1, 0, 0, 1, -1, +1, +1, 0, 1, 1, -1, +1, -1, 0, 1, 0,
+1, -1, -1, 1, 0, 0, +1, -1, +1, 1, 0, 1, +1, +1, +1, 1, 1, 1, +1, +1, -1, 1, 1, 0,
-1, -1, -1, 0, 0, 0, -1, -1, +1, 0, 0, 1, +1, -1, +1, 1, 0, 1, +1, -1, -1, 1, 0, 0,
-1, +1, -1, 0, 1, 0, -1, +1, +1, 0, 1, 1, +1, +1, +1, 1, 1, 1, +1, +1, -1, 1, 1, 0,
-1, -1, -1, 0, 0, 0, -1, +1, -1, 0, 1, 0, +1, +1, -1, 1, 1, 0, +1, -1, -1, 1, 0, 0,
-1, -1, 1, 0, 0, 1, -1, +1, +1, 0, 1, 1, +1, +1, +1, 1, 1, 1, +1, -1, +1, 1, 0, 1,
};
GLuint offset = 0;
GLuint vds_size = 6 * sizeof(GLfloat); // this vds has 2 attributes (position 3f + color 3f = 6f)
m_vbo.setup_begin(&data, sizeof(data), vds_size);
{
offset += m_vbo.declare_position_format(offset, 3, GL_FLOAT, vds_size);
offset += m_vbo.declare_color_format(offset, 3, GL_FLOAT, vds_size);
}
m_vbo.setup_end();
}
virtual void on_display()
{
glMatrixMode(GL_PROJECTION_MATRIX);
gluPerspective(45, 1.F, 0.1, 100);
glMatrixMode(GL_MODELVIEW_MATRIX);
glTranslatef(0, 0, -5);
// rotate cube by Y-axis
static float rotate_angle = 0;
if (rotate_angle++ > 360.) rotate_angle = 0;
glRotatef(rotate_angle, 0, 1, 0);
// draw cube via vbo
m_vbo.render(GL_QUADS);
}
protected:
VBO m_vbo;
};
// Example VDS with fully 4 attributes : position + normal + color + texcoord
class GLViewExampleVDS4A : public GLView
{
public:
GLViewExampleVDS4A() : GLView() {}
virtual ~GLViewExampleVDS4A() {}
virtual void initial()
{
// enable lighting
// glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
// enable tracking material ambient and diffuse from surface color
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
// set-up the lighting colors (ambient, diffuse, specular)
GLfloat light_ambient[] = { .3f, .3f, .3f, 1.0f }; // ambient light
GLfloat light_diffuse[] = { .7f, .7f, .7f, 1.0f }; // diffuse light
GLfloat light_specular[] = { 1, 1, 1, 1 }; // specular light
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
// set-up the position of the light
float light_position[4] = { 0, 0, 1, 0 }; // directional light
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0); // MUST enable each light source after configuration
// initialize the 2d texture
m_tex2d.initialize_from_image_file("assets\\512x512.bmp");
// initialize vbo and define vds format
static GLfloat data[] =
{
// position array
+.5f, +.5f, +.5f, -.5f, +.5f, +.5f, -.5f, -.5f, +.5f, +.5f, -.5f, +.5f, // v0,v1,v2,v3 (front)
+.5f, +.5f, +.5f, +.5f, -.5f, +.5f, +.5f, -.5f, -.5f, +.5f, +.5f, -.5f, // v0,v3,v4,v5 (right)
+.5f, +.5f, +.5f, +.5f, +.5f, -.5f, -.5f, +.5f, -.5f, -.5f, +.5f, +.5f, // v0,v5,v6,v1 (top)
-.5f, +.5f, +.5f, -.5f, +.5f, -.5f, -.5f, -.5f, -.5f, -.5f, -.5f, +.5f, // v1,v6,v7,v2 (left)
-.5f, -.5f, -.5f, +.5f, -.5f, -.5f, +.5f, -.5f, +.5f, -.5f, -.5f, +.5f, // v7,v4,v3,v2 (bottom)
+.5f, -.5f, -.5f, -.5f, -.5f, -.5f, -.5f, +.5f, -.5f, +.5f, +.5f, -.5f, // v4,v7,v6,v5 (back)
// normal array
+0, +0, +1, +0, +0, +1, +0, +0, +1, +0, +0, +1, // v0,v1,v2,v3 (front)
+1, +0, +0, +1, +0, +0, +1, +0, +0, +1, +0, +0, // v0,v3,v4,v5 (right)
+0, +1, +0, +0, +1, +0, +0, +1, +0, +0, +1, +0, // v0,v5,v6,v1 (top)
-1, +0, +0, -1, +0, +0, -1, +0, +0, -1, +0, +0, // v1,v6,v7,v2 (left)
+0, -1, +0, +0, -1, +0, +0, -1, +0, +0, -1, +0, // v7,v4,v3,v2 (bottom)
+0, +0, -1, +0, +0, -1, +0, +0, -1, +0, +0, -1, // v4,v7,v6,v5 (back)
// color array
+1, +1, +1, +1, +1, +0, +1, +0, +0, +1, +0, +1, // v0,v1,v2,v3 (front)
+1, +1, +1, +1, +0, +1, +0, +0, +1, +0, +1, +1, // v0,v3,v4,v5 (right)
+1, +1, +1, +0, +1, +1, +0, +1, +0, +1, +1, +0, // v0,v5,v6,v1 (top)
+1, +1, +0, +0, +1, +0, +0, +0, +0, +1, +0, +0, // v1,v6,v7,v2 (left)
+0, +0, +0, +0, +0, +1, +1, +0, +1, +1, +0, +0, // v7,v4,v3,v2 (bottom)
+0, +0, +1, +0, +0, +0, +0, +1, +0, +0, +1, +1, // v4,v7,v6,v5 (back)
// texcoord array
+1, +0, +0, +0, +0, +1, +1, +1, // v0,v1,v2,v3 (front)
+0, +0, +0, +1, +1, +1, +1, +0, // v0,v3,v4,v5 (right)
+1, +1, +1, +0, +0, +0, +0, +1, // v0,v5,v6,v1 (top)
+1, +0, +0, +0, +0, +1, +1, +1, // v1,v6,v7,v2 (left)
+0, +1, +1, +1, +1, +0, +0, +0, // v7,v4,v3,v2 (bottom)
+0, +1, +1, +1, +1, +0, +0, +0, // v4,v7,v6,v5 (back)
};
GLuint vds_size = (3 + 3 + 3 + 2) * sizeof(GLfloat);
m_vbo.setup_begin(&data, sizeof(data), vds_size);
{
GLuint attr_offset, attr_size;
attr_offset = 0;
attr_size = m_vbo.declare_position_format(attr_offset, 3, GL_FLOAT);
attr_offset += attr_size * m_vbo.get_num_elements();
attr_size = m_vbo.declare_normal_format(attr_offset, 3, GL_FLOAT);
attr_offset += attr_size * m_vbo.get_num_elements();
m_vbo.declare_color_format(attr_offset, 3, GL_FLOAT, 0);
attr_offset += attr_size * m_vbo.get_num_elements();
attr_size = m_vbo.declare_texture_format(attr_offset, 2, GL_FLOAT);
}
m_vbo.setup_end();
}
virtual void on_display()
{
glMatrixMode(GL_PROJECTION_MATRIX);
gluPerspective(45, 1.F, 0.1, 100);
glMatrixMode(GL_MODELVIEW_MATRIX);
glTranslatef(0, 0, -2.5);
// rotate cube by Y-axis
static float rotate_angle = 0;
if (rotate_angle++ > 360.) rotate_angle = 0;
glRotatef(rotate_angle, 0, 1, 0);
// use the 2d for uv mapping on cube
m_tex2d.use();
// draw cube via vbo
m_vbo.render(GL_QUADS);
}
protected:
VBO m_vbo;
Tex2D m_tex2d;
//public:
// static void test(GLWindow& parent)
// {
// parent.enable_coordiates();
// parent.set_layout(GLLayout::_2x2(parent));
// parent.layout().replace_view(new GLViewExampleVDS4A, 0);
// parent.layout().replace_view(new GLViewExampleVDS4A, 1);
// parent.layout().replace_view(new GLViewExampleVDS4A, 2);
// parent.layout().replace_view(new GLViewExampleVDS4A, 3);
// }
};