unsigned int getPixel(GCamera& camera, int x, int y, double* pZ) { // Compute the ray vector G3DVector ray; camera.computeRayDirection(x, y, &ray); // Compute the intersection with the billboard plane double denom = m_face.dotProduct(&ray); if(std::abs(denom) < 1e-12) { *pZ = -1e200; return 0; // ray runs parallel to the plane } G3DVector t(m_pCorners[0]); t.subtract(camera.lookFromPoint()); double d = m_face.dotProduct(&t) / denom; *pZ = d; if(d < 0) return 0; // the intersection is behind the camera ray.multiply(d); ray.add(camera.lookFromPoint()); ray.subtract(m_pCorners[0]); // Compute the image coordinates int xx = (int)floor(ray.dotProduct(&m_bb.m_x) * m_pixelsX / m_squaredMagX + 0.5); int yy = (int)floor(ray.dotProduct(&m_bb.m_y) * m_pixelsY / m_squaredMagY + 0.5); // Get the pixel if(xx < 0 || yy < 0 || xx >= m_pixelsX || yy >= m_pixelsY) return 0; return m_bb.m_pImage->pixel(xx % m_bb.m_pImage->width(), m_bb.m_pImage->height() - 1 - (yy % m_bb.m_pImage->height())); }
void GCube::Draw() { if(_is_inited) { glUseProgram(_program); { glUniform1f(_scaleUniformLocal,_scale); glUniform4fv(_colorUniformLocal,1,glm::value_ptr(_color)); glUniform3fv(_moveUniformLocal,1,glm::value_ptr(_translate)); GCamera* camera = nullptr; if(_camera_getter) { camera = _camera_getter(); } if(camera) { if(_viewMatrixUniformLocal!=-1) { glm::mat4x4 view_matrix; camera->GetViewMatrix(view_matrix); glUniformMatrix4fv(_viewMatrixUniformLocal,1,GL_FALSE,glm::value_ptr(view_matrix)); } if(_projectionMatrixUniformLocal!=-1) { glm::mat4x4 projection_matrix; camera->GetCurProjectionMatrix(projection_matrix); glUniformMatrix4fv(_projectionMatrixUniformLocal,1,GL_FALSE,glm::value_ptr(projection_matrix)); } } else { if(_view_matrix_getter && _viewMatrixUniformLocal!=-1) { glm::mat4x4 view_matrix; _view_matrix_getter(view_matrix); glUniformMatrix4fv(_viewMatrixUniformLocal,1,GL_FALSE,glm::value_ptr(view_matrix)); } if(_projection_matrix_getter && _projectionMatrixUniformLocal!=-1) { glm::mat4x4 projection_matrix; _projection_matrix_getter(projection_matrix); glUniformMatrix4fv(_projectionMatrixUniformLocal,1,GL_FALSE,glm::value_ptr(projection_matrix)); } } glBindVertexArray(_vertex_arr_obj); //glDrawElements(GL_TRIANGLES,16,GL_UNSIGNED_INT,BUFF_OFFSET(0)); glDrawElements(GL_TRIANGLE_STRIP,8,GL_UNSIGNED_INT,BUFF_OFFSET(0)); glDrawElements(GL_TRIANGLE_STRIP,8,GL_UNSIGNED_INT,BUFF_OFFSET(9*sizeof(GLuint))); glBindVertexArray(0); } glUseProgram(0); } }
//------------------------------------------------------------------------- // draw texture //------------------------------------------------------------------------- void GSkyBox::Draw(GCamera& Camera) { // get texture GTexture* pTexture = GAssets::g_Textures.Find(m_Texture); if ( !pTexture ) return; float2& Min = Camera.m_OrthoMin; float2& Max = Camera.m_OrthoMax; glPushAttrib( GL_ALL_ATTRIB_BITS ); glEnable(GL_TEXTURE_2D); pTexture->Select(); float2 WorldVisibility = Camera.GetWorldVisibility(); float2 WorldEnvMapScroll = Camera.GetEnvMapScroll(); //GDebug_Print("%3.3f\n",skyscroll); // only use "top half" of what would be an enviroment map WorldEnvMapScroll.y -= 0.5f; WorldVisibility.y *= 2.f; //if ( WorldEnvMapScroll.y < 0.f ) // WorldEnvMapScroll.y = 0.f; float skyxmin = WorldEnvMapScroll.x - ( WorldVisibility.x * 0.5f ); float skyxmax = WorldEnvMapScroll.x + ( WorldVisibility.x * 0.5f ); float skyymin = WorldEnvMapScroll.y - ( WorldVisibility.y * 0.5f ); float skyymax = WorldEnvMapScroll.y + ( WorldVisibility.y * 0.5f ); //float skyymin = 0.f; //float skyymax = 1.f; glColor3f( 1, 1, 1 ); glBegin(GL_QUADS); glTexCoord2f( skyxmin, skyymin ); glVertex3f( Min.x, Min.y, Camera.m_NearZ ); glTexCoord2f( skyxmax, skyymin ); glVertex3f( Max.x, Min.y, Camera.m_NearZ ); glTexCoord2f( skyxmax, skyymax ); glVertex3f( Max.x, Max.y, Camera.m_NearZ ); glTexCoord2f( skyxmin, skyymax ); glVertex3f( Min.x, Max.y, Camera.m_NearZ ); glEnd(); glPopAttrib(); }
void testGCamera() { printf("GCamera..."); GCamera camera; camera.setCoordinateFrame(CFrame()); camera.setFieldOfView(toRadians(90), GCamera::HORIZONTAL); camera.setNearPlaneZ(-1); camera.setFarPlaneZ(-100); Rect2D viewport = Rect2D::xywh(0,0,200,100); Array<Plane> plane; camera.getClipPlanes(viewport, plane); debugAssertM(plane.size() == 6, "Missing far plane"); debugAssertM(plane[0].fuzzyContains(Vector3(0,0,-1)), plane[0].center().toString()); debugAssertM(plane[0].normal() == Vector3(0,0,-1), plane[0].normal().toString()); debugAssertM(plane[5].fuzzyContains(Vector3(0,0,-100)), plane[1].center().toString()); debugAssertM(plane[5].normal() == Vector3(0,0,1), plane[1].normal().toString()); debugAssertM(plane[1].normal().fuzzyEq(Vector3(-1,0,-1).direction()), plane[1].normal().toString()); debugAssertM(plane[2].normal().fuzzyEq(Vector3(1,0,-1).direction()), plane[2].normal().toString()); // top debugAssertM(plane[3].normal().fuzzyEq(Vector3(0,-0.894427f,-0.447214f).direction()), plane[3].normal().toString()); debugAssertM(plane[4].normal().fuzzyEq(Vector3(0,0.894427f,-0.447214f).direction()), plane[4].normal().toString()); Vector3 ll, lr, ul, ur; camera.getNearViewportCorners(viewport, ur, ul, ll, lr); debugAssertM(ur == Vector3(1, 0.5, -1), ur.toString()); debugAssertM(lr == Vector3(1, -0.5, -1), lr.toString()); debugAssertM(ll == Vector3(-1, -0.5, -1), ll.toString()); debugAssertM(ul == Vector3(-1, 0.5, -1), ul.toString()); printf("passed\n"); }
void App::doFunStuff() { renderDevice->pushState(); GCamera camera; camera.setCoordinateFrame(Vector3(0,1,10)); camera.lookAt(Vector3(0,2.8f,0)); renderDevice->setProjectionAndCameraMatrix(camera); knight.renderShadow(renderDevice); ogre.renderShadow(renderDevice); renderDevice->enableLighting(); renderDevice->setLight(0, GLight::directional(Vector3(-1,1,2).direction(), Color3(.8f,.8f,.7f))); renderDevice->setLight(1, GLight::directional(Vector3(.5f,-1,1).direction(), Color3::red() * 0.2f)); renderDevice->setAmbientLightColor(Color3(.5f,.5f,.6f)); knight.render(renderDevice); ogre.render(renderDevice); renderDevice->popState(); }
void doSimulation(GameTime timeStep) { // Simulation controller->doSimulation(clamp(timeStep, 0.0, 0.1)); camera.setCoordinateFrame(controller->getCoordinateFrame()); }
// get/update camera from values in node GvBool GvViewpoint::getCamera(GCamera &camera, BBox &bbox, float visibilityLimitNear,float visibilityLimit, Matrix *cameraTransform) { Matrix m; float viewAngle = 0.785398; float aspectRatio = 1.0f; orientation.get(m); camera.position=position; viewAngle = (float) fieldOfView; Point dir(0,0,-1.0); Point up(0,1.0,0); // apply orientation to standard dir and up vectors dir *= m; up *= m; up.Normalize(); if (cameraTransform) { camera.position *= *cameraTransform; dir = RotateOnly(*cameraTransform,dir); up = RotateOnly(*cameraTransform,up); // near far focalDistance ?????????? } dir.Normalize(); up.Normalize(); Point size = bbox.Size(); // computed bounding box float field = max(max(fabs(size.x),fabs(size.y)),fabs(size.z)); int positionInBox = bbox.Inside(camera.position); if (bbox.IsEmpty() || (field<=1E-20f)) field = 2.0f; // no bounding box yet, bad // compute distance to target point //xx float targetDistance = field*2.0f; float targetDistance = field*1.0f; // viewpoint inside scene if (positionInBox) targetDistance = 0.2 * field; camera.targetDistanceIsDefault=1; camera.zrangeIsDefault=1; // compute a reasonable z-range if (visibilityLimit >0.0f) { camera.zfar = visibilityLimit; camera.zrangeIsDefault=0; } else { if (positionInBox) camera.zfar = field*1.5; else camera.zfar = field*3.0f; Point center = bbox.Center(); Point d = camera.position - center; float dist = d.Length(); // make shure object is visible from viewpoint if ((dist+field) > camera.zfar) camera.zfar = dist + field; } if (visibilityLimitNear > 0.0f) camera.znear = visibilityLimitNear; else camera.znear = camera.zfar * camera.znearFactor; // compute target camera.target = camera.position + targetDistance*dir; camera.up = up; // field of view camera.height = 2.0 * tan(viewAngle * 0.5)*targetDistance; camera.width = camera.height * aspectRatio; if (!bbox.IsEmpty()) camera.SetWorldReference(bbox); camera.ComputeWorldUpFromUp(); camera.OnChanged(); return gtrue; }
void GBillboardWorld::draw(GImage* pImage, double* pDepthMap, GCamera& camera) { G3DVector coords[VERTEX_COUNT + 1]; int coordMap[VERTEX_COUNT + 1]; for(size_t i = 0; i < pImage->width() * pImage->height(); i++) pDepthMap[i] = 1e200; for(vector<GBillboard*>::iterator it = m_billboards.begin(); it != m_billboards.end(); it++) { // Project each of the corners onto the view GBBAugmented bb(**it); int gap = -1; int coordCount = 0; for(int i = 0; i < VERTEX_COUNT; i++) { camera.project(bb.m_pCorners[i], &coords[coordCount]); if(coords[coordCount].m_vals[2] > 0.0) coordMap[coordCount++] = i; else if(gap < 0) { gap = coordCount; coordCount = gap + 2; } } // Fudge the gap due to vertices that are behind the camera if(gap >= 0) { if(coordCount == 0) continue; // Nothing to draw // Fudge the vertex that follows the chain of valid vertices G3DVector tmp; G3DVector tmp2; int indexValid = coordMap[(gap + coordCount - 1) % coordCount]; // The corner just before the gap int indexFudge = (indexValid + 1) % VERTEX_COUNT; // The first corner that was behind the camera tmp.copy(bb.m_pCorners[indexValid]); tmp.subtract(bb.m_pCorners[indexFudge]); tmp2.copy(camera.lookFromPoint()); tmp2.subtract(bb.m_pCorners[indexFudge]); double d = (1e-6 + tmp2.dotProduct(camera.lookDirection())) / tmp.dotProduct(camera.lookDirection()); tmp.multiply(d); tmp.add(bb.m_pCorners[indexFudge]); camera.project(&tmp, &coords[gap]); // Fudge the vertex that precedes the chain of valid vertices indexValid = coordMap[(gap + 2) % coordCount]; // The corner just after the gap indexFudge = (indexValid + VERTEX_COUNT - 1) % VERTEX_COUNT; // The last corner that was behind the camera tmp.copy(bb.m_pCorners[indexValid]); tmp.subtract(bb.m_pCorners[indexFudge]); tmp2.copy(camera.lookFromPoint()); tmp2.subtract(bb.m_pCorners[indexFudge]); d = (1e-6 + tmp2.dotProduct(camera.lookDirection())) / tmp.dotProduct(camera.lookDirection()); tmp.multiply(d); tmp.add(bb.m_pCorners[indexFudge]); camera.project(&tmp, &coords[gap + 1]); } // Find the starting point (which is the lowest coordinate in the view) int a = 0; for(int i = 1; i < coordCount; i++) { if(coords[i].m_vals[1] < coords[a].m_vals[1]) a = i; } int b = a; // Find the left and right order in which the corners will be visted (bottom to top) while(true) { int c = (b + 1) % coordCount; if(a == c) break; int d = (a + coordCount - 1) % coordCount; drawSection(pImage, pDepthMap, camera, bb, &coords[a], &coords[b], &coords[c], &coords[d]); if(coords[d].m_vals[1] < coords[c].m_vals[1]) a = d; else b = c; } } }