2013-03-08 01:50:15 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// Copyright 2013 Pixar
|
2013-03-08 01:50:15 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License
|
|
|
|
// and the following modification to it: Section 6 Trademarks.
|
|
|
|
// deleted and replaced with:
|
2013-03-08 01:50:15 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// 6. Trademarks. This License does not grant permission to use the
|
|
|
|
// trade names, trademarks, service marks, or product names of the
|
|
|
|
// Licensor and its affiliates, except as required for reproducing
|
|
|
|
// the content of the NOTICE file.
|
2013-03-08 01:50:15 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// You may obtain a copy of the License at
|
2013-03-08 01:50:15 +00:00
|
|
|
//
|
2013-07-18 21:19:50 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing,
|
|
|
|
// software distributed under the License is distributed on an
|
|
|
|
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|
|
|
// either express or implied. See the License for the specific
|
|
|
|
// language governing permissions and limitations under the
|
|
|
|
// License.
|
2013-03-08 01:50:15 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
2013-06-26 07:52:57 +00:00
|
|
|
#if defined(OSD_USES_GLEW)
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#else
|
|
|
|
#include <OpenGL/gl3.h>
|
|
|
|
#endif
|
2013-03-08 01:50:15 +00:00
|
|
|
#define GLFW_INCLUDE_GL3
|
|
|
|
#define GLFW_NO_GLU
|
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <GL/glew.h>
|
|
|
|
#if defined(WIN32)
|
|
|
|
#include <GL/wglew.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(GLFW_VERSION_3)
|
2013-06-20 18:53:33 +00:00
|
|
|
#include <GLFW/glfw3.h>
|
2013-03-08 01:50:15 +00:00
|
|
|
GLFWwindow* g_window=0;
|
|
|
|
GLFWmonitor* g_primary=0;
|
|
|
|
#else
|
|
|
|
#include <GL/glfw.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <osd/error.h>
|
|
|
|
#include <osd/vertex.h>
|
|
|
|
#include <osd/glDrawContext.h>
|
|
|
|
#include <osd/glDrawRegistry.h>
|
|
|
|
|
|
|
|
#include <osd/cpuGLVertexBuffer.h>
|
|
|
|
#include <osd/cpuComputeContext.h>
|
|
|
|
#include <osd/cpuComputeController.h>
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENMP
|
|
|
|
#include <osd/ompComputeController.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_GCD
|
|
|
|
#include <osd/gcdComputeController.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
#include <osd/clGLVertexBuffer.h>
|
|
|
|
#include <osd/clComputeContext.h>
|
|
|
|
#include <osd/clComputeController.h>
|
|
|
|
|
|
|
|
#include "../common/clInit.h"
|
|
|
|
|
|
|
|
cl_context g_clContext;
|
|
|
|
cl_command_queue g_clQueue;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_CUDA
|
|
|
|
#include <osd/cudaGLVertexBuffer.h>
|
|
|
|
#include <osd/cudaComputeContext.h>
|
|
|
|
#include <osd/cudaComputeController.h>
|
|
|
|
|
|
|
|
#include <cuda_runtime_api.h>
|
|
|
|
#include <cuda_gl_interop.h>
|
|
|
|
|
|
|
|
#include "../common/cudaInit.h"
|
|
|
|
|
|
|
|
bool g_cudaInitialized = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_GLSL_TRANSFORM_FEEDBACK
|
|
|
|
#include <osd/glslTransformFeedbackComputeContext.h>
|
|
|
|
#include <osd/glslTransformFeedbackComputeController.h>
|
|
|
|
#include <osd/glVertexBuffer.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_GLSL_COMPUTE
|
|
|
|
#include <osd/glslComputeContext.h>
|
|
|
|
#include <osd/glslComputeController.h>
|
|
|
|
#include <osd/glVertexBuffer.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <far/meshFactory.h>
|
|
|
|
|
2013-05-22 00:20:22 +00:00
|
|
|
#include <osdutil/batch.h>
|
2013-06-05 17:44:21 +00:00
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
#include <osdutil/batchCL.h>
|
|
|
|
#endif
|
2013-05-22 00:20:22 +00:00
|
|
|
#include <osdutil/drawItem.h>
|
|
|
|
#include <osdutil/drawController.h>
|
2013-05-10 02:18:32 +00:00
|
|
|
#include "delegate.h"
|
|
|
|
#include "effect.h"
|
|
|
|
|
|
|
|
#include <common/shape_utils.h>
|
|
|
|
#include "../common/stopwatch.h"
|
|
|
|
#include "../common/simple_math.h"
|
|
|
|
#include "../common/gl_hud.h"
|
|
|
|
|
|
|
|
#include <cfloat>
|
|
|
|
#include <vector>
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
template <class CONTROLLER> class Controller
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static CONTROLLER *GetInstance() {
|
|
|
|
static CONTROLLER *instance = NULL;
|
|
|
|
if (not instance) instance = new CONTROLLER();
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
template<> class Controller<OpenSubdiv::OsdCLComputeController>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static OpenSubdiv::OsdCLComputeController *GetInstance() {
|
|
|
|
static OpenSubdiv::OsdCLComputeController *instance = NULL;
|
|
|
|
if (not instance) instance = new OpenSubdiv::OsdCLComputeController(g_clContext, g_clQueue);
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef OpenSubdiv::HbrMesh<OpenSubdiv::OsdVertex> OsdHbrMesh;
|
|
|
|
typedef OpenSubdiv::HbrVertex<OpenSubdiv::OsdVertex> OsdHbrVertex;
|
|
|
|
typedef OpenSubdiv::HbrFace<OpenSubdiv::OsdVertex> OsdHbrFace;
|
|
|
|
typedef OpenSubdiv::HbrHalfedge<OpenSubdiv::OsdVertex> OsdHbrHalfedge;
|
|
|
|
|
|
|
|
enum KernelType { kCPU = 0,
|
|
|
|
kOPENMP = 1,
|
|
|
|
kGCD = 2,
|
|
|
|
kCUDA = 3,
|
|
|
|
kCL = 4,
|
|
|
|
kGLSL = 5,
|
|
|
|
kGLSLCompute = 6 };
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
enum HudCheckBox { HUD_CB_BATCHING,
|
2013-03-08 01:50:15 +00:00
|
|
|
HUD_CB_ADAPTIVE,
|
|
|
|
HUD_CB_DISPLAY_PATCH_COLOR,
|
|
|
|
HUD_CB_VIEW_LOD,
|
|
|
|
HUD_CB_FREEZE };
|
|
|
|
|
|
|
|
struct SimpleShape {
|
|
|
|
std::string name;
|
|
|
|
Scheme scheme;
|
2013-03-19 21:41:49 +00:00
|
|
|
std::string data;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
SimpleShape() { }
|
2013-03-19 21:41:49 +00:00
|
|
|
SimpleShape( std::string const & idata, char const * iname, Scheme ischeme )
|
2013-03-08 01:50:15 +00:00
|
|
|
: name(iname), scheme(ischeme), data(idata) { }
|
|
|
|
};
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
OpenSubdiv::OsdUtilMeshBatchBase<MyDrawContext> *g_batch = NULL;
|
|
|
|
|
|
|
|
MyDrawDelegate g_drawDelegate;
|
|
|
|
MyEffect g_effect;
|
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
std::vector<SimpleShape> g_defaultShapes;
|
|
|
|
|
|
|
|
int g_currentShape = 0;
|
|
|
|
|
|
|
|
int g_frame = 0,
|
|
|
|
g_repeatCount = 0;
|
|
|
|
|
|
|
|
// GUI variables
|
|
|
|
int g_fullscreen = 0,
|
|
|
|
g_freeze = 0,
|
2013-07-02 18:18:06 +00:00
|
|
|
g_displayStyle = kWireShaded,
|
2013-03-08 01:50:15 +00:00
|
|
|
g_adaptive = 1,
|
2013-05-10 02:18:32 +00:00
|
|
|
g_batching = 1,
|
2013-03-08 01:50:15 +00:00
|
|
|
g_mbutton[3] = {0, 0, 0},
|
|
|
|
g_running = 1;
|
|
|
|
|
|
|
|
int g_displayPatchColor = 1;
|
|
|
|
|
|
|
|
float g_rotate[2] = {0, 0},
|
|
|
|
g_dolly = 5,
|
|
|
|
g_pan[2] = {0, 0},
|
|
|
|
g_center[3] = {0, 0, 0},
|
|
|
|
g_size = 1;
|
|
|
|
|
|
|
|
int g_prev_x = 0,
|
|
|
|
g_prev_y = 0;
|
|
|
|
|
|
|
|
int g_width = 1024,
|
|
|
|
g_height = 1024;
|
|
|
|
|
|
|
|
GLhud g_hud;
|
|
|
|
|
|
|
|
// performance
|
|
|
|
float g_cpuTime = 0;
|
|
|
|
float g_gpuTime = 0;
|
|
|
|
Stopwatch g_fpsTimer;
|
|
|
|
double g_totalTime = 0;
|
|
|
|
|
|
|
|
// geometry
|
|
|
|
std::vector<std::vector<float> > g_positions;
|
|
|
|
std::vector<int> g_vertexOffsets;
|
|
|
|
|
|
|
|
struct BBox {
|
|
|
|
BBox(const float *min, const float *max) {
|
|
|
|
for (int i = 0; i < 3; ++i) {
|
|
|
|
_p[0][i] = min[i];
|
|
|
|
_p[1][i] = max[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bool OutOfFrustum(const float *modelViewProjection) {
|
|
|
|
int clip_or = 0;
|
|
|
|
for (int i = 0; i < 2; ++i) {
|
|
|
|
for (int j = 0; j < 2; ++j) {
|
|
|
|
for (int k = 0; k < 2; ++k) {
|
|
|
|
float v[4];
|
|
|
|
v[0] = _p[i][0];
|
|
|
|
v[1] = _p[j][1];
|
|
|
|
v[2] = _p[k][2];
|
|
|
|
v[3] = 1.0f;
|
|
|
|
apply(v, modelViewProjection);
|
|
|
|
int clip = 0;
|
|
|
|
clip = (v[0] < v[3]);
|
|
|
|
clip = (v[1] < v[3]) | (clip << 1);
|
|
|
|
clip = (v[0] > -v[3]) | (clip << 1);
|
|
|
|
clip = (v[1] > -v[3]) | (clip << 1);
|
|
|
|
clip_or |= clip;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return clip_or != 0xf;
|
|
|
|
}
|
|
|
|
float _p[2][3];
|
|
|
|
};
|
|
|
|
std::vector<BBox> g_bboxes;
|
|
|
|
struct Matrix {
|
|
|
|
float value[16];
|
|
|
|
};
|
|
|
|
std::vector<Matrix> g_transforms;
|
|
|
|
|
|
|
|
Scheme g_scheme;
|
|
|
|
|
|
|
|
int g_level = 2;
|
|
|
|
int g_tessLevel = 1;
|
|
|
|
int g_tessLevelMin = 0;
|
|
|
|
int g_screenSpaceTess = 1;
|
|
|
|
int g_kernel = kCPU;
|
|
|
|
#define MAX_MODELS 600
|
|
|
|
int g_modelCount = 4;
|
2013-05-16 23:55:53 +00:00
|
|
|
int g_moveModels = g_modelCount;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
GLuint g_queries[2] = {0, 0};
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
checkGLErrors(std::string const & where = "")
|
|
|
|
{
|
|
|
|
GLuint err;
|
|
|
|
while ((err = glGetError()) != GL_NO_ERROR) {
|
|
|
|
/*
|
|
|
|
std::cerr << "GL error: "
|
|
|
|
<< (where.empty() ? "" : where + " ")
|
|
|
|
<< err << "\n";
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2013-05-10 02:18:32 +00:00
|
|
|
#include "shapes.h"
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
static void
|
2013-05-10 02:18:32 +00:00
|
|
|
updateGeom(bool forceAll) {
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
float r = (float)sin(g_totalTime);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < g_modelCount * g_modelCount; ++j) {
|
2013-05-10 02:18:32 +00:00
|
|
|
if (forceAll == false && j >= g_moveModels) break;
|
2013-03-08 01:50:15 +00:00
|
|
|
int nverts = (int)g_positions[j].size()/3;
|
|
|
|
|
2013-07-02 18:18:06 +00:00
|
|
|
std::vector<float> vertex, varying;
|
|
|
|
vertex.reserve(nverts * 3);
|
|
|
|
|
|
|
|
if (g_displayStyle == kVaryingColor)
|
|
|
|
varying.reserve(nverts * 3);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
const float *p = &g_positions[j][0];
|
|
|
|
for (int i = 0; i < nverts; ++i) {
|
|
|
|
float ct = cos(p[2] * r);
|
|
|
|
float st = sin(p[2] * r);
|
|
|
|
float v[4];
|
|
|
|
v[0] = p[0]*ct + p[1]*st;
|
|
|
|
v[1] = -p[0]*st + p[1]*ct;
|
|
|
|
v[2] = p[2];
|
|
|
|
v[3] = 1;
|
|
|
|
apply(v, g_transforms[j].value);
|
2013-07-02 18:18:06 +00:00
|
|
|
vertex.push_back(v[0]);
|
|
|
|
vertex.push_back(v[1]);
|
|
|
|
vertex.push_back(v[2]);
|
|
|
|
|
|
|
|
if (g_displayStyle == kVaryingColor) {
|
|
|
|
varying.push_back(p[2]);
|
|
|
|
varying.push_back(p[1]);
|
|
|
|
varying.push_back(p[0]);
|
|
|
|
}
|
2013-05-10 02:18:32 +00:00
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
|
2013-05-16 23:55:53 +00:00
|
|
|
g_batch->UpdateCoarseVertices(j, &vertex[0], nverts);
|
2013-07-02 18:18:06 +00:00
|
|
|
|
|
|
|
if (g_displayStyle == kVaryingColor) {
|
|
|
|
g_batch->UpdateCoarseVaryings(j, &varying[0], nverts);
|
|
|
|
}
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Stopwatch s;
|
|
|
|
s.Start();
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
g_batch->FinalizeUpdate();
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
s.Stop();
|
|
|
|
g_cpuTime = float(s.GetElapsed() * 1000.0f);
|
|
|
|
s.Start();
|
|
|
|
|
2013-05-10 16:20:45 +00:00
|
|
|
if (g_kernel == kCPU) Controller<OpenSubdiv::OsdCpuComputeController>::GetInstance()->Synchronize();
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENMP
|
|
|
|
else if (g_kernel == kOPENMP) Controller<OpenSubdiv::OsdOmpComputeController>::GetInstance()->Synchronize();
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
else if (g_kernel == kCL) Controller<OpenSubdiv::OsdCLComputeController>::GetInstance()->Synchronize();
|
|
|
|
#endif
|
2013-05-10 02:18:32 +00:00
|
|
|
#ifdef OPENSUBDIV_HAS_CUDA
|
2013-05-10 16:20:45 +00:00
|
|
|
else if (g_kernel == kCUDA) Controller<OpenSubdiv::OsdCudaComputeController>::GetInstance()->Synchronize();
|
2013-05-10 02:18:32 +00:00
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_GLSL_TRANSFORM_FEEDBACK
|
2013-05-10 16:20:45 +00:00
|
|
|
else if (g_kernel == kGLSL) Controller<OpenSubdiv::OsdGLSLTransformFeedbackComputeController>::GetInstance()->Synchronize();
|
2013-05-10 02:18:32 +00:00
|
|
|
#endif
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
s.Stop();
|
|
|
|
g_gpuTime = float(s.GetElapsed() * 1000.0f);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static OpenSubdiv::FarMesh<OpenSubdiv::OsdVertex> *
|
|
|
|
createFarMesh( const char * shape, int level, bool adaptive, Scheme scheme=kCatmark ) {
|
|
|
|
|
|
|
|
checkGLErrors("create osd enter");
|
|
|
|
// generate Hbr representation from "obj" description
|
|
|
|
std::vector<float> positions;
|
2013-07-02 18:18:06 +00:00
|
|
|
OsdHbrMesh * hmesh = simpleHbr<OpenSubdiv::OsdVertex>(shape, scheme, positions,
|
|
|
|
g_displayStyle == kFaceVaryingColor);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
size_t nModel = g_bboxes.size();
|
|
|
|
float x = nModel%g_modelCount - g_modelCount*0.5f;
|
|
|
|
float y = nModel/g_modelCount - g_modelCount*0.5f;
|
|
|
|
// align origins
|
|
|
|
Matrix matrix;
|
|
|
|
identity(matrix.value);
|
|
|
|
translate(matrix.value, 3*x, 3*y, 0);
|
|
|
|
g_transforms.push_back(matrix);
|
|
|
|
g_positions.push_back(positions);
|
|
|
|
|
|
|
|
OpenSubdiv::FarMeshFactory<OpenSubdiv::OsdVertex> meshFactory(hmesh, level, adaptive);
|
2013-05-10 02:18:32 +00:00
|
|
|
OpenSubdiv::FarMesh<OpenSubdiv::OsdVertex> *farMesh = meshFactory.Create(true);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
// Hbr mesh can be deleted
|
|
|
|
delete hmesh;
|
|
|
|
|
|
|
|
// compute model bounding (vertex animation isn't taken into account)
|
|
|
|
float min[4] = { FLT_MAX, FLT_MAX, FLT_MAX, 1};
|
|
|
|
float max[4] = {-FLT_MAX, -FLT_MAX, -FLT_MAX, 1};
|
|
|
|
for (size_t i=0; i <positions.size()/3; ++i) {
|
|
|
|
float v[4] = {positions[i*3], positions[i*3+1], positions[i*3+2], 1 };
|
|
|
|
for(int j=0; j<3; ++j) {
|
|
|
|
min[j] = std::min(min[j], v[j]);
|
|
|
|
max[j] = std::max(max[j], v[j]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_bboxes.push_back(BBox(min, max));
|
|
|
|
|
|
|
|
return farMesh;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-05-10 02:18:32 +00:00
|
|
|
rebuild()
|
2013-03-08 01:50:15 +00:00
|
|
|
{
|
|
|
|
g_positions.clear();
|
|
|
|
g_bboxes.clear();
|
|
|
|
g_transforms.clear();
|
|
|
|
|
|
|
|
// align scheme and adaptive to the first shape
|
|
|
|
int shape = g_currentShape;
|
|
|
|
Scheme scheme = g_defaultShapes[ shape ].scheme;
|
|
|
|
bool adaptive = scheme==kCatmark ? g_adaptive!=0 : false;
|
|
|
|
|
|
|
|
g_vertexOffsets.clear();
|
|
|
|
int vertexOffset = 0;
|
|
|
|
|
|
|
|
// prepare meshes
|
|
|
|
std::vector<OpenSubdiv::FarMesh<OpenSubdiv::OsdVertex> const *> farMeshes;
|
|
|
|
for (int i = 0; i < g_modelCount*g_modelCount; ++i) {
|
|
|
|
g_vertexOffsets.push_back(vertexOffset);
|
|
|
|
|
|
|
|
OpenSubdiv::FarMesh<OpenSubdiv::OsdVertex> *farMesh = createFarMesh(
|
2013-03-19 21:41:49 +00:00
|
|
|
g_defaultShapes[ shape ].data.c_str(), g_level, adaptive, scheme);
|
2013-03-08 01:50:15 +00:00
|
|
|
farMeshes.push_back(farMesh);
|
|
|
|
|
|
|
|
vertexOffset += farMesh->GetNumVertices();
|
|
|
|
shape++;
|
|
|
|
if (shape >= (int)g_defaultShapes.size()) shape = 0;
|
|
|
|
}
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
delete g_batch;
|
|
|
|
g_batch = NULL;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-07-02 18:18:06 +00:00
|
|
|
int numVertexElements = 3;
|
|
|
|
int numVaryingElements = g_displayStyle == kVaryingColor ? 3 : 0;
|
|
|
|
bool requireFVarData = (g_displayStyle == kFaceVaryingColor);
|
2013-05-22 00:20:22 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
// create multimesh batch
|
2013-05-10 16:20:45 +00:00
|
|
|
if (g_kernel == kCPU) {
|
2013-05-16 23:55:53 +00:00
|
|
|
g_batch = OpenSubdiv::OsdUtilMeshBatch<OpenSubdiv::OsdCpuGLVertexBuffer,
|
2013-05-17 00:48:06 +00:00
|
|
|
MyDrawContext, OpenSubdiv::OsdCpuComputeController>::Create(
|
2013-05-10 02:18:32 +00:00
|
|
|
Controller<OpenSubdiv::OsdCpuComputeController>::GetInstance(),
|
2013-07-02 18:18:06 +00:00
|
|
|
farMeshes, numVertexElements, numVaryingElements, 0, requireFVarData);
|
2013-05-10 16:20:45 +00:00
|
|
|
#ifdef OPENSUBDIV_HAS_OPENMP
|
|
|
|
} else if (g_kernel == kOPENMP) {
|
2013-05-16 23:55:53 +00:00
|
|
|
g_batch = OpenSubdiv::OsdUtilMeshBatch<OpenSubdiv::OsdCpuGLVertexBuffer,
|
2013-05-17 00:48:06 +00:00
|
|
|
MyDrawContext,
|
|
|
|
OpenSubdiv::OsdOmpComputeController>::Create(
|
2013-05-10 16:20:45 +00:00
|
|
|
Controller<OpenSubdiv::OsdOmpComputeController>::GetInstance(),
|
2013-07-02 18:18:06 +00:00
|
|
|
farMeshes, numVertexElements, numVaryingElements, 0, requireFVarData);
|
2013-05-10 16:20:45 +00:00
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
} else if (g_kernel == kCL) {
|
2013-05-16 23:55:53 +00:00
|
|
|
g_batch = OpenSubdiv::OsdUtilMeshBatch<OpenSubdiv::OsdCLGLVertexBuffer,
|
2013-05-17 00:48:06 +00:00
|
|
|
MyDrawContext, OpenSubdiv::OsdCLComputeController>::Create(
|
2013-05-10 16:20:45 +00:00
|
|
|
Controller<OpenSubdiv::OsdCLComputeController>::GetInstance(),
|
2013-07-02 18:18:06 +00:00
|
|
|
farMeshes, numVertexElements, numVaryingElements, 0, requireFVarData);
|
2013-05-10 16:20:45 +00:00
|
|
|
#endif
|
2013-05-10 02:18:32 +00:00
|
|
|
#ifdef OPENSUBDIV_HAS_CUDA
|
|
|
|
} else if (g_kernel == kCUDA) {
|
2013-05-16 23:55:53 +00:00
|
|
|
g_batch = OpenSubdiv::OsdUtilMeshBatch<OpenSubdiv::OsdCudaGLVertexBuffer,
|
2013-05-17 00:48:06 +00:00
|
|
|
MyDrawContext, OpenSubdiv::OsdCudaComputeController>::Create(
|
|
|
|
Controller<OpenSubdiv::OsdCudaComputeController>::GetInstance(),
|
2013-07-02 18:18:06 +00:00
|
|
|
farMeshes, numVertexElements, numVaryingElements, 0, requireFVarData);
|
2013-05-10 02:18:32 +00:00
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_GLSL_TRANSFORM_FEEDBACK
|
|
|
|
} else if (g_kernel == kGLSL) {
|
2013-05-17 00:48:06 +00:00
|
|
|
g_batch = OpenSubdiv::OsdUtilMeshBatch<OpenSubdiv::OsdGLVertexBuffer,
|
|
|
|
MyDrawContext, OpenSubdiv::OsdGLSLTransformFeedbackComputeController>::Create(
|
2013-05-10 02:18:32 +00:00
|
|
|
Controller<OpenSubdiv::OsdGLSLTransformFeedbackComputeController>::GetInstance(),
|
2013-07-02 18:18:06 +00:00
|
|
|
farMeshes, numVertexElements, numVaryingElements, 0, requireFVarData);
|
2013-08-29 00:15:42 +00:00
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_GLSL_COMPUTE
|
|
|
|
} else if (g_kernel == kGLSLCompute) {
|
|
|
|
g_batch = OpenSubdiv::OsdUtilMeshBatch<OpenSubdiv::OsdGLVertexBuffer,
|
|
|
|
MyDrawContext, OpenSubdiv::OsdGLSLComputeController>::Create(
|
|
|
|
Controller<OpenSubdiv::OsdGLSLComputeController>::GetInstance(),
|
|
|
|
farMeshes, numVertexElements, numVaryingElements, 0, requireFVarData);
|
2013-03-08 01:50:15 +00:00
|
|
|
#endif
|
|
|
|
} else {
|
2013-05-10 02:18:32 +00:00
|
|
|
assert(false);
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
assert(g_batch);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
g_scheme = scheme;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
for (size_t i = 0; i < farMeshes.size(); ++i) {
|
|
|
|
delete farMeshes[i];
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
g_tessLevelMin = 0;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
g_tessLevel = std::max(g_tessLevel,g_tessLevelMin);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
updateGeom(true);
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
2013-05-10 02:18:32 +00:00
|
|
|
static void
|
|
|
|
fitFrame() {
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
g_pan[0] = g_pan[1] = 0;
|
|
|
|
g_dolly = g_size;
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
display() {
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
// set effect
|
2013-07-02 18:18:06 +00:00
|
|
|
g_effect.displayStyle = g_displayStyle;
|
2013-05-14 17:48:48 +00:00
|
|
|
g_effect.screenSpaceTess = (g_screenSpaceTess != 0);
|
|
|
|
g_effect.displayPatchColor = (g_displayPatchColor != 0);
|
2013-05-10 02:18:32 +00:00
|
|
|
|
|
|
|
// prepare view matrix
|
|
|
|
float aspect = g_width/(float)g_height;
|
|
|
|
float modelview[16], projection[16];
|
|
|
|
identity(modelview);
|
|
|
|
translate(modelview, -g_pan[0], -g_pan[1], -g_dolly);
|
|
|
|
rotate(modelview, g_rotate[1], 1, 0, 0);
|
|
|
|
rotate(modelview, g_rotate[0], 0, 1, 0);
|
|
|
|
rotate(modelview, -90, 1, 0, 0);
|
|
|
|
translate(modelview, -g_center[0], -g_center[1], -g_center[2]);
|
|
|
|
perspective(projection, 45.0f, aspect, 0.01f, 500.0f);
|
|
|
|
|
|
|
|
g_effect.SetMatrix(modelview, projection);
|
|
|
|
g_effect.SetTessLevel((float)(1 << g_tessLevel));
|
|
|
|
g_effect.SetLighting();
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// prepare draw items
|
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
Stopwatch s;
|
|
|
|
s.Start();
|
|
|
|
|
2013-05-22 00:20:22 +00:00
|
|
|
OpenSubdiv::OsdUtilDrawItem<MyDrawDelegate::EffectHandle, MyDrawContext>::Collection items;
|
|
|
|
OpenSubdiv::OsdUtilDrawItem<MyDrawDelegate::EffectHandle, MyDrawContext>::Collection cachedDrawItems;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-16 23:55:53 +00:00
|
|
|
int numModels = g_modelCount*g_modelCount;
|
|
|
|
items.reserve(numModels);
|
|
|
|
|
|
|
|
for (int i = 0; i < numModels; ++i) {
|
2013-05-10 02:18:32 +00:00
|
|
|
// Here, client can pack arbitrary mesh and effect into drawItems.
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-22 00:20:22 +00:00
|
|
|
items.push_back(OpenSubdiv::OsdUtilDrawItem<MyDrawDelegate::EffectHandle, MyDrawContext>(
|
|
|
|
g_batch, &g_effect, g_batch->GetPatchArrays(i)));
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
if (g_batching) {
|
|
|
|
// create cached draw items
|
2013-05-22 00:20:22 +00:00
|
|
|
OpenSubdiv::OsdUtil::OptimizeDrawItem(items, cachedDrawItems, &g_drawDelegate);
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
s.Stop();
|
|
|
|
float prepCpuTime = float(s.GetElapsed() * 1000.0f);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
// -----------------------------------------------------------------------
|
|
|
|
// draw items
|
|
|
|
s.Start();
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
glViewport(0, 0, g_width, g_height);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
// primitive counting
|
|
|
|
glBeginQuery(GL_PRIMITIVES_GENERATED, g_queries[0]);
|
2013-05-17 22:53:36 +00:00
|
|
|
#if defined(GL_VERSION_3_3)
|
2013-05-10 02:18:32 +00:00
|
|
|
glBeginQuery(GL_TIME_ELAPSED, g_queries[1]);
|
2013-05-17 22:53:36 +00:00
|
|
|
#endif
|
2013-05-10 02:18:32 +00:00
|
|
|
g_drawDelegate.ResetNumDrawCalls();
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-07-02 18:18:06 +00:00
|
|
|
if (g_displayStyle == kWire) glDisable(GL_CULL_FACE);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
if (g_batching) {
|
|
|
|
OpenSubdiv::OsdUtil::DrawCollection(cachedDrawItems, &g_drawDelegate);
|
|
|
|
} else {
|
|
|
|
OpenSubdiv::OsdUtil::DrawCollection(items, &g_drawDelegate);
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
2013-07-02 18:18:06 +00:00
|
|
|
if (g_displayStyle == kWire) glEnable(GL_CULL_FACE);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
glEndQuery(GL_PRIMITIVES_GENERATED);
|
2013-05-17 22:53:36 +00:00
|
|
|
#if defined(GL_VERSION_3_3)
|
2013-05-10 02:18:32 +00:00
|
|
|
glEndQuery(GL_TIME_ELAPSED);
|
2013-05-17 22:53:36 +00:00
|
|
|
#endif
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
s.Stop();
|
|
|
|
float drawCpuTime = float(s.GetElapsed() * 1000.0f);
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
// -----------------------------------------------------------------------
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
GLuint numPrimsGenerated = 0;
|
|
|
|
GLuint timeElapsed = 0;
|
|
|
|
glGetQueryObjectuiv(g_queries[0], GL_QUERY_RESULT, &numPrimsGenerated);
|
2013-05-17 22:53:36 +00:00
|
|
|
#if defined(GL_VERSION_3_3)
|
2013-05-10 02:18:32 +00:00
|
|
|
glGetQueryObjectuiv(g_queries[1], GL_QUERY_RESULT, &timeElapsed);
|
2013-05-17 22:53:36 +00:00
|
|
|
#endif
|
2013-05-14 17:48:48 +00:00
|
|
|
float drawGpuTime = timeElapsed / 1000.0f / 1000.0f;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
if (g_hud.IsVisible()) {
|
|
|
|
g_fpsTimer.Stop();
|
|
|
|
g_totalTime += g_fpsTimer.GetElapsed();
|
|
|
|
double fps = 1.0/g_fpsTimer.GetElapsed();
|
|
|
|
g_fpsTimer.Start();
|
2013-05-10 02:18:32 +00:00
|
|
|
g_hud.DrawString(10, -200, "Draw Calls : %d", g_drawDelegate.GetNumDrawCalls());
|
2013-03-08 01:50:15 +00:00
|
|
|
g_hud.DrawString(10, -180, "Tess level : %d", g_tessLevel);
|
|
|
|
g_hud.DrawString(10, -160, "Primitives : %d", numPrimsGenerated);
|
2013-05-10 02:18:32 +00:00
|
|
|
g_hud.DrawString(10, -120, "GPU Kernel : %.3f ms", g_gpuTime);
|
|
|
|
g_hud.DrawString(10, -100, "CPU Kernel : %.3f ms", g_cpuTime);
|
|
|
|
g_hud.DrawString(10, -80, "GPU Draw : %.3f ms", drawGpuTime);
|
|
|
|
g_hud.DrawString(10, -60, "CPU Draw : %.3f ms", drawCpuTime);
|
|
|
|
g_hud.DrawString(10, -40, "CPU Prep : %.3f ms", prepCpuTime);
|
2013-03-08 01:50:15 +00:00
|
|
|
g_hud.DrawString(10, -20, "FPS : %3.1f", fps);
|
|
|
|
|
|
|
|
g_hud.Flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkGLErrors("display leave");
|
2013-05-10 16:20:45 +00:00
|
|
|
glFinish();
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
2013-04-19 02:15:30 +00:00
|
|
|
motion(GLFWwindow *, double dx, double dy) {
|
|
|
|
int x=(int)dx, y=(int)dy;
|
2013-03-08 01:50:15 +00:00
|
|
|
#else
|
|
|
|
motion(int x, int y) {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (g_mbutton[0] && !g_mbutton[1] && !g_mbutton[2]) {
|
|
|
|
// orbit
|
|
|
|
g_rotate[0] += x - g_prev_x;
|
|
|
|
g_rotate[1] += y - g_prev_y;
|
|
|
|
} else if (!g_mbutton[0] && !g_mbutton[1] && g_mbutton[2]) {
|
|
|
|
// pan
|
|
|
|
g_pan[0] -= g_dolly*(x - g_prev_x)/g_width;
|
|
|
|
g_pan[1] += g_dolly*(y - g_prev_y)/g_height;
|
|
|
|
} else if ((g_mbutton[0] && !g_mbutton[1] && g_mbutton[2]) or
|
|
|
|
(!g_mbutton[0] && g_mbutton[1] && !g_mbutton[2])) {
|
|
|
|
// dolly
|
|
|
|
g_dolly -= g_dolly*0.01f*(x - g_prev_x);
|
|
|
|
if(g_dolly <= 0.01) g_dolly = 0.01f;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_prev_x = x;
|
|
|
|
g_prev_y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
2013-06-20 18:53:33 +00:00
|
|
|
mouse(GLFWwindow *, int button, int state, int mods) {
|
2013-03-08 01:50:15 +00:00
|
|
|
#else
|
|
|
|
mouse(int button, int state) {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (button == 0 && state == GLFW_PRESS && g_hud.MouseClick(g_prev_x, g_prev_y))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (button < 3) {
|
|
|
|
g_mbutton[button] = (state == GLFW_PRESS);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
uninitGL() {
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
glDeleteQueries(2, g_queries);
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
delete g_batch;
|
|
|
|
g_batch = NULL;
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_CUDA
|
|
|
|
cudaDeviceReset();
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
uninitCL(g_clContext, g_clQueue);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
|
|
|
reshape(GLFWwindow *, int width, int height) {
|
|
|
|
#else
|
|
|
|
reshape(int width, int height) {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
g_width = width;
|
|
|
|
g_height = height;
|
|
|
|
|
|
|
|
g_hud.Rebuild(width, height);
|
|
|
|
}
|
|
|
|
|
2013-09-12 17:14:48 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
|
|
|
void windowClose(GLFWwindow*) {
|
|
|
|
g_running = false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
int windowClose() {
|
|
|
|
g_running = false;
|
|
|
|
return GL_TRUE;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-03-08 01:50:15 +00:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
2013-06-20 18:53:33 +00:00
|
|
|
keyboard(GLFWwindow *, int key, int scancode, int event, int mods) {
|
2013-03-08 01:50:15 +00:00
|
|
|
#else
|
2013-06-20 18:53:33 +00:00
|
|
|
#define GLFW_KEY_ESCAPE GLFW_KEY_ESC
|
2013-03-08 01:50:15 +00:00
|
|
|
keyboard(int key, int event) {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (event == GLFW_RELEASE) return;
|
|
|
|
if (g_hud.KeyDown(tolower(key))) return;
|
|
|
|
|
|
|
|
switch (key) {
|
|
|
|
case 'Q': g_running = 0; break;
|
|
|
|
case 'F': fitFrame(); break;
|
|
|
|
case '+':
|
2013-05-10 02:18:32 +00:00
|
|
|
case '=': g_tessLevel++; break;
|
|
|
|
case '-': g_tessLevel = std::max(g_tessLevelMin, g_tessLevel-1); break;
|
|
|
|
case '.': g_moveModels = std::max(g_moveModels*2, 1); break;
|
|
|
|
case ',': g_moveModels = std::max(g_moveModels/2, 0); break;
|
|
|
|
case 'I': g_modelCount = std::max(g_modelCount/2, 1); rebuild(); break;
|
|
|
|
case 'O': g_modelCount = std::min(g_modelCount*2, MAX_MODELS); rebuild(); break;
|
2013-06-20 18:53:33 +00:00
|
|
|
case GLFW_KEY_ESCAPE: g_hud.SetVisible(!g_hud.IsVisible()); break;
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
2013-07-02 18:18:06 +00:00
|
|
|
callbackDisplayStyle(int b)
|
2013-03-08 01:50:15 +00:00
|
|
|
{
|
2013-07-02 18:18:06 +00:00
|
|
|
if (g_displayStyle == kVaryingColor or b == kVaryingColor or
|
|
|
|
g_displayStyle == kFaceVaryingColor or b == kFaceVaryingColor) {
|
|
|
|
// need to rebuild for varying reconstruct
|
|
|
|
g_displayStyle = b;
|
|
|
|
rebuild();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
g_displayStyle = b;
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
callbackKernel(int k)
|
|
|
|
{
|
|
|
|
g_kernel = k;
|
|
|
|
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
if (g_kernel == kCL and g_clContext == NULL) {
|
|
|
|
if (initCL(&g_clContext, &g_clQueue) == false) {
|
|
|
|
printf("Error in initializing OpenCL\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_CUDA
|
|
|
|
if (g_kernel == kCUDA and g_cudaInitialized == false) {
|
|
|
|
g_cudaInitialized = true;
|
|
|
|
cudaGLSetGLDevice( cutGetMaxGflopsDeviceId() );
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
rebuild();
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
callbackLevel(int l)
|
|
|
|
{
|
|
|
|
g_level = l;
|
2013-05-10 02:18:32 +00:00
|
|
|
rebuild();
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
callbackModel(int m)
|
|
|
|
{
|
|
|
|
if (m < 0)
|
|
|
|
m = 0;
|
|
|
|
|
|
|
|
if (m >= (int)g_defaultShapes.size())
|
|
|
|
m = (int)g_defaultShapes.size() - 1;
|
|
|
|
|
|
|
|
g_currentShape = m;
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
rebuild();
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
callbackCheckBox(bool checked, int button)
|
|
|
|
{
|
|
|
|
switch(button) {
|
2013-05-10 02:18:32 +00:00
|
|
|
case HUD_CB_BATCHING:
|
|
|
|
g_batching = checked;
|
2013-03-08 01:50:15 +00:00
|
|
|
break;
|
|
|
|
case HUD_CB_ADAPTIVE:
|
|
|
|
if (OpenSubdiv::OsdGLDrawContext::SupportsAdaptiveTessellation()) {
|
|
|
|
g_adaptive = checked;
|
2013-05-10 02:18:32 +00:00
|
|
|
rebuild();
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case HUD_CB_DISPLAY_PATCH_COLOR:
|
|
|
|
g_displayPatchColor = checked;
|
|
|
|
break;
|
|
|
|
case HUD_CB_VIEW_LOD:
|
|
|
|
g_screenSpaceTess = checked;
|
|
|
|
break;
|
|
|
|
case HUD_CB_FREEZE:
|
|
|
|
g_freeze = checked;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
initHUD()
|
|
|
|
{
|
|
|
|
g_hud.Init(g_width, g_height);
|
|
|
|
|
|
|
|
g_hud.AddRadioButton(0, "CPU (K)", g_kernel == kCPU, 10, 10, callbackKernel, kCPU, 'k');
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENMP
|
|
|
|
g_hud.AddRadioButton(0, "OPENMP", g_kernel == kOPENMP, 10, 30, callbackKernel, kOPENMP, 'k');
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_GCD
|
|
|
|
g_hud.AddRadioButton(0, "GCD", g_kernel == kGCD, 10, 30, callbackKernel, kGCD, 'k');
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_CUDA
|
|
|
|
g_hud.AddRadioButton(0, "CUDA", g_kernel == kCUDA, 10, 50, callbackKernel, kCUDA, 'k');
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_OPENCL
|
|
|
|
g_hud.AddRadioButton(0, "OPENCL", g_kernel == kCL, 10, 70, callbackKernel, kCL, 'k');
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_GLSL_TRANSFORM_FEEDBACK
|
|
|
|
g_hud.AddRadioButton(0, "GLSL TransformFeedback", g_kernel == kGLSL, 10, 90, callbackKernel, kGLSL, 'k');
|
|
|
|
#endif
|
|
|
|
#ifdef OPENSUBDIV_HAS_GLSL_COMPUTE
|
|
|
|
// Must also check at run time for OpenGL 4.3
|
|
|
|
// if (GLEW_VERSION_4_3) {
|
|
|
|
g_hud.AddRadioButton(0, "GLSL Compute", g_kernel == kGLSLCompute, 10, 110, callbackKernel, kGLSLCompute, 'k');
|
|
|
|
// }
|
|
|
|
#endif
|
|
|
|
|
2013-07-02 18:18:06 +00:00
|
|
|
g_hud.AddRadioButton(1, "Wire (W)", g_displayStyle == kWire,
|
|
|
|
200, 10, callbackDisplayStyle, kWire, 'w');
|
|
|
|
g_hud.AddRadioButton(1, "Shaded", g_displayStyle == kShaded,
|
|
|
|
200, 30, callbackDisplayStyle, kShaded, 'w');
|
|
|
|
g_hud.AddRadioButton(1, "Wire+Shaded", g_displayStyle == kWireShaded,
|
|
|
|
200, 50, callbackDisplayStyle, kWireShaded, 'w');
|
|
|
|
g_hud.AddRadioButton(1, "Varying color", g_displayStyle == kVaryingColor,
|
|
|
|
200, 70, callbackDisplayStyle, kVaryingColor, 'w');
|
|
|
|
g_hud.AddRadioButton(1, "Face varying color", g_displayStyle == kFaceVaryingColor,
|
|
|
|
200, 90, callbackDisplayStyle, kFaceVaryingColor, 'w');
|
2013-03-08 01:50:15 +00:00
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
g_hud.AddCheckBox("Batching (B)", g_batching != 0, 350, 10, callbackCheckBox, HUD_CB_BATCHING, 'b');
|
2013-03-08 01:50:15 +00:00
|
|
|
g_hud.AddCheckBox("Patch Color (P)", true, 350, 50, callbackCheckBox, HUD_CB_DISPLAY_PATCH_COLOR, 'p');
|
|
|
|
g_hud.AddCheckBox("Screen space LOD (V)", g_screenSpaceTess != 0, 350, 70, callbackCheckBox, HUD_CB_VIEW_LOD, 'v');
|
|
|
|
g_hud.AddCheckBox("Freeze (spc)", false, 350, 90, callbackCheckBox, HUD_CB_FREEZE, ' ');
|
|
|
|
|
|
|
|
if (OpenSubdiv::OsdGLDrawContext::SupportsAdaptiveTessellation())
|
|
|
|
g_hud.AddCheckBox("Adaptive (`)", g_adaptive!=0, 10, 150, callbackCheckBox, HUD_CB_ADAPTIVE, '`');
|
|
|
|
|
|
|
|
for (int i = 1; i < 11; ++i) {
|
|
|
|
char level[16];
|
|
|
|
sprintf(level, "Lv. %d", i);
|
|
|
|
g_hud.AddRadioButton(3, level, i==g_level, 10, 170+i*20, callbackLevel, i, '0'+(i%10));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)g_defaultShapes.size(); ++i) {
|
|
|
|
g_hud.AddRadioButton(4, g_defaultShapes[i].name.c_str(), i==g_currentShape, -220, 10+i*16, callbackModel, i, 'n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
initGL()
|
|
|
|
{
|
|
|
|
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthFunc(GL_LEQUAL);
|
|
|
|
glCullFace(GL_BACK);
|
|
|
|
glEnable(GL_CULL_FACE);
|
|
|
|
|
2013-05-10 02:18:32 +00:00
|
|
|
glGenQueries(2, g_queries);
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
idle() {
|
|
|
|
|
|
|
|
if (not g_freeze) {
|
|
|
|
g_frame++;
|
2013-05-10 02:18:32 +00:00
|
|
|
updateGeom(false);
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (g_repeatCount != 0 and g_frame >= g_repeatCount)
|
|
|
|
g_running = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
callbackError(OpenSubdiv::OsdErrorType err, const char *message)
|
|
|
|
{
|
|
|
|
printf("OsdError: %d\n", err);
|
|
|
|
printf("%s", message);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
static void
|
|
|
|
setGLCoreProfile()
|
|
|
|
{
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
|
|
|
#define glfwOpenWindowHint glfwWindowHint
|
|
|
|
#define GLFW_OPENGL_VERSION_MAJOR GLFW_CONTEXT_VERSION_MAJOR
|
|
|
|
#define GLFW_OPENGL_VERSION_MINOR GLFW_CONTEXT_VERSION_MINOR
|
|
|
|
#endif
|
|
|
|
|
|
|
|
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
|
|
#if not defined(__APPLE__)
|
|
|
|
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 4);
|
|
|
|
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
|
|
|
|
#else
|
|
|
|
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
|
|
|
|
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
|
|
|
|
#endif
|
2013-09-12 17:14:48 +00:00
|
|
|
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
int main(int argc, char ** argv)
|
|
|
|
{
|
|
|
|
bool fullscreen = false;
|
|
|
|
std::string str;
|
|
|
|
for (int i = 1; i < argc; ++i) {
|
|
|
|
if (!strcmp(argv[i], "-d"))
|
|
|
|
g_level = atoi(argv[++i]);
|
|
|
|
else if (!strcmp(argv[i], "-c"))
|
|
|
|
g_repeatCount = atoi(argv[++i]);
|
|
|
|
else if (!strcmp(argv[i], "-f"))
|
|
|
|
fullscreen = true;
|
|
|
|
else {
|
|
|
|
std::ifstream ifs(argv[1]);
|
|
|
|
if (ifs) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << ifs.rdbuf();
|
|
|
|
ifs.close();
|
|
|
|
str = ss.str();
|
|
|
|
g_defaultShapes.push_back(SimpleShape(str.c_str(), argv[1], kCatmark));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
initializeShapes();
|
|
|
|
OsdSetErrorCallback(callbackError);
|
|
|
|
|
|
|
|
if (not glfwInit()) {
|
|
|
|
printf("Failed to initialize GLFW\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2013-07-05 02:17:34 +00:00
|
|
|
static const char windowTitle[] = "OpenSubdiv glBatchViewer";
|
2013-03-08 01:50:15 +00:00
|
|
|
|
|
|
|
#define CORE_PROFILE
|
|
|
|
#ifdef CORE_PROFILE
|
|
|
|
setGLCoreProfile();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
|
|
|
if (fullscreen) {
|
|
|
|
|
|
|
|
g_primary = glfwGetPrimaryMonitor();
|
|
|
|
|
|
|
|
// apparently glfwGetPrimaryMonitor fails under linux : if no primary,
|
|
|
|
// settle for the first one in the list
|
|
|
|
if (not g_primary) {
|
|
|
|
int count=0;
|
|
|
|
GLFWmonitor ** monitors = glfwGetMonitors(&count);
|
|
|
|
|
|
|
|
if (count)
|
|
|
|
g_primary = monitors[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (g_primary) {
|
2013-06-20 18:53:33 +00:00
|
|
|
GLFWvidmode const * vidmode = glfwGetVideoMode(g_primary);
|
|
|
|
g_width = vidmode->width;
|
|
|
|
g_height = vidmode->height;
|
2013-03-08 01:50:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not (g_window=glfwCreateWindow(g_width, g_height, windowTitle,
|
|
|
|
fullscreen and g_primary ? g_primary : NULL, NULL))) {
|
|
|
|
printf("Failed to open window.\n");
|
|
|
|
glfwTerminate();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
glfwMakeContextCurrent(g_window);
|
|
|
|
glfwSetKeyCallback(g_window, keyboard);
|
|
|
|
glfwSetCursorPosCallback(g_window, motion);
|
|
|
|
glfwSetMouseButtonCallback(g_window, mouse);
|
|
|
|
glfwSetWindowSizeCallback(g_window, reshape);
|
2013-09-12 17:14:48 +00:00
|
|
|
glfwSetWindowCloseCallback(g_window, windowClose);
|
2013-03-08 01:50:15 +00:00
|
|
|
#else
|
|
|
|
if (glfwOpenWindow(g_width, g_height, 8, 8, 8, 8, 24, 8,
|
|
|
|
fullscreen ? GLFW_FULLSCREEN : GLFW_WINDOW) == GL_FALSE) {
|
|
|
|
printf("Failed to open window.\n");
|
|
|
|
glfwTerminate();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
glfwSetWindowTitle(windowTitle);
|
|
|
|
glfwSetKeyCallback(keyboard);
|
|
|
|
glfwSetMousePosCallback(motion);
|
|
|
|
glfwSetMouseButtonCallback(mouse);
|
|
|
|
glfwSetWindowSizeCallback(reshape);
|
2013-09-19 19:08:12 +00:00
|
|
|
glfwSetWindowCloseCallback(windowClose);
|
2013-03-08 01:50:15 +00:00
|
|
|
#endif
|
|
|
|
|
2013-06-26 07:52:57 +00:00
|
|
|
#if defined(OSD_USES_GLEW)
|
2013-03-08 01:50:15 +00:00
|
|
|
#ifdef CORE_PROFILE
|
|
|
|
// this is the only way to initialize glew correctly under core profile context.
|
|
|
|
glewExperimental = true;
|
|
|
|
#endif
|
|
|
|
if (GLenum r = glewInit() != GLEW_OK) {
|
|
|
|
printf("Failed to initialize glew. Error = %s\n", glewGetErrorString(r));
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#ifdef CORE_PROFILE
|
|
|
|
// clear GL errors which was generated during glewInit()
|
|
|
|
glGetError();
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// activate feature adaptive tessellation if OSD supports it
|
|
|
|
g_adaptive = OpenSubdiv::OsdGLDrawContext::SupportsAdaptiveTessellation();
|
|
|
|
|
|
|
|
initGL();
|
|
|
|
|
|
|
|
glfwSwapInterval(0);
|
|
|
|
|
|
|
|
initHUD();
|
|
|
|
callbackModel(g_currentShape);
|
|
|
|
|
|
|
|
g_fpsTimer.Start();
|
|
|
|
while (g_running) {
|
|
|
|
idle();
|
|
|
|
display();
|
|
|
|
|
|
|
|
#if GLFW_VERSION_MAJOR>=3
|
|
|
|
glfwPollEvents();
|
|
|
|
glfwSwapBuffers(g_window);
|
|
|
|
#else
|
|
|
|
glfwSwapBuffers();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
uninitGL();
|
|
|
|
glfwTerminate();
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|