Example #1
0
void FBO::texture3D(const unsigned int attachment, const unsigned int textureID, const int level, const int layer){
	attachments.push_back(attachment);

	glFramebufferTexture3D(target,
						   attachment,
						   GL_TEXTURE_3D,
						   textureID,
						   level,
						   layer);

	loaded = true;
}
Example #2
0
File: tr_fbo.c Project: otty/cake3
/*
=================
R_AttachFBOTexture3D
=================
*/
void R_AttachFBOTexture3D(int texId, int index, int zOffset)
{
#if defined(USE_D3D10)
	// TODO
#else
	if(index < 0 || index >= glConfig2.maxColorAttachments)
	{
		ri.Printf(PRINT_WARNING, "R_AttachFBOTexture3D: invalid attachment index %i\n", index);
		return;
	}

	glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + index, GL_TEXTURE_3D, texId, 0, zOffset);
#endif
}
Example #3
0
	void GLTextureBuffer::bindToFramebuffer(GLenum attachment, UINT32 zoffset, bool allLayers)
	{
		if(mTarget == GL_TEXTURE_1D || mTarget == GL_TEXTURE_2D)
			allLayers = true;

		if(allLayers)
		{
			switch (mTarget)
			{
			case GL_TEXTURE_1D:
				glFramebufferTexture1D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, mLevel);
				BS_CHECK_GL_ERROR();
				break;
			case GL_TEXTURE_2D:
				glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, mLevel);
				BS_CHECK_GL_ERROR();
				break;
			case GL_TEXTURE_2D_MULTISAMPLE:
				glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, 0);
				BS_CHECK_GL_ERROR();
				break;
			case GL_TEXTURE_CUBE_MAP:
			case GL_TEXTURE_3D:
			default: // Texture arrays (binding all layers)
				glFramebufferTexture(GL_FRAMEBUFFER, attachment, mTextureID, mLevel);
				BS_CHECK_GL_ERROR();
				break;
			}
		}
		else
		{
			switch (mTarget)
			{
			case GL_TEXTURE_3D:
				glFramebufferTexture3D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, mLevel, zoffset);
				BS_CHECK_GL_ERROR();
				break;
			case GL_TEXTURE_CUBE_MAP:
				glFramebufferTexture2D(GL_FRAMEBUFFER, attachment, mFaceTarget, mTextureID, mLevel);
				BS_CHECK_GL_ERROR();
				break;
			default: // Texture arrays
				glFramebufferTextureLayer(GL_FRAMEBUFFER, attachment, mTextureID, mLevel, mFace);
				BS_CHECK_GL_ERROR();
				break;
			}
		}
	}
