-
Notifications
You must be signed in to change notification settings - Fork 0
/
shader.cpp
128 lines (96 loc) · 2.71 KB
/
shader.cpp
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
#include <cstdlib>
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>
using namespace std;
sf::RenderWindow window;
sf::VideoMode videoMode = sf::VideoMode(800, 600);
sf::String title = "Shader display";
sf::Shader shader;
sf::Clock globalClock;
bool running = true;
void handle_input();
void render();
int main(int argc, char* argv[])
{
window.create(videoMode, title);
globalClock.restart();
window.setVerticalSyncEnabled(true);
window.setActive(true);
window.resetGLStates();
glClearColor(0.2, 0.2, 0.2, 0.0);
if (!sf::Shader::isAvailable())
{
cout << "Shaders are not available." << endl;
exit(EXIT_FAILURE);
}
if (!shader.loadFromFile("./shaders/vert.glsl", "./shaders/frag.glsl"))
{
cout << "Failed to load shaders." << endl;
exit(EXIT_FAILURE);
}
sf::Vector2f res(videoMode.width, videoMode.height);
sf::Time gt = globalClock.getElapsedTime();
shader.setParameter("iResolution", res);
shader.setParameter("iGlobalTime", gt.asSeconds());
sf::Shader::bind(&shader);
while (running)
{
handle_input();
render();
}
sf::Shader::bind(NULL);
window.close();
exit(EXIT_SUCCESS);
}
void handle_input()
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Escape)
{
running = false;
}
break;
case sf::Event::Closed:
running = false;
break;
case sf::Event::Resized:
glViewport(0, 0, event.size.width, event.size.height);
break;
default:
break;
}
}
}
void render()
{
window.clear();
float ratio = videoMode.width / videoMode.height;
sf::Vector2f res(videoMode.width, videoMode.height);
sf::Time gt = globalClock.getElapsedTime();
shader.setParameter("iResolution", res);
shader.setParameter("iGlobalTime", gt.asSeconds());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio, ratio, -1.0f, 1.0f, 1.0f, -1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glBegin(GL_QUADS);
glNormal3f(0.0, 0.0, 1.0);
glVertex3f(-1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
glPopMatrix();
window.display();
}