flat-planedigital projectionn是什么意思

工程制图知识点(英文)_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
工程制图知识点(英文)
上传于||暂无简介
阅读已结束,如果下载本文需要使用0下载券
想免费下载更多文档?
你可能喜欢zoom - Move camera to fit 3D scene - Stack Overflow
to customize your list.
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.
J it only takes a minute:
Join the Stack Overflow community to:
Ask programming questions
Answer and help your peers
Get recognized for your expertise
I'm looking for an algorithm to fit a bounding box inside a viewport (in my case a DirectX scene). I know about algorithms for centering a bounding sphere in a orthographic camera but would need the same for a bounding box and a perspective camera. I can not just change the FOV because this app has FOV as a user editable variable, so it must move the camera.
I have most of the data:
I have the up-vector for the camera
I have the center point of the bounding box
I have the look-at vector (direction and distance) from the camera point to the box center
I have projected the points on a plane perpendicular to the camera and retrieved the coefficients describing how much the max/min X and Y coords are within or outside the viewing plane.
Problems I have:
Center of the bounding box isn't necessarily in the center of the viewport (that is, it's bounding rectangle after projection).
Since the field of view "skew" the projection (see ) I cannot simply use the coefficients as a scale factor to move the camera because it will overshoot/undershoot the desired camera position
How do I find the camera position so that it fills the viewport as pixel perfect as possible (exception being if the aspect ratio is far from 1.0, it only needs to fill one of the screen axis)?
I've tried some other things:
Using a bounding sphere and Tangent to find a scale factor to move the camera. This doesn't work well, because, it doesn't take into account the perspective projection, and secondly spheres are bad bounding volumes for my use because I have a lot of flat and long geometries.
Iterating calls to the function to get a smaller and smaller error in the camera position. This has worked somewhat, but I can sometimes run into weird edge cases where the camera position overshoots too much and the error factor increases. Also, when doing this I didn't recenter the model based on the position of the bounding rectangle. I couldn't find a solid, robust way to do that reliably.
Help please!
3,39211333
There are many possible camera positions + orientations where the bounding box would fit inside the view frustum. But any procedure would select one specific camera position and orientation.
If you would consider bounding spheres, one solution could be to
first change orientation to look at bounding sphere center
then move back sufficiently (negative look direction) for bounding sphere to fit inside frustum
With bounding boxes you could consider an earlier step of first positioning the camera at perpendicular to the center of the largest (or smallest, whatever you prefer) cube face.
I have no experience with DirectX, but moving and changing the looking direction of the camera to center a certain point should be easy.
The hard part is to do the math of deciding how far to move to view the object.
If you know the bounding size s of the object in world coordinates (we are not interested in pixels or camera coordinates, since those are dependent on your distance) from the orientation of the camera, you can compute the required distance d of the camera to the bounding shape if you know the x and y Field-Of-View angle a of the perspective projection.
*bounding box
| BB size s
camera -----
|-------------------|
distance d
is tan(a/2) = (s/2) / d => d = (s/2) / tan(a/2)
Which will give you the distance the camera should be placed from the closest bounding surface.
Since you have a bounding box, you should have a basis describing it's orientation.
It seems that you want to position the camera on the line coincident with the basis vector describing the smallest dimension of the box, then roll the camera so that the largest dimension is horizontal (assuming you have OBB and not AABB). This assumes that the aspect ratio is greater than 1.0; if not you'll want to use the vertical dimension.
What I would attempt:
Find the smallest box dimension.
Find the associated basis vector.
Scale the basis vector by the distance from the center of the box the camera should be. This distance is just boxWidth / (2 * tan(horizontalFov / 2)). Note that boxWidth is the width of the largest dimension of the box.
Place the camera at boxCenter + scaledBasis looking at the boxCenter.
Roll the camera if necessary to align the camera's up vector with the appropriate box basis vector.
So I think what you're getting at is that you have the camera at an arbitrary position looking somewhere, and you have an AABB at another position.
Without moving the camera to face a side of the box, you want to:
Look at the center of the box
Translate the camera along it's look vector so that the box takes the maximum amount of screen space
If this is the case you'll
here's what I suggest:
Rotate the camera to look at the center of the bounding box.
Project all the points of the box into screen space and find the min/max bounding box in screen space (you already have this).
Now Unproject two opposing corners of the screen space bounding box into world space.
For a Z value use the closest world space points of your AABB to the camera.
This should get you a world space plane facing the camera positioned at the point on the AABB that is closest to the camera.
Now use our existing side-facing method to move the camera to the appropriate spot, treating this plane as the side of your box.
I know there are some excellent answers above, but I wanted to add a rediculously simple solution to fit the bounding sphere inside the camera frustrum. It makes the assumption that you want to keep the camera Target and Forward vector the same, and simply adjust camera distance to target.
Note, this won't give you the best fit but it will give you an approximate fit, showing all geometry, and only in a few lines of code, and without screen to world transformations
// Compute camera radius to fit bounding sphere
// Implementation in C#
// Given a bounding box around your scene
BoundingBox bounds = new BoundingBox();
// Compute the centre point of the bounding box
// NOTE: The implementation for this is to take the mid-way point between
// two opposing corners of the bounding box
Vector3 center = bounds.C
// Find the corner of the bounding box which is maximum distance from the
// centre of the bounding box. Vector3.Distance computes the distance between
// two vectors. Select is just nice syntactic sugar to loop
// over Corners and find the max distance.
double boundSphereRadius = bounds.Corners.Select(x =& Vector3.Distance(x, bounds.Center)).Max();
// Given the camera Field of View in radians
double fov = Math3D.DegToRad(FieldOfView);
// Compute the distance the camera should be to fit the entire bounding sphere
double camDistance = (boundSphereRadius * 2.0) / Math.Tan(fov / 2.0);
// Now, set camera.Target to bounds.Center
// set camera.Radius to camDistance
// Keep current forward vector the same
The implementation of BoundingBox in C# is found below. The important points are the Centre and Corners properties. Vector3 is a pretty standard implementation of a 3 component (X,Y,Z) vector
public struct BoundingBox
public Vector3 Vec0;
public Vector3 Vec1;
public BoundingBox(Vector3 vec0, Vector3 vec1)
Vec0 = vec0;
Vec1 = vec1;
public Vector3 Center
get { return (Vec0 + Vec1)*0.5; }
public IList&Vector3& Corners
Vector3[] corners = new[]
new Vector3( Vec0.X, Vec0.Y, Vec0.Z ),
new Vector3( Vec1.X, Vec0.Y, Vec0.Z ),
new Vector3( Vec0.X, Vec1.Y, Vec0.Z ),
new Vector3( Vec0.X, Vec0.Y, Vec1.Z ),
new Vector3( Vec1.X, Vec1.Y, Vec0.Z ),
new Vector3( Vec1.X, Vec0.Y, Vec1.Z ),
new Vector3( Vec0.X, Vec1.Y, Vec1.Z ),
new Vector3( Vec1.X, Vec1.Y, Vec1.Z ),
10.8k23895
I don't have it at hand at the moment but the book you want is
He has a whole chapter on this
This is copied straight from my engine, it creates 6 planes which represent each of the six sides of the frutsum.
I hope it comes in useful.
internal class BoundingFrustum
private readonly float4x4 matrix = new float4x4(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
private readonly Plane[]
internal BoundingFrustum(float4x4 value)
planes = new Plane[6];
for (int i = 0; i & 6; i++)
planes[i] = new Plane();
Setfloat4x4(value);
private void Setfloat4x4(float4x4 value)
planes[2].Normal.X = -value.M14 - value.M11;
planes[2].Normal.Y = -value.M24 - value.M21;
planes[2].Normal.Z = -value.M34 - value.M31;
planes[2].D = -value.M44 - value.M41;
planes[3].Normal.X = -value.M14 + value.M11;
planes[3].Normal.Y = -value.M24 + value.M21;
planes[3].Normal.Z = -value.M34 + value.M31;
planes[3].D = -value.M44 + value.M41;
planes[4].Normal.X = -value.M14 + value.M12;
planes[4].Normal.Y = -value.M24 + value.M22;
planes[4].Normal.Z = -value.M34 + value.M32;
planes[4].D = -value.M44 + value.M42;
planes[5].Normal.X = -value.M14 - value.M12;
planes[5].Normal.Y = -value.M24 - value.M22;
planes[5].Normal.Z = -value.M34 - value.M32;
planes[5].D = -value.M44 - value.M42;
planes[0].Normal.X = -value.M13;
planes[0].Normal.Y = -value.M23;
planes[0].Normal.Z = -value.M33;
planes[0].D = -value.M43;
planes[1].Normal.X = -value.M14 + value.M13;
planes[1].Normal.Y = -value.M24 + value.M23;
planes[1].Normal.Z = -value.M34 + value.M33;
planes[1].D = -value.M44 + value.M43;
for (int i = 0; i & 6; i++)
float num2 = planes[i].Normal.Length();
planes[i].Normal = planes[i].Normal / num2;
planes[i].D /= num2;
internal Plane Bottom
get { return planes[5]; }
internal Plane Far
get { return planes[1]; }
internal Plane Left
get { return planes[2]; }
internal Plane Near
get { return planes[0]; }
internal Plane Right
get { return planes[3]; }
internal Plane Top
get { return planes[4]; }
3,82032464
I had same problem.
Maybe this link can help :)
Check this link
float distance = sphere.radius / sin(fov / 2);
float3 eyePoint = sphere.centerPoint - distance * camera.frontV
Your Answer
Sign up or
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Post as a guest
By posting your answer, you agree to the
Not the answer you're looking for?
Browse other questions tagged
Stack Overflow works best with JavaScript enabled 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
数控机数控车床常用术语中英文对照表
下载积分:1000
内容提示:数控机数控车床常用术语中英文对照表
文档格式:DOC|
浏览次数:17|
上传日期: 23:08:01|
文档星级:
该用户还上传了这些文档
数控机数控车床常用术语中英文对照表
官方公共微信TABLE OF CONTENTS
I assume that you have some knowledge of OpenGL. Otherwise, read &&.
Example 1: 3D Shapes (OGL01Shape3D.cpp)
This example is taken from Nehe OpenGL Tutorial Lesson # 5 (@ ), which displays a 3D color-cube and a pyramid. The cube is made of of 6 quads, each having different colors. The hallow pyramid is made up of 4 triangle, with different colors on each of the vertices.
#include &windows.h&
#include &GL/glut.h&
char title[] = &3D Shapes&;
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.5f, 0.0f, -7.0f);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f,
glVertex3f( 1.0f, 1.0f,
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f( 1.0f, -1.0f,
glVertex3f(-1.0f, -1.0f,
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 1.0f,
1.0f, 1.0f);
glVertex3f(-1.0f,
1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f,
1.0f, -1.0f);
glVertex3f( 1.0f,
1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f,
glVertex3f(-1.0f,
1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f,
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f,
1.0f, -1.0f);
glVertex3f(1.0f,
glVertex3f(1.0f, -1.0f,
glVertex3f(1.0f, -1.0f, -1.0f);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glutSwapBuffers();
void reshape(GLsizei width, GLsizei height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)width / (GLfloat)
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutInitWindowPosition(50, 50);
glutCreateWindow(title);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
GLUT Setup - main()
The program contains a initGL(), display() and reshape() functions.
The main() program:
Initializes the GLUT.
Creates a window with a title, initial width and height positioned at initial top-left corner.
Registers display() as the re-paint event handler. That is, the graphics sub-system calls back display() when the window first appears and whenever there is a re-paint request.
Registers reshape() as the re-sized event handler. That is, the graphics sub-system calls back reshape() when the window first appears and whenever the window is re-sized.
Enables double buffering. In display(), we use glutSwapBuffers() to signal to the GPU to swap the front-buffer and back-buffer during the next VSync (Vertical Synchronization).
Invokes the initGL() once to perform all one-time initialization tasks.
Finally, enters the event-processing loop.
One-Time Initialization Operations - initGL()
The initGL() function performs the one-time initialization tasks. It is invoked from main() once (and only once).
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Set the clearing (background) color to black (R=0, G=0, B=0) and opaque (A=1), and the clearing (background) depth to the farthest (Z=1). In display(), we invoke glClear() to clear the color and depth buffer, with the clearing color and depth, before rendering the graphics. (Besides the color buffer and depth buffer, OpenGL also maintains an accumulation buffer and a stencil buffer which shall be discussed later.)
glDepthFunc(GL_LEQUAL);
We need to enable depth-test to remove the hidden surface, and set the function used for the depth test.
We enable smooth shading in color transition. The alternative is GL_FLAT. Try it out and see the difference.
In graphics rendering, there is often a trade-off between processing speed and visual quality. We can use glHint() to decide on the trade-off. In this case, we ask for the best perspective correction, which may involve more processing. The default is GL_DONT_CARE.
Defining the Color-cube and Pyramid
OpenGL's object is made up of primitives (such as triangle, quad, polygon, point and line). A primitive is defined via one or more vertices. The color-cube is made up of 6 quads. Each quad is made up of 4 vertices, defined in counter-clockwise (CCW) order, such as the normal vector is pointing out, indicating the front face. All the 4 vertices have the same color. The color-cube is defined in its local space (called model space) with origin at the center of the cube with sides of 2 units.
Similarly, the pyramid is made up of 4 triangles (without the base). Each triangle is made up of 3 vertices, defined in CCW order. The 5 vertices of the pyramid are assigned
different colors. The color of the triangles are interpolated (and blend smoothly) from its 3 vertices. Again, the pyramid is defined in its local space with origin at the center of the pyramid.
Model Transform
The objects are defined in their local spaces (model spaces). We need to transform them to the common world space, known as model transform.
To perform model transform, we need to operate on the so-called model-view matrix (OpenGL has a few transformation matrices), by setting the current matrix mode to model-view matrix:
We perform translations on cube and pyramid, respectively, to position them on the world space:
glLoadIdentity();
glTranslatef(1.5f, 0.0f, -7.0f);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
View Transform
The default camera position is:
gluLookAt(0.0, 0.0, 0.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0)
That is, EYE=(0,0,0) at the origin, AT=(0,0,-100) pointing at negative-z axis (into the screen), and UP=(0,1,0) corresponds to y-axis.
OpenGL graphics rendering pipeline performs so-called view transform to bring the world space to camera's view space. In the case of the default camera position, no transform is needed.
Viewport Transform
The graphics sub-system calls back reshape() when the window first appears and whenever the window is resized, given the new window's width and height, in pixels. We set our application viewport to cover the entire window, top-left corner at (0, 0) of width and height, with default minZ of 0 and maxZ of 1. We also use the same aspect ratio of the viewport for the projection view frustum to prevent distortion. In the viewport, a pixel has (x, y) value as well as z-value for depth processing.
Projection Transform
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
A camera has limited field of view. The projection models the view captured by the camera. There are two types of projection: perspective projection and orthographic projection.
In perspective projection, object further to the camera appears smaller compared with object of the same size nearer to the camera. In orthographic projection, the objects appear the same regardless of the z-value. Orthographic projection is a special case of perspective projection where the camera is placed very far away. We shall discuss the orthographic projection in the later example.
To set the projection, we need to operate on the projection matrix. (Recall that we operated on the model-view matrix in model transform.)
We set the matrix mode to projection matrix and reset the matrix. We use the gluPerspective() to enable perspective projection, and set the fovy (view angle from the bottom-plane to the top-plane), aspect ratio (width/height), zNear and zFar of the View Frustum (truncated pyramid). In this example, we set the fovy to 45&. We use the same aspect ratio as the viewport to avoid distortion. We set the zNear to 0.1 and zFar to 100 (z=-100). Take that note the color-cube (1.5, 0, -7) and the pyramid (-1.5, 0, -6) are contained within the View Frustum.
The projection transform transforms the view frustum to a 2x2x1 cuboid clipping-volume centered on the near plane (z=0). The subsequent viewport transform transforms the clipping-volume to the viewport in screen space. The viewport is set earlier via the glViewport() function.
Example 2: 3D Shape with Animation (OGL02Animation.cpp)
Let's modify the previous example to carry out animation (rotating the cube and pyramid).
#include &windows.h&
#include &GL/glut.h&
char title[] = &3D Shapes with animation&;
GLfloat anglePyramid = 0.0f;
GLfloat angleCube = 0.0f;
int refreshMills = 15;
void initGL() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(1.5f, 0.0f, -7.0f);
glRotatef(angleCube, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f,
glVertex3f( 1.0f, 1.0f,
glColor3f(1.0f, 0.5f, 0.0f);
glVertex3f( 1.0f, -1.0f,
glVertex3f(-1.0f, -1.0f,
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 1.0f,
1.0f, 1.0f);
glVertex3f(-1.0f,
1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f,
1.0f, -1.0f);
glVertex3f( 1.0f,
1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f,
glVertex3f(-1.0f,
1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f,
glColor3f(1.0f, 0.0f, 1.0f);
glVertex3f(1.0f,
1.0f, -1.0f);
glVertex3f(1.0f,
glVertex3f(1.0f, -1.0f,
glVertex3f(1.0f, -1.0f, -1.0f);
glLoadIdentity();
glTranslatef(-1.5f, 0.0f, -6.0f);
glRotatef(anglePyramid, 1.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glutSwapBuffers();
anglePyramid += 0.2f;
angleCube -= 0.15f;
void timer(int value) {
glutPostRedisplay();
glutTimerFunc(refreshMills, timer, 0);
void reshape(GLsizei width, GLsizei height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)width / (GLfloat)
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutInitWindowPosition(50, 50);
glutCreateWindow(title);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
glutMainLoop();
The new codes are:
GLfloat angleCube = 0.0f;
int refreshMills = 15;
We define two global variables to keep track of the current rotational angles of the cube and pyramid. We also define the refresh period as 15 msec (66 frames per second).
&&&glutTimerFunc(refreshMills, timer, 0); //
To perform animation, we define a function called timer(), which posts a re-paint request to activate display() when the timer expired, and then run the timer again. In main(), we perform the first timer() call via glutTimerFunc(0, timer, 0).
glRotatef(anglePyramid, 1.0f, 1.0f, 0.0f);
anglePyramid += 0.2f;
angleCube -= 0.15f;
In display(), we rotate the cube and pyramid based on their rotational angles, and update the angles after each refresh.
Example 3: Orthographic Projection (OGL03Orthographic.cpp)
As mentioned, OpenGL support two type of projections: perspective and orthographic. In orthographic projection, an object appears to be the same size regardless of the depth. Orthographic is a special case of perspective projection, where the camera is placed very far away.
To use orthographic projection, change the reshape() function to invoke glOrtho().
void reshape(GLsizei width, GLsizei height) {
if (height == 0) height = 1;
GLfloat aspect = (GLfloat)width / (GLfloat)
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width &= height) {
glOrtho(-3.0 * aspect, 3.0 * aspect, -3.0, 3.0, 0.1, 100);
glOrtho(-3.0, 3.0, -3.0 / aspect, 3.0 / aspect, 0.1, 100);
In this example, we set the cross-section of view-volume according to the aspect ratio of the viewport, and depth from 0.1 to 100, corresponding to z=-0.1 to z=-100. Take note that the cube and pyramid are contained within the view-volume.
Example 4: Vertex Array
In the earlier example, drawing a cube requires at least 24 glVertex functions and a pair of glBegin and glEnd. Function calls may involve high overhead and hinder the performance. Furthermore, each vertex is specified and processed three times.

我要回帖

更多关于 digital projection 的文章

 

随机推荐