Example #4
0
/// Bind a texture to the "attachment" point of this FBO (GL_COLOR_ATTACHMENT0_EXT, textarget, texID)
void FrameBufferObject::attachTexture( GLenum attachment, GLenum texType, GLuint texId,int mipLevel, int zSlice)
{
	if (texType == GL_TEXTURE_1D) {
		glFramebufferTexture1D( GL_FRAMEBUFFER, attachment,
								   GL_TEXTURE_1D, texId, mipLevel );
	}
	else if (texType == GL_TEXTURE_3D) {
		glFramebufferTexture3D( GL_FRAMEBUFFER, attachment,
								   GL_TEXTURE_3D, texId, mipLevel, zSlice );
	}
	else {
		// Default is GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE, or cube faces
		glFramebufferTexture2D( GL_FRAMEBUFFER, attachment,
								   texType, texId, mipLevel );
	}
}
Example #5
0
Fbo::Fbo(int width, int height, int int_method): width(width), height(height)
{
    glGenFramebuffers(1, &fb);
    glGenRenderbuffers(1, &rb);
    glBindFramebuffer(GL_FRAMEBUFFER, fb);
	if(int_method == 3) {
		glEnable(GL_TEXTURE_3D);

		glGenTextures(1, &texid);
		glBindTexture(GL_TEXTURE_3D, texid);
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);

		glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texid, 0, 0);
	}
	else {
		glGenTextures(1, &texid);
		glBindTexture(GL_TEXTURE_2D, texid);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		if(false)//int_method == 0) 
		{
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		}
		else 
		{
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		}
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, NULL);

		glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texid, 0);
	}
    // Renderbuffer
    // initialize depth renderbuffer
    glBindRenderbuffer(GL_RENDERBUFFER, rb);
    //util::CHECK_FRAMEBUFFER_STATUS();

    //printf("Framebuffer object %d\n", fb);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);

}
Example #6
0
void FramebufferObject::attachTexture(Texture* texture, GLenum attachment, int mipLevel, int zSlice) {
	switch(texture->type()) {
		case GL_TEXTURE_1D:
			glFramebufferTexture1D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_1D, *texture, mipLevel );
			break;
		case GL_TEXTURE_3D:
			glFramebufferTexture3D( GL_FRAMEBUFFER, attachment, GL_TEXTURE_3D, *texture, mipLevel, zSlice );
			break;
		case GL_TEXTURE_2D_ARRAY:
			glFramebufferTextureLayer( GL_FRAMEBUFFER, attachment, *texture, mipLevel, zSlice );
			break;
		default: //GL_TEXTURE_2D, GL_TEXTURE_RECTANGLE
			glFramebufferTexture2D( GL_FRAMEBUFFER, attachment, texture->type(), GLuint(*texture), mipLevel );
			break;
	}
	_attachedTextures[attachment] = texture;
}
Example #7
0
bool	FrameBuffer :: attach3DTextureSlice ( Texture * tex, int zOffs, int no )
{
	if ( frameBuffer == 0 )
		return false;

	if ( tex -> getTarget () != GL_TEXTURE_3D )
		return false;
		
	if ( tex -> getDepth () >= zOffs )
		return false;
		
	colorBuffer [no] = tex;
	
	glBindTexture          ( GL_TEXTURE_3D, tex -> getId () );
	glFramebufferTexture3D ( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + no, GL_TEXTURE_3D, tex -> getId (), 0, zOffs );

	return true;
}
Example #8
0
void
piglit_init(int argc, char **argv)
{
	int i, j;
	bool pass = true;
	GLuint fbo, texture;
	float colorData[2][10*10*3];

	/* fill in colorData */
	for(j = 0; j < 2; j++)
	for(i = 0; i < 10*10; i++) {
		colorData[j][i*3+0] = color[j][0];
		colorData[j][i*3+1] = color[j][1];
		colorData[j][i*3+2] = color[j][2];
	}

	/* Create the Source framebuffer object */
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_3D, texture);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_REPEAT);
	glTexImage3D(GL_TEXTURE_3D, 0, GL_RGB, 10, 10, 2, 0, GL_RGB,
		     GL_FLOAT, colorData);

	glGenFramebuffers(1, &fbo);
	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
	glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
			       GL_TEXTURE_3D, texture, 0, 0);

	if(!piglit_check_gl_error(GL_NO_ERROR)) {
		piglit_report_result(PIGLIT_FAIL);
	}

	/* piglit_probe_rect_rgb internally calls ReadPixel(). Check that
	 * the color probed is the same as the Zero layer of the texture.
	 */
	pass = piglit_probe_rect_rgb(0, 0, 10, 10, color[0]) && pass;

	piglit_report_result(pass ? PIGLIT_PASS : PIGLIT_FAIL);
}
	void GL3FrameBufferProvider::detach_object(GLenum opengl_attachment)
	{
		int internal_attachment_offset = decode_internal_attachment_offset(opengl_attachment);

		GLenum target = GL_DRAW_FRAMEBUFFER;
		if (bind_target == framebuffer_read)
			target = GL_READ_FRAMEBUFFER;

		if (!attached_renderbuffers[internal_attachment_offset].is_null())
		{
			glFramebufferRenderbuffer(target, opengl_attachment, GL_RENDERBUFFER, 0);
			attached_renderbuffers[internal_attachment_offset] = RenderBuffer();
		}

		if (!attached_textures[internal_attachment_offset].is_null())
		{
			GL3TextureProvider *gl_texture_provider = dynamic_cast<GL3TextureProvider*>(attached_textures[internal_attachment_offset].get_provider());
			GLuint texture_type = gl_texture_provider->get_texture_type();

			if (texture_type == GL_TEXTURE_1D)
			{
				glFramebufferTexture1D(target, opengl_attachment, texture_type, 0, 0);
			}
			else if (texture_type == GL_TEXTURE_2D)
			{
				glFramebufferTexture2D(target, opengl_attachment, texture_type, 0, 0);
			}
			else if (texture_type == GL_TEXTURE_3D)
			{
				glFramebufferTexture3D(target, opengl_attachment, texture_type, 0, 0, 0);
			}
			else if (texture_type == GL_TEXTURE_2D_ARRAY || texture_type == GL_TEXTURE_1D_ARRAY)
			{
				glFramebufferTextureLayer(target, opengl_attachment, 0, 0, 0);
			}

			attached_textures[internal_attachment_offset] = Texture();
		}
	}
