1 | //////////////////////////////////////////////////////////// |
2 | // Headers |
3 | //////////////////////////////////////////////////////////// |
4 | #include <SFML/Window.hpp> |
5 | |
6 | #define GLAD_GL_IMPLEMENTATION |
7 | #include "gl.h" |
8 | |
9 | #ifdef SFML_SYSTEM_IOS |
10 | #include <SFML/Main.hpp> |
11 | #endif |
12 | |
13 | //////////////////////////////////////////////////////////// |
14 | /// Entry point of application |
15 | /// |
16 | /// \return Application exit code |
17 | /// |
18 | //////////////////////////////////////////////////////////// |
19 | int main() |
20 | { |
21 | // Request a 24-bits depth buffer when creating the window |
22 | sf::ContextSettings contextSettings; |
23 | contextSettings.depthBits = 24; |
24 | |
25 | // Create the main window |
26 | sf::Window window(sf::VideoMode(640, 480), "SFML window with OpenGL" , sf::Style::Default, contextSettings); |
27 | |
28 | // Make it the active window for OpenGL calls |
29 | window.setActive(); |
30 | |
31 | // Load OpenGL or OpenGL ES entry points using glad |
32 | #ifdef SFML_OPENGL_ES |
33 | gladLoadGLES1(reinterpret_cast<GLADloadfunc>(sf::Context::getFunction)); |
34 | #else |
35 | gladLoadGL(reinterpret_cast<GLADloadfunc>(sf::Context::getFunction)); |
36 | #endif |
37 | |
38 | // Set the color and depth clear values |
39 | #ifdef SFML_OPENGL_ES |
40 | glClearDepthf(1.f); |
41 | #else |
42 | glClearDepth(1.f); |
43 | #endif |
44 | glClearColor(0.f, 0.f, 0.f, 1.f); |
45 | |
46 | // Enable Z-buffer read and write |
47 | glEnable(GL_DEPTH_TEST); |
48 | glDepthMask(GL_TRUE); |
49 | |
50 | // Disable lighting and texturing |
51 | glDisable(GL_LIGHTING); |
52 | glDisable(GL_TEXTURE_2D); |
53 | |
54 | // Configure the viewport (the same size as the window) |
55 | glViewport(0, 0, window.getSize().x, window.getSize().y); |
56 | |
57 | // Setup a perspective projection |
58 | glMatrixMode(GL_PROJECTION); |
59 | glLoadIdentity(); |
60 | GLfloat ratio = static_cast<float>(window.getSize().x) / window.getSize().y; |
61 | #ifdef SFML_OPENGL_ES |
62 | glFrustumf(-ratio, ratio, -1.f, 1.f, 1.f, 500.f); |
63 | #else |
64 | glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f); |
65 | #endif |
66 | |
67 | // Define a 3D cube (6 faces made of 2 triangles composed by 3 vertices) |
68 | GLfloat cube[] = |
69 | { |
70 | // positions // colors (r, g, b, a) |
71 | -50, -50, -50, 0, 0, 1, 1, |
72 | -50, 50, -50, 0, 0, 1, 1, |
73 | -50, -50, 50, 0, 0, 1, 1, |
74 | -50, -50, 50, 0, 0, 1, 1, |
75 | -50, 50, -50, 0, 0, 1, 1, |
76 | -50, 50, 50, 0, 0, 1, 1, |
77 | |
78 | 50, -50, -50, 0, 1, 0, 1, |
79 | 50, 50, -50, 0, 1, 0, 1, |
80 | 50, -50, 50, 0, 1, 0, 1, |
81 | 50, -50, 50, 0, 1, 0, 1, |
82 | 50, 50, -50, 0, 1, 0, 1, |
83 | 50, 50, 50, 0, 1, 0, 1, |
84 | |
85 | -50, -50, -50, 1, 0, 0, 1, |
86 | 50, -50, -50, 1, 0, 0, 1, |
87 | -50, -50, 50, 1, 0, 0, 1, |
88 | -50, -50, 50, 1, 0, 0, 1, |
89 | 50, -50, -50, 1, 0, 0, 1, |
90 | 50, -50, 50, 1, 0, 0, 1, |
91 | |
92 | -50, 50, -50, 0, 1, 1, 1, |
93 | 50, 50, -50, 0, 1, 1, 1, |
94 | -50, 50, 50, 0, 1, 1, 1, |
95 | -50, 50, 50, 0, 1, 1, 1, |
96 | 50, 50, -50, 0, 1, 1, 1, |
97 | 50, 50, 50, 0, 1, 1, 1, |
98 | |
99 | -50, -50, -50, 1, 0, 1, 1, |
100 | 50, -50, -50, 1, 0, 1, 1, |
101 | -50, 50, -50, 1, 0, 1, 1, |
102 | -50, 50, -50, 1, 0, 1, 1, |
103 | 50, -50, -50, 1, 0, 1, 1, |
104 | 50, 50, -50, 1, 0, 1, 1, |
105 | |
106 | -50, -50, 50, 1, 1, 0, 1, |
107 | 50, -50, 50, 1, 1, 0, 1, |
108 | -50, 50, 50, 1, 1, 0, 1, |
109 | -50, 50, 50, 1, 1, 0, 1, |
110 | 50, -50, 50, 1, 1, 0, 1, |
111 | 50, 50, 50, 1, 1, 0, 1, |
112 | }; |
113 | |
114 | // Enable position and color vertex components |
115 | glEnableClientState(GL_VERTEX_ARRAY); |
116 | glEnableClientState(GL_COLOR_ARRAY); |
117 | glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), cube); |
118 | glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), cube + 3); |
119 | |
120 | // Disable normal and texture coordinates vertex components |
121 | glDisableClientState(GL_NORMAL_ARRAY); |
122 | glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
123 | |
124 | // Create a clock for measuring the time elapsed |
125 | sf::Clock clock; |
126 | |
127 | // Start the game loop |
128 | while (window.isOpen()) |
129 | { |
130 | // Process events |
131 | sf::Event event; |
132 | while (window.pollEvent(event)) |
133 | { |
134 | // Close window: exit |
135 | if (event.type == sf::Event::Closed) |
136 | window.close(); |
137 | |
138 | // Escape key: exit |
139 | if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)) |
140 | window.close(); |
141 | |
142 | // Resize event: adjust the viewport |
143 | if (event.type == sf::Event::Resized) |
144 | { |
145 | glViewport(0, 0, event.size.width, event.size.height); |
146 | glMatrixMode(GL_PROJECTION); |
147 | glLoadIdentity(); |
148 | GLfloat ratio = static_cast<float>(event.size.width) / event.size.height; |
149 | #ifdef SFML_OPENGL_ES |
150 | glFrustumf(-ratio, ratio, -1.f, 1.f, 1.f, 500.f); |
151 | #else |
152 | glFrustum(-ratio, ratio, -1.f, 1.f, 1.f, 500.f); |
153 | #endif |
154 | } |
155 | } |
156 | |
157 | // Clear the color and depth buffers |
158 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
159 | |
160 | // Apply some transformations to rotate the cube |
161 | glMatrixMode(GL_MODELVIEW); |
162 | glLoadIdentity(); |
163 | glTranslatef(0.f, 0.f, -200.f); |
164 | glRotatef(clock.getElapsedTime().asSeconds() * 50, 1.f, 0.f, 0.f); |
165 | glRotatef(clock.getElapsedTime().asSeconds() * 30, 0.f, 1.f, 0.f); |
166 | glRotatef(clock.getElapsedTime().asSeconds() * 90, 0.f, 0.f, 1.f); |
167 | |
168 | // Draw the cube |
169 | glDrawArrays(GL_TRIANGLES, 0, 36); |
170 | |
171 | // Finally, display the rendered frame on screen |
172 | window.display(); |
173 | } |
174 | |
175 | return EXIT_SUCCESS; |
176 | } |
177 | |