61阅读

一切关于我和你-关于GL_PROJECTION和GL_MODELVIEW

发布时间:2018-01-06 所属栏目:关于紫薇的一切

一 : 关于GL_PROJECTION和GL_MODELVIEW

OpenGL(2)-----关于GL_PROJECTION和GL_MODELVIEW (转) Cameras are not the right way to think about OpenGL

Sigh, the tales of an old programmer. I'm teaching myself 3d graphics. My bag of 2d tricks has been used up, 3d has been around for almost 10 years in the mainstream, and it's time to move on.

To learn, I set out to make a little 3d representation of the mandelbrot set. I thought I could just whip that baby out in no time. But, as it turned out, I can't. I had to overcome some basic hurdles with Opengl.

A few things went through my head.

A lot of OpenGL tutorials talk about cameras, and yet, OpenGL doesn't have cameras. Instead, you get a bevy of matrixes and glMatrixModes. Computer science is full of horrific analogies, and I really think that "camera" metaphor people use to describe OpenGL is one of them. I ask myself, in my best Battlestar Galactica parlance, "what the frak going on!!!!"

As I was smashing my keyboard with my fist, causing my shepherd to hide under the bed, two things dawned on me. First is, thank God Logitech keyboards are indestructible. Second, all these matrixes do is move pixels around. There's nothing magic going on. The camera is a lie.

OpenGL is about a set of vertices, or points, in a 3d space. You draw them into 3d coordinates using any of various primitives, such as TRIANGLE_FAN, TRIANGLE_LIST, etc. OpenGL marches through each of these points and "transforms" them into a set of coordinates that look three-dish on screen.

Thus, when you specify various transformations, you are telling OpenGL how to move your points around. With that perspective, you can understand GL_PROJECTION and GL_MODELVIEW this way:

GL_PROJECTION is a matrix transformation that is applied to every point that comes after it, always. GL_MODELVIEW is a matrix transformation that is applied to every point in a particular model.There's a hierarchy of transformations, with GL_PROJECTION at the top, and a set of GL_MODEL branches. You set the matrix transformation with glMatrixMode. By default, the matrix mode is GL_MODELVIEW, which assumes everything you draw will be in one 3d space.

wowmodelview 关于GL_PROJECTION和GL_MODELVIEW

Let's talk about GL_PROJECTION first.

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-1, 1, -1, 1, -1.0, 1.0);

It's all about matrix math. I basically use the glOrtho to apply some perspective to what I'm doing. Everything in 3d graphics is about matrix math.

Once you set the matrix mode, each subsequent operation is applied to that particular matrix mode and below it. They almost should have called it matrix levels and not matrix modes. I load identity first to give it a place to start because most of the gl matrix commands work by multiplying themselves against whatever the matrix was for that mode before. For example:

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-1, 1, -1, 1, -1.0, 1.0);

glTranslate( 100, 100, 100 );

glRotateF( 45, 1, 0, 0 );

Really means: GL_PROJECTION_MATRIX = IDENTITY * ORTHOGRAPHIC_MATRIX *

TRANSLATION_MATRIX * ROTATION_MATRIX.

The order of these calls seems extremely important.

So what's MODEL_VIEW all about? GL_PROJECTION transformations are always applied, so, we can use it to define a camera, set the perspective, among other things. What model view lets us to do is set up different measuring systems for vertices of different things. I select model view by calling

wowmodelview 关于GL_PROJECTION和GL_MODELVIEW

glMatrixMode(GL_MODELVIEW);

Then I can apply stuff to the model:

glLoadIdentity();

glTranslate( modelx, modely, modelz );

To move it in space somewhere, for example.

GL_MODELVIEW is about having different objects being pushed into a "world space". The biggest reason is that I can draw each object using coordinates based around 0, merely specifying the translations or rotations or scaling based on how I want my model to go.

To really appreciate this, let's consider a simple example. I want to draw a world that has two things in it, a car and a battleship. To do this, I might:

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-1, 1, -1, 1, -1.0, 1.0);

glTranslate( camerax, cameray, cameraz );

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

glTranslate( carx, cary, carz );

// draw car here, by specifying various gl vertices

wowmodelview 关于GL_PROJECTION和GL_MODELVIEW

glLoadIdentity();