Example #10
0
 void RenderTargetFBO::AttachmentTexture::bind3D( AttachmentTarget attachment, GLuint textureId )
 {
   // INFO this could use bindLayer too, but will fail if the GL_EXT_texture_array extension is not available.
   glFramebufferTexture3D( GL_FRAMEBUFFER_EXT, static_cast<GLenum>(attachment), m_textureTarget, textureId, m_level, m_zoffset );
 }
Example #11
0
bool NzRenderTexture::AttachTexture(nzAttachmentPoint attachmentPoint, nzUInt8 index, NzTexture* texture, unsigned int z)
{
	#if NAZARA_RENDERER_SAFE
	if (!m_impl)
	{
		NazaraError("Render texture not created");
		return false;
	}

	if (attachmentPoint != nzAttachmentPoint_Color && index > 0)
	{
		NazaraError("Index must be 0 for non-color attachments");
		return false;
	}

	if (attachmentPoint == nzAttachmentPoint_Stencil)
	{
		NazaraError("Targeting stencil-only textures is not supported");
		return false;
	}

	unsigned int depthStencilIndex = attachmentIndex[nzAttachmentPoint_DepthStencil];
	if (attachmentPoint == nzAttachmentPoint_Stencil && m_impl->attachements.size() > depthStencilIndex &&
		m_impl->attachements[depthStencilIndex].isUsed)
	{
		NazaraError("Stencil target already attached by DepthStencil attachment");
		return false;
	}

	if (!texture || !texture->IsValid())
	{
		NazaraError("Invalid texture");
		return false;
	}

	if (texture->GetWidth() < m_impl->width || texture->GetHeight() < m_impl->height)
	{
		NazaraError("Texture cannot be smaller than render texture");
		return false;
	}

	unsigned int depth = (texture->GetType() == nzImageType_Cubemap) ? 6 : texture->GetDepth();
	if (z >= depth)
	{
		NazaraError("Z value exceeds depth (" + NzString::Number(z) + " >= (" + NzString::Number(depth) + ')');
		return false;
	}

	if (texture->GetRenderTexture() != nullptr)
	{
		NazaraError("Texture already used by another render texture");
		return false;
	}

	if (formatTypeToAttachment[NzPixelFormat::GetType(texture->GetFormat())] != attachmentPoint)
	{
		NazaraError("Pixel format type does not match attachment point type");
		return false;
	}
	#endif

	if (!Lock())
	{
		NazaraError("Failed to lock render texture");
		return false;
	}

	// Détachement de l'attache précédente (Si il y a)
	Detach(attachmentPoint, index);

	switch (texture->GetType())
	{
		case nzImageType_1D:
			glFramebufferTexture1D(GL_DRAW_FRAMEBUFFER, NzOpenGL::Attachment[attachmentPoint]+index, GL_TEXTURE_1D, texture->GetOpenGLID(), 0);
			break;

		case nzImageType_1D_Array:
		case nzImageType_2D_Array:
			glFramebufferTextureLayer(GL_DRAW_FRAMEBUFFER, NzOpenGL::Attachment[attachmentPoint]+index, texture->GetOpenGLID(), 0, z);
			break;

		case nzImageType_2D:
			glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, NzOpenGL::Attachment[attachmentPoint]+index, NzOpenGL::TextureTarget[texture->GetType()], texture->GetOpenGLID(), 0);
			break;

		case nzImageType_3D:
			glFramebufferTexture3D(GL_DRAW_FRAMEBUFFER, NzOpenGL::Attachment[attachmentPoint]+index, GL_TEXTURE_3D, texture->GetOpenGLID(), 0, z);
			break;

		case nzImageType_Cubemap:
			glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, NzOpenGL::Attachment[attachmentPoint]+index, NzOpenGL::CubemapFace[z], texture->GetOpenGLID(), 0);
			break;
	}

	Unlock();

	unsigned int minSize = attachmentIndex[attachmentPoint]+index+1;
	if (m_impl->attachements.size() < minSize)
		m_impl->attachements.resize(minSize);

	Attachment& attachment = m_impl->attachements[minSize-1];
	attachment.isBuffer = false;
	attachment.isUsed = true;
	attachment.texture = texture;

	texture->AddResourceListener(this);
	texture->SetRenderTexture(this);

	m_impl->checked = false;
	m_impl->drawBuffersUpdated = false;

	return true;
}
Example #12
0
void GLfbo::Texture3D(u32 attachment, u32 texture, int zoffset, int level)
{
	glFramebufferTexture3D(m_type, attachment, GL_TEXTURE_3D, texture, level, zoffset);
}
Example #13
0
	void attach3d(GLenum attachment, GLenum texture_target, GLuint texture_id, GLint level, GLint layer) { scoped_bind<framebuffer> bound(*this); glFramebufferTexture3D(target, attachment, texture_target, texture_id, level, layer); throw_on_texture_opengl_error(); }
