Geometry* Geometry::Teapot(uint tess) { Neumont::ShapeData geoData = Neumont::ShapeGenerator::makeTeapot(tess, mat4()); return setupPreset(geoData); }
// This returns the camera's view matrix that is used to pass to our shaders mat4 Camera::GetViewMatrix() { // Return a view matrix by multiplying our rotation matrix by the inverse of a translation matrix return GetRotationMatrix() * inverse(translate(mat4(), Position)); }
void Terrain::makeGridUnderModel(Model m) { if(m.getMeshName()[0]!='S'&&m.getMeshName()[1]!='P'&&m.getMeshName()[2]!='A') { //4 is an offset, if the buldings doesnt fit , increase 4 int minX = (this->worldClickX-4)*(this->gridMap.GetWidth()/this->width); int minY = (this->worldClickZ-4)*(this->gridMap.GetHeight()/this->height); int maxX=(this->worldClickX+4)*(this->gridMap.GetWidth()/this->width); int maxY=(this->worldClickZ+4)*(this->gridMap.GetHeight()/this->height); if(maxY>this->gridMap.GetHeight()) maxY=this->gridMap.GetHeight(); if(maxX>this->gridMap.GetWidth()) maxX=this->gridMap.GetHeight(); if(minY<2) minY=1; if(minY%2==0) minY+=1; if(maxY%2==0) maxY-=1; if(minX<2) minX=1; if(minX%2==0) minX+=1; if(maxX%2==0) maxX-=1; for(int j=minY;j<maxY;j+=2) { for(int i=minX;i<maxX;i+=2) { bool found=true; float t1=0; float t2=0; //grid propertis float wogmw = width/gridMap.GetWidth(); float hogmh = height/gridMap.GetHeight(); float gmwow = gridMap.GetWidth()/width; float gmhoh = gridMap.GetHeight()/height; float gmwowo2 = wogmw/2; float gmhoho2 = hogmh/2; vec4 mm = inverse(mat4(m.getModelMatrix()))*vec4(vec3(gmwowo2+(i+wogmw)/gmwow,0,-gmhoho2+(-j-hogmh)/gmhoh),1); vec3 t = vec3(gmwowo2+(i+wogmw)/gmwow,1,-gmhoho2+(-j-hogmh)/gmhoh)-vec3(gmwowo2+(i+wogmw)/gmwow,0,-gmhoho2+(-j-hogmh)/gmhoh); vec3 rayd=inverse(mat3(m.getModelMatrix()))*t; rayd = (rayd); float sides[3] = { m.getBoundingBox()->getBboxSide().x+0.3, m.getBoundingBox()->getBboxSide().y, m.getBoundingBox()->getBboxSide().z+0.3 }; vec3 normalizedSides[3] = { vec3(1,0,0), vec3(0,1,0), vec3(0,0,1) }; float tmin=-0.000001; float tmax=99999999; vec3 p = m.getBoundingBox()->getBboxPos()-vec3(mm.x/mm.w,mm.y/mm.w,mm.z/mm.w); for(int k=0;k<3;k++) { float e = dot(normalizedSides[k],p); float f = dot(normalizedSides[k],rayd); if(abs(f)>0.000001) { t1=(e+sides[k])/f; t2=(e-sides[k])/f; if(t1>t2) { float tmp=t1; t1=t2; t2=tmp; } if(t1>tmin) tmin=t1; if(t2<tmax) tmax=t2; if(tmin>tmax) found=false; if(tmax<0) found=false; } else if(-e-sides[k]>0 || -e+sides[k]<0) found=false; } if(found) { this->gridMap.SetPixel(i,j,sf::Color(120,120,255,255)); } } } glActiveTexture(GL_TEXTURE11); this->makeBlendMap(this->gridTexHandle,this->gridMap); } }
mat4 operator-(const mat4 &a) { return mat4(-a.v[0],-a.v[1],-a.v[2],-a.v[3]); }
mat4 operator/(const mat4 &a, float d) { return mat4(a.v[0]/d, a.v[1]/d, a.v[2]/d, a.v[3]/d); }
void Drawable::Translate(glm::vec3& v) { _trans = mat4(1.0f); _trans *= glm::translate(mat4(1.0f), v); }
void init() { map.initialize(); cam_moves[0] = Translate(-0.01, 0, 0); cam_moves[1] = Translate(0, 0, -0.01); cam_moves[2] = Translate(0.01, 0, 0); cam_moves[3] = Translate(0, 0, 0.01); int* arr = map.decrypt(Snake_Head); int i = arr[0]- Length/2, j= arr[1]-Width/2; cam_COF = Translate((i+2)*sphere_width, 0, j*sphere_width) * cam_COF; cam_eye = Translate(-(i-2)*sphere_width, 15, j*sphere_width) * cam_eye; vec4 a(0, 0, 0, 1); vec4 b(sphere_width, 0, 0, 1); vec4 c(0, 0, sphere_width, 1); vec4 d(sphere_width, 0, sphere_width, 1); vec3 normal = vec3(0, 1, 0); normals[index] = normal; points[index] = a; index++; normals[index] = normal; points[index] = b; index++; normals[index] = normal; points[index] = c; index++; normals[index] = normal; points[index] = b; index++; normals[index] = normal; points[index] = c; index++; normals[index] = normal; points[index] = d; index++; //generate the sphere tetrahedron(NumTimesToSubdivide); GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint buffer[2]; glGenBuffers(2, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, buffer[1]); glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW); program = InitShader("src/vshader.glsl", "src/fshader.glsl"); glUseProgram(program); glBindBuffer(GL_ARRAY_BUFFER, buffer[0]); GLuint vPosition = glGetAttribLocation(program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); glBindBuffer(GL_ARRAY_BUFFER, buffer[1]); GLuint vNormal = glGetAttribLocation(program, "vNormal"); glEnableVertexAttribArray(vNormal); glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); vec4 ambient_product = light_ambient * material_ambient; vec4 diffuse_product = light_diffuse * material_diffuse; vec4 specular_product = light_specular * material_specular; glUniform4fv(glGetUniformLocation(program, "AmbientProduct"), 1, ambient_product); glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, diffuse_product); glUniform4fv(glGetUniformLocation(program, "SpecularProduct"), 1, specular_product); lightPos_loc = glGetUniformLocation(program, "LightPosition"); glUniform4fv(lightPos_loc, 1, light_position); glUniform1f(glGetUniformLocation(program, "Shininess"), material_shininess); model_loc = glGetUniformLocation(program, "Model"); glUniformMatrix4fv(model_loc, 1, GL_TRUE, mat4(1)); camera_loc = glGetUniformLocation(program, "Camera"); projection_loc = glGetUniformLocation(program, "Projection"); glEnable(GL_DEPTH_TEST); glClearColor(1.0, 1.0, 1.0, 1.0); direction = Periodic; step(0); }
gl::OBB Object3D::obb() const { gl::OBB ret(aabb(), mat4()); return ret; }
void Object3D::set_look_at(const glm::vec3 &theLookAt, const glm::vec3 &theUp) { set_transform(glm::inverse(glm::lookAt(position(), theLookAt, theUp)) * glm::scale(mat4(), scale())); }
#include <Texture2D.h> #include <ObjectModel.h> //char* objFileName = "../models/cube.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/teapot.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/utah_teapot_hires.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/glove_hipoly.obj"; mat4 initialTransform = mat4(); //char* objFileName = "../models/guitar.obj"; mat4 initialTransform = RotateX(-90); //char* objFileName = "../models/hat.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/Pencil_OBJ.obj";mat4 initialTransform = Scale(10, 10, 10); //char* objFileName = "../models/table.obj";mat4 initialTransform = RotateX(-90); //char* objFileName = "../models/Chair_03.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/Key_B_02.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/sphere.obj";mat4 initialTransform = mat4(); char* objFileName = "../models/torus_knot_mtl.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/bunny.obj";mat4 initialTransform = mat4(); //char* objFileName = "../models/dragon.obj";mat4 initialTransform = mat4(); vec4 lpos = vec4(); Camera * camera; CameraControl * cameraControl; Shader* vertexColorShader; Shader* phongShader; Shader* silhShader; ObjectModel* object; VertexArray* axesVao;
mat4 Camera::getViewMatrix() const { return orientation() * translate(mat4(), -_position); }
/* Returns the transpose of this matrix (x_ij == x_ji). */ mat4 mat4::transpose() const { /* Make a new matrix with rows equal to the columns of this matrix */ return mat4(this->getColumn(0), this->getColumn(1), this->getColumn(2), this->getColumn(3)); }
UpwardCone::UpwardCone( OpenGLApplicationBase * OpenGLApp, float height, float base, vec4 color, int numMajor, int numMinor) : Cone( OpenGLApp, height, base, color, numMajor, numMinor) { mat4 fixedtrans = translate(mat4(1.0f), vec3(0.0f, 0.0f, 0.0f) ); fixedTransformation = rotate( fixedtrans, -90.0f, vec3(1.0f, 0.0f, 0.0f) ); }
// Draws the FBO texture for Oculus rift. void ApplicationCompositor::displayOverlayTextureHmd(RenderArgs* renderArgs, int eye) { if (_alpha == 0.0f) { return; } GLuint texture = qApp->getApplicationOverlay().getOverlayTexture(); if (!texture) { return; } updateTooltips(); vec2 canvasSize = qApp->getCanvasSize(); _textureAspectRatio = aspect(canvasSize); renderArgs->_context->syncCache(); auto geometryCache = DependencyManager::get<GeometryCache>(); gpu::Batch batch; geometryCache->useSimpleDrawPipeline(batch); batch._glDisable(GL_DEPTH_TEST); batch._glDisable(GL_CULL_FACE); batch._glBindTexture(GL_TEXTURE_2D, texture); batch._glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); batch._glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); batch.setViewTransform(Transform()); batch.setProjectionTransform(qApp->getEyeProjection(eye)); mat4 eyePose = qApp->getEyePose(eye); glm::mat4 overlayXfm = glm::inverse(eyePose); #ifdef DEBUG_OVERLAY { batch.setModelTransform(glm::translate(mat4(), vec3(0, 0, -2))); geometryCache->renderUnitQuad(batch, glm::vec4(1)); } #else { batch.setModelTransform(overlayXfm); drawSphereSection(batch); } #endif // Doesn't actually render renderPointers(batch); vec3 reticleScale = vec3(Cursor::Manager::instance().getScale() * reticleSize); bindCursorTexture(batch); MyAvatar* myAvatar = DependencyManager::get<AvatarManager>()->getMyAvatar(); //Controller Pointers for (int i = 0; i < (int)myAvatar->getHand()->getNumPalms(); i++) { PalmData& palm = myAvatar->getHand()->getPalms()[i]; if (palm.isActive()) { glm::vec2 polar = getPolarCoordinates(palm); // Convert to quaternion mat4 pointerXfm = glm::mat4_cast(quat(vec3(polar.y, -polar.x, 0.0f))) * glm::translate(mat4(), vec3(0, 0, -1)); mat4 reticleXfm = overlayXfm * pointerXfm; reticleXfm = glm::scale(reticleXfm, reticleScale); batch.setModelTransform(reticleXfm); // Render reticle at location geometryCache->renderUnitQuad(batch, glm::vec4(1), _reticleQuad); } } //Mouse Pointer if (_reticleActive[MOUSE]) { glm::vec2 projection = screenToSpherical(glm::vec2(_reticlePosition[MOUSE].x(), _reticlePosition[MOUSE].y())); mat4 pointerXfm = glm::mat4_cast(quat(vec3(-projection.y, projection.x, 0.0f))) * glm::translate(mat4(), vec3(0, 0, -1)); mat4 reticleXfm = overlayXfm * pointerXfm; reticleXfm = glm::scale(reticleXfm, reticleScale); batch.setModelTransform(reticleXfm); geometryCache->renderUnitQuad(batch, glm::vec4(1), _reticleQuad); } renderArgs->_context->render(batch); }
vec3 InverseTransformVector(const vec3 vector); vec3 InverseTransformDirection(const vec3 direction); private: mat4 CalculateMatrix(); mat4 CalculateInverseMatrix(); // TODO: Transform: If a parent entity is deleted, it must have a list of child entities to remove them too. Transform* mParent; vec3 mPosition{0.f, 0.f, 0.f}; vec3 mScale{1.f, 1.f, 1.f}; quat mOrientation{1.f, 0.f, 0.f, 0.f}; bool mEulerRefreshNeeded{false}; vec3 mEulerAnglesCache{0.f, 0.f, 0.f}; bool mRightRefreshNeeded{false}; vec3 mRightCache{1.f, 0.f, 0.f}; bool mUpRefreshNeeded{false}; vec3 mUpCache{0.f, 1.f, 0.f}; bool mForwardRefreshNeeded{false}; vec3 mForwardCache{0.f, 0.f, 1.f}; bool mMatrixRefreshNeeded{false}; mat4 mMatrixCache{mat4(1.f)}; mat4 mWorldMatrixCache{mat4(1.f)}; mat4 mParentWorldMatrixCache{mat4(1.f)}; bool mInverseMatrixRefreshNeeded{false}; mat4 mInverseMatrixCache{mat4(1.f)}; mat4 mWorldInverseMatrixCache{mat4(1.f)}; mat4 mParentWorldInverseMatrixCache{mat4(1.f)}; }; }
void Camera::recompose() { mat4 orientation = glm::mat4_cast(_orientation); mat4 translation = glm::translate(mat4(), _position); _transform = translation * orientation; }
glm::mat4& Drawable::Model() { _trans = glm::translate(mat4(1.0f), _position); _model = _trans * _rot * _model; return _model; }
Matrix4 Quaternion::mat4() const { return mat4(Vector3()); }
Ogre::Matrix4 Node::getLocalTransform() const { Ogre::Matrix4 mat4(Ogre::Matrix4::IDENTITY); mat4.makeTransform(trafo.pos, Ogre::Vector3(trafo.scale), Ogre::Quaternion(trafo.rotation)); return mat4; }
void main() { // Pick one of the endpoints, and translate // it to the origin in the x,y plane. mat4 trans = mat4 (1, 0, 0, -gl_in[0].gl_Position.x, 0, 1, 0, -gl_in[0].gl_Position.y, 0, 0, 1, 0, 0, 0, 0, 1); // Inverse translation mat4 transInv = mat4 (1, 0, 0, gl_in[0].gl_Position.x, 0, 1, 0, gl_in[0].gl_Position.y, 0, 0, 1, 0, 0, 0, 0, 1); vec4 pos[6]; pos[2] = gl_in[0].gl_Position; // At (x,y) origin pos[3] = gl_in[1].gl_Position; // Not at (x,y) origin float width = 0.5; mat4 moveUp = mat4(1, 0, 0, 0, 0, 1, 0, width, 0, 0, 1, 0, 0, 0, 0, 1); mat4 moveDown = mat4(1, 0, 0, 0, 0, 1, 0, -width, 0, 0, 1, 0, 0, 0, 0, 1); #if 0 // Rotate about the z-axis to get the line // into the x / z plane. Actually, we know // what the end result of that rotation will // look like without performing the rotation, // all that is need to be known is the length // of the line. // // What is needed is the inverse of the // rotation matrix to rotate the new points // back into their original basis float cosTheta; float sinTheta; float d = sqrt(pos[3].x * pos[3].x + pos[3].y * pos[3].y); pos[3].y = 0; if(pos[1].x >= 0 && pos[1].y >= 0) { sinTheta = pos[1].y / d; cosTheta = pos[1].x / d; pos[3].x = d; } if(pos[1].x < 0 && pos[1].y >= 0) { // Make sin(theta) -sin(theta) for // the inv rotation. -cos(theta) = cos(theta) sinTheta = -pos[1].y / d; cosTheta = -pos[1].x / d; pos[3].x = -d; } if(pos[1].x < 0 && pos[1].y < 0) { sinTheta = -pos[1].y / d; cosTheta = -pos[1].x / d; pos[3].x = -d; pos[3].y = 0; } if(pos[1].x >= 0 && pos[1].y < 0) { // Make sin(theta) -sin(theta) for // the inv rotation. -cos(theta) = cos(theta) sinTheta = pos[1].y / d; cosTheta = pos[1].x / d; pos[3].x = d; } mat4 rotInv = mat4 (cosTheta, sinTheta, 0, 0, sinTheta, cosTheta, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); vec4 width = vec4(0, 0.5, 0, 0) * 0.1; pos[0] = pos[2] + width; pos[4] = pos[2] - width; pos[1] = pos[3] + width; pos[5] = pos[3] - width; // Emit transformed vertices for(int i = 0; i < 6; i++) { // Transform the vertex into the view plane gl_Position = proj * transInv * pos[i]; // Set the out color for this vertex vertexColor = geomColor[i]; // Emit the vertex EmitVertex(); } #endif gl_Position = proj * transInv * pos[2]; vertexColor = geomColor[0]; EmitVertex(); gl_Position = proj * transInv * pos[3]; vertexColor = geomColor[1]; EmitVertex(); // Done composing the primitive EndPrimitive(); }
void display(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); mat4 camera = LookAt(cam_eye, cam_COF, cam_upvec); glUniformMatrix4fv(camera_loc, 1, GL_TRUE, camera); glUniformMatrix4fv(model_loc, 1, GL_TRUE, mat4(1)); material_diffuse = vec4(1,1,1,1); // // vec4 ambient_product = light_ambient * material_ambient; vec4 diffuse_product = light_diffuse * material_diffuse; // vec4 specular_product = light_specular * material_specular; // // glUniform4fv(glGetUniformLocation(program, "AmbientProduct"), 1, // ambient_product); glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, diffuse_product); // glUniform4fv(glGetUniformLocation(program, "SpecularProduct"), 1, // specular_product); glUniform1f(glGetUniformLocation(program, "Shininess"), material_shininess); for (int i = -Length / 2; i < Length / 2; i++) for (int j = -Width / 2; j < Width / 2; j++) { glUniformMatrix4fv(model_loc, 1, GL_TRUE, Translate(i * sphere_width, 0, j * sphere_width)); glDrawArrays(GL_TRIANGLES, 0, 2 * 3); } // material_diffuse = vec4(0,1,1,1); // material_ambient = vec4(0,0,0,1); // // ambient_product = light_ambient * material_ambient; // diffuse_product = light_diffuse * material_diffuse; // specular_product = light_specular * material_specular; // // glUniform4fv(glGetUniformLocation(program, "AmbientProduct"), 1, // ambient_product); // glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, // diffuse_product); // glUniform4fv(glGetUniformLocation(program, "SpecularProduct"), 1, // specular_product); // glUniform1f(glGetUniformLocation(program, "Shininess"), material_shininess); list<int>::iterator iter; int* oldcord; int k = 0; for(iter=map.snake_old.begin();iter!=map.snake_old.end();iter++) { oldcord = map.decrypt(*iter); int i = (oldcord[0]-Length/2),j = (oldcord[1]-Width/2); material_diffuse = vec4(1,0,0,1); diffuse_product = light_diffuse * material_diffuse; glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, diffuse_product); glUniformMatrix4fv(model_loc, 1, GL_TRUE, Translate(i * sphere_width + sphere_width / 2 + (dx[k]*trans), sphere_width / 2, j * sphere_width + sphere_width / 2 + (dy[k]*trans))); glDrawArrays(GL_TRIANGLES, 2 * 3, NumVertices - 6); k++; } for (int i = -Length / 2; i < Length / 2; i++) for (int j = -Width / 2; j < Width / 2; j++) { if(map.isFood(i + Length / 2, j + Width / 2)) { if(map.isSpecial(i + Length / 2, j + Width / 2)) { material_diffuse = vec4(0,0,0,1); } else material_diffuse = vec4(0,1,1,1); diffuse_product = light_diffuse * material_diffuse; glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, diffuse_product); glUniformMatrix4fv(model_loc, 1, GL_TRUE, Translate(i * sphere_width + sphere_width / 2, sphere_width / 2, j * sphere_width + sphere_width / 2)); glDrawArrays(GL_TRIANGLES, 2 * 3, NumVertices - 6); } } glutSwapBuffers(); }
void * AttributeValues::getProp(unsigned int prop, Enums::DataType type) { void *val; switch (type) { case Enums::ENUM: if (m_EnumProps.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_EnumProps[prop] = *(int *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type ENUM"); SLOG("Accessing undefined attribute of type ENUM - This should never occur"); m_EnumProps[prop] = 0; } } return(&(m_EnumProps[prop])); break; case Enums::INT: if (m_IntProps.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_IntProps[prop] = *(int *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type INT"); SLOG("Accessing undefined attribute of type INT - This should never occur"); m_IntProps[prop] = 0; } } return(&(m_IntProps[prop])); break; case Enums::IVEC2: if (m_Int2Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_Int2Props[prop] = *(ivec2 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type IVEC2"); SLOG("Accessing undefined attribute of type IVEC2 - This should never occur"); m_Int2Props[prop] = 0; } } return(&(m_Int2Props[prop])); break; case Enums::UINT: if (m_UIntProps.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_UIntProps[prop] = *(unsigned int *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type UINT"); SLOG("Accessing undefined attribute of type UINT - This should never occur"); m_UIntProps[prop] = 0; } } return(&(m_UIntProps[prop])); break; case Enums::UIVEC2: if (m_UInt2Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_UInt2Props[prop] = *(uivec2 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type UINT2"); SLOG("Accessing undefined attribute of type UINT2 - This should never occur"); m_UInt2Props[prop] = uivec2(0); } } return(&(m_UInt3Props[prop])); break; case Enums::UIVEC3: if (m_UInt3Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_UInt3Props[prop] = *(uivec3 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type UINT3"); SLOG("Accessing undefined attribute of type UINT3 - This should never occur"); m_UInt3Props[prop] = uivec3(0); } } return(&(m_UInt3Props[prop])); break; case Enums::BOOL: if (m_BoolProps.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_BoolProps[prop] = *(bool *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type BOOL"); SLOG("Accessing undefined attribute of type BOOL - This should never occur"); m_BoolProps[prop] = 0; } } return(&(m_BoolProps[prop])); break; case Enums::BVEC4: if (m_Bool4Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_Bool4Props[prop] = *(bvec4 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined attribute of type BVEC4"); SLOG("Accessing undefined attribute of type BVEC4 - This should never occur"); m_Bool4Props[prop] = 0; } } return(&(m_Bool4Props[prop])); break; case Enums::FLOAT: if (m_FloatProps.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_FloatProps[prop] = *(float *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined user attribute of type FLOAT"); SLOG("Accessing undefined user attribute of type FLOAT - This should never occur"); m_FloatProps[prop] = 0; } } return(&(m_FloatProps[prop])); break; case Enums::VEC4: if (m_Float4Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_Float4Props[prop] = *(vec4 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined user attribute of type VEC4"); SLOG("Accessing undefined user attribute of type VEC4 - This should never occur"); m_Float4Props[prop] = vec4(0.0f); } } return(&(m_Float4Props[prop])); break; case Enums::VEC3: if (m_Float3Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_Float3Props[prop] = *(vec3 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined user attribute of type VEC3"); SLOG("Accessing undefined user attribute of type VEC3 - This should never occur"); m_Float3Props[prop] = vec3(0.0f); } } return(&(m_Float3Props[prop])); break; case Enums::VEC2: if (m_Float2Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_Float2Props[prop] = *(vec2 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined user attribute of type VEC2"); SLOG("Accessing undefined user attribute of type VEC2 - This should never occur"); m_Float2Props[prop] = vec2(0.0f); } } return(&(m_Float2Props[prop])); break; case Enums::MAT4: if (m_Mat4Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_Mat4Props[prop] = *(mat4 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined user attribute of type MAT4"); SLOG("Accessing undefined user attribute of type MAT4 - This should never occur"); m_Mat4Props[prop] = mat4(); } } return(&(m_Mat4Props[prop])); break; case Enums::MAT3: if (m_Mat3Props.count(prop) == 0) { val = m_Attribs->getDefault(prop, type); if (val != NULL) m_Mat3Props[prop] = *(mat3 *)val; else { // life goes on ... except in debug mode assert(false && "Accessing undefined user attribute of type MAT3"); SLOG("Accessing undefined user attribute of type MAT3 - This should never occur"); m_Mat3Props[prop] = mat3(); } } return(&(m_Mat3Props[prop])); break; default: assert(false && "Missing Data Type in class attributeValues"); return NULL; } };
mat4 operator*(const mat4 &a, float d) { return mat4(a.v[0]*d, a.v[1]*d, a.v[2]*d, a.v[3]*d); }
//------------------------------------------------------------------------------ void initGL() { //-------------------------------------------------------------------------- #ifdef USESVCUI LOGI("adding more UI controls...\n"); class ControlsEvents : public IEventsWnd { void ScalarChanged(IControlScalar *pWin, float &v, float prev) { g_renderCnt++; }; void Button(IWindow *pWin, int pressed) { size_t tag; pWin->GetUserData(&tag); keyboard(tag, 0,0); }; void ToolbarChanged(IControlToolbar *pWin, int selecteditem, int prev) { int states; size_t tag; int ddsidx; pWin->GetItemInfos(selecteditem, states, tag, NULL, 0, NULL, 0, ddsidx); keyboard(tag, 0,0); } void CheckBoxChanged(IControlScalar *pWin, bool &value, bool prev) { g_renderCnt++; if(!strcmp("WIRE", pWin->GetID())) { //g_bWireFrame = value; glPolygonMode(GL_FRONT_AND_BACK, value ? GL_LINE:GL_FILL); } } void ComboSelectionChanged(IControlCombo *pWin, unsigned int selectedidx) { fx_Tech = fx_Effect->findTechnique(selectedidx+TECH_DEFAULT); if(fx_Tech == NULL) fx_Tech = fx_Effect->findTechnique(TECH_DEFAULT); g_renderCnt++; }; }; static ControlsEvents controlsEvents; //--------------------------------------------------------------------------- if(g_pWinHandler) { g_pControls = g_pWinHandler->CreateWindowFolding("CTRLS", "Controls"); g_pComboTech = g_pWinHandler->CreateCtrlCombo("MTECH", "Techniques", g_pControls); g_pWinHandler->CreateCtrlButton("LM", "Reload Materials", g_pControls)->SetUserData(NULL, 'm'); g_pWinHandler->VariableBind(g_pWinHandler->CreateCtrlCheck("RT", "Render Realtime", g_pControls), &g_realtime.bNonStopRendering); g_pWinHandler->VariableBind( g_pWinHandler->CreateCtrlScalar("SCALE", "Scale", g_pControls)->SetBounds(0.0, 2.0) , &g_scale); g_pWinHandler->VariableBind( g_pWinHandler->CreateCtrlCheck("WIRE", "Wireframe", g_pControls), &g_bWireFrame); g_pControls->SetVisible(1)->SetLocation(g_winSz[0]+16,0)->SetSize(250,g_winSz[1]); g_pWinHandler->Register(&controlsEvents); } #endif //--------------------------------------------------------------------------- glewInit(); glClearColor(0.1f, 0.1f, 0.2f, 1.0f); // Bind only one vao glGenVertexArrays(1, &g_vao); glBindVertexArray(g_vao); nvFX::setErrorCallback(errorCallbackFunc); nvFX::setIncludeCallback(includeCallbackFunc); loadMaterialEffect(); // Load a model const char* p = RESOURCE_DIR; //--------------- LOGI("Loading Mesh... " MODEL1 "\n"); meshFile = bk3d::load(MODEL1); if(!meshFile) { LOGI("Trying " RESOURCE_DIR "/" MODEL1 "\n"); meshFile = bk3d::load(RESOURCE_DIR "/" MODEL1); } if(!meshFile) { LOGI("Loading Mesh... " MODEL2 "\n"); meshFile = bk3d::load(MODEL2); if(!meshFile) { LOGI("Trying " RESOURCE_DIR "/" MODEL2 "\n"); meshFile = bk3d::load(RESOURCE_DIR "/" MODEL2); } // use tech #0 fx_Tech = fx_Effect->findTechnique(TECH_DEFAULT); #ifdef USESVCUI if(g_pComboTech) g_pComboTech->SetSelectedByIndex(0); #endif } if(!meshFile) { LOGE("error in loading mesh\n"); return; } // To be Core OpenGL, we must create some buffer objects // Check the bounding boxes and adjust scaling float min[3] = {10000.0, 10000.0, 10000.0}; float max[3] = {-10000.0, -10000.0, -10000.0}; for(int i=0; i<meshFile->pMeshes->n; i++) { bk3d::Mesh *pMesh = meshFile->pMeshes->p[i]; // walk through the slots (== buffers of attributes) and create a VBO for it // bk3d already has all needed for a quick setup for(int j=0; j<pMesh->pSlots->n; j++) { bk3d::Slot* pSlot = pMesh->pSlots->p[j]; glGenBuffers(1, (unsigned int*)&pSlot->userData); // store directly in the structure... easier glBindBuffer(GL_ARRAY_BUFFER, pSlot->userData); glBufferData(GL_ARRAY_BUFFER, pSlot->vtxBufferSizeBytes, pSlot->pVtxBufferData, GL_STATIC_DRAW); } glBindBuffer(GL_ARRAY_BUFFER, 0); for(int j=0; j<pMesh->pPrimGroups->n; j++) { bk3d::PrimGroup* pPG = pMesh->pPrimGroups->p[j]; // the element buffer can be shared between many primitive groups // the main owner is specified by "pOwnerOfIB" // if no pOwnerOfIB or if pOwnerOfIB is itself, then we create the buffer // later, the indexArrayByteOffset will tell where to fetch indices if((pPG->pOwnerOfIB == pPG) || (pPG->pOwnerOfIB == NULL)) { glGenBuffers(1, (unsigned int*)&pPG->userPtr); // store directly in the structure... easier glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (long)pPG->userPtr); glBufferData(GL_ELEMENT_ARRAY_BUFFER, pPG->indexArrayByteSize, pPG->pIndexBufferData, GL_STATIC_DRAW); } else { // here the primitive group uses a subset of an existing buffer // we always assume the owner was first in the list of primgroups pPG->userPtr = pPG->pOwnerOfIB->userPtr; } } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); // NOTE: to be accurate, we should go through primitive groups // see if this prim group used a transformation. If so, we should // take its bounding box into account... // For now, let's only take the Mesh's bbox mat4 matModel; vec3 meshMin(pMesh->aabbox.min); vec3 meshMax(pMesh->aabbox.max); if(pMesh->pTransforms && pMesh->pTransforms->n == 1) matModel = mat4(pMesh->pTransforms->p[0]->MatrixAbs()); else matModel.identity(); meshMin = matModel * meshMin; meshMax = matModel * meshMax; if(meshMin[0] < min[0]) min[0] = meshMin[0]; if(meshMin[1] < min[1]) min[1] = meshMin[1]; if(meshMin[2] < min[2]) min[2] = meshMin[2]; if(meshMax[0] > max[0]) max[0] = meshMax[0]; if(meshMax[1] > max[1]) max[1] = meshMax[1]; if(meshMax[2] > max[2]) max[2] = meshMax[2]; } g_posOffset[0] = (max[0] + min[0])*0.5f; g_posOffset[1] = (max[1] + min[1])*0.5f; g_posOffset[2] = (max[2] + min[2])*0.5f; float bigger = 0; if((max[0]-min[0]) > bigger) bigger = (max[0]-min[0]); if((max[1]-min[1]) > bigger) bigger = (max[1]-min[1]); if((max[2]-min[2]) > bigger) bigger = (max[2]-min[2]); if((bigger) > 0.001) { g_scale = 1.0f / bigger; LOGI("Scaling the model by %f...\n", g_scale); } // Commented the offset because the gargoyl model is better without it... if(fx_scaleBias) fx_scaleBias->setValue4f(0, 0, 0, g_scale); //fx_scaleBias->setValue4f(g_posOffset[0], g_posOffset[1], g_posOffset[2], g_scale); LOGI("Pg Up/Down : zoom\n"); LOGI("Arrows: rotate the camera\n"); #ifdef NOGLUT LOGI("Ctrl + Arrows: pan the camera taget\n"); #endif LOGI("Mouse + left button: rotate the camera\n"); LOGI("Mouse + middle button: Pan the camera target\n"); LOGI("Mouse + right button: rotate Horizontally and change camera distance\n"); LOGI("space: toggle redraw on every frame\n"); LOGI("1,2,3 : switch between techniques\n"); LOGI("m : reload material effect\n"); LOGI("w : wireframe\n"); // // Timer Query // glGenQueries(4, timerQueries); }
void CCylinder::Render(const glm::mat4 & view, const GLfloat & max_flux) { NormalizeFlux(max_flux); // Look up the parameters: const double diameter = mParams["diameter"].getValue(); const double radius = diameter / 2; const double height = mParams["height"].getValue(); // Activate the shader GLuint shader_program = mShader->GetProgram(); mShader->UseShader(); // bind back to the VAO glBindVertexArray(mVAO); // Define the view: GLint uniView = glGetUniformLocation(shader_program, "view"); glUniformMatrix4fv(uniView, 1, GL_FALSE, glm::value_ptr(view)); GLint uniTranslation = glGetUniformLocation(shader_program, "translation"); glUniformMatrix4fv(uniTranslation, 1, GL_FALSE, glm::value_ptr(Translate())); GLint uniRotation = glGetUniformLocation(shader_program, "rotation"); glUniformMatrix4fv(uniRotation, 1, GL_FALSE, glm::value_ptr(Rotate())); GLint uniScale = glGetUniformLocation(shader_program, "scale"); glm::mat4 r_scale = glm::scale(mat4(), glm::vec3(radius, radius, 1.0)); glm::mat4 h_scale = glm::scale(mat4(), glm::vec3(1.0, 1.0, height)); glm::mat4 scale = r_scale * h_scale; glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale)); // bind to this object's texture, upload the image. glBindTexture(GL_TEXTURE_RECTANGLE, mFluxTextureID); glTexImage2D(GL_TEXTURE_RECTANGLE, 0, GL_RGBA, mFluxTexture.size(), 1, 0, GL_RGBA, GL_FLOAT, &mFluxTexture[0]); // Render glDisable(GL_DEPTH_TEST); // Draw the cylindrical wall. glDrawElements(GL_TRIANGLE_STRIP, mRimSize, GL_UNSIGNED_INT, 0); // Draw the top mat4 z_offset; z_offset = glm::translate(mat4(), vec3(0, 0, 0.5)); scale = r_scale * h_scale * z_offset; glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale)); glDrawElements(GL_TRIANGLE_STRIP, mMidplaneSize, GL_UNSIGNED_INT, (void*) (mMidplaneStart * sizeof(float))); // Draw the bottom z_offset = glm::translate(mat4(), vec3(0, 0, -0.5)); scale = r_scale * h_scale * z_offset; glUniformMatrix4fv(uniScale, 1, GL_FALSE, glm::value_ptr(scale)); glDrawElements(GL_TRIANGLE_STRIP, mMidplaneSize, GL_UNSIGNED_INT, (void*) (mMidplaneStart * sizeof(float))); glEnable(GL_DEPTH_TEST); // Unbind glBindTexture(GL_TEXTURE_RECTANGLE, 0); CHECK_OPENGL_STATUS_ERROR(glGetError(), "Rendering failed."); }
//------------------------------------------------------------------------------ // //------------------------------------------------------------------------------ void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glViewport(0, 0, g_winSz[0], g_winSz[1]); //SwapBuffers( g_hDC ); //return; // // setup the block1 with base matrices // // g_transfBlock1.m4_Proj already done vec3 up(0,1,0); look_at(g_transfBlock1.m4_View, g_camera.curEyePos, g_camera.curFocusPos, up); //g_transfBlock1.m4_ViewIT = ...todo g_transfBlock1.m4_ViewProj = g_transfBlock1.m4_Proj * g_transfBlock1.m4_View; g_transfBlock1.eyePos = g_camera.curEyePos; // copy the block to OGL if(fx_transfBlock1) { void* p; fx_transfBlock1->mapBuffer(&p); memcpy(p, &g_transfBlock1, sizeof(transfBlock1)); fx_transfBlock1->unmapBuffer(); } //----------------------------------------------------------------- // // Render a basic floor // //glBeginQuery(GL_TIME_ELAPSED, timerQueries[tqOffset]); #define U 1.0f #define DU 0.1f struct Grid { Grid() { Elts = 0; for(float i=-U; i<=(U+DU); i+=DU) { vtx[Elts++] = vec3(-U, 0, i); vtx[Elts++] = vec3( U, 0, i); vtx[Elts++] = vec3(i, 0,-U); vtx[Elts++] = vec3(i, 0, U); } glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); glBufferData(GL_ARRAY_BUFFER, sizeof(vec3)*(10*10+2), vtx[0].vec_array, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); } int Elts; GLuint vbo; vec3 vtx[10*10+2]; }; static Grid sgrid; if(fx_TechFloor) { fx_pass = fx_TechFloor->getPass(0); fx_pass->execute(); } glEnableVertexAttribArray(0); glDisableVertexAttribArray(1); glDisableVertexAttribArray(2); glDisableVertexAttribArray(3); glDisableVertexAttribArray(4); glBindBuffer(GL_ARRAY_BUFFER, sgrid.vbo); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL); glDrawArrays(GL_LINES, 0, sgrid.Elts); glDisableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); if(fx_pass) fx_pass->unbindProgram(); fx_pass = NULL; //glEndQuery(GL_TIME_ELAPSED); // // Mesh rendering // //glBeginQuery(GL_TIME_ELAPSED, timerQueries[tqOffset+1]); if(!meshFile) { #ifdef NOGLUT SwapBuffers( g_hDC ); #else glutSwapBuffers(); #endif return; } // // default values for second block made of World... // mat4 m4_world1; g_transfBlock2.m4_World.identity(); g_transfBlock2.m4_WorldView = g_transfBlock1.m4_View * g_transfBlock2.m4_World; g_transfBlock2.m4_WorldViewProj = g_transfBlock1.m4_Proj * g_transfBlock2.m4_WorldView; mat4 WI; invert(WI, g_transfBlock2.m4_World); transpose(g_transfBlock2.m4_WorldIT, WI); if(fx_transfBlock2) { #ifdef USECSTBUFUNIFORMS // we use setXXX() for constant buffer's uniforms fx_m4_World->setMatrix4f(g_transfBlock2.m4_World.mat_array); fx_m4_WorldIT->setMatrix4f(g_transfBlock2.m4_WorldIT.mat_array); fx_m4_WorldView->setMatrix4f(g_transfBlock2.m4_WorldView.mat_array); fx_m4_WorldViewProj->setMatrix4f(g_transfBlock2.m4_WorldViewProj.mat_array); // we dont need this update call: the Pass::execute() will take care of it if not done //fx_transfBlock2->update(); no need #else void* p; fx_transfBlock2->mapBuffer(&p); memcpy(p, &g_transfBlock2, sizeof(transfBlock2)); fx_transfBlock2->unmapBuffer(); #endif } nvFX::PassInfo passInfo; // structure in which many things are returned by execute() if(fx_Tech) { fx_pass = fx_Tech->getPass(0); fx_pass->execute(&passInfo); } else { glClearColor(1.0,0,0,0.0); } for(int i=0; i< meshFile->pMeshes->n; i++) { bk3d::Mesh *pMesh = meshFile->pMeshes->p[i]; // case where the mesh references a transformation // the absolute value must be available by default // if more than one transf, skip it : this might be a list of skeleton transformations if(pMesh->pTransforms && pMesh->pTransforms->n == 1) { g_transfBlock2.m4_World = mat4(pMesh->pTransforms->p[0]->MatrixAbs()); g_transfBlock2.m4_WorldView = g_transfBlock1.m4_View * g_transfBlock2.m4_World; g_transfBlock2.m4_WorldViewProj = g_transfBlock1.m4_Proj * g_transfBlock2.m4_WorldView; mat4 WI; invert(WI, g_transfBlock2.m4_World); transpose(g_transfBlock2.m4_WorldIT, WI); if(fx_transfBlock2) { #ifdef USECSTBUFUNIFORMS fx_m4_World->setMatrix4f(g_transfBlock2.m4_World.mat_array); fx_m4_WorldIT->setMatrix4f(g_transfBlock2.m4_WorldIT.mat_array); fx_m4_WorldView->setMatrix4f(g_transfBlock2.m4_WorldView.mat_array); fx_m4_WorldViewProj->setMatrix4f(g_transfBlock2.m4_WorldViewProj.mat_array); // Because we are AFTER the Pass::execute(), we must make sure that the constant buffer // gets updated as soon as we changed all the uniforms we wanted to change fx_transfBlock2->update(); #else void* p; fx_transfBlock2->mapBuffer(&p); memcpy(p, &g_transfBlock2, sizeof(transfBlock2)); fx_transfBlock2->unmapBuffer(); #endif } } // // let's make it very simple : each mesh attribute is *supposed* to match // the attribute Id of the effect. In real, meshes might not always match // but this is totally arbitrary... // we assume the mesh got baked as follow: // 0: would be pos; 1: normal; 2: TC; 3 and 4: Tan Binorm // int j = 0; for(int k=0; k<pMesh->pSlots->n;k++) { bk3d::Slot* pSlot = pMesh->pSlots->p[k]; glBindBuffer(GL_ARRAY_BUFFER, pSlot->userData); // assign buffers for(int l=0; l<pSlot->pAttributes->n; l++) { glEnableVertexAttribArray(j); glVertexAttribPointer(j, pSlot->pAttributes->p[l].p->numComp, pSlot->pAttributes->p[l].p->formatGL, GL_FALSE, pSlot->pAttributes->p[l].p->strideBytes, (void*)pSlot->pAttributes->p[l].p->dataOffsetBytes); j++; } } glBindBuffer(GL_ARRAY_BUFFER, 0); int MaxAttr = 16; //glGetIntegerv(GL_MAX_VERTEX_ATTRIBS, &MaxAttr); for(; j<MaxAttr;j++) glDisableVertexAttribArray(j); for(int pg=0; pg<pMesh->pPrimGroups->n; pg++) { bk3d::PrimGroup* pPG = pMesh->pPrimGroups->p[pg]; //if( ( (pPG->topologyGL == GL_LINES) // ||(pPG->topologyGL == GL_LINE_LOOP) // ||(pPG->topologyGL == GL_LINE_STRIP))) //{ } // case where the Primitive group references a transformation // the absolute value must be available by default if(pPG->pTransforms && pPG->pTransforms->n > 0) { g_transfBlock2.m4_World = mat4(pPG->pTransforms->p[0]->MatrixAbs()); g_transfBlock2.m4_WorldView = g_transfBlock1.m4_View * g_transfBlock2.m4_World; g_transfBlock2.m4_WorldViewProj = g_transfBlock1.m4_Proj * g_transfBlock2.m4_WorldView; //g_transfBlock2.m4_WorldIT = ... todo; if(fx_transfBlock2) { #ifdef USECSTBUFUNIFORMS fx_m4_World->setMatrix4f(g_transfBlock2.m4_World.mat_array); fx_m4_WorldView->setMatrix4f(g_transfBlock2.m4_WorldView.mat_array); fx_m4_WorldViewProj->setMatrix4f(g_transfBlock2.m4_WorldViewProj.mat_array); // Because we are AFTER the Pass::execute(), we must make sure that the constant buffer // gets updated as soon as we changed all the uniforms we wanted to change fx_transfBlock2->update(); #else void* p; fx_transfBlock2->mapBuffer(&p); memcpy(p, &g_transfBlock2, sizeof(transfBlock2)); fx_transfBlock2->unmapBuffer(); #endif } } bk3d::Material *pMat = pPG->pMaterial; if(pMat && g_bUseMaterial && fx_materialBlock) { MaterialBlock* p; fx_materialBlock->mapBuffer((void**)&p); // small issue with original modell (has a black material by default...) if(pMat->Diffuse()[0]==0.0==pMat->Diffuse()[1]==pMat->Diffuse()[2]) pMat->Diffuse()[0]=pMat->Diffuse()[1]=pMat->Diffuse()[2]= 0.7f; // simply copy of the data as they are in the original model memory int sz = sizeof(MaterialBlock); memcpy(p, pMat->Diffuse(), sz); fx_materialBlock->unmapBuffer(); } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, (long)pPG->userPtr); // arbitrarily stating that (passInfo.renderingGroup == 3) means we need to feed // the GPU with patches, instead of regular primitives // assuming here that we are dealing only with triangles if(pPG->topologyGL == GL_TRIANGLES) { if(passInfo.renderingGroup == 3) glPatchParameteri(GL_PATCH_VERTICES, 3); glDrawElements( (passInfo.renderingGroup == 3) ? GL_PATCHES : GL_TRIANGLES, pPG->indexCount, pPG->indexFormatGL, (void*)pPG->indexArrayByteOffset);// pIndexBufferData); } } } if(fx_pass) fx_pass->unbindProgram(); fx_pass = NULL; //glEndQuery(GL_TIME_ELAPSED); #ifdef NOGLUT SwapBuffers( g_hDC ); #else glutSwapBuffers(); #endif // // Timer Query results // //tqOffset = tqOffset ? 0 : 2; // alternate between 2 groups //if(tqStart) // special case of the first render // tqStart = false; //else //{ // int available = 0; // while (!available) // { // glGetQueryObjectiv(timerQueries[tqOffset+1], GL_QUERY_RESULT_AVAILABLE, &available); // } // GLuint64 timeElapsed; // for (int i = 0; i < 2; i++) // { // // See how much time the rendering of object i took in nanoseconds. // glGetQueryObjectui64v(timerQueries[tqOffset+i], GL_QUERY_RESULT, &timeElapsed); // } //} }
glm::mat4 StereoDisplayPlugin::getEyePose(Eye eye) const { return mat4(); }
mat4 scale(float x, float y) { return mat4(x, 0.0, 0.0, 0.0, 0.0, y, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0); }
void Terrain::recalcGridAroundModel(vector<Model> removedModels, vector<Model> models) { for(unsigned int o=0;o<removedModels.size();o++) { if(removedModels[o].getMeshName()[0]!='S'&&removedModels[o].getMeshName()[1]!='P'&&removedModels[o].getMeshName()[2]!='A') { float rad= removedModels[o].getBoundingBox()->getBboxSide().z+2; if(rad<removedModels[o].getBoundingBox()->getBboxSide().x) rad=rad<removedModels[o].getBoundingBox()->getBboxSide().x+2; rad*=1.5; int minX = (removedModels[o].getPos().x-rad)*(this->gridMap.GetWidth()/this->width)-1; int minY = (-removedModels[o].getPos().z-rad)*(this->gridMap.GetHeight()/this->height)-1; int maxX=(removedModels[o].getPos().x+rad)*(this->gridMap.GetWidth()/this->width)+1; int maxY=(-removedModels[o].getPos().z+rad)*(this->gridMap.GetHeight()/this->height)+1; if(maxY>this->gridMap.GetHeight()) maxY=this->gridMap.GetHeight(); if(maxX>this->gridMap.GetWidth()) maxX=this->gridMap.GetHeight(); if(minY<2) minY=1; if(minY%2==0) minY+=1; if(maxY%2==0) maxY-=1; if(minX<2) minX=1; if(minX%2==0) minX+=1; if(maxX%2==0) maxX-=1; for(int j=minY;j<maxY;j+=2) { for(int i=minX;i<maxX;i+=2) { this->gridMap.SetPixel(i,j,sf::Color(0,255,0,0)); } } for(int z=0;z<models.size();z++) { if(models[z].getMeshName()[0]!='S'&&models[z].getMeshName()[1]!='P'&&models[z].getMeshName()[2]!='A') if(this->inCircle(removedModels[o].getPos().x,-removedModels[o].getPos().z,models[z].getPos().x,-models[z].getPos().z,rad*2.5)) { for(int j=1;j<maxY;j+=2) { for(int i=1;i<maxX;i+=2) { bool found=true; float t1=0; float t2=0; //grid propertis float wogmw = width/gridMap.GetWidth(); float hogmh = height/gridMap.GetHeight(); float gmwow = gridMap.GetWidth()/width; float gmhoh = gridMap.GetHeight()/height; float gmwowo2 = wogmw/2; float gmhoho2 = hogmh/2; vec4 mm = inverse(mat4(models[z].getModelMatrix()))*vec4(vec3(gmwowo2+(i+wogmw)/gmwow,0,-gmhoho2+(-j-hogmh)/gmhoh),1); vec3 t = vec3(gmwowo2+(i+wogmw)/gmwow,1,-gmhoho2+(-j-hogmh)/gmhoh)-vec3(gmwowo2+(i+wogmw)/gmwow,0,-gmhoho2+(-j-hogmh)/gmhoh); vec3 rayd=inverse(mat3(models[z].getModelMatrix()))*t; rayd = (rayd); float sides[3] = { models[z].getBoundingBox()->getBboxSide().x+0.3, models[z].getBoundingBox()->getBboxSide().y, models[z].getBoundingBox()->getBboxSide().z+0.3 }; vec3 normalizedSides[3] = { vec3(1,0,0), vec3(0,1,0), vec3(0,0,1) }; float tmin=-0.000001; float tmax=99999999; vec3 p = models[z].getBoundingBox()->getBboxPos()-vec3(mm.x/mm.w,mm.y/mm.w,mm.z/mm.w); for(int k=0;k<3;k++) { float e = dot(normalizedSides[k],p); float f = dot(normalizedSides[k],rayd); if(abs(f)>0.000001) { t1=(e+sides[k])/f; t2=(e-sides[k])/f; if(t1>t2) { float tmp=t1; t1=t2; t2=tmp; } if(t1>tmin) tmin=t1; if(t2<tmax) tmax=t2; if(tmin>tmax) found=false; if(tmax<0) found=false; } else if(-e-sides[k]>0 || -e+sides[k]<0) found=false; } if(found) { this->gridMap.SetPixel(i,j,sf::Color(120,120,255,255)); } } } } } } } glActiveTexture(GL_TEXTURE11); this->makeBlendMap(this->gridTexHandle,this->gridMap); }
void Entity::recalcMatrix() { mat4 mat(translate(glm::scale(mat4(1.0f), scale), pos)); trans->setMatrix(false, (float*)&mat, (float*)&inverse(mat)); }