forked from TinyTitan/SPH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdividers_gl.c
186 lines (155 loc) · 5.9 KB
/
dividers_gl.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
/*
The MIT License (MIT)
Copyright (c) 2014 Adam Simpson
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include <stdio.h>
#include <assert.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "dividers_gl.h"
#include "ogl_utils.h"
#ifdef GLFW
#include "glfw_utils.h"
#else
#include "egl_utils.h"
#endif
void init_dividers(dividers_t *state, int screen_width, int screen_height)
{
state->screen_width = screen_width;
state->screen_height = screen_height;
// Create circle buffers
create_dividers_buffers(state);
// Create and set dividers shaders
// Also links dividers program
create_dividers_shaders(state);
}
// Update coordinate of dividers and render
void render_dividers(dividers_t *state, float *node_edges, float *colors_by_rank, int num_nodes)
{
// 6 verticies per line, 5(x,y,r,g,b) elements per vertex, 2 lines per node
float *verticies = (float*)malloc(sizeof(float)*30*2*num_nodes);
// Create verticies
int i, offset;
float edge_x;
float half_width = state->screen_width*0.0000012;
float *color;
for(i=0; i<2*num_nodes; i++) {
edge_x = node_edges[i];
// Node start
if(i%2 == 0)
edge_x += half_width;
else // Node end
edge_x -= half_width;
// Set color same as particle color
color = &colors_by_rank[3*(i/2)];
// Left triangle
offset = i*30;
verticies[offset] = edge_x - half_width;
verticies[offset+1] = -1.0f;
verticies[offset+2] = color[0];
verticies[offset+3] = color[1];
verticies[offset+4] = color[2];
verticies[offset+5] = edge_x + half_width;
verticies[offset+6] = -1.0f;
verticies[offset+7] = color[0];
verticies[offset+8] = color[1];
verticies[offset+9] = color[2];
verticies[offset+10] = edge_x - half_width;
verticies[offset+11] = 1.0f;
verticies[offset+12] = color[0];
verticies[offset+13] = color[1];
verticies[offset+14] = color[2];
// Right triangle
verticies[offset+15] = edge_x + half_width;
verticies[offset+16] = -1.0f;
verticies[offset+17] = color[0];
verticies[offset+18] = color[1];
verticies[offset+19] = color[2];
verticies[offset+20] = edge_x + half_width;
verticies[offset+21] = 1.0f;
verticies[offset+22] = color[0];
verticies[offset+23] = color[1];
verticies[offset+24] = color[2];
verticies[offset+25] = edge_x - half_width;
verticies[offset+26] = 1.0f;
verticies[offset+27] = color[0];
verticies[offset+28] = color[1];
verticies[offset+29] = color[2];
}
// Set buffer
glBindBuffer(GL_ARRAY_BUFFER, state->vbo);
// Orphan current buffer
glBufferData(GL_ARRAY_BUFFER, 30*2*num_nodes*sizeof(GLfloat), NULL, GL_STREAM_DRAW);
// Fill buffer
glBufferData(GL_ARRAY_BUFFER, 30*2*num_nodes*sizeof(GLfloat), verticies, GL_STREAM_DRAW);
// Unbind buffer
draw_dividers(state, num_nodes);
free(verticies);
}
void create_dividers_buffers(dividers_t *state)
{
// VAO is REQUIRED for OpenGL 3+ when using VBO I believe
#ifndef RASPI
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
#endif
// Generate vertex buffer
glGenBuffers(1, &state->vbo);
}
void create_dividers_shaders(dividers_t *state)
{
// Compile vertex shader
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
#ifdef RASPI
compile_shader(vertexShader, "SPH/shaders/divider_es.vert");
#else
compile_shader(vertexShader, "shaders/divider.vert");
#endif
// Compile frag shader
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
#ifdef RASPI
compile_shader(fragmentShader, "SPH/shaders/divider_es.frag");
#else
compile_shader(fragmentShader, "shaders/divider.frag");
#endif
// Create shader program
state->program = glCreateProgram();
glAttachShader(state->program, vertexShader);
glAttachShader(state->program, fragmentShader);
// Link and use program
glLinkProgram(state->program);
show_program_log(state->program);
// Get position location
state->position_location = glGetAttribLocation(state->program, "position");
// Get color location
state->color_location = glGetAttribLocation(state->program, "color");
}
void draw_dividers(dividers_t *state, int num_nodes)
{
// Bind circle shader program
glUseProgram(state->program);
// Set buffer
glBindBuffer(GL_ARRAY_BUFFER, state->vbo);
glVertexAttribPointer(state->position_location, 2, GL_FLOAT, GL_FALSE, 5*sizeof(GL_FLOAT), 0);
glEnableVertexAttribArray(state->position_location);
glVertexAttribPointer(state->color_location, 3, GL_FLOAT, GL_FALSE, 5*sizeof(GL_FLOAT), (void*)(2*sizeof(GL_FLOAT)));
glEnableVertexAttribArray(state->color_location);
// Draw, two triangles per line
glDrawArrays(GL_TRIANGLES, 0, 6*2*num_nodes);
}