Example #14
0
void ShaderNode::bindOutputBuffers( QVector<GLenum> &Buffers, QList< QSharedPointer<InterfacePin> > &OutPinLst, int &W, int &H, int &D )
{
	for( QList< QSharedPointer<InterfacePin> >::iterator it = OutPinLst.begin() ; it != OutPinLst.end() ; it++ )
	{
		QSharedPointer<InterfacePin>	OutPin = *it;

		if( !OutPin->isConnected() )
		{
			continue;
		}

		QSharedPointer<InterfacePin>			DstPin = OutPin->connectedPin();

		if( DstPin == 0 )
		{
			continue;
		}

		QSharedPointer<InterfacePinControl>		DstControl = DstPin->control();

		if( DstControl == 0 )
		{
			continue;
		}

		InterfaceTexture		*DstTexture = qobject_cast<InterfaceTexture *>( DstControl->object() );

		if( DstTexture == 0 )
		{
			continue;
		}

		if( DstTexture->textureId() == 0 )
		{
			DstTexture->update( 0, 0 );

			if( DstTexture->textureId() == 0 )
			{
				continue;
			}
		}

		if( Buffers.empty() )
		{
			W = DstTexture->size().at( 0 );
			H = DstTexture->size().at( 1 );
			D = DstTexture->size().at( 2 );
		}

		//glBindTexture( DstTexture->target(), 0 );

		if( mFrameBufferId == 0 )
		{
			glGenFramebuffers( 1, &mFrameBufferId );

			if( mFrameBufferId == 0 )
			{
				continue;
			}
		}

		glBindFramebuffer( GL_FRAMEBUFFER, mFrameBufferId );

		switch( DstTexture->target() )
		{
			case GL_TEXTURE_1D:
				glFramebufferTexture1D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + Buffers.size(), DstTexture->target(), DstTexture->textureId(), 0 );
				break;

			case GL_TEXTURE_2D:
			case GL_TEXTURE_RECTANGLE:
				glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + Buffers.size(), DstTexture->target(), DstTexture->textureId(), 0 );
				break;

			case GL_TEXTURE_3D:
				glFramebufferTexture3D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + Buffers.size(), DstTexture->target(), DstTexture->textureId(), 0, 0 );
				break;
		}

		OPENGL_PLUGIN_DEBUG;

		Buffers.append( GL_COLOR_ATTACHMENT0 + Buffers.size() );
	}
}
Example #15
0
/**
 * OpenGL ES does not support glGetTexImage. Obtain the pixels by attaching the
 * texture to a framebuffer.
 */
