void NGLScene::initializeGL() { // we need to initialise the NGL lib which will load all of the OpenGL functions, this must // be done once we have a valid GL context but before we call any GL commands. If we dont do // this everything will crash ngl::NGLInit::instance(); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // enable depth testing for drawing glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); // Now we will create a basic Camera from the graphics library // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(0,1,4); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); m_view=ngl::lookAt(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_project=ngl::perspective(45,1024.0f/720.0f,0.01f,150); // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib *shader=ngl::ShaderLib::instance(); shader->use("nglColourShader"); shader->setUniform("Colour",1.0f,1.0f,1.0f,1.0f); buildVAO(); glViewport(0,0,width(),height()); }
void NGLScene::initialize() { // we need to initialise the NGL lib which will load all of the OpenGL functions, this must // be done once we have a valid GL context but before we call any GL commands. If we dont do // this everything will crash ngl::NGLInit::instance(); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // enable depth testing for drawing glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); // Now we will create a basic Camera from the graphics library // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(0,1,4); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); m_cam= new ngl::Camera(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_cam->setShape(45,(float)720.0/576.0,0.001,150); // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib *shader=ngl::ShaderLib::instance(); // load a frag and vert shaders shader->createShaderProgram("ColourShader"); shader->attachShader("ColourVertex",ngl::VERTEX); shader->attachShader("ColourFragment",ngl::FRAGMENT); shader->loadShaderSource("ColourVertex","shaders/ColourVertex.glsl"); shader->loadShaderSource("ColourFragment","shaders/ColourFragment.glsl"); shader->compileShader("ColourVertex"); shader->compileShader("ColourFragment"); shader->attachShaderToProgram("ColourShader","ColourVertex"); shader->attachShaderToProgram("ColourShader","ColourFragment"); shader->linkProgramObject("ColourShader"); (*shader)["ColourShader"]->use(); buildVAO(); glViewport(0,0,width(),height()); }
CrossModel::CrossModel() : BaseModel() { ObjModelLoader crossObj = ObjModelLoader("thinLine", false); mPositions = crossObj.mPositions; mPositionArraySize = crossObj.mPositionArraySize; mPositionSize = crossObj.mPositionSize; mPositionType = crossObj.mPositionType; mNormals = crossObj.mNormals; mNormalArraySize = crossObj.mNormalArraySize; mNormalSize = crossObj.mNormalSize; mNormalType = crossObj.mNormalType; mNumVertices = crossObj.mNumVertices; buildVAO(); }
void MeshCommand::preBatchDraw() { // Set material GL::bindTexture2D(_textureID); GL::blendFunc(_blendType.src, _blendType.dst); if (Configuration::getInstance()->supportsShareableVAO() && _vao == 0) buildVAO(); if (_vao) { GL::bindVAO(_vao); } else { glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); _glProgramState->applyAttributes(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); } }
void MeshCommand::preBatchDraw() { // Do nothing if using material since each pass needs to bind its own VAO if (!_material) { if (Configuration::getInstance()->supportsShareableVAO() && _vao == 0) buildVAO(); if (_vao) { GL::bindVAO(_vao); } else { glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer); // FIXME: Assumes that all the passes in the Material share the same Vertex Attribs GLProgramState* programState = _material ? _material->_currentTechnique->_passes.at(0)->getGLProgramState() : _glProgramState; programState->applyAttributes(); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer); } } }
/*void VSSurfRevLib::computeVAO(int numP, float *p, float *points, int sides, float smoothCos) { // Compute and store vertices int numSides = sides; int numPoints = numP + 2; std::vector<float> vertex, normal, textco, tangent, bitangent; vertex.resize(numP * 2 * 4 * (numSides + 1)); normal.resize(numP * 2 * 3 * (numSides + 1)); textco.resize(numP * 2 * 2 * (numSides + 1)); tangent.resize(numP * 2 * 3 * (numSides + 1)); bitangent.resize(numP * 2 * 3 * (numSides + 1)); float inc = 2 * 3.14159f / (numSides); float nx,ny; float delta; int smooth; std::vector<int> smoothness; int k = 0; float y[3] = { 0.0f, 1.0f, 0.0f }; float z[3] = { 0.0f, 0.0f, 1.0f }; // init bounding box for(int i = 0; i < 3; ++i) { bb[1][i] = -FLT_MAX; bb[0][i] = FLT_MAX; } for(int i=0; i < numP; i++) { revSmoothNormal2(points+(i*2),&nx,&ny, smoothCos, 0); for(int j=0; j<=numSides;j++) { if ((i == 0 && p[0] == 0.0f) || ( i == numP-1 && p[(i+1)*2] == 0.0)) delta = inc * 0.5f; else delta = 0.0f; normal[((k)*(numSides+1) + j)*3] = nx * cosf(j*inc+delta); normal[((k)*(numSides+1) + j)*3+1] = ny; normal[((k)*(numSides+1) + j)*3+2] = nx * sinf(-j*inc+delta); vertex[((k)*(numSides+1) + j)*4] = p[i*2] * cosf(j*inc); vertex[((k)*(numSides+1) + j)*4+1] = p[(i*2)+1]; vertex[((k)*(numSides+1) + j)*4+2] = p[i*2] * sinf(-j*inc); vertex[((k)*(numSides+1) + j)*4+3] = 1.0f; textco[((k)*(numSides+1) + j)*2] = ((j+0.0f)/numSides); textco[((k)*(numSides+1) + j)*2+1] = (i+0.0f)/(numP-1); float t[3]; float *n = (float *)&(normal[((k)*(numSides + 1) + j) * 3]); if (fabs(VSMathLib::dotProduct(y, n)) > 0.98) { t[0] = sinf(-j*inc + delta); t[1] = 0; t[2] = -cosf(j*inc + delta); } else { VSMathLib::crossProduct(y, n, t); } tangent[((k)*(numSides + 1) + j) * 3] = t[0]; tangent[((k)*(numSides + 1) + j) * 3 + 1] = t[1]; tangent[((k)*(numSides + 1) + j) * 3 + 2] = t[2]; float bt[3]; VSMathLib::crossProduct(t, n, bt); bitangent[((k)*(numSides + 1) + j) * 3] = bt[0]; bitangent[((k)*(numSides + 1) + j) * 3 + 1] = bt[1]; bitangent[((k)*(numSides + 1) + j) * 3 + 2] = bt[2]; // find bounding box if (vertex[((k)*(numSides+1) + j)*4] < bb[0][0]) bb[0][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4] > bb[1][0]) bb[1][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4+1] < bb[0][1]) bb[0][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+1] > bb[1][1]) bb[1][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+2] < bb[0][2]) bb[0][2] = vertex[((k)*(numSides+1) + j)*4+2]; if (vertex[((k)*(numSides+1) + j)*4+2] > bb[1][2]) bb[1][2] = vertex[((k)*(numSides+1) + j)*4+2]; } k++; if (i < numP-1) { smooth = revSmoothNormal2(points+((i+1)*2),&nx,&ny, smoothCos, 1); if (!smooth) { smoothness.push_back(1); for(int j=0; j<=numSides;j++) { normal[((k)*(numSides+1) + j)*3] = nx * cosf(j*inc); normal[((k)*(numSides+1) + j)*3+1] = ny; normal[((k)*(numSides+1) + j)*3+2] = nx * sinf(-j*inc); vertex[((k)*(numSides+1) + j)*4] = p[(i+1)*2] * cosf(j*inc); vertex[((k)*(numSides+1) + j)*4+1] = p[((i+1)*2)+1]; vertex[((k)*(numSides+1) + j)*4+2] = p[(i+1)*2] * sinf(-j*inc); vertex[((k)*(numSides+1) + j)*4+3] = 1.0f; textco[((k)*(numSides+1) + j)*2] = ((j+0.0f)/numSides); textco[((k)*(numSides+1) + j)*2+1] = (i+1+0.0f)/(numP-1); float t[3]; float *n = (float *)&(normal[((k)*(numSides + 1) + j) * 3]); if (fabs(VSMathLib::dotProduct(y, n)) > 0.98) { t[0] = sinf(-j*inc + delta); t[1] = 0; t[2] = -cosf(j*inc + delta); } else { VSMathLib::crossProduct(y, n, t); } tangent[((k)*(numSides + 1) + j) * 3] = t[0]; tangent[((k)*(numSides + 1) + j) * 3 + 1] = t[1]; tangent[((k)*(numSides + 1) + j) * 3 + 2] = t[2]; float bt[3]; VSMathLib::crossProduct(t, n, bt); bitangent[((k)*(numSides + 1) + j) * 3] = bt[0]; bitangent[((k)*(numSides + 1) + j) * 3 + 1] = bt[1]; bitangent[((k)*(numSides + 1) + j) * 3 + 2] = bt[2]; // find bounding box if (vertex[((k)*(numSides+1) + j)*4] < bb[0][0]) bb[0][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4] > bb[1][0]) bb[1][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4+1] < bb[0][1]) bb[0][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+1] > bb[1][1]) bb[1][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+2] < bb[0][2]) bb[0][2] = vertex[((k)*(numSides+1) + j)*4+2]; if (vertex[((k)*(numSides+1) + j)*4+2] > bb[1][2]) bb[1][2] = vertex[((k)*(numSides+1) + j)*4+2]; } k++; } else smoothness.push_back(0); } } bbInit = true; std::vector<unsigned int> faceIndex; faceIndex.resize((numP - 1) * (numSides + 1) * 6); int count = 0; k = 0; for (int i = 0; i < numP-1; ++i) { for (int j = 0; j < numSides; ++j) { { faceIndex[count++] = (unsigned int)(k * (numSides + 1) + j); faceIndex[count++] = (unsigned int)((k + 1) * (numSides + 1) + j + 1); faceIndex[count++] = (unsigned int)((k + 1) * (numSides + 1) + j); } { faceIndex[count++] = (unsigned int)(k * (numSides + 1) + j); faceIndex[count++] = (unsigned int)(k * (numSides + 1) + j + 1); faceIndex[count++] = (unsigned int)((k + 1) * (numSides + 1) + j + 1); } } k++; k += smoothness[i]; } MyMesh aMesh; buildVAO(aMesh, (k + 1)*(numSides + 1), &(vertex[0]), &(normal[0]), &(textco[0]), &(tangent[0]), &(bitangent[0]), count, &(faceIndex[0])); aMesh.type = GL_TRIANGLES; aMesh.mat.ambient[0] = 0.2f; aMesh.mat.ambient[1] = 0.2f; aMesh.mat.ambient[2] = 0.2f; aMesh.mat.ambient[3] = 1.0f; aMesh.mat.diffuse[0] = 0.8f; aMesh.mat.diffuse[1] = 0.8f; aMesh.mat.diffuse[2] = 0.8f; aMesh.mat.diffuse[3] = 1.0f; aMesh.mat.specular[0] = 0.8f; aMesh.mat.specular[1] = 0.8f; aMesh.mat.specular[2] = 0.8f; aMesh.mat.specular[3] = 1.0f; aMesh.mat.shininess = 100.0f; aMesh.mat.texCount = 0; mVSML->loadIdentity(VSMathLib::AUX0); memcpy(aMesh.transform, mVSML->get(VSMathLib::AUX0), sizeof(float) * 16); mMyMeshes.push_back(aMesh); }*/ void VSSurfRevLib::computeVAO(int numP, float *p, float *points, int sides, float smoothCos) { // Compute and store vertices int numSides = sides; int numPoints = numP + 2; std::vector<float> vertex, normal, textco, tangent, bitangent; vertex.resize(numP * 2 * 4 * (numSides + 1)); normal.resize(numP * 2 * 3 * (numSides + 1)); textco.resize(numP * 2 * 2 * (numSides + 1)); tangent.resize(numP * 2 * 3 * (numSides + 1)); bitangent.resize(numP * 2 * 3 * (numSides + 1)); float inc = 2 * 3.14159f / (numSides); float nx,ny; float delta; int smooth; std::vector<int> smoothness; int k = 0; float y[3] = { 0.0f, 1.0f, 0.0f }; float z[3] = { 0.0f, 0.0f, 1.0f }; // init bounding box for(int i = 0; i < 3; ++i) { bb[1][i] = -FLT_MAX; bb[0][i] = FLT_MAX; } for(int i=0; i < numP; i++) { revSmoothNormal2(points+(i*2),&nx,&ny, smoothCos, 0); for(int j=0; j<=numSides;j++) { if ((i == 0 && p[0] == 0.0f) || ( i == numP-1 && p[(i+1)*2] == 0.0)) delta = inc * 0.5f; else delta = 0.0f; normal[((k)*(numSides+1) + j)*3] = nx * sinf(j*inc+delta); normal[((k)*(numSides+1) + j)*3+1] = ny; normal[((k)*(numSides+1) + j)*3+2] = nx * cosf(-j*inc+delta); vertex[((k)*(numSides+1) + j)*4] = p[i*2] * sinf(j*inc); vertex[((k)*(numSides+1) + j)*4+1] = p[(i*2)+1]; vertex[((k)*(numSides+1) + j)*4+2] = p[i*2] * cosf(-j*inc); vertex[((k)*(numSides+1) + j)*4+3] = 1.0f; textco[((k)*(numSides+1) + j)*2] = ((j+0.0f)/numSides); textco[((k)*(numSides+1) + j)*2+1] = (i+0.0f)/(numP-1); float t[3]; float *n = (float *)&(normal[((k)*(numSides + 1) + j) * 3]); if (fabs(VSMathLib::dotProduct(y, n)) > 0.98) { t[0] = cosf(-j*inc + delta); t[1] = 0; t[2] = -sinf(j*inc + delta); } else { VSMathLib::crossProduct(y, n, t); } tangent[((k)*(numSides + 1) + j) * 3] = t[0]; tangent[((k)*(numSides + 1) + j) * 3 + 1] = t[1]; tangent[((k)*(numSides + 1) + j) * 3 + 2] = t[2]; float bt[3]; VSMathLib::crossProduct(t, n, bt); bitangent[((k)*(numSides + 1) + j) * 3] = bt[0]; bitangent[((k)*(numSides + 1) + j) * 3 + 1] = bt[1]; bitangent[((k)*(numSides + 1) + j) * 3 + 2] = bt[2]; // find bounding box if (vertex[((k)*(numSides+1) + j)*4] < bb[0][0]) bb[0][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4] > bb[1][0]) bb[1][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4+1] < bb[0][1]) bb[0][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+1] > bb[1][1]) bb[1][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+2] < bb[0][2]) bb[0][2] = vertex[((k)*(numSides+1) + j)*4+2]; if (vertex[((k)*(numSides+1) + j)*4+2] > bb[1][2]) bb[1][2] = vertex[((k)*(numSides+1) + j)*4+2]; } k++; if (i < numP-1) { smooth = revSmoothNormal2(points+((i+1)*2),&nx,&ny, smoothCos, 1); if (!smooth) { smoothness.push_back(1); for(int j=0; j<=numSides;j++) { normal[((k)*(numSides+1) + j)*3] = nx * sinf(j*inc); normal[((k)*(numSides+1) + j)*3+1] = ny; normal[((k)*(numSides+1) + j)*3+2] = nx * cosf(-j*inc); vertex[((k)*(numSides+1) + j)*4] = p[(i+1)*2] * sinf(j*inc); vertex[((k)*(numSides+1) + j)*4+1] = p[((i+1)*2)+1]; vertex[((k)*(numSides+1) + j)*4+2] = p[(i+1)*2] * cosf(-j*inc); vertex[((k)*(numSides+1) + j)*4+3] = 1.0f; textco[((k)*(numSides+1) + j)*2] = ((j+0.0f)/numSides); textco[((k)*(numSides+1) + j)*2+1] = (i+1+0.0f)/(numP-1); float t[3]; float *n = (float *)&(normal[((k)*(numSides + 1) + j) * 3]); if (fabs(VSMathLib::dotProduct(y, n)) > 0.98) { t[0] = cos(-j*inc + delta); t[1] = 0; t[2] = -sinf(j*inc + delta); } else { VSMathLib::crossProduct(y, n, t); } tangent[((k)*(numSides + 1) + j) * 3] = t[0]; tangent[((k)*(numSides + 1) + j) * 3 + 1] = t[1]; tangent[((k)*(numSides + 1) + j) * 3 + 2] = t[2]; float bt[3]; VSMathLib::crossProduct(t, n, bt); bitangent[((k)*(numSides + 1) + j) * 3] = bt[0]; bitangent[((k)*(numSides + 1) + j) * 3 + 1] = bt[1]; bitangent[((k)*(numSides + 1) + j) * 3 + 2] = bt[2]; // find bounding box if (vertex[((k)*(numSides+1) + j)*4] < bb[0][0]) bb[0][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4] > bb[1][0]) bb[1][0] = vertex[((k)*(numSides+1) + j)*4]; if (vertex[((k)*(numSides+1) + j)*4+1] < bb[0][1]) bb[0][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+1] > bb[1][1]) bb[1][1] = vertex[((k)*(numSides+1) + j)*4+1]; if (vertex[((k)*(numSides+1) + j)*4+2] < bb[0][2]) bb[0][2] = vertex[((k)*(numSides+1) + j)*4+2]; if (vertex[((k)*(numSides+1) + j)*4+2] > bb[1][2]) bb[1][2] = vertex[((k)*(numSides+1) + j)*4+2]; } k++; } else smoothness.push_back(0); } } bbInit = true; std::vector<unsigned int> faceIndex; faceIndex.resize((numP - 1) * (numSides + 1) * 6); int count = 0; k = 0; for (int i = 0; i < numP-1; ++i) { for (int j = 0; j < numSides; ++j) { /*if (i != 0 || p[0] != 0.0)*/ { faceIndex[count++] = (unsigned int)(k * (numSides+1) + j); faceIndex[count++] = (unsigned int)((k+1) * (numSides+1) + j + 1); faceIndex[count++] = (unsigned int)((k+1) * (numSides+1) + j); } /*if (i != numP-2 || p[(numP-1)*2] != 0.0)*/ { faceIndex[count++] = (unsigned int)(k * (numSides+1) + j); faceIndex[count++] = (unsigned int)(k * (numSides+1) + j + 1); faceIndex[count++] = (unsigned int)((k+1) * (numSides+1) + j + 1); } } k++; k += smoothness[i]; } MyMesh aMesh; buildVAO(aMesh, (k + 1)*(numSides + 1), &(vertex[0]), &(normal[0]), &(textco[0]), &(tangent[0]), &(bitangent[0]), count, &(faceIndex[0])); aMesh.type = GL_TRIANGLES; aMesh.mat.ambient[0] = 0.2f; aMesh.mat.ambient[1] = 0.2f; aMesh.mat.ambient[2] = 0.2f; aMesh.mat.ambient[3] = 1.0f; aMesh.mat.diffuse[0] = 0.8f; aMesh.mat.diffuse[1] = 0.8f; aMesh.mat.diffuse[2] = 0.8f; aMesh.mat.diffuse[3] = 1.0f; aMesh.mat.specular[0] = 0.8f; aMesh.mat.specular[1] = 0.8f; aMesh.mat.specular[2] = 0.8f; aMesh.mat.specular[3] = 1.0f; aMesh.mat.shininess = 100.0f; aMesh.mat.texCount = 0; mVSML->loadIdentity(VSMathLib::AUX0); memcpy(aMesh.transform, mVSML->get(VSMathLib::AUX0), sizeof(float) * 16); mMyMeshes.push_back(aMesh); }
bool CSkin::Load() { bool bRet = false; BEATS_ASSERT(!IsLoaded(), _T("Can't Load a skin which is already loaded!")); // Load From File CSerializer serializer(_T("..\\Resource\\skin\\org.skin")); CSerializer tmpVerticesBufferPos, tmpVerticesBufferUV; CSerializer indexBuffer; size_t uVertexCount = 0; serializer >> uVertexCount; m_uVertexCount = uVertexCount; m_vertices = new CVertexPTB[uVertexCount]; float x, y, z; float u,v; for (size_t i = 0; i < uVertexCount; ++i) { ESkeletonBoneType bone, bone1, bone2, bone3; float weight, weight1, weight2,weight3; serializer >> x >> y >> z >> u >> v; serializer >> bone; serializer >> weight; serializer >> bone1; serializer >> weight1; serializer >> bone2; serializer >> weight2; serializer >> bone3; serializer >> weight3; CVertexPTB &vertex = m_vertices[i]; kmVec3Fill(&vertex.position,x,y,z); vertex.tex = CTex(u,v); vertex.bones = CIVector4(bone, bone1, bone2, bone3); kmVec4Fill(&vertex.weights,weight,weight1,weight2,weight3); #ifdef _DEBUG float sum = weight + weight1 + weight2+weight3; BEATS_ASSERT(sum < 1.01F, _T("Weight can't be greater than 1.01F, cur Value : %f!"), sum); BEATS_WARNING(sum > 0.99F, _T("Weight can't be smaller than 0.99F, cur Value : %f!"), sum); #endif } for (size_t i = 0; i < uVertexCount; ++i) { indexBuffer << (short)i; } CRenderer* pRenderer = CRenderer::GetInstance(); pRenderer->GenVertexArrays(1, &m_uVAO); pRenderer->GenBuffers(2, m_uVBO); #ifndef SW_SKEL_ANIM buildVBOVertex(m_vertices, m_uVertexCount*sizeof(CVertexPTB)); BEATS_SAFE_DELETE_ARRAY(m_vertices); #endif buildVBOIndex(indexBuffer.GetBuffer(), indexBuffer.GetWritePos()); buildVAO(); SetLoadedFlag(true); return bRet; }
void NGLScene::initializeGL() { // we need to initialise the NGL lib which will load all of the OpenGL functions, this must // be done once we have a valid GL context but before we call any GL commands. If we dont do // this everything will crash ngl::NGLInit::instance(); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // enable depth testing for drawing glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); // Now we will create a basic Camera from the graphics library // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(0,1,20); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); m_cam= new ngl::Camera(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_cam->setShape(45,(float)720.0/576.0,0.001,150); // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib *shader=ngl::ShaderLib::instance(); // load a frag and vert shaders // we are creating a shader called Phong shader->createShaderProgram("Phong"); // now we are going to create empty shaders for Frag and Vert shader->attachShader("PhongVertex",ngl::ShaderType::VERTEX); shader->attachShader("PhongFragment",ngl::ShaderType::FRAGMENT); // attach the source shader->loadShaderSource("PhongVertex","shaders/PhongVertex.glsl"); shader->loadShaderSource("PhongFragment","shaders/PhongFragment.glsl"); // compile the shaders shader->compileShader("PhongVertex"); shader->compileShader("PhongFragment"); // add them to the program shader->attachShaderToProgram("Phong","PhongVertex"); shader->attachShaderToProgram("Phong","PhongFragment"); // now we have associated this data we can link the shader shader->linkProgramObject("Phong"); // and make it active ready to load values (*shader)["Phong"]->use(); // the shader will use the currently active material and light0 so set them ngl::Material m(ngl::STDMAT::GOLD); // load our material values to the shader into the structure material (see Vertex shader) m.loadToShader("material"); shader->setShaderParam3f("viewerPos",m_cam->getEye().m_x,m_cam->getEye().m_y,m_cam->getEye().m_z); // now create our light this is done after the camera so we can pass the // transpose of the projection matrix to the light to do correct eye space // transformations ngl::Mat4 iv=m_cam->getViewMatrix(); iv.transpose(); iv=iv.inverse(); ngl::Light l(ngl::Vec3(0,1,0),ngl::Colour(1,1,1,1),ngl::Colour(1,1,1,1),ngl::LightModes::POINTLIGHT); l.setTransform(iv); // load these values to the shader as well l.loadToShader("light"); buildVAO(); buildVAO2(); glViewport(0,0,width(),height()); }
void NGLScene::initializeGL() { // we need to initialise the NGL lib which will load all of the OpenGL functions, this must // be done once we have a valid GL context but before we call any GL commands. If we dont do // this everything will crash ngl::NGLInit::instance(); glClearColor(0.4f, 0.4f, 0.4f, 1.0f); // Grey Background // enable depth testing for drawing glEnable(GL_DEPTH_TEST); glEnable(GL_MULTISAMPLE); // Now we will create a basic Camera from the graphics library // This is a static camera so it only needs to be set once // First create Values for the camera position ngl::Vec3 from(0,1,-3); ngl::Vec3 to(0,0,0); ngl::Vec3 up(0,1,0); m_view=ngl::lookAt(from,to,up); // set the shape using FOV 45 Aspect Ratio based on Width and Height // The final two are near and far clipping planes of 0.5 and 10 m_project=ngl::perspective(45,720.0f/576.0f,0.001f,150); // now to load the shader and set the values // grab an instance of shader manager ngl::ShaderLib* shader = ngl::ShaderLib::instance(); // we are creating a shader called Phong to save typos // in the code create some constexpr constexpr auto shaderProgram = "Phong"; constexpr auto vertexShader = "PhongVertex"; constexpr auto fragShader = "PhongFragment"; // create the shader program shader->createShaderProgram( shaderProgram ); // now we are going to create empty shaders for Frag and Vert shader->attachShader( vertexShader, ngl::ShaderType::VERTEX ); shader->attachShader( fragShader, ngl::ShaderType::FRAGMENT ); // attach the source shader->loadShaderSource( vertexShader, "shaders/PhongVertex.glsl" ); shader->loadShaderSource( fragShader, "shaders/PhongFragment.glsl" ); // compile the shaders shader->compileShader( vertexShader ); shader->compileShader( fragShader ); // add them to the program shader->attachShaderToProgram( shaderProgram, vertexShader ); shader->attachShaderToProgram( shaderProgram, fragShader ); // now we have associated that data we can link the shader shader->linkProgramObject( shaderProgram ); // and make it active ready to load values ( *shader )[ shaderProgram ]->use(); ngl::Vec4 lightPos(-2.0f,5.0f,2.0f,0.0f); shader->setUniform("light.position",lightPos); shader->setUniform("light.ambient",0.0f,0.0f,0.0f,1.0f); shader->setUniform("light.diffuse",1.0f,1.0f,1.0f,1.0f); shader->setUniform("light.specular",0.8f,0.8f,0.8f,1.0f); // gold like phong material shader->setUniform("material.ambient",0.274725f,0.1995f,0.0745f,0.0f); shader->setUniform("material.diffuse",0.75164f,0.60648f,0.22648f,0.0f); shader->setUniform("material.specular",0.628281f,0.555802f,0.3666065f,0.0f); shader->setUniform("material.shininess",51.2f); shader->setUniform("viewerPos",from); buildVAO(); ngl::VAOFactory::listCreators(); }