glTranslate( battleshipx, battleshipy, battleshipz );

// draw battleship here, by specifying various gl vertices

In that example, each car vertice I specify is being altered as follows:

car_vertice_matrix = projection_ortho_matrix * projection_translation *

car_translation_matrix

61阅读提醒您本文地址:

and, for the battleship

battleship_vertice_matrix = projection_ortho_matrix * projection_translation * battleship_translation_matrix

So, to actually draw the stuff, OpenGl takes the projectionmatrix multiplied by the current model matrix and uses that to translate each specific vertice within that model. Wow, 3d graphics is not hard. Hell I should go write my own OpenGL. You know, if there wasn't the issue of hardware accelaration, it would betempting. Well, I still got a ways to go.

I'll have my little multithreaded 3d demo up shortly. There's actually a bug in my thread pool that I will be posting a fix for as well.

I remember reading somewhere in a 3d graphics book that the glProjection matrix should not be used to fake a camera. I think that was an evil thing to remember. But, wow, it just seems like it would be such a good spot to do it. What I have to try is something like this:

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(-1, 1, -1, 1, -1.0, 1.0);

glTranslate( camerax, cameray, cameraz );

glRotateF( cameraanglex, 1, 0, 0 );

wowmodelview 关于GL_PROJECTION和GL_MODELVIEW

In that case, the glTranslate would be where the camera is, and glRotateF, where it is facing.

Talking about cameras

Now, I know I said that the camera is a lie. It is, because its pretty obvious that we're really moving the entire world to get our picture, and not some "camera".

61阅读提醒您本文地址:

二 : 删除关于你的一切

就在今天我把关于你的一切都删除掉了很舍不得但是我还是这么做了

不知道当时你在我的空间里写下想我的时候是什么样的心情现在现在什么都没有了不想让你的痕迹留在我这里不想让我看到你的身影我们只当是陌路吧

真不知道当时你为什么要来招惹我真的其实挺恨你的恨死了

当我听着你讲你自己的爱情我还是祝福你祝你幸福

我从头再来就在今天

三 : 有关我和你。

世界上只有两个人我看不透。一个是我,一个是你。

世界上只有两个人我想保护。

一个是我,一个是你。

世界上只有两个人我会放下伪装。

一个是我。

一个是你。( 文章阅读网:www.61k.com )

四 : 关于梦和你

黑夜慢慢降临,悄悄的,无声无息。

黄色的灯光一直闪耀着,任冷风包围着我。

何必咎由自取,成为最讨厌的模样。

风声羞涩刮过耳旁,曾经也是那样。

我深知,我的心归宿消失了,我不知道是否还会出现。

你走过的路,牵着手,如今一地落叶寒。( 文章阅读网:www.61k.com )

每一个都害怕失去,一如过往。

我没有一次被原谅的机会,只能怪我。

我不曾记得我伤害过多少人,这番滋味。

一切轮回,自找痛悲。

你的心里面,是否还留着我的一丝足迹。

无论如何,我都不能成为你最讨厌的人。

一个朋友,一个最熟悉的陌生人。

我仿佛记起了多少年我也说过如此的话,大笑话。

一种满足感自卑的残忍的环绕身体每一寸细胞。

原来每一个人都一样,习惯感伤。

我以为可以,却忘了。

没那个资格去拥抱你。

熬过年少轻狂,熬过你闺女出房,你接受我可好。

待那时我年华正貌,资质正好。

三年,四年,六年。

我知道我会遇到多少个人。

可人生,可青春。

相爱的人不能在一起就是笑话。

人相逢,戏一场,不得心愿,闭眼归土。

宁愿你说起最心痛的答案。

你怎知我内心的狂热一直未变

稳稳的幸福你还记得它吗。

我写的也许你都不曾看到,可是我。

一言不语,好比江鸟飞鸿。

慢慢的,慢慢的。

时间会把我变成什么模样

会把你变成什么模样

十字未过的年龄怎能天长地久

让时间说话,让时间告诉你真话。

本文标题:一切关于我和你-关于GL_PROJECTION和GL_MODELVIEW
本文地址: http://www.61k.com/1127852.html

61阅读| 精彩专题| 最新文章| 热门文章| 苏ICP备13036349号-1