static inline void
getTexImageOES(GLenum target, GLint level, ImageDesc &desc, GLubyte *pixels)
{
    memset(pixels, 0x80, desc.height * desc.width * 4);

    GLenum texture_binding = GL_NONE;
    switch (target) {
    case GL_TEXTURE_2D:
        texture_binding = GL_TEXTURE_BINDING_2D;
        break;
    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
        texture_binding = GL_TEXTURE_BINDING_CUBE_MAP;
        break;
    case GL_TEXTURE_3D_OES:
        texture_binding = GL_TEXTURE_BINDING_3D_OES;
    default:
        return;
    }

    GLint texture = 0;
    glGetIntegerv(texture_binding, &texture);
    if (!texture) {
        return;
    }

    GLint prev_fbo = 0;
    GLuint fbo = 0;
    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &prev_fbo);
    glGenFramebuffers(1, &fbo);
    glBindFramebuffer(GL_FRAMEBUFFER, fbo);

    GLenum status;

    switch (target) {
    case GL_TEXTURE_2D:
    case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
    case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
    case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
    case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
    case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
        glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, level);
        status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
        if (status != GL_FRAMEBUFFER_COMPLETE) {
            std::cerr << __FUNCTION__ << ": " << enumToString(status) << "\n";
        }
        glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
        break;
    case GL_TEXTURE_3D_OES:
        for (int i = 0; i < desc.depth; i++) {
            glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, texture, level, i);
            glReadPixels(0, 0, desc.width, desc.height, GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * desc.width * desc.height);
        }
        break;
    }

    glBindFramebuffer(GL_FRAMEBUFFER, prev_fbo);

    glDeleteFramebuffers(1, &fbo);
}
    //-----------------------------------------------------------------------------  
    void GLES2TextureBuffer::download(const PixelBox &data)
    {
        if(data.getWidth() != getWidth() ||
           data.getHeight() != getHeight() ||
           data.getDepth() != getDepth())
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS, "only download of entire buffer is supported by GL ES",
                        "GLES2TextureBuffer::download");

        if(PixelUtil::isCompressed(data.format))
        {
            OGRE_EXCEPT(Exception::ERR_INVALIDPARAMS,
                        "Compressed images cannot be downloaded by GL ES",
                        "GLES2TextureBuffer::download");
        }

        GLenum texBinding = GL_NONE;
        switch (mTarget)
        {
            case GL_TEXTURE_2D:
                texBinding = GL_TEXTURE_BINDING_2D;
                break;
            case GL_TEXTURE_CUBE_MAP:
                texBinding = GL_TEXTURE_BINDING_CUBE_MAP;
                break;
#if OGRE_NO_GLES3_SUPPORT == 0
            case GL_TEXTURE_3D:
                texBinding = GL_TEXTURE_BINDING_3D;
                break;
#endif
            default:
                return;
        }

#if OGRE_NO_GLES3_SUPPORT == 0
        if(data.getWidth() != data.rowPitch)
            OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_ROW_LENGTH, data.rowPitch));
        if(data.getHeight()*data.getWidth() != data.slicePitch)
            OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_IMAGE_HEIGHT, (data.slicePitch/data.getWidth())));
        if(data.left > 0 || data.top > 0 || data.front > 0)
            OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_SKIP_PIXELS, data.left + data.rowPitch * data.top + data.slicePitch * data.front));
