|
16 | 16 |
|
17 | 17 | USING_NS_GPUPIXEL
|
18 | 18 |
|
| 19 | +SourceImage::SourceImage() { |
| 20 | + _displayProgram = GLProgram::createByShaderString(kDefaultVertexShader, kDefaultFragmentShader); |
| 21 | + _positionAttribLocation = _displayProgram->getAttribLocation("position"); |
| 22 | + _texCoordAttribLocation = _displayProgram->getAttribLocation("inputTextureCoordinate"); |
| 23 | + _colorMapUniformLocation = _displayProgram->getUniformLocation("textureCoordinate"); |
| 24 | + GPUPixelContext::getInstance()->setActiveShaderProgram(_displayProgram); |
| 25 | + CHECK_GL(glEnableVertexAttribArray(_positionAttribLocation)); |
| 26 | + CHECK_GL(glEnableVertexAttribArray(_texCoordAttribLocation)); |
| 27 | + _backgroundColor = { 0.f, 1.f, 0.f, 0.f }; |
| 28 | +} |
| 29 | + |
| 30 | +SourceImage::~SourceImage() { |
| 31 | + delete _displayProgram; |
| 32 | + _displayProgram = nullptr; |
| 33 | +} |
| 34 | + |
19 | 35 | std::shared_ptr<SourceImage> SourceImage::create_from_memory(int width, int height, int channel_count, const unsigned char *pixels) {
|
20 | 36 | auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
|
21 | 37 | sourceImage->init(width, height, channel_count, pixels);
|
@@ -74,7 +90,38 @@ void SourceImage::init(int width, int height, int channel_count, int texid) {
|
74 | 90 | this->setFramebuffer(_framebuffer);
|
75 | 91 | }
|
76 | 92 |
|
77 |
| - CHECK_GL(glCopyImageSubData(_framebuffer->getTexture(), GL_TEXTURE_2D, 0, /*src_x*/0, /*src_y*/0, 0, |
78 |
| - texid, GL_TEXTURE_2D, 0, /*dst_x*/0, /*dst_y*/0, 0, |
79 |
| - width, height, 1)); |
| 93 | + GPUPixelContext::getInstance()->setActiveShaderProgram(_displayProgram); |
| 94 | + _framebuffer->active(); |
| 95 | + |
| 96 | + static const GLfloat noRotationTextureCoordinates[] = { |
| 97 | + 0.0f, 0.0f, |
| 98 | + 1.0f, 0.0f, |
| 99 | + 0.0f, 1.0f, |
| 100 | + 1.0f, 1.0f, |
| 101 | + }; |
| 102 | + |
| 103 | + float scaledWidth = 1.0; |
| 104 | + float scaledHeight = 1.0; |
| 105 | + |
| 106 | + const GLfloat _displayVertices[8] = { |
| 107 | + -scaledWidth, |
| 108 | + -scaledHeight, |
| 109 | + scaledWidth, |
| 110 | + -scaledHeight, |
| 111 | + -scaledWidth, |
| 112 | + scaledHeight, |
| 113 | + scaledWidth, |
| 114 | + scaledHeight |
| 115 | + }; |
| 116 | + |
| 117 | + CHECK_GL(glClearColor(_backgroundColor.r, _backgroundColor.g, _backgroundColor.b, _backgroundColor.a)); |
| 118 | + CHECK_GL(glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)); |
| 119 | + CHECK_GL(glActiveTexture(GL_TEXTURE0)); |
| 120 | + CHECK_GL(glBindTexture(GL_TEXTURE_2D, texid)); |
| 121 | + CHECK_GL(glUniform1i(_colorMapUniformLocation, 0)); |
| 122 | + CHECK_GL(glVertexAttribPointer(_positionAttribLocation, 2, GL_FLOAT, 0, 0, _displayVertices)); |
| 123 | + CHECK_GL(glVertexAttribPointer(_texCoordAttribLocation, 2, GL_FLOAT, 0, 0, noRotationTextureCoordinates)); |
| 124 | + CHECK_GL(glDrawArrays(GL_TRIANGLE_STRIP, 0, 4)); |
| 125 | + |
| 126 | + _framebuffer->inactive(); |
80 | 127 | }
|
0 commit comments