| 1 |
void OpenGLSurface::allocate() { |
| 2 |
glGenVertexArrays(1, &vao); |
| 3 |
glBindVertexArray(vao); |
| 4 |
glGenBuffers(3, &vbo[0]); |
| 5 |
} |
| 6 |
|
| 7 |
void OpenGLSurface::size(unsigned w, unsigned h) { |
| 8 |
if(width == w && height == h) return; |
| 9 |
width = w, height = h; |
| 10 |
w = glrSize(w), h = glrSize(h); |
| 11 |
|
| 12 |
if(texture) { glDeleteTextures(1, &texture); texture = 0; } |
| 13 |
if(buffer) { delete[] buffer; buffer = nullptr; } |
| 14 |
|
| 15 |
buffer = new uint32_t[w * h](); |
| 16 |
glGenTextures(1, &texture); |
| 17 |
glBindTexture(GL_TEXTURE_2D, texture); |
| 18 |
glTexImage2D(GL_TEXTURE_2D, 0, format, w, h, 0, getFormat(), getType(), buffer); |
| 19 |
|
| 20 |
if(framebuffer) { |
| 21 |
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, framebuffer); |
| 22 |
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0); |
| 23 |
delete[] buffer; |
| 24 |
buffer = nullptr; |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
void OpenGLSurface::release() { |
| 29 |
if(vbo[0]) { glDeleteBuffers(3, &vbo[0]); for(auto &o : vbo) o = 0; } |
| 30 |
if(vao) { glDeleteVertexArrays(1, &vao); vao = 0; } |
| 31 |
if(vertex) { glDetachShader(program, vertex); glDeleteShader(vertex); vertex = 0; } |
| 32 |
if(geometry) { glDetachShader(program, geometry); glDeleteShader(geometry); geometry = 0; } |
| 33 |
if(fragment) { glDetachShader(program, fragment); glDeleteShader(fragment); fragment = 0; } |
| 34 |
if(texture) { glDeleteTextures(1, &texture); texture = 0; } |
| 35 |
if(framebuffer) { glDeleteFramebuffers(1, &framebuffer); framebuffer = 0; } |
| 36 |
if(program) { glDeleteProgram(program); program = 0; } |
| 37 |
width = 0, height = 0; |
| 38 |
} |
| 39 |
|
| 40 |
void OpenGLSurface::render(unsigned sourceWidth, unsigned sourceHeight, unsigned targetWidth, unsigned targetHeight) { |
| 41 |
glViewport(0, 0, targetWidth, targetHeight); |
| 42 |
|
| 43 |
float w = (float)sourceWidth / (float)glrSize(sourceWidth); |
| 44 |
float h = (float)sourceHeight / (float)glrSize(sourceHeight); |
| 45 |
float u = (float)targetWidth, v = (float)targetHeight; |
| 46 |
GLint location; |
| 47 |
|
| 48 |
GLfloat modelView[] = { |
| 49 |
1, 0, 0, 0, |
| 50 |
0, 1, 0, 0, |
| 51 |
0, 0, 1, 0, |
| 52 |
0, 0, 0, 1, |
| 53 |
}; |
| 54 |
|
| 55 |
GLfloat projection[] = { |
| 56 |
2.0f/u, 0.0f, 0.0f, 0.0f, |
| 57 |
0.0f, 2.0f/v, 0.0f, 0.0f, |
| 58 |
0.0f, 0.0f, -1.0f, 0.0f, |
| 59 |
-1.0f, -1.0f, 0.0f, 1.0f, |
| 60 |
}; |
| 61 |
|
| 62 |
GLfloat modelViewProjection[4 * 4]; |
| 63 |
Matrix::Multiply(modelViewProjection, modelView, 4, 4, projection, 4, 4); |
| 64 |
|
| 65 |
GLfloat vertices[] = { |
| 66 |
0, 0, 0, 1, |
| 67 |
u, 0, 0, 1, |
| 68 |
0, v, 0, 1, |
| 69 |
u, v, 0, 1, |
| 70 |
}; |
| 71 |
|
| 72 |
GLfloat positions[4 * 4]; |
| 73 |
for(unsigned n = 0; n < 16; n += 4) { |
| 74 |
Matrix::Multiply(&positions[n], &vertices[n], 1, 4, modelViewProjection, 4, 4); |
| 75 |
} |
| 76 |
|
| 77 |
GLfloat texCoords[] = { |
| 78 |
0, 0, |
| 79 |
w, 0, |
| 80 |
0, h, |
| 81 |
w, h, |
| 82 |
}; |
| 83 |
|
| 84 |
glrUniformMatrix4fv("modelView", modelView); |
| 85 |
glrUniformMatrix4fv("projection", projection); |
| 86 |
glrUniformMatrix4fv("modelViewProjection", modelViewProjection); |
| 87 |
|
| 88 |
glBindVertexArray(vao); |
| 89 |
|
| 90 |
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); |
| 91 |
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); |
| 92 |
GLuint locationVertex = glGetAttribLocation(program, "vertex"); |
| 93 |
glEnableVertexAttribArray(locationVertex); |
| 94 |
glVertexAttribPointer(locationVertex, 4, GL_FLOAT, GL_FALSE, 0, 0); |
| 95 |
|
| 96 |
glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); |
| 97 |
glBufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), positions, GL_STATIC_DRAW); |
| 98 |
GLuint locationPosition = glGetAttribLocation(program, "position"); |
| 99 |
glEnableVertexAttribArray(locationPosition); |
| 100 |
glVertexAttribPointer(locationPosition, 4, GL_FLOAT, GL_FALSE, 0, 0); |
| 101 |
|
| 102 |
glBindBuffer(GL_ARRAY_BUFFER, vbo[2]); |
| 103 |
glBufferData(GL_ARRAY_BUFFER, 8 * sizeof(GLfloat), texCoords, GL_STATIC_DRAW); |
| 104 |
GLuint locationTexCoord = glGetAttribLocation(program, "texCoord"); |
| 105 |
glEnableVertexAttribArray(locationTexCoord); |
| 106 |
glVertexAttribPointer(locationTexCoord, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 107 |
|
| 108 |
glBindFragDataLocation(program, 0, "fragColor"); |
| 109 |
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 110 |
|
| 111 |
glDisableVertexAttribArray(locationVertex); |
| 112 |
glDisableVertexAttribArray(locationPosition); |
| 113 |
glDisableVertexAttribArray(locationTexCoord); |
| 114 |
} |