GteGL4DrawTarget.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
9 #include <LowLevel/GteLogger.h>
11 using namespace gte;
12 
14 {
16 }
17 
19  std::vector<GL4TextureRT*>& rtTextures, GL4TextureDS* dsTexture)
20  :
21  GEDrawTarget(target),
22  mRTTextures(rtTextures),
23  mDSTexture(dsTexture),
24  mFrameBuffer(0)
25 {
26  LogAssert(target->GetNumTargets() <= rtTextures.size(),
27  "DrawTargets has more targets than there are RT textures provided.");
28 
30 }
31 
32 std::shared_ptr<GEDrawTarget> GL4DrawTarget::Create(DrawTarget const* target,
33  std::vector<GEObject*>& rtTextures, GEObject* dsTexture)
34 {
35  std::vector<GL4TextureRT*> dxRTTextures(rtTextures.size());
36  for (size_t i = 0; i < rtTextures.size(); ++i)
37  {
38  dxRTTextures[i] = static_cast<GL4TextureRT*>(rtTextures[i]);
39  }
40  GL4TextureDS* dxDSTexture = static_cast<GL4TextureDS*>(dsTexture);
41 
42  return std::make_shared<GL4DrawTarget>(target, dxRTTextures, dxDSTexture);
43 }
44 
46 {
47  // Save the current viewport settings so they can be restored when Disable is called.
48  GLint intVals[4];
49  GLdouble doubleVals[2];
50  glGetIntegerv(GL_VIEWPORT, intVals);
51  glGetDoublev(GL_DEPTH_RANGE, doubleVals);
52  mSaveViewportX = intVals[0];
53  mSaveViewportY = intVals[1];
54  mSaveViewportWidth = intVals[2];
55  mSaveViewportHeight = intVals[3];
56  mSaveViewportNear = doubleVals[0];
57  mSaveViewportFar = doubleVals[1];
58 
59  // Set viewport according to draw target;
60  auto viewportWidth = static_cast<GLsizei>(mTarget->GetWidth());
61  auto viewportHeight = static_cast<GLsizei>(mTarget->GetHeight());
62  glViewport(0, 0, viewportWidth, viewportHeight);
63 
64  // Set depth range to full.
65  glDepthRange(0.0, 1.0);
66 
67  // Bind the frame buffer.
69 
70  // Attach depth buffer if there is one.
71  if (mDSTexture)
72  {
74  }
75 
76  // Attach each render target.
77  // Build list of attachments to use for drawing to.
78  auto const numTargets = mTarget->GetNumTargets();
79  std::vector<GLenum> useDrawBuffers(numTargets);
80  for (unsigned i = 0; i < numTargets; ++i)
81  {
82  auto colorTarget = GL_COLOR_ATTACHMENT0 + i;
83 
84  useDrawBuffers[i] = colorTarget;
85 
86  auto textureRT = mRTTextures[i];
87  glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, colorTarget, GL_TEXTURE_2D, textureRT->GetGLHandle(), 0);
88  }
89 
90  // Set which draw buffers to use.
91  glDrawBuffers(static_cast<GLsizei>(useDrawBuffers.size()), useDrawBuffers.data());
92 }
93 
95 {
96  // Restore to default frame buffer rendering.
98 
99  // Restore viewport.
102 
103  // When done, test each render target texture if it needs to have it's
104  // mipmaps auto generated.
105  auto const numTargets = mTarget->GetNumTargets();
106  for (unsigned i = 0; i < numTargets; ++i)
107  {
108  auto textureRT = mRTTextures[i];
109  if (textureRT->CanAutoGenerateMipmaps())
110  {
111  textureRT->GenerateMipmaps();
112  }
113  }
114 }
unsigned int GetWidth() const
void APIENTRY glBindFramebuffer(GLenum target, GLuint framebuffer)
Definition: GteOpenGL.cpp:4182
#define GL_TEXTURE_2D
Definition: glcorearb.h:304
int GLint
Definition: glcorearb.h:85
void APIENTRY glViewport(GLint x, GLint y, GLsizei width, GLsizei height)
Definition: GteOpenGL.cpp:755
unsigned int GetHeight() const
void APIENTRY glDepthRange(GLdouble near, GLdouble far)
Definition: GteOpenGL.cpp:742
#define LogAssert(condition, message)
Definition: GteLogger.h:86
DrawTarget * mTarget
static std::shared_ptr< GEDrawTarget > Create(DrawTarget const *target, std::vector< GEObject * > &rtTextures, GEObject *dsTexture)
#define GL_FRAMEBUFFER
Definition: glcorearb.h:1165
#define GL_VIEWPORT
Definition: glcorearb.h:271
GL4TextureDS * mDSTexture
void APIENTRY glDeleteFramebuffers(GLsizei n, const GLuint *framebuffers)
Definition: GteOpenGL.cpp:4195
#define GL_COLOR_ATTACHMENT0
Definition: glcorearb.h:1147
GLenum target
Definition: glcorearb.h:1662
GL4DrawTarget(DrawTarget const *target, std::vector< GL4TextureRT * > &rtTextures, GL4TextureDS *dsTexture)
int GLsizei
Definition: glcorearb.h:86
void APIENTRY glFramebufferTexture2D(GLenum target, GLenum attachment, GLenum textarget, GLuint texture, GLint level)
Definition: GteOpenGL.cpp:4250
unsigned int GetNumTargets() const
#define GL_DEPTH_STENCIL_ATTACHMENT
Definition: glcorearb.h:1116
void APIENTRY glDrawBuffers(GLsizei n, const GLenum *bufs)
Definition: GteOpenGL.cpp:1821
double GLdouble
Definition: glcorearb.h:88
void APIENTRY glGetIntegerv(GLenum pname, GLint *data)
Definition: GteOpenGL.cpp:632
#define GL_DRAW_FRAMEBUFFER
Definition: glcorearb.h:1132
void APIENTRY glGetDoublev(GLenum pname, GLdouble *data)
Definition: GteOpenGL.cpp:591
std::vector< GL4TextureRT * > mRTTextures
#define GL_DEPTH_RANGE
Definition: glcorearb.h:257
void APIENTRY glGenFramebuffers(GLsizei n, GLuint *framebuffers)
Definition: GteOpenGL.cpp:4208


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 04:00:00