Commit 64017f58 authored by Joachim Pouderoux's avatar Joachim Pouderoux

Introduce cube map creation from raw images.

Add a test with a reflection shader.
parent 62b8936c
Pipeline #19361 passed with stage
......@@ -2,6 +2,7 @@ vtk_add_test_cxx(${vtk-module}CxxTests tests
TestAppleBug.cxx
TestBlurAndSobelPasses.cxx
TestCoincident.cxx
TestCubeMap.cxx
TestDepthOfFieldPass.cxx
TestDepthPeelingPass.cxx
TestEDLPass.cxx
......
/*=========================================================================
Program: Visualization Toolkit
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 "vtkActor.h"
#include "vtkCamera.h"
#include "vtkImageData.h"
#include "vtkImageFlip.h"
#include "vtkInteractorStyleTrackballCamera.h"
#include "vtkJPEGReader.h"
#include "vtkNew.h"
#include "vtkOpenGLPolyDataMapper.h"
#include "vtkOpenGLRenderWindow.h"
#include "vtkOpenGLTexture.h"
#include "vtkPLYReader.h"
#include "vtkPolyDataNormals.h"
#include "vtkProperty.h"
#include "vtkRegressionTestImage.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkShaderProgram.h"
#include "vtkSmartPointer.h"
#include "vtkTestUtilities.h"
#include "vtkTextureObject.h"
//----------------------------------------------------------------------------
int TestCubeMap(int argc, char *argv[])
{
vtkNew<vtkRenderer> renderer;
renderer->SetBackground(0.0, 0.0, 0.0);
vtkNew<vtkRenderWindow> renderWindow;
renderWindow->SetSize(400, 400);
renderWindow->AddRenderer(renderer.Get());
vtkNew<vtkRenderWindowInteractor> iren;
iren->SetRenderWindow(renderWindow.Get());
// We call Render to create the OpenGL context as it will
// be needed by the texture object
renderWindow->Render();
const char* fileName =
vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/bunny.ply");
vtkNew<vtkPLYReader> reader;
reader->SetFileName(fileName);
vtkNew<vtkPolyDataNormals> norms;
norms->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkImageData> imgs[6];
const char* fpath[] =
{
"Data/skybox-px.jpg",
"Data/skybox-nx.jpg",
"Data/skybox-py.jpg",
"Data/skybox-ny.jpg",
"Data/skybox-pz.jpg",
"Data/skybox-nz.jpg"
};
void* images[6];
for (int i = 0; i < 6; i++)
{
vtkNew<vtkJPEGReader> imgReader;
imgReader->SetFileName(
vtkTestUtilities::ExpandDataFileName(argc, argv, fpath[i]));
vtkNew<vtkImageFlip> flip;
flip->SetInputConnection(imgReader->GetOutputPort());
flip->SetFilteredAxis(1); // flip y axis
flip->Update();
imgs[i] = flip->GetOutput();
images[i] = imgs[i]->GetScalarPointer();
}
int dims[3];
imgs[0]->GetDimensions(dims);
// Create a texture object from our set of cube map images
vtkNew<vtkTextureObject> texObject;
texObject->SetContext(
vtkOpenGLRenderWindow::SafeDownCast(renderWindow.Get()));
texObject->CreateCubeFromRaw(
dims[0], dims[1], 3, imgs[0]->GetScalarType(), images);
// Setup a texture with our home made texture object
vtkNew<vtkOpenGLTexture> texture;
texture->SetTextureObject(texObject.Get());
vtkNew<vtkOpenGLPolyDataMapper> mapper;
mapper->SetInputConnection(norms->GetOutputPort());
vtkNew<vtkActor> actor;
renderer->AddActor(actor.Get());
actor->SetTexture(texture.Get());
actor->SetMapper(mapper.Get());
// Add new code in default VTK vertex shader
mapper->AddShaderReplacement(
vtkShader::Vertex,
"//VTK::PositionVC::Dec", // replace the normal block
true, // before the standard replacements
"//VTK::PositionVC::Dec\n" // we still want the default
"varying vec3 TexCoords;\n",
false // only do it once
);
mapper->AddShaderReplacement(
vtkShader::Vertex,
"//VTK::PositionVC::Impl", // replace the normal block
true, // before the standard replacements
"//VTK::PositionVC::Impl\n" // we still want the default
"vec3 camPos = -MCVCMatrix[3].xyz * mat3(MCVCMatrix);\n"
"TexCoords.xyz = reflect(vertexMC.xyz - camPos, normalize(normalMC));\n",
false // only do it once
);
// Replace VTK fragment shader
mapper->SetFragmentShaderCode(
"//VTK::System::Dec\n" // always start with this line
"//VTK::Output::Dec\n" // always have this line in your FS
"varying vec3 TexCoords;\n"
"uniform samplerCube CubeMap;\n"
"void main () {\n"
" gl_FragData[0] = texture(CubeMap, TexCoords);\n"
"}\n"
);
renderer->ResetCamera();
renderer->GetActiveCamera()->Zoom(1.4);
renderWindow->Render();
vtkNew<vtkInteractorStyleTrackballCamera> style;
renderWindow->GetInteractor()->SetInteractorStyle(style.Get());
int retVal = vtkRegressionTestImage(renderWindow.Get());
if ( retVal == vtkRegressionTester::DO_INTERACTOR)
{
iren->Start();
}
return EXIT_SUCCESS;
}
8596c9aa7eb0e4ff6f01d2dbe05e1ba4
\ No newline at end of file
......@@ -585,6 +585,11 @@ bool vtkTextureObject::IsBound()
case GL_TEXTURE_BUFFER:
target=GL_TEXTURE_BINDING_BUFFER;
break;
#endif
#if defined(GL_TEXTURE_CUBE_MAP) && defined(GL_TEXTURE_BINDING_CUBE_MAP)
case GL_TEXTURE_CUBE_MAP:
target=GL_TEXTURE_BINDING_CUBE_MAP;
break;
#endif
default:
assert("check: impossible case" && 0);
......@@ -1604,6 +1609,60 @@ bool vtkTextureObject::Create2DFromRaw(unsigned int width, unsigned int height,
return true;
}
//----------------------------------------------------------------------------
bool vtkTextureObject::CreateCubeFromRaw(unsigned int width, unsigned int height,
int numComps, int dataType, void *data[6])
{
assert(this->Context);
// Now determine the texture parameters using the arguments.
this->GetDataType(dataType);
this->GetInternalFormat(dataType, numComps, false);
this->GetFormat(dataType, numComps, false);
if (!this->InternalFormat || !this->Format || !this->Type)
{
vtkErrorMacro("Failed to determine texture parameters. IF="
<< this->InternalFormat << " F=" << this->Format << " T=" << this->Type);
return false;
}
GLenum target = GL_TEXTURE_CUBE_MAP;
this->Target = target;
this->Components = numComps;
this->Width = width;
this->Height = height;
this->Depth = 1;
this->NumberOfDimensions = 2;
this->Context->ActivateTexture(this);
this->CreateTexture();
this->Bind();
// Source texture data from the PBO.
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (int i = 0; i < 6; i++)
{
if (data[i])
{
glTexImage2D(
GL_TEXTURE_CUBE_MAP_POSITIVE_X + i,
0,
this->InternalFormat,
static_cast<GLsizei>(this->Width),
static_cast<GLsizei>(this->Height),
0,
this->Format,
this->Type,
static_cast<const GLvoid *>(data[i]));
vtkOpenGLCheckErrorMacro("failed at glTexImage2D");
}
}
this->Deactivate();
return true;
}
// ----------------------------------------------------------------------------
// Description:
// Create a 2D depth texture using a raw pointer.
......
......@@ -208,6 +208,13 @@ public:
bool CreateTextureBuffer(unsigned int numValues, int numComps,
int dataType, vtkOpenGLBufferObject *bo);
// Description:
// Create a cube texture from 6 buffers from client memory.
// Image data must be provided in the following order: +X -X +Y -Y +Z -Z.
// numComps must be in [1-4].
bool CreateCubeFromRaw(unsigned int width, unsigned int height,
int numComps, int dataType, void *data[6]);
// 1D textures are not supported in ES 2.0 or 3.0
#if GL_ES_VERSION_2_0 != 1
......
35034574e114423babdea92b6a2eb97a
\ No newline at end of file
35808d21d1d17ac1b84d3184e365093b
\ No newline at end of file
d73d18a8b7994524dc39a6ea3fcf43fc
\ No newline at end of file
d235fb97df77b607be04b84125f40e7c
\ No newline at end of file
d0c4e1e7979d75923d4d8b49c191bc6e
\ No newline at end of file
de29aa8396196e4b040cd5305e351dbe
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment