TeapotTessellation
::TeapotTessellation(QObject *parent
) : AbstractScene(parent),
mTessellationShader(0),
mTeapotPatch(0),
mMaxPatchVertices(0),
mInnerTessellationFactor(1.0f),
mOuterTessellationFactor(1.0f),
mZoom(22.0f),
mInitialized(false)
{
}
TeapotTessellation::~TeapotTessellation()
{
if(mTessellationShader)
{
mTessellationShader->DeleteShaderProgram();
delete mTessellationShader;
mTessellationShader = 0;
}
if(mTeapotPatch)
{
delete mTeapotPatch;
mTeapotPatch = 0;
}
}
void TeapotTessellation::initialise()
{
//initialize the opengl functions
initializeOpenGLFunctions();
loadShaders();
//initialize the teapot patch
mTeapotPatch = new TeapotVboPatch(mTessellationShader);
//set the clearing color for the background
glClearColor(0.5f,0.5f,0.5f,1.0f);
glGetIntegerv(GL_MAX_PATCH_VERTICES,&mMaxPatchVertices);
ModelViewMatrix = glm::mat4(1.0f);
ProjectionMatrix = glm::mat4(1.0f);
ViewportMatrix = glm::mat4(1.0f);
ViewMatrix = glm::mat4(1.0f);
ModelMatrix = glm::mat4(1.0f);
NormalMatrix = glm::mat3(1.0f);
//enable depth testing
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
mInitialized = true;
}
void TeapotTessellation::loadShaders()
{
mTessellationShader = new GLSLShader();
mTessellationShader->LoadFromFile(GL_VERTEX_SHADER,"shaders/teapottessellation.vert");
mTessellationShader->LoadFromFile(GL_TESS_CONTROL_SHADER,"shaders/teapottessellation.tcs");
mTessellationShader->LoadFromFile(GL_TESS_EVALUATION_SHADER,"shaders/teapottessellation.tes");
mTessellationShader->LoadFromFile(GL_GEOMETRY_SHADER,"shaders/teapottessellation.geom");
mTessellationShader->LoadFromFile(GL_FRAGMENT_SHADER,"shaders/teapottessellation.frag");
mTessellationShader->CreateAndLinkProgram();
mTessellationShader->Use();
mTessellationShader->AddUniform("inner");
mTessellationShader->AddUniform("outer");
mTessellationShader->AddUniform("ModelviewMatrix");
mTessellationShader->AddUniform("ProjectionMatrix");
mTessellationShader->AddUniform("NormalMatrix");
mTessellationShader->AddUniform("ViewportMatrix");
mTessellationShader->AddUniform("Line.Width");
mTessellationShader->AddUniform("Line.Color");
mTessellationShader->AddUniform("Light.Position");
mTessellationShader->AddUniform("Light.Intensity");
mTessellationShader->AddUniform("Material.Kd");
mTessellationShader->AddUniform("Material.Ka");
mTessellationShader->AddUniform("Material.Ks");
mTessellationShader->AddUniform("Material.Shininess");
//send the width of the line
glUniform1f((*mTessellationShader)("Line.Width"),0.8f);
//define the color of the line
glm::vec4 lineColor = glm::vec4(0.05f,0.0f,0.05f,1.0f);
//send the line color to the shader
glUniform4fv((*mTessellationShader)("Line.Color"),1,glm::value_ptr(lineColor));
//define the light position
glm::vec4 lightPosition = glm::vec4(0.25f,0.25f,1.0f,1.0f);
//send the light position to the shader
glUniform4fv((*mTessellationShader)("Light.Position"),1,glm::value_ptr(lightPosition));
//define the light intensity
glm::vec3 lightIntensity = glm::vec3(1.f,1.f,1.f);
//send the light intensity to the shader
glUniform3fv((*mTessellationShader)("Light.Intensity"),1,glm::value_ptr(lightIntensity));
//now set and send the material property
glm::vec3 materialAmbientReflectivity = glm::vec3(0.2f,0.2f,0.2f);
glUniform3fv((*mTessellationShader)("Material.Ka"),1,glm::value_ptr(materialAmbientReflectivity));
glm::vec3 materialDiffuseReflectivity = glm::vec3(0.7f,0.7f,0.7f);
glUniform3fv((*mTessellationShader)("Material.Kd"),1,glm::value_ptr(materialDiffuseReflectivity));
glm::vec3 materialSpecularReflectivity = glm::vec3(0.8f,0.8f,0.8f);
glUniform3fv((*mTessellationShader)("Material.Ks"),1,glm::value_ptr(materialSpecularReflectivity));
float materialShininess = 100.0f;
glUniform1f((*mTessellationShader)("Material.Shininess"),materialShininess);
mTessellationShader->UnUse();
}
void TeapotTessellation::update(float t)
{
mTime = t;
}
void TeapotTessellation::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 Ty = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,1.5f,0.0f));
glm::mat4 Tz = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,0.0f,-mZoom));
glm::mat4 Ry = glm::rotate(glm::mat4(1.0),mTime,glm::vec3(0.0f,1.0f,0.0f));
glm::mat4 Rx = glm::rotate(glm::mat4(1.0f),20.0f,glm::vec3(1.0f,0.0f,0.0f));
ModelMatrix = Tz * Ty * Ry * Rx;
ModelViewMatrix = ViewMatrix * ModelMatrix;
NormalMatrix = glm::inverseTranspose(glm::mat3(ModelViewMatrix));
mTessellationShader->Use();
glUniformMatrix4fv((*mTessellationShader)("ModelviewMatrix"),1,GL_FALSE,glm::value_ptr(ModelViewMatrix));
glUniformMatrix4fv((*mTessellationShader)("ProjectionMatrix"),1,GL_FALSE,glm::value_ptr(ProjectionMatrix));
glUniformMatrix4fv((*mTessellationShader)("ViewportMatrix"),1,GL_FALSE,glm::value_ptr(ViewportMatrix));
glUniformMatrix3fv((*mTessellationShader)("NormalMatrix"),1,GL_FALSE,glm::value_ptr(NormalMatrix));
glUniform1f((*mTessellationShader)("outer"),mOuterTessellationFactor);
glUniform1f((*mTessellationShader)("inner"),mInnerTessellationFactor);
mTeapotPatch->render();
mTessellationShader->UnUse();
}
void TeapotTessellation::resize(int w,int h)
{
if(h < 1)
mViewportHeight = 1;
mViewportWidth = w;
//set the viewport matrix
ViewportMatrix = glm::mat4(glm::vec4((GLfloat)mViewportWidth/2.0f,0.0f,0.0f,0.0f),
glm::vec4(0.0f,(GLfloat)mViewportHeight/2.0f,0.0f,0.0f),
glm::vec4(0.0f,0.0f,1.0f,0.0f),
glm::vec4((GLfloat)mViewportWidth/2.0f,(GLfloat)mViewportHeight/2.0f,0.0f,1.0f));
GLfloat aspect = w/(GLfloat)h;
//setup the projection matrix
ProjectionMatrix = glm::perspective(50.0f,aspect,0.1f,1000.0f);
glViewport(0,0,mViewportWidth,mViewportHeight);
}
void TeapotTessellation::setViewportSize(int width,int height)
{
mViewportWidth = width;
mViewportHeight = height;
}
void TeapotTessellation::paint()
{
if(!mInitialized)
initialise();
resize(mViewportWidth,mViewportHeight);
render();
}
TeapotTessellation::TeapotTessellation(QObject *parent)
: AbstractScene(parent),
mTessellationShader(0),
mTeapotPatch(0),
mMaxPatchVertices(0),
mInnerTessellationFactor(1.0f),
mOuterTessellationFactor(1.0f),
mZoom(22.0f),
mInitialized(false)
{
}
TeapotTessellation::~TeapotTessellation()
{
if(mTessellationShader)
{
mTessellationShader->DeleteShaderProgram();
delete mTessellationShader;
mTessellationShader = 0;
}
if(mTeapotPatch)
{
delete mTeapotPatch;
mTeapotPatch = 0;
}
}
void TeapotTessellation::initialise()
{
//initialize the opengl functions
initializeOpenGLFunctions();
loadShaders();
//initialize the teapot patch
mTeapotPatch = new TeapotVboPatch(mTessellationShader);
//set the clearing color for the background
glClearColor(0.5f,0.5f,0.5f,1.0f);
glGetIntegerv(GL_MAX_PATCH_VERTICES,&mMaxPatchVertices);
ModelViewMatrix = glm::mat4(1.0f);
ProjectionMatrix = glm::mat4(1.0f);
ViewportMatrix = glm::mat4(1.0f);
ViewMatrix = glm::mat4(1.0f);
ModelMatrix = glm::mat4(1.0f);
NormalMatrix = glm::mat3(1.0f);
//enable depth testing
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
mInitialized = true;
}
void TeapotTessellation::loadShaders()
{
mTessellationShader = new GLSLShader();
mTessellationShader->LoadFromFile(GL_VERTEX_SHADER,"shaders/teapottessellation.vert");
mTessellationShader->LoadFromFile(GL_TESS_CONTROL_SHADER,"shaders/teapottessellation.tcs");
mTessellationShader->LoadFromFile(GL_TESS_EVALUATION_SHADER,"shaders/teapottessellation.tes");
mTessellationShader->LoadFromFile(GL_GEOMETRY_SHADER,"shaders/teapottessellation.geom");
mTessellationShader->LoadFromFile(GL_FRAGMENT_SHADER,"shaders/teapottessellation.frag");
mTessellationShader->CreateAndLinkProgram();
mTessellationShader->Use();
mTessellationShader->AddUniform("inner");
mTessellationShader->AddUniform("outer");
mTessellationShader->AddUniform("ModelviewMatrix");
mTessellationShader->AddUniform("ProjectionMatrix");
mTessellationShader->AddUniform("NormalMatrix");
mTessellationShader->AddUniform("ViewportMatrix");
mTessellationShader->AddUniform("Line.Width");
mTessellationShader->AddUniform("Line.Color");
mTessellationShader->AddUniform("Light.Position");
mTessellationShader->AddUniform("Light.Intensity");
mTessellationShader->AddUniform("Material.Kd");
mTessellationShader->AddUniform("Material.Ka");
mTessellationShader->AddUniform("Material.Ks");
mTessellationShader->AddUniform("Material.Shininess");
//send the width of the line
glUniform1f((*mTessellationShader)("Line.Width"),0.8f);
//define the color of the line
glm::vec4 lineColor = glm::vec4(0.05f,0.0f,0.05f,1.0f);
//send the line color to the shader
glUniform4fv((*mTessellationShader)("Line.Color"),1,glm::value_ptr(lineColor));
//define the light position
glm::vec4 lightPosition = glm::vec4(0.25f,0.25f,1.0f,1.0f);
//send the light position to the shader
glUniform4fv((*mTessellationShader)("Light.Position"),1,glm::value_ptr(lightPosition));
//define the light intensity
glm::vec3 lightIntensity = glm::vec3(1.f,1.f,1.f);
//send the light intensity to the shader
glUniform3fv((*mTessellationShader)("Light.Intensity"),1,glm::value_ptr(lightIntensity));
//now set and send the material property
glm::vec3 materialAmbientReflectivity = glm::vec3(0.2f,0.2f,0.2f);
glUniform3fv((*mTessellationShader)("Material.Ka"),1,glm::value_ptr(materialAmbientReflectivity));
glm::vec3 materialDiffuseReflectivity = glm::vec3(0.7f,0.7f,0.7f);
glUniform3fv((*mTessellationShader)("Material.Kd"),1,glm::value_ptr(materialDiffuseReflectivity));
glm::vec3 materialSpecularReflectivity = glm::vec3(0.8f,0.8f,0.8f);
glUniform3fv((*mTessellationShader)("Material.Ks"),1,glm::value_ptr(materialSpecularReflectivity));
float materialShininess = 100.0f;
glUniform1f((*mTessellationShader)("Material.Shininess"),materialShininess);
mTessellationShader->UnUse();
}
void TeapotTessellation::update(float t)
{
mTime = t;
}
void TeapotTessellation::render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glm::mat4 Ty = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,1.5f,0.0f));
glm::mat4 Tz = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f,0.0f,-mZoom));
glm::mat4 Ry = glm::rotate(glm::mat4(1.0),mTime,glm::vec3(0.0f,1.0f,0.0f));
glm::mat4 Rx = glm::rotate(glm::mat4(1.0f),20.0f,glm::vec3(1.0f,0.0f,0.0f));
ModelMatrix = Tz * Ty * Ry * Rx;
ModelViewMatrix = ViewMatrix * ModelMatrix;
NormalMatrix = glm::inverseTranspose(glm::mat3(ModelViewMatrix));
mTessellationShader->Use();
glUniformMatrix4fv((*mTessellationShader)("ModelviewMatrix"),1,GL_FALSE,glm::value_ptr(ModelViewMatrix));
glUniformMatrix4fv((*mTessellationShader)("ProjectionMatrix"),1,GL_FALSE,glm::value_ptr(ProjectionMatrix));
glUniformMatrix4fv((*mTessellationShader)("ViewportMatrix"),1,GL_FALSE,glm::value_ptr(ViewportMatrix));
glUniformMatrix3fv((*mTessellationShader)("NormalMatrix"),1,GL_FALSE,glm::value_ptr(NormalMatrix));
glUniform1f((*mTessellationShader)("outer"),mOuterTessellationFactor);
glUniform1f((*mTessellationShader)("inner"),mInnerTessellationFactor);
mTeapotPatch->render();
mTessellationShader->UnUse();
}
void TeapotTessellation::resize(int w,int h)
{
if(h < 1)
mViewportHeight = 1;
mViewportWidth = w;
//set the viewport matrix
ViewportMatrix = glm::mat4(glm::vec4((GLfloat)mViewportWidth/2.0f,0.0f,0.0f,0.0f),
glm::vec4(0.0f,(GLfloat)mViewportHeight/2.0f,0.0f,0.0f),
glm::vec4(0.0f,0.0f,1.0f,0.0f),
glm::vec4((GLfloat)mViewportWidth/2.0f,(GLfloat)mViewportHeight/2.0f,0.0f,1.0f));
GLfloat aspect = w/(GLfloat)h;
//setup the projection matrix
ProjectionMatrix = glm::perspective(50.0f,aspect,0.1f,1000.0f);
glViewport(0,0,mViewportWidth,mViewportHeight);
}
void TeapotTessellation::setViewportSize(int width,int height)
{
mViewportWidth = width;
mViewportHeight = height;
}
void TeapotTessellation::paint()
{
if(!mInitialized)
initialise();
resize(mViewportWidth,mViewportHeight);
render();
}
To copy to clipboard, switch view to plain text mode
Bookmarks