#endif
        if((data.getWidth()*PixelUtil::getNumElemBytes(data.format)) & 3) {
            // Standard alignment of 4 is not right
            OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_ALIGNMENT, 1));
        }

        GLint currentFBO = 0;
        GLuint tempFBO = 0;
        OGRE_CHECK_GL_ERROR(glGetIntegerv(GL_FRAMEBUFFER_BINDING, &currentFBO));
        OGRE_CHECK_GL_ERROR(glGenFramebuffers(1, &tempFBO));
        OGRE_CHECK_GL_ERROR(glBindFramebuffer(GL_FRAMEBUFFER, tempFBO));

        switch (mTarget)
        {
            case GL_TEXTURE_2D:
            case GL_TEXTURE_CUBE_MAP:
                OGRE_CHECK_GL_ERROR(glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mTextureID, 0));
                OGRE_CHECK_GL_ERROR(glCheckFramebufferStatus(GL_FRAMEBUFFER));
                OGRE_CHECK_GL_ERROR(glReadPixels(0, 0, data.getWidth(), data.getHeight(),
                                                 GLES2PixelUtil::getGLOriginFormat(data.format),
                                                 GLES2PixelUtil::getGLOriginDataType(data.format), data.data));
                break;
#if OGRE_NO_GLES3_SUPPORT == 0
            case GL_TEXTURE_3D:
                for (int i = 0; i < desc.depth; i++) {
                    OGRE_CHECK_GL_ERROR(glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, mTextureID, mLevel, i));
                    OGRE_CHECK_GL_ERROR(glReadPixels(0, 0, data.getWidth(), data.getHeight(), GL_RGBA, GL_UNSIGNED_BYTE, pixels + 4 * i * data.getWidth() * data.getHeight()));
                }
                break;
#endif
        }

        // Restore defaults
#if OGRE_NO_GLES3_SUPPORT == 0
        OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_ROW_LENGTH, 0));
        OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_IMAGE_HEIGHT, 0));
        OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_SKIP_PIXELS, 0));
#endif
        OGRE_CHECK_GL_ERROR(glPixelStorei(GL_PACK_ALIGNMENT, 4));

        OGRE_CHECK_GL_ERROR(glBindFramebuffer(GL_FRAMEBUFFER, currentFBO));
        OGRE_CHECK_GL_ERROR(glDeleteFramebuffers(1, &tempFBO));
    }
