summaryrefslogtreecommitdiff
path: root/source/modules/asura-core/graphics/canvas.cpp
blob: 0a170855e7fd2835d3f33905ba3138be76694a8a (plain)
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
#include "Canvas.h"

namespace AsuraEngine
{
	namespace Graphics
	{

		Canvas::Canvas()
			: mWidth(0)
			, mHeight(0)
			, mFBO(0)
		{
			// Fix: ôСʼʱframebufferԴ
			//glGenFramebuffers(1, &mFBO);
			//GLint current_fbo;
			//glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &current_fbo);
			//glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
			//glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexID, 0);
			//glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
		}

		void Canvas::SetSize(uint w, uint h)
		{
			if (mFBO == 0)
			{
				glGenFramebuffers(1, &mFBO);
				if (mFBO == 0)
					throw Exception("OpenGL glGenFramebuffers cannot generate frame buffer object.");
				// 
				if (mTexID == 0)
				{
					glGenTextures(1, &mTexID);
					if (mTexID == 0)
						throw Exception("OpenGL glGenTextures cannot generate texture.");
				}
				GLint current_fbo;
				glGetIntegerv(GL_DRAW_FRAMEBUFFER_BINDING, &current_fbo);
				glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
				glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTexID, 0);
				glBindFramebuffer(GL_FRAMEBUFFER, current_fbo);
			}
			GLint current_tex; 
			glGetIntegerv(GL_TEXTURE_BINDING_2D, &current_tex);
			glBindTexture(GL_TEXTURE_2D, mTexID);
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
			glBindTexture(GL_TEXTURE_2D, current_tex);
		}

	}
}