-
Notifications
You must be signed in to change notification settings - Fork 0
/
sb3_2.cpp
119 lines (85 loc) · 2.61 KB
/
sb3_2.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
/*
* Really basic shader from OpenGL SuperBible, chapter 2
*/
#include "fw1/fw1.h"
#include "SB6_BasicApp.h"
#define COLOUR_RED 0
#define COLOUR_GREEN 1
#define COLOUR_BLUE 2
#define COLOUR_ALPHA 3
class SB6_Chapter3 : public SB6_BasicApp {
public:
SB6_Chapter3();
bool appMain();
void appInit();
private:
GLuint vertexarray;
GLfloat offset_x, offset_y, time;
bool increase_x, increase_y, increase_time;
void colour_for_time(GLfloat *colour);
};
SB6_Chapter3::SB6_Chapter3() {
display = SDLDisplay::basicDisplay("Chapter 3.2 - Coloured Triangle", 300, 400);
running = true;
offset_x = 0.0f;
increase_x = true;
offset_y = 0.0f;
increase_y = true;
time = 0.0f;
increase_time = true;
}
void SB6_Chapter3::appInit() {
loadVFProgram("sb3_2-vertex.sdr", "sb3-fragment.sdr");
glGenVertexArrays(1, &vertexarray);
glBindVertexArray(vertexarray);
}
bool SB6_Chapter3::appMain() {
GLenum glerror;
GLfloat offset[] = { offset_x, offset_y, 0.0f, 0.0f };
GLfloat colour[4];
glClearColor(1.0f - colour[0], 1.0f - colour[1], 1.0f - colour[2], 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
colour_for_time(colour);
glVertexAttrib4fv(1, offset);
glVertexAttrib4fv(2, colour);
if (glerror != GL_NO_ERROR) {
Logger::logprintf(Logger::LOG_ERROR, Logger::LOG_APPLICATION, "Error encountered while calling glVertexAttrib4fv: %s\n", gluErrorString(glerror));
}
glDrawArrays(GL_TRIANGLES, 0, 3);
glerror = glGetError();
if (glerror != GL_NO_ERROR) {
Logger::logprintf(Logger::LOG_ERROR, Logger::LOG_APPLICATION, "Error encountered while calling glDrawArrays: %s\n", gluErrorString(glerror));
}
display->swapBuffers();
display->mainLoop(*this);
SDL_Delay(10);
valueSwing(offset_x, 0.02f, increase_x, -0.8f, 0.8f);
valueSwing(offset_y, 0.015f, increase_y, -0.8f, 0.8f);
valueSwing(time, 1.0f, increase_time, 0.0f, 512.0f );
return running;
}
void SB6_Chapter3::colour_for_time(GLfloat *colour) {
if ( time >= 256 ) {
colour[COLOUR_RED] = (512 - time) / 256.0f;
} else {
colour[COLOUR_RED] = time / 256.0f;
};
if ( time >= 256 ) {
colour[COLOUR_GREEN] = (time - 256) / 256.0f;
} else {
colour[COLOUR_GREEN] = 0.0f;
};
if ( time >= 256 ) {
colour[COLOUR_BLUE] = 0.0f;
} else {
colour[COLOUR_BLUE] = (256 - time) / 256.0f;
};
colour[COLOUR_ALPHA] = 1.0f;
}
int main( int argc, char* args[] ) {
FrameworkOneApp *thisApp = new SB6_Chapter3();
thisApp->appInit();
while (thisApp->appMain()) { };
delete thisApp;
return 0;
}