OpenSubdiv/examples/glBatchViewer/effect.cpp

154 lines
5.0 KiB
C++
Raw Normal View History

2013-05-10 02:18:32 +00:00
//
// Copyright 2013 Pixar
2013-05-10 02:18:32 +00:00
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
2013-05-10 02:18:32 +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 to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
2013-05-10 02:18:32 +00:00
//
// You may obtain a copy of the Apache License at
2013-05-10 02:18:32 +00:00
//
// http://www.apache.org/licenses/LICENSE-2.0
2013-07-18 21:19:50 +00:00
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
2013-05-10 02:18:32 +00:00
//
#include "effect.h"
2013-05-10 02:18:32 +00:00
#include "../common/simple_math.h"
#include "../common/patchColors.h"
2013-05-10 02:18:32 +00:00
#include <osd/opengl.h>
2013-05-10 02:18:32 +00:00
#include <stdio.h>
#include <string.h>
GLuint g_transformUB = 0,
g_tessellationUB = 0,
g_lightingUB = 0;
GLuint g_transformBinding = 0;
GLuint g_tessellationBinding = 1;
GLuint g_lightingBinding = 3;
void
MyEffect::SetMatrix(const float *modelview, const float *projection) {
float mvp[16];
multMatrix(mvp, modelview, projection);
struct Transform {
float ModelViewMatrix[16];
float ProjectionMatrix[16];
float ModelViewProjectionMatrix[16];
} transformData;
memcpy(transformData.ModelViewMatrix, modelview, sizeof(float)*16);
memcpy(transformData.ProjectionMatrix, projection, sizeof(float)*16);
memcpy(transformData.ModelViewProjectionMatrix, mvp, sizeof(float)*16);
// set transform
if (! g_transformUB) {
glGenBuffers(1, &g_transformUB);
glBindBuffer(GL_UNIFORM_BUFFER, g_transformUB);
glBufferData(GL_UNIFORM_BUFFER,
sizeof(transformData), NULL, GL_STATIC_DRAW);
};
glBindBuffer(GL_UNIFORM_BUFFER, g_transformUB);
glBufferSubData(GL_UNIFORM_BUFFER,
0, sizeof(transformData), &transformData);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, g_transformBinding, g_transformUB);
}
void
MyEffect::SetTessLevel(float tessLevel) {
struct Tessellation {
float TessLevel;
} tessellationData;
tessellationData.TessLevel = tessLevel;
if (! g_tessellationUB) {
glGenBuffers(1, &g_tessellationUB);
glBindBuffer(GL_UNIFORM_BUFFER, g_tessellationUB);
glBufferData(GL_UNIFORM_BUFFER,
sizeof(tessellationData), NULL, GL_STATIC_DRAW);
};
glBindBuffer(GL_UNIFORM_BUFFER, g_tessellationUB);
glBufferSubData(GL_UNIFORM_BUFFER,
0, sizeof(tessellationData), &tessellationData);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, g_tessellationBinding, g_tessellationUB);
}
void
MyEffect::SetLighting() {
struct Lighting {
struct Light {
float position[4];
float ambient[4];
float diffuse[4];
float specular[4];
} lightSource[2];
} lightingData = {
{{ { 0.5, 0.2f, 1.0f, 0.0f },
{ 0.1f, 0.1f, 0.1f, 1.0f },
{ 0.7f, 0.7f, 0.7f, 1.0f },
{ 0.8f, 0.8f, 0.8f, 1.0f } },
{ { -0.8f, 0.4f, -1.0f, 0.0f },
{ 0.0f, 0.0f, 0.0f, 1.0f },
{ 0.5f, 0.5f, 0.5f, 1.0f },
{ 0.8f, 0.8f, 0.8f, 1.0f } }}
};
if (! g_lightingUB) {
glGenBuffers(1, &g_lightingUB);
glBindBuffer(GL_UNIFORM_BUFFER, g_lightingUB);
glBufferData(GL_UNIFORM_BUFFER,
sizeof(lightingData), NULL, GL_STATIC_DRAW);
};
glBindBuffer(GL_UNIFORM_BUFFER, g_lightingUB);
glBufferSubData(GL_UNIFORM_BUFFER,
0, sizeof(lightingData), &lightingData);
glBindBuffer(GL_UNIFORM_BUFFER, 0);
glBindBufferBase(GL_UNIFORM_BUFFER, g_lightingBinding, g_lightingUB);
}
void
MyEffect::BindDrawConfig(MyDrawConfig *config, OpenSubdiv::OsdDrawContext::PatchDescriptor desc) {
// bind uniforms
2013-05-17 22:53:36 +00:00
// currently, these are used only in conjunction with tessellation shaders
#if defined(GL_EXT_direct_state_access) || defined(GL_VERSION_4_1)
2013-05-10 02:18:32 +00:00
GLint program = config->program;
GLint diffuseColor = config->diffuseColorUniform;
if (displayPatchColor) {
if (desc.GetType() == OpenSubdiv::FarPatchTables::QUADS or
desc.GetType() == OpenSubdiv::FarPatchTables::TRIANGLES) {
glProgramUniform4f(program, diffuseColor, 0.4f, 0.4f, 0.8f, 1);
} else {
const float *color = getAdaptivePatchColor( desc );
glProgramUniform4f(program, diffuseColor, color[0], color[1], color[2], color[3]);
}
2013-05-10 02:18:32 +00:00
}
2013-05-17 22:53:36 +00:00
#endif
2013-05-10 02:18:32 +00:00
}