ofMatrix4x4 ParallelTransportFrames::normalMatrix() const { ofMatrix4x4 normalMatrix = ofMatrix4x4::getTransposedOf(const_cast<ofMatrix4x4&>(frames.back()).getInverse()); return ofMatrix4x4(normalMatrix(0, 0), normalMatrix(0, 1), normalMatrix(0, 2), 0.f, normalMatrix(1, 0), normalMatrix(1, 1), normalMatrix(1, 2), 0.f, normalMatrix(2, 0), normalMatrix(2, 1), normalMatrix(2, 2), 0.f, 0.f, 0.f, 0.f, 1.f); }
virtual void redisplay () { static GLuint buf [] = { 0, 0, 0, 0 }; static GLuint counters [4]; counterBuf.setData ( sizeof ( buf ), buf, GL_DYNAMIC_DRAW ); glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); mat4 mv = mat4 :: rotateZ ( toRadians ( rot.z ) ) * mat4 :: rotateY ( toRadians ( rot.y ) ) * mat4 :: rotateX ( toRadians ( rot.x ) ); mat3 nm = normalMatrix ( mv ); program.bind (); program.setUniformMatrix ( "mv", mv ); program.setUniformMatrix ( "nm", nm ); mesh -> render (); program.unbind (); glFinish (); counterBuf.getSubData ( 0, sizeof ( buf ), counters ); printf ( "%4d %4d %d %d\n", counters [0], counters [1], counters [2], counters [3] ); }
void Node::Render(GLint texLoc, GLint matLoc, GLint normalMatLoc, glm::mat4 viewMat) { transform_global = transform_local; if (m_Mesh != 0 && m_Texture != 0) { glm::mat3 normalMatrix(viewMat * transform_global); glm::inverseTranspose(normalMatrix); glUniformMatrix3fv(normalMatLoc, 1, GL_FALSE, glm::gtc::type_ptr::value_ptr(normalMatrix)); CheckOpenGLError("passing normalMat in root node"); glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::gtc::type_ptr::value_ptr(transform_global)); glUniform1i(texLoc, m_Texture->getUnit()); CheckOpenGLError("passing uniforms in root node"); glActiveTexture(GL_TEXTURE0 + m_Texture->getUnit()); glBindTexture(GL_TEXTURE_2D, m_Texture->getID()); glBindSampler(m_Texture->getUnit(), m_Texture->getSampler()); CheckOpenGLError("binding texture in root node"); m_Mesh->draw(); glBindTexture(GL_TEXTURE_2D, 0); } for (GLuint i = 0; i < m_children.size(); i++) { m_children[i]->Render(texLoc, matLoc, transform_global, normalMatLoc, viewMat); CheckOpenGLError("render children in node"); } }
Eigen::MatrixXd RMFE::getNormalMatrix() { Eigen::MatrixXd normalMatrix(3, this->cloud_filtered_normals->points.size()); for (int i = 0; i < this->cloud_filtered_normals->points.size(); i++) { normalMatrix.col(i) << this->cloud_filtered_normals->points[i].normal_x, this->cloud_filtered_normals->points[i].normal_y, this->cloud_filtered_normals->points[i].normal_z; } return normalMatrix; }
UtilityMath::S_Matrix3x3<T> UtilityMath::S_Matrix3x3<T>::s_CreateNormal(const UtilityMath::S_Matrix4x4<T>& rMatrix) { S_Matrix3x3 normalMatrix(s_ConvertMatrix4x4ToMatrix3x3(rMatrix)); normalMatrix.Invert(); normalMatrix.Transpose(); return normalMatrix; }
void Viewer::drawSphere(const QMatrix4x4& transform, bool picking) { auto r = sceneRoot->get_transform() * puppetRotation * sceneRoot->get_inverse(); auto modelMatrix = puppetPosition * r * transform; auto vp = getCameraMatrix(); auto mvMatrix = mTransformMatrix * modelMatrix; mSphereBufferObject.bind(); sphereShaders.setAttributeBuffer("vert", GL_FLOAT, 0, 3); mSphereNormalBuffer.bind(); sphereShaders.setAttributeBuffer("norm", GL_FLOAT, 0, 3); sphereShaders.setUniformValue(mvpMatrixLoc, vp * modelMatrix); sphereShaders.setUniformValue(mvMatrixLoc, mvMatrix); sphereShaders.setUniformValue(normMatrixLoc, mvMatrix.normalMatrix()); // Whether or not we are picking (to do lighting or not) sphereShaders.setUniformValue(flatLoc, picking); // Light position shouldn't change: always on the eye sphereShaders.setUniformValue(lightPositionLoc, mTransformMatrix * QVector3D()); glDrawArrays(GL_TRIANGLES, 0, numTriangles * 9); }
void Node::Render(GLint texLoc, GLint matLoc, glm::mat4 const &transform, GLint normalMatLoc, glm::mat4 viewMat) { transform_global = transform * transform_local; if (m_Mesh != 0 && m_Texture != 0) { glm::mat3 normalMatrix(viewMat * transform_global); glm::inverseTranspose(normalMatrix); glUniformMatrix3fv(normalMatLoc, 1, GL_FALSE, glm::gtc::type_ptr::value_ptr(normalMatrix)); CheckOpenGLError("passing normalMat in child node"); glUniformMatrix4fv(matLoc, 1, GL_FALSE, glm::value_ptr(transform_global)); glUniform1i(texLoc, m_Texture->getUnit()); CheckOpenGLError("passing uniform in child node"); glActiveTexture(GL_TEXTURE0 + m_Texture->getUnit()); glBindTexture(GL_TEXTURE_2D, m_Texture->getID()); m_Mesh->draw(); glBindTexture(GL_TEXTURE_2D, 0); } }
// Display function for GLUT void display() { glViewport(0,0,WIN_WIDTH,WIN_HEIGHT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // camera = crTrans * csTrans * ctTrans * camera; mat4 modelCam = camera * modelView; // Grab the normal matrix from the modelview matrix (upper 3x3 entries of // modelview). mat3 normalMatrix(modelCam); normalMatrix = inverse(normalMatrix); normalMatrix = transpose(normalMatrix); // Tell OpenGL which shader program we want to use. In this case, we are only // using one, but in general we might have many shader programs. glUseProgram(shader->program); // Pass the matrices and animation time to GPU glUniformMatrix4fv(shader->modelViewLoc, 1, GL_FALSE, value_ptr(modelCam)); glUniformMatrix4fv(shader->projectionLoc, 1, GL_FALSE, value_ptr(projection)); glUniformMatrix3fv(shader->normalMatrixLoc, 1, GL_FALSE, value_ptr(normalMatrix)); glUniform3fv(shader->lightPosLoc, 1, value_ptr(lightPos)); glUniform3fv(shader->viewPosLoc, 1, value_ptr(viewPos)); // Buffer vertex data glBindBuffer(GL_ARRAY_BUFFER, shader->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, verts.size() * sizeof(float), verts.data(), GL_DYNAMIC_DRAW); // Enable vertex array glEnableVertexAttribArray(shader->vertexLoc); glVertexAttribPointer(shader->vertexLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Buffer normal data glBindBuffer(GL_ARRAY_BUFFER, shader->normalBuffer); glBufferData(GL_ARRAY_BUFFER, norms.size() * sizeof(float), norms.data(), GL_DYNAMIC_DRAW); // Enable normal array glEnableVertexAttribArray(shader->normalLoc); glVertexAttribPointer(shader->normalLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Buffer color data glBindBuffer(GL_ARRAY_BUFFER, shader->colorBuffer); glBufferData(GL_ARRAY_BUFFER, color.size() * sizeof(float), color.data(), GL_DYNAMIC_DRAW); // Enable color array glEnableVertexAttribArray(shader->colorLoc); glVertexAttribPointer(shader->colorLoc, 4, GL_FLOAT, GL_FALSE, 0, NULL); //Bind element buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, shader->indexBuffer); for (int i = 0; i < (int) shapes.size(); i++) { glDrawArrays(GL_TRIANGLE_FAN, shapes[i]->startIndex / 3, shapes[i]->numVertices()); } //----------------------------DRAW ARROW----------------------------// if(ballMoving == false){ // Update arrow rotation arrow->rotate(launchAngle); modelCam = camera * arrow->getModelTransformMatrix(); //Pass the matrix glUniformMatrix4fv(shader->modelViewLoc, 1, GL_FALSE, value_ptr(modelCam)); // Buffer vertex data glBindBuffer(GL_ARRAY_BUFFER, shader->vertexBuffer); glBufferData(GL_ARRAY_BUFFER, arrow->getVerts().size() * sizeof(glm::vec3), arrow->getVerts().data(), GL_DYNAMIC_DRAW); // Enable vertex array glEnableVertexAttribArray(shader->vertexLoc); glVertexAttribPointer(shader->vertexLoc, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Disable normal array glDisableVertexAttribArray(shader->normalLoc); // Buffer color data glBindBuffer(GL_ARRAY_BUFFER, shader->colorBuffer); glBufferData(GL_ARRAY_BUFFER, arrow->getVertColors().size() * sizeof(glm::vec4), arrow->getVertColors().data(), GL_DYNAMIC_DRAW); // Enable color array glEnableVertexAttribArray(shader->colorLoc); glVertexAttribPointer(shader->colorLoc, 4, GL_FLOAT, GL_FALSE, 0, NULL); glDrawArrays(GL_LINES, 0, 6); } glutSwapBuffers(); }
ofVec3f ParallelTransportFrames::calcCurrentNormal() const { return getStartNormal() * normalMatrix(); }
//----------------------------------------------------------------------------- // set standard variables for shader void mgGL33Services::setShaderStdUniforms( mgShaderHandle shader) { mgMatrix4 mvpMatrix(m_worldMatrix); mvpMatrix.multiply(m_worldProjection); GLint index; GLfloat matrix[16]; CHECK_THREAD(); index = glGetUniformLocation(shader, "mgMVPMatrix"); if (index != -1) { matrix4toGL(mvpMatrix, matrix); glUniformMatrix4fv(index, 1, GL_FALSE, matrix); } index = glGetUniformLocation(shader, "mgMVMatrix"); if (index != -1) { matrix4toGL(m_worldMatrix, matrix); glUniformMatrix4fv(index, 1, GL_FALSE, matrix); } index = glGetUniformLocation(shader, "mgModelMatrix"); if (index != -1) { matrix4toGL(m_modelMatrix, matrix); glUniformMatrix4fv(index, 1, GL_FALSE, matrix); } index = glGetUniformLocation(shader, "mgNormalMatrix"); if (index != -1) { normalMatrix(m_worldMatrix, matrix); glUniformMatrix3fv(index, 1, GL_FALSE, matrix); } index = glGetUniformLocation(shader, "mgVPMatrix"); if (index != -1) { mgMatrix4 vpMatrix; vpMatrix.translate(-m_eyePt.x, -m_eyePt.y, -m_eyePt.z); vpMatrix.multiply(m_eyeMatrix); vpMatrix.multiply(m_worldProjection); matrix4toGL(vpMatrix, matrix); glUniformMatrix4fv(index, 1, GL_FALSE, matrix); } index = glGetUniformLocation(shader, "mgEyePt"); if (index != -1) { glUniform3f(index, (GLfloat) m_eyePt.x, (GLfloat) m_eyePt.y, (GLfloat) m_eyePt.z); } // transform the light dir by eye matrix. negate, since we want vector from point to light index = glGetUniformLocation(shader, "mgLightDir"); if (index != -1) { mgPoint3 eyeLightDir; m_eyeMatrix.mapPt(m_lightDir, eyeLightDir); glUniform3f(index, (GLfloat) eyeLightDir.x, (GLfloat) eyeLightDir.y, (GLfloat) eyeLightDir.z); } index = glGetUniformLocation(shader, "mgLightColor"); if (index != -1) { glUniform3f(index, (GLfloat) m_lightColor.x, (GLfloat) m_lightColor.y, (GLfloat) m_lightColor.z); } index = glGetUniformLocation(shader, "mgLightAmbient"); if (index != -1) { glUniform3f(index, (GLfloat) m_lightAmbient.x, (GLfloat) m_lightAmbient.y, (GLfloat) m_lightAmbient.z); } index = glGetUniformLocation(shader, "mgMatColor"); if (index != -1) { glUniform4f(index, (GLfloat) m_matColor.x, (GLfloat) m_matColor.y, (GLfloat) m_matColor.z, (GLfloat) m_matColor.w); } char varName[128]; for (int unit = 0; ; unit++) { sprintf(varName, "mgTextureUnit%d", unit); index = glGetUniformLocation(shader, varName); if (index != -1) glUniform1i(index, unit); else break; } // vars up to date m_updateShaderVars = false; }
void Earth::draw() const { Camera* cam = static_cast<Camera*>(getGame()->getObjectByName("cam")); // mat4f t = glm::scale(fullTransform, vec3f(radius)); // mat4f modelViewProjectionMatrix = cam->projection * cam->view * t; // mat4f modelViewMatrix = t; // mat4f normalMatrix( glm::transpose(glm::inverse(modelViewMatrix))); // vec3f lightPos = vec3f(0,0,0); // parentObject->pos; // Texture* earth_day = TextureManager::get("earth_daytime"); // earth_day->bind(); // sphere.program->uniform("sampler")->set(2); // Texture* earth_night = TextureManager::get("earth_nighttime"); // earth_night->bind(); // sphere.program->uniform("EarthNight")->set(3); // Texture* earth_cloud = TextureManager::get("earth_cloud"); // earth_cloud->bind(); // sphere.program->uniform("EarthCloudGloss")->set(4); // Texture* earth_specular_map = TextureManager::get("earth_specular"); // earth_specular_map->bind(); // sphere.program->uniform("EarthSpecularMap")->set(5); // sphere.program->uniform("LightPosition")->set(lightPos); // sphere.program->uniform("MVPMatrix")->set(modelViewProjectionMatrix); // sphere.program->uniform("MVMatrix")->set(modelViewMatrix); // sphere.program->uniform("NormalMatrix")->set(normalMatrix); mat4f projection = cam->projection; mat4f view = cam->view; mat4f model = glm::scale(fullTransform, getScale()); mat4f t = projection*view*model; mat4f normalMatrix( glm::transpose(glm::inverse(model))); vec3f lightPos = vec3f(0.0f);// - position; float shininess = 10.0f; vec3f emission = vec3f(0.0f); //vec3f specular = vec3f(0.5f); vec3f specular = vec3f(1.0f, 0.9255f, 0.698f)*0.3f; vec3f lightAmbient = vec3f(0.0f); vec3f lightDiffuse(1.0f); vec3f lightSpecular(1.0f); mat4f viewModel = glm::inverse(view*model); vec4f camPos = viewModel[3]; mat4f iModel = ( (glm::inverse(model))); vec3f cameraPos = vec3f(camPos); //(vec3f) (iModel*(vec4f(cam->getPosition(), 0)));//**** //vec3f(model*vec4f(cam->getPosition(),1.0));// float Kr = 0.0025f; float Km = 0.0010f; float ESun = 2.f; float fScale = 1.f/(outerRadius-innerRadius); float fScaleDepth = 0.25f; //Must be 25% float fCameraHeight = glm::length(cameraPos); float g = -0.750f; // Mie aerosol scattering constant float g2 = g*g; vec3f wavelength = vec3f(0.650f, 0.570f, 0.475f); vec3f v3InvWavelength = vec3f(1.0f / powf(wavelength.x, 4.0f), 1.0f / powf(wavelength.y, 4.0f), 1.0f / powf(wavelength.z, 4.0f)); vec3f lightPos_v2 = glm::normalize(mul4(iModel, -getPosition())); // vec3f lightPos = vec3f(0.f); sphere.program->uniform("v3CameraPos")->set(cameraPos); // The camera's current position sphere.program->uniform("v3LightPos")->set(lightPos_v2); // The direction vector to the light source sphere.program->uniform("v3InvWavelength")->set(v3InvWavelength); // 1 / pow(wavelength, 4) for the red, green, and blue channels // sphere.program->uniform("fCameraHeight")->set(fCameraHeight); // The camera's current height sphere.program->uniform("fCameraHeight2")->set(fCameraHeight*fCameraHeight); // fCameraHeight^2 sphere.program->uniform("fOuterRadius")->set(outerRadius); // The outer (spheresphere) radius sphere.program->uniform("fOuterRadius2")->set(outerRadius*outerRadius); // fOuterRadius^2 sphere.program->uniform("fInnerRadius")->set(innerRadius); // The inner (planetary) radius // sphere.program->uniform("fInnerRadius2")->set(innerRavec3f(0.0f);//******* //dius*innerRadius); // fInnerRadius^2 sphere.program->uniform("fKrESun")->set(Kr*ESun); // Kr * ESun sphere.program->uniform("fKmESun")->set(Km*ESun); // Kr * ESun sphere.program->uniform("fKr4PI")->set(Kr*4.f*PI); // Kr * 4 * PI sphere.program->uniform("fKm4PI")->set(Km*4.f*PI); // Km * 4 * PI sphere.program->uniform("fScale")->set(fScale); // 1 / (fOuterRadius - fInnerRadius) sphere.program->uniform("fScaleDepth")->set(fScaleDepth); // The scale depth (i.e. the altitude at which the spheresphere's average density is found) sphere.program->uniform("fScaleOverScaleDepth")->set(fScale / (fScaleDepth) ); // fScale / fScaleDepth sphere.program->uniform("lightPos")->set(lightPos); sphere.program->uniform("shininess")->set(shininess); sphere.program->uniform("emission")->set(emission); sphere.program->uniform("specular")->set(specular); sphere.program->uniform("lightAmbient")->set(lightAmbient); sphere.program->uniform("lightDiffuse")->set(lightDiffuse); //sphere.program->uniform("lightSpecular")->set(lightSpecular); Texture* tex; tex = Textures.get("earth"); tex->bind(); sphere.program->uniform("sampler")->set((int)tex->getSlot()); tex = Textures.get("earthNight"); tex->bind(); sphere.program->uniform("samplerNight")->set((int)tex->getSlot()); tex = Textures.get("earthWater"); tex->bind(); sphere.program->uniform("samplerWater")->set((int)tex->getSlot()); tex = Textures.get("earthWaterTex"); tex->bind(); sphere.program->uniform("samplerWaterTex")->set((int)tex->getSlot()); tex = Textures.get("earthNormal"); tex->bind(); sphere.program->uniform("samplerNormal")->set((int)tex->getSlot()); tex = Textures.get("earthClouds"); tex->bind(); sphere.program->uniform("samplerCloud")->set((int)tex->getSlot()); sphere.program->uniform("modelViewProjectionMatrix")->set(t); sphere.program->uniform("modelMatrix")->set(model); sphere.program->uniform("viewMatrix")->set(view); sphere.program->uniform("normalMatrix")->set(normalMatrix); sphere.program->uniform("globaltime")->set(time); //glDisable(GL_CULL_FACE); //glDepthMask(GL_FALSE); sphere.draw(); //glDepthMask(GL_TRUE); //glEnable(GL_CULL_FACE); }