forked from AdamSimpson/PiBrot
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathcursor_gl.c
229 lines (183 loc) · 7.25 KB
/
cursor_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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
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 <stdlib.h>
#include "lodepng.h"
#include "cursor_gl.h"
#include "ogl_utils.h"
void create_cursor_program(cursor_t *state)
{
// Compile vertex shader
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
#ifdef RASPI
compile_shader(vertex_shader, "SPH/shaders/cursor_es.vert");
#else
compile_shader(vertex_shader, "shaders/cursor.vert");
#endif
// Compile fragment shader
GLuint frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
#ifdef RASPI
compile_shader(frag_shader, "SPH/shaders/cursor_es.frag");
#else
compile_shader(frag_shader, "shaders/cursor.frag");
#endif
// Create shader program
state->program = glCreateProgram();
glAttachShader(state->program, vertex_shader);
glAttachShader(state->program, frag_shader);
// Link program
glLinkProgram(state->program);
show_program_log(state->program);
// Get position attribute location
state->position_location = glGetAttribLocation(state->program, "position");
// Get tex_coord location
state->tex_coord_location = glGetAttribLocation(state->program, "tex_coord");
// Get tex uniform location
state->tex_location = glGetUniformLocation(state->program, "tex");
}
void create_cursor_buffers(cursor_t *state)
{
// Generate vertex buffer
glGenBuffers(1, &state->vbo);
// Generate element buffer
glGenBuffers(1, &state->ebo);
}
void set_cursor_position(cursor_t *state, float gl_x, float gl_y)
{
// Set cursor center in gl coordinates
state->center_x = gl_x;
state->center_y = gl_y;
// Vertices: Pos(x,y) Tex(x,y)
// For simplicity only single vbo is generated and offset used as needed
// cursor dimensions in gl screen coordinates
float cursor_width = 2.0*(state->cursor_width/(float)state->gl_state->screen_width);
float cursor_height = 2.0*(state->cursor_height/(float)state->gl_state->screen_height);
float lower_left_x = gl_x - cursor_width/2.0;
float lower_left_y = gl_y - cursor_height/2.0;
float lower_right_x = lower_left_x + cursor_width;
float lower_right_y = lower_left_y;
float upper_right_x = lower_right_x;
float upper_right_y = lower_right_y + cursor_height;
float upper_left_x = lower_left_x;
float upper_left_y = lower_left_y + cursor_height;
float vertices[] = {
// Full screen vertices
upper_left_x, upper_left_y, 0.0f, 0.0f, // Upper left
upper_right_x, upper_right_y, 1.0f, 0.0f, // Upper right
lower_right_x, lower_right_y, 1.0f, 1.0f, // Lower right
lower_left_x, lower_left_y, 0.0f, 1.0f // Lower left
};
// Set buffer
glBindBuffer(GL_ARRAY_BUFFER, state->vbo);
// Fill buffer
glBufferData(GL_ARRAY_BUFFER, 3*4*4*sizeof(GLfloat), vertices, GL_DYNAMIC_DRAW);
// Elements
GLubyte elements[] = {
2, 3, 0,
0, 1, 2
};
// Set buffer
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->ebo);
// Fill buffer
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 2*3*sizeof(GLubyte), elements, GL_DYNAMIC_DRAW);
}
void create_cursor_texture(cursor_t *state)
{
// Read in image PNG
unsigned error;
unsigned char* image;
unsigned width, height;
error = lodepng_decode32_file(&image, &width, &height, state->file_name);
if(error) printf("error %u: %s\n", error, lodepng_error_text(error));
printf("loaded image: %dx%d\n", width, height);
// Photoshop sets transparent pixels to (1,1,1,0) in PNG
// When averaged mipmaps are generated this will produce white border artifacts
// Setting any 0 transparency pixels to RGB = 0 will fix this bleeding
int i;
for(i=3; i<height*width*4; i+=4){
if(image[i] == 0) {
image[i-1] = 0;
image[i-2] = 0;
image[i-3] = 0;
}
}
// Generate texture
glGenTextures(1, &state->tex_uniform);
// Set texture unit 0 and bind texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, state->tex_uniform);
// Buffer texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
// Set texture parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Create Mipmap
glGenerateMipmap(GL_TEXTURE_2D);
// Release image host memory
free(image);
// Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
}
void init_cursor(cursor_t *state, gl_t *gl_state, char *file_name, int cursor_width, int cursor_height)
{
// Set GL state
state->gl_state = gl_state;
// Set filename
strcpy(state->file_name, file_name);
// Image display dimensions in pixels
state->cursor_width = cursor_width;
state->cursor_height = cursor_height;
// Create program
create_cursor_program(state);
// Generate buffers
create_cursor_buffers(state);
// Create texture from image
// Must be called before create_image_verticies
// so image dimensions known
create_cursor_texture(state);
// Set verticies
set_cursor_position(state, 0.0, 0.0);
}
void draw_cursor(cursor_t *state)
{
// Setup program
glUseProgram(state->program);
// Setup buffers
size_t vert_size = 4*sizeof(GL_FLOAT);
glBindBuffer(GL_ARRAY_BUFFER, state->vbo);
glVertexAttribPointer(state->position_location, 2, GL_FLOAT, GL_FALSE, vert_size, 0);
glEnableVertexAttribArray(state->position_location);
glVertexAttribPointer(state->tex_coord_location, 2, GL_FLOAT, GL_FALSE, vert_size,(void*)(2*sizeof(GL_FLOAT)));
glEnableVertexAttribArray(state->tex_coord_location);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, state->ebo);
// Enable Blend
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Setup texture
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, state->tex_uniform);
glUniform1i(state->tex_location, 0);
// Draw image
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
// Unbind texture
glBindTexture(GL_TEXTURE_2D, 0);
}