Example #17
0
MyEngine::MyEngine() : SimpleGraphicsEngine()
{
  // Shaders
  ShaderManager::instance()->loadShader(
    "SHADER_PHONG",
    "../shaders/phong.vert",
    nullptr,
    nullptr,
    nullptr,
    "../shaders/phong.frag");
  ShaderManager::instance()->loadShader(
    "SHADER_PLAINTEXTURE",
    "../shaders/plaintextureshader.vert",
    nullptr, 
    nullptr,
    nullptr,
    "../shaders/plaintextureshader.frag");
  ShaderManager::instance()->loadShader(
    "SHADER_SIMPLEVOLUME",
    "../shaders/simplevolumerenderer.vert",
    nullptr, 
    nullptr,
    nullptr,
    "../shaders/simplevolumerenderer.frag");
  ShaderManager::instance()->loadShader(
    "SHADER_WORLDPOSITIONOUTPUT",
    "../shaders/worldpositionoutputshader.vert",
    nullptr, 
    nullptr,
    nullptr,
    "../shaders/worldpositionoutputshader.frag");
  ShaderManager::instance()->loadShader(
    "SHADER_GLOBALRENDERER",
    "../shaders/globalrenderer.vert",
    nullptr, 
    nullptr,
    nullptr,
    "../shaders/globalrenderer.frag");

  shader_phong_ = ShaderManager::instance()->getShader("SHADER_PHONG");
  shader_plaintexture_ = ShaderManager::instance()->getShader("SHADER_PLAINTEXTURE");
  shader_simplevolume_ = ShaderManager::instance()->getShader("SHADER_SIMPLEVOLUME");
  shader_worldpositionoutput_ = ShaderManager::instance()->getShader("SHADER_WORLDPOSITIONOUTPUT");
  shader_global_ = ShaderManager::instance()->getShader("SHADER_GLOBALRENDERER");

  yaw_goal = pitch_goal = roll_goal = 0;
  yaw = pitch = roll = 0;

  // FBO
  fbo3D_ = new FBO3D(64);
  fbo1_ = new FBO(640, 480, 0);
  fbo2_ = new FBO(640, 480, 0);

  // Cameras
  camera_ = new Object3D();
  basic_cam_ = new PerspectiveCamera(window_);
  slicer_camera_ = new OrthoCamera();

  camera_->addChild(basic_cam_);

  // Materials
  material1_.color_diffuse = glm::vec3(0,1,1);
  material1_.color_specular = glm::vec3(1,1,1);
  material1_.reflectance = 1;
  material1_.specular_reflectance = 0;
  material1_.specular_polish = 0.5;

  material2_.color_diffuse = glm::vec3(1,1,1);
  material2_.color_specular = glm::vec3(1,1,1);
  material2_.reflectance = 1;
  material2_.specular_reflectance = 0.0;
  material2_.specular_polish = 0.5;

  // Objects
  //planet_ = new Planet();
  quad_ = new Quad();
  cube_ = new TriangleMesh("../data/meshes/cube.obj");
  floor_mesh_ = new TriangleMesh("../data/meshes/floor.obj");
  bunny_mesh_ = new TriangleMesh("../data/meshes/bunny.obj");

  floor_ = new MyObject3D(material1_);
  bunny_ = new MyObject3D(material2_);

  floor_->addChild(floor_mesh_);
  floor_->transform_matrix_ = glm::rotate(70.0f, glm::vec3(1.0f,0.0f,0.0f));
  floor_->transform_matrix_ = glm::translate(glm::vec3(0.0f,-0.5f,-0.5f)) * floor_->transform_matrix_;
  
  bunny_->addChild(bunny_mesh_);
  bunny_->transform_matrix_ = glm::scale(glm::mat4(), glm::vec3(0.3,0.3,0.3));

  scene_->addChild(bunny_);
  scene_->addChild(floor_);

  // Set callback functions
  glfwSetScrollCallback(window_, mouseScrollCallback);
  glfwSetKeyCallback(window_, keyCallback);
  glfwSetInputMode(window_, GLFW_CURSOR, GLFW_CURSOR_DISABLED);



  // Voxelize the mesh
  glBindFramebuffer(GL_FRAMEBUFFER, fbo3D_->fb_);
  glViewport(0, 0, fbo3D_->size_, fbo3D_->size_);

  glClearColor(0.0, 0.0, 0.0, 0.0);
  //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

  glDisable(GL_CULL_FACE);
  glEnable(GL_DEPTH_TEST);
 
  for (int i = 0; i < fbo3D_->size_; ++i)
  {
    glFramebufferTexture3D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_3D, fbo3D_->texid_, 0, i);    
    float scene_scale = 1;

    slicer_camera_->render(
      glm::mat4(),
      shader_phong_,
      -scene_scale, // left
      scene_scale, // right
      -scene_scale, // bottom
      scene_scale, // top
      scene_scale - (float)i / fbo3D_->size_ * scene_scale * 2, // near
      scene_scale - (float)(i + 1) / fbo3D_->size_ * scene_scale * 2); // far

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    scene_->render(glm::mat4(), shader_phong_);
  }

  glActiveTexture(GL_TEXTURE0);
  glBindTexture(GL_TEXTURE_3D, fbo3D_->texid_);
  glGenerateMipmap(GL_TEXTURE_3D);
}