File: GLRenderToTexture.cpp

package info (click to toggle)
bullet 2.83.7%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 48,772 kB
  • sloc: cpp: 355,312; lisp: 12,087; ansic: 11,969; python: 644; makefile: 116; xml: 27
file content (137 lines) | stat: -rw-r--r-- 2,849 bytes parent folder | download
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
#ifndef NO_OPENGL3

///See http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-14-render-to-texture/


#include "GLRenderToTexture.h"
#include "Bullet3Common/b3Scalar.h" // for b3Assert
#include <string.h>
#include <stdio.h>

bool gIntelLinuxglDrawBufferWorkaround=false;

GLRenderToTexture::GLRenderToTexture()
:m_framebufferName(0)
{
#if !defined(_WIN32) && !defined(__APPLE__)
 const GLubyte* ven = glGetString(GL_VENDOR);
	printf("ven = %s\n",ven);

 if (strncmp((const char*)ven,"Intel",5)==0)
       {
               printf("Workaround for some crash in the Intel OpenGL driver on Linux/Ubuntu\n");
               gIntelLinuxglDrawBufferWorkaround=true;
       }
#endif//!defined(_WIN32) && !defined(__APPLE__)

}

void GLRenderToTexture::init(int width, int height, GLuint textureId, int renderTextureType)
{
	m_renderTextureType = renderTextureType;

	glGenFramebuffers(1, &m_framebufferName);
	glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferName);



	// The depth buffer
//	glGenRenderbuffers(1, &m_depthrenderbuffer);

//	glBindRenderbuffer(GL_RENDERBUFFER, m_depthrenderbuffer);
//	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, width, height);
//	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, m_depthrenderbuffer);

		switch (m_renderTextureType)
	{
	case RENDERTEXTURE_COLOR:
		{
			glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, textureId, 0);
			break;
		}
		case RENDERTEXTURE_DEPTH:
		{
			glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, textureId, 0);
			break;
		}
		default:
			{
			b3Assert(0);
			}
	};


	glBindFramebuffer( GL_FRAMEBUFFER, 0 );


}

bool GLRenderToTexture::enable()
{
	bool status = false;

	glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferName);


	switch (m_renderTextureType)
	{
	case RENDERTEXTURE_COLOR:
		{
			// Set the list of draw buffers.
			GLenum drawBuffers[2] = {GL_COLOR_ATTACHMENT0,0};
			glDrawBuffers(1, drawBuffers);
			break;
		}
		case RENDERTEXTURE_DEPTH:
		{
			//Intel OpenGL driver crashes when using GL_NONE for glDrawBuffer on Linux, so use a workaround
			if (gIntelLinuxglDrawBufferWorkaround)
			{
				GLenum drawBuffers[2] = { GL_COLOR_ATTACHMENT0,0};
                glDrawBuffers(1, drawBuffers);
			} else
			{
				glDrawBuffer(GL_NONE);
			}
			break;
		}
		default:
			{
			b3Assert(0);
			}
	};


	// Always check that our framebuffer is ok
	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
	{
		status = true;
	}


	return status;

}

void GLRenderToTexture::disable()
{
	glBindFramebuffer( GL_FRAMEBUFFER, 0 );
}

GLRenderToTexture::~GLRenderToTexture()
{
	glBindFramebuffer( GL_FRAMEBUFFER, 0 );

	if (m_depthrenderbuffer)
	{
		glDeleteRenderbuffers(1,&m_depthrenderbuffer);
	}


	if( m_framebufferName)
	{
		glDeleteFramebuffers(1, &m_framebufferName);
	}
}
#endif