Commit fe2ba10b authored by tanselhalic's avatar tanselhalic
Browse files

Shader code is modified. Viewer is modified to add support for the shaders.

parent 3412b24c
......@@ -32,6 +32,48 @@
#include "smGeometry/smMeshModel.h"
#include "smRenderDelegates/smConfig.h"
class CubeShader :public smShader{
protected:
smFloat lightPower;
smGLInt lightPowerGLBinding;
smFloat roughness;
smGLInt roughnessGLBinding;
public:
CubeShader(std::shared_ptr<smErrorLog> logger) :smShader(logger)
{
createParam("textureDecal");
createParam("textureBump");
createParam("lightPower");
createParam("roughness");
lightPower = 2.0;
roughness = 160.0;
this->checkShaderUpdate(3000);
}
void initDraw(){
smShader::initDraw();
this->print();
lightPowerGLBinding = getFragmentShaderParam("lightPower");
roughnessGLBinding = getFragmentShaderParam("roughness");
}
void predraw(std::shared_ptr<smMesh> p_mesh){
this->checkShaderUpdate(3000);
smShader::predraw(p_mesh);
glUniform1fARB(lightPowerGLBinding, lightPower);
glUniform1fARB(roughnessGLBinding, roughness);
}
};
int main()
{
SIMMEDTK_REGISTER_RENDER_DELEGATES();
......@@ -41,9 +83,19 @@ int main()
std::shared_ptr<smLight> light;
std::shared_ptr<smCamera> sceneCamera;
std::shared_ptr<smStaticSceneObject> cube;
std::shared_ptr<smStaticSceneObject> plane;
std::shared_ptr<smtk::Examples::Common::wasdCameraController> camCtl;
std::shared_ptr<smtk::Examples::Common::KeyPressSDKShutdown> keyShutdown;
std::shared_ptr<smtk::Examples::Common::pzrMouseCameraController> pzrCamCtl;
std::shared_ptr<CubeShader> shader;
std::shared_ptr<smErrorLog> errorLog;
//Shader creation
errorLog = std::make_shared<smErrorLog>();
shader = std::make_shared<CubeShader>(errorLog);
shader->registerShader();
shader->setShaderFileName("Shaders/wet_vert.glsl","", "Shaders/wet_frag.glsl");
//Create an instance of the SimMedTK framework/SDK
sdk = smSDK::getInstance();
......@@ -52,24 +104,54 @@ int main()
//Create a viewer to see the scene through
viewer = std::make_shared<smViewer>();
//viewer->viewerRenderDetail |= SIMMEDTK_VIEWERRENDER_GLOBAL_AXIS;
viewer->setGlobalAxisLength(10.0);
sdk->addViewer(viewer);
//Create the camera controller
camCtl = std::make_shared<smtk::Examples::Common::wasdCameraController>();
keyShutdown = std::make_shared<smtk::Examples::Common::KeyPressSDKShutdown>();
pzrCamCtl = std::make_shared<smtk::Examples::Common::pzrMouseCameraController>();
smTextureManager::init(sdk->getErrorLog());
smTextureManager::loadTexture("textures/metalbump.bmp", "cubeBump");
auto cubeModel = std::make_shared<smMeshModel>();
cubeModel->load("models/cube.obj", "textures/cube.png", "cubetex");
auto renderDetail = std::make_shared<smRenderDetail>(SIMMEDTK_RENDER_FACES | SIMMEDTK_RENDER_TEXTURE);
// cubeModel->load("models/blade.3ds", "textures/metal1.bmp", "cubetex");
cubeModel->load("models/brain.3ds", "textures/brainx.bmp", "cubetex");
cubeModel->getMesh()->updateVertexNormals();
auto renderDetail = std::make_shared<smRenderDetail>(SIMMEDTK_RENDER_FACES | SIMMEDTK_RENDER_TEXTURE );
renderDetail->addShader(shader->getUniqueId());
shader->attachTexture(cubeModel->getMesh()->getUniqueId(), "cubetex", "textureDecal");
shader->attachTexture(cubeModel->getMesh()->getUniqueId(), "cubeBump", "textureBump");
cubeModel->setRenderDetail(renderDetail);
cubeModel->getMesh()->calcNeighborsVertices();
cubeModel->getMesh()->calcTriangleTangents();
auto planeModel = std::make_shared<smMeshModel>();
planeModel->load("models/plane.3ds", "textures/brain_outside.bmp", "planetex");
planeModel->getMesh()->updateVertexNormals();
auto renderDetailPlane = std::make_shared<smRenderDetail>(SIMMEDTK_RENDER_FACES | SIMMEDTK_RENDER_TEXTURE);
renderDetailPlane->addShader(shader->getUniqueId());
shader->attachTexture(planeModel->getMesh()->getUniqueId(), "planetex", "textureDecal");
shader->attachTexture(planeModel->getMesh()->getUniqueId(), "cubeBump", "textureBump");
planeModel->setRenderDetail(renderDetailPlane);
planeModel->getMesh()->calcNeighborsVertices();
planeModel->getMesh()->calcTriangleTangents();
cube = std::make_shared<smStaticSceneObject>();
cube->setModel(cubeModel);
plane = std::make_shared<smStaticSceneObject>();
plane->setModel(planeModel);
//Add the cube to the scene to be rendered
scene1->addSceneObject(cube);
scene1->addSceneObject(plane);
//Register the scene with the viewer, and setup render target
viewer->registerScene(scene1, SMRENDERTARGET_SCREEN, "");
......@@ -78,13 +160,14 @@ int main()
viewer->setWindowTitle("SimMedTK RENDER TEST");
//Set some viewer properties
viewer->setScreenResolution(800, 640);
viewer->setScreenResolution(1200, 768);
//Uncomment the following line for fullscreen
//viewer->viewerRenderDetail |= SIMMEDTK_VIEWERRENDER_FULLSCREEN;
viewer->viewerRenderDetail |= SIMMEDTK_VIEWERRENDER_GLOBAL_AXIS;// SIMMEDTK_VIEWERRENDER_FULLSCREEN;
// Setup Scene lighting
light = smLight::getDefaultLighting();
light->lightPos.setPosition(smVec3d(0, 0, 100));
assert(light);
scene1->addLight(light);
......
......@@ -197,7 +197,7 @@ int main()
viewer->attachEvent(smtk::Event::EventType::Keyboard, keyShutdown);
viewer->attachEvent(smtk::Event::EventType::MouseMove, pzrCamCtl);
viewer->attachEvent(smtk::Event::EventType::MouseButton, pzrCamCtl);
//-------------------------------------------------------
// Run the SDK
//-------------------------------------------------------
......
......@@ -87,7 +87,7 @@ enum smVBOResult
#define SIMMEDTK_RENDER_NONE (1<<31)
/// \brief type definitions for variable viewerRenderDetail in smViewer
#define SIMMEDTK_VIEWERRENDER_GLOBALAXIS (1<<1)
//#define SIMMEDTK_VIEWERRENDER_GLOBALAXIS (1<<1)
#define SIMMEDTK_VIEWERRENDER_TEXT (1<<2)
#define SIMMEDTK_VIEWERRENDER_FADEBACKGROUND (1<<3)
#define SIMMEDTK_VIEWERRENDER_FADEBACKGROUNDIMAGE (1<<4)
......
......@@ -49,7 +49,9 @@ public:
/// \brief draws surface mesh with given rendertdail and draw paramters
static void drawSurfaceMeshTriangles(std::shared_ptr<smMesh> p_surfaceMesh, std::shared_ptr<smRenderDetail> renderDetail);
/// \brief draws surface mesh with given shader in the rendertdail and draw paramters
static void drawSurfaceMeshTrianglesWithShaders(std::shared_ptr<smUnifiedId>p_shader, std::shared_ptr<smMesh> p_surfaceMesh, std::shared_ptr<smRenderDetail> renderDetail);
/// \brief draw normals
static void drawNormals(std::shared_ptr<smMesh> p_mesh, smColor p_color = smColor::colorBlue, smFloat length=1.0);
......
......@@ -26,6 +26,7 @@
#include "smRendering/smGLRenderer.h"
#include "smRendering/smViewer.h"
#include "smCore/smFactory.h"
#include <iostream>
smBaseMesh::smBaseMesh()
{
......@@ -170,16 +171,17 @@ void smMesh::calcTriangleTangents()
{
for (smInt v = 0; v < nbrVertices; v++)
{
vertTangents[v][0] = vertTangents[v][1] = vertTangents[v][2] = 0;
vertTangents[v][0] = vertTangents[v][1] = vertTangents[v][2] = 0.0;
for (size_t i = 0; i < vertTriNeighbors[v].size(); i++)
{
vertTangents[v] += triTangents[vertTriNeighbors[v][i]];
}
vertTangents[v].normalized();
//vertTangents[v].normalized();
vertTangents[v] = (vertTangents[v] - vertNormals[v] * vertNormals[v].dot(vertTangents[v]));
vertTangents[v].normalized();
vertTangents[v].normalize();
//std::cout << vertTangents[v]<< std::endl;
}
}
}
......@@ -248,7 +250,7 @@ void smMesh::updateVertexNormals()
}
vertNormals[i] = temp;
vertNormals[i].normalized();
vertNormals[i].normalize();
temp = smVec3d::Zero();
}
}
......
......@@ -171,13 +171,33 @@ void smGLRenderer::drawLineMesh(std::shared_ptr<smLineMesh> p_lineMesh, std::sha
void smGLRenderer::drawSurfaceMeshTriangles(
std::shared_ptr<smMesh> p_surfaceMesh,
std::shared_ptr<smRenderDetail> renderDetail)
std::shared_ptr<smRenderDetail> renderDetail)
{
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_NONE)
{
return;
}
//check if there is shader attached to the mesh. If there is, render with the shader. Otherwise, go ahead with the regular rendering
for (smInt i = 0; i < renderDetail->shaders.size(); i++){
if( renderDetail->shaderEnable[i])
{
smGLRenderer::drawSurfaceMeshTrianglesWithShaders(renderDetail->shaders[i], p_surfaceMesh, renderDetail);
return;
}
}
//render the mesh with using VAO.
for (smInt i = 0; i < renderDetail->VAOs.size(); i++){
if (renderDetail->VAOs[i])
{
smGLRenderer::drawSurfaceMeshTrianglesWithShaders(renderDetail->shaders[i], p_surfaceMesh, renderDetail);
}
}
glDisable(GL_TEXTURE_2D);
glPointSize(renderDetail->getPointSize());
glLineWidth(renderDetail->getLineSize());
......@@ -297,6 +317,134 @@ void smGLRenderer::drawSurfaceMeshTriangles(
glLineWidth(1.0);
}
void smGLRenderer::drawSurfaceMeshTrianglesWithShaders(std::shared_ptr<smUnifiedId> p_shader, std::shared_ptr<smMesh> p_surfaceMesh, std::shared_ptr<smRenderDetail> renderDetail){
std::shared_ptr<smShader> shader = smShader::getShader(p_shader);
shader->enableShader();
shader->predraw(p_surfaceMesh);
glDisable(GL_TEXTURE_2D);
glPointSize(renderDetail->getPointSize());
glLineWidth(renderDetail->getLineSize());
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_TRANSPARENT)
{
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_DOUBLE, 0, p_surfaceMesh->vertices.data());
glEnableClientState(GL_NORMAL_ARRAY);
glNormalPointer(GL_DOUBLE, 0, p_surfaceMesh->vertNormals);
if (p_surfaceMesh->vertTangents != NULL&&p_surfaceMesh->tangentChannel){
glVertexAttribPointerARB(shader->getTangentAttributes(), 3, GL_DOUBLE, GL_FALSE, 0, p_surfaceMesh->vertTangents);
glEnableVertexAttribArrayARB(shader->getTangentAttributes());
}
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_TEXTURE)
{
if (p_surfaceMesh->getRenderDelegate()->isTargetTextured())
{
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, smGLRealType, 0, p_surfaceMesh->texCoord);
shader->activeGLTextures(p_surfaceMesh->getUniqueId());
/*for (size_t t = 0; t < p_surfaceMesh->textureIds.size(); t++)
{
glActiveTexture(GL_TEXTURE0 + t);
smTextureManager::activateTexture(p_shader->textureIds[t].textureId);
}*/
}
}
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_COLORMAP)
{
glEnableClientState(GL_COLOR_ARRAY);
}
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_FACES)
{
glDrawElements(GL_TRIANGLES, p_surfaceMesh->nbrTriangles * 3, smGLUIntType, p_surfaceMesh->triangles);
}
if ((p_surfaceMesh->getRenderDetail()->getRenderType() & (SIMMEDTK_RENDER_VERTICES)))
{
glPolygonMode(GL_FRONT_AND_BACK, GL_POINT);
glDisable(GL_LIGHTING);
glColor3fv(renderDetail->getVertexColor().toGLColor());
glDrawElements(GL_TRIANGLES, p_surfaceMesh->nbrTriangles * 3, smGLUIntType, p_surfaceMesh->triangles);
glEnable(GL_LIGHTING);
//default rendering
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_WIREFRAME)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glLineWidth(renderDetail->getLineSize() + 0.5);
glPolygonOffset(1.0, 1.0);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);
glColor4fv(renderDetail->getWireFrameColor().toGLColor());
glDrawElements(GL_TRIANGLES, p_surfaceMesh->nbrTriangles * 3, smGLUIntType, p_surfaceMesh->triangles);
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glLineWidth(renderDetail->getLineSize());
//default rendering
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
}
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_HIGHLIGHTVERTICES)
{
glDisable(GL_LIGHTING);
glColor3fv(renderDetail->getHighLightColor().toGLColor());
glDrawArrays(GL_POINTS, 0, p_surfaceMesh->nbrVertices);
glEnable(GL_LIGHTING);
}
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_TRANSPARENT)
{
glDisable(GL_BLEND);
}
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_TEXTURE)
{
if (p_surfaceMesh->getRenderDelegate()->isTargetTextured())
{
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
/*for (size_t t = 0; t < p_surfaceMesh->textureIds.size(); t++)
{
glActiveTexture(GL_TEXTURE0 + t);
smTextureManager::disableTexture(p_surfaceMesh->textureIds[t].textureId);
}*/
}
}
if (p_surfaceMesh->getRenderDetail()->getRenderType() & SIMMEDTK_RENDER_COLORMAP)
{
glDisableClientState(GL_COLOR_ARRAY);
}
glEnable(GL_LIGHTING);
glPointSize(1.0);
glLineWidth(1.0);
shader->posdraw(p_surfaceMesh);
shader->disableShader();
}
void smGLRenderer::drawNormals(std::shared_ptr<smMesh> p_mesh, smColor p_color, smFloat length)
{
......@@ -526,7 +674,7 @@ void smGLRenderer::draw(smPlane &p_plane, smFloat p_scale, smColor p_color)
smVec3d point = p_plane.getPoint();
angle = std::acos(defaultDir.dot(normal));
axisOfRot = normal.cross(defaultDir);
axisOfRot.normalized();
axisOfRot.normalize();
smQuaterniond rot = getRotationQuaternion(-angle,axisOfRot);
......
......@@ -58,6 +58,8 @@ smShader::smShader(std::shared_ptr<smErrorLog> logger)
checkErrorEnabled = true;
setModelViewMatrixShaderName("ModelMatrix");
setProjectionMatrixShaderName("ProjectionMatrix");
///add attribute pointer for the shader
createAttrib("vertTangents");
currentShaderEnabled = false;
time.start();
......@@ -208,7 +210,7 @@ smBool smShader::initShaders(const smString& p_vertexProgFileName,
printInfoLog(shaderProgramObject);
modelViewMatrix = glGetUniformLocation(shaderProgramObject, modelViewMatrixName.data());
projectionMatrix = glGetUniformLocation(shaderProgramObject, projectionMatrixName.data());
std::cout << "[InitShaders] Shaders-> Vertex:" << p_vertexProgFileName << " Geometry:" << p_geometryProgFileName <<" Fragment:"<< p_fragmentProgFileName << " " << std::endl;
return true;
}
......@@ -642,7 +644,7 @@ smBool smShader::attachTexture(std::shared_ptr<smUnifiedId> p_meshID,
{
smTextureShaderAssignment assign;
//std::cout << p_textureName << std::endl;
if (smTextureManager::findTextureId(p_textureName, assign.textureId) == SIMMEDTK_TEXTURE_NOTFOUND)
{
std::cout << "texture " << p_textureName << " is not found in shader:" << p_textureShaderName << " for mesh id:" << p_meshID->getId() << "\n";
......@@ -786,6 +788,8 @@ void smShader::getAttribAndParamLocations()
{
param = glGetAttribLocation(shaderProgramObject, attribParamsString[i].data());
attribShaderParams.push_back(param);
std::cout << "[smShader::glGetAttribLocation] " << attribParamsString[i] << " " << param << "\n";
}
}
......
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