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 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkOpenGLRenderUtilities.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtk_glew.h"
#include "vtkOpenGLRenderUtilities.h"
#include "vtkNew.h"
#include "vtkOpenGLBufferObject.h"
#include "vtkOpenGLVertexArrayObject.h"
#include "vtkShaderProgram.h"
// ----------------------------------------------------------------------------
vtkOpenGLRenderUtilities::vtkOpenGLRenderUtilities()
{
}
// ----------------------------------------------------------------------------
vtkOpenGLRenderUtilities::~vtkOpenGLRenderUtilities()
{
}
void vtkOpenGLRenderUtilities::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os,indent);
}
// ---------------------------------------------------------------------------
// a program must be bound
// a VAO must be bound
void vtkOpenGLRenderUtilities::RenderQuad(
float *verts,
float *tcoords,
vtkShaderProgram *program, vtkOpenGLVertexArrayObject *vao)
{
GLuint iboData[] = {0, 1, 2, 0, 2, 3};
vtkOpenGLRenderUtilities::RenderTriangles(verts, 4,
iboData, 6,
tcoords,
program, vao);
}
// ---------------------------------------------------------------------------
// a program must be bound
// a VAO must be bound
void vtkOpenGLRenderUtilities::RenderTriangles(
float *verts, unsigned int numVerts,
GLuint *iboData, unsigned int numIndices,
float *tcoords,
vtkShaderProgram *program, vtkOpenGLVertexArrayObject *vao)
{
if (!program || !vao || !verts)
{
vtkGenericWarningMacro(<< "Error must have verts, program and vao");
return;
}
if (!program->isBound())
{
vtkGenericWarningMacro("attempt to render to unbound program");
}
vtkNew<vtkOpenGLBufferObject> vbo;
vbo->Upload(verts, numVerts*3, vtkOpenGLBufferObject::ArrayBuffer);
vao->Bind();
if (!vao->AddAttributeArray(program, vbo.Get(), "vertexMC", 0,
sizeof(float)*3, VTK_FLOAT, 3, false))
{
vtkGenericWarningMacro(<< "Error setting 'vertexMC' in shader VAO.");
}
vtkNew<vtkOpenGLBufferObject> tvbo;
if (tcoords)
{
tvbo->Upload(tcoords, numVerts*2, vtkOpenGLBufferObject::ArrayBuffer);
if (!vao->AddAttributeArray(program, tvbo.Get(), "tcoordMC", 0,
sizeof(float)*2, VTK_FLOAT, 2, false))
{
vtkGenericWarningMacro(<< "Error setting 'tcoordMC' in shader VAO.");
}
}
vtkNew<vtkOpenGLBufferObject> ibo;
vao->Bind();
ibo->Upload(iboData, numIndices, vtkOpenGLBufferObject::ElementArrayBuffer);
glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_INT,
reinterpret_cast<const GLvoid *>(NULL));
ibo->Release();
ibo->ReleaseGraphicsResources();
vao->RemoveAttributeArray("vertexMC");
vao->RemoveAttributeArray("tcoordMC");
vao->Release();
vbo->Release();
vbo->ReleaseGraphicsResources();
if (tcoords)
{
tvbo->Release();
tvbo->ReleaseGraphicsResources();
}
}
//------------------------------------------------------------------------------
std::string vtkOpenGLRenderUtilities::GetFullScreenQuadVertexShader()
{
// Pass through:
return "//VTK::System::Dec\n"
"attribute vec4 ndCoordIn;\n"
"attribute vec2 texCoordIn;\n"
"varying vec2 texCoord;\n"
"void main()\n"
"{\n"
" gl_Position = ndCoordIn;\n"
" texCoord = texCoordIn;\n"
"}\n";
}
//------------------------------------------------------------------------------
std::string vtkOpenGLRenderUtilities::GetFullScreenQuadFragmentShaderTemplate()
{
return "//VTK::System::Dec\n"
"//VTK::Output::Dec\n"
"in vec2 texCoord;\n"
"//VTK::FSQ::Decl\n"
"void main()\n"
"{\n"
"//VTK::FSQ::Impl\n"
"}\n";
}
//------------------------------------------------------------------------------
std::string vtkOpenGLRenderUtilities::GetFullScreenQuadGeometryShader()
{
return "";
}
//------------------------------------------------------------------------------
bool vtkOpenGLRenderUtilities::PrepFullScreenVAO(vtkOpenGLBufferObject *vertBuf,
vtkOpenGLVertexArrayObject *vao,
vtkShaderProgram *prog)
{
bool res;
// ndCoord_x, ndCoord_y, texCoord_x, texCoord_y
float verts[16] = { 1.f, 1.f, 1.f, 1.f,
-1.f, 1.f, 0.f, 1.f,
1.f,-1.f, 1.f, 0.f,
-1.f,-1.f, 0.f, 0.f };
vertBuf->SetType(vtkOpenGLBufferObject::ArrayBuffer);
res = vertBuf->Upload(verts, 16, vtkOpenGLBufferObject::ArrayBuffer);
if (!res)
{
vtkGenericWarningMacro("Error uploading fullscreen quad vertex data.");
return false;
}
vao->Bind();
res = vao->AddAttributeArray(prog, vertBuf, "ndCoordIn", 0, 4 * sizeof(float),
VTK_FLOAT, 2, false);
if (!res)
{
vao->Release();
vtkGenericWarningMacro("Error binding ndCoords to VAO.");
return false;
}
res = vao->AddAttributeArray(prog, vertBuf, "texCoordIn", 2 * sizeof(float),
4 * sizeof(float), VTK_FLOAT, 2, false);
if (!res)
{
vao->Release();
vtkGenericWarningMacro("Error binding texCoords to VAO.");
return false;
}
vao->Release();
return true;
}
//------------------------------------------------------------------------------
void vtkOpenGLRenderUtilities::DrawFullScreenQuad()
{
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
|