开始使用OpenGL ES。绘图使用像素像素、OpenGL、ES

2023-09-09 21:11:22 作者:村姑也狠潮

我开始学习开GL的机器人(GL10)使用Java和我跟着一些教程绘制方形,三角形等。

现在我开始画一些想法我有,但我真的很困惑与屏幕的绘图vertexs。当我画使用OpenGL ES的东西,我必须指定我想提请屏幕的一部分,同为纹理...

于是,我开始做一些测试,我打印了全屏质感与此vertexs:

 ( -  1,-1,//左上
 -1,1,//左下
 1,-1,//右上
 1,1); //右下角
 

这是为什么全屏?是不是OpenGL的中心坐标在左上角(0,0)?为什么与vertexs平局是正确的?看来,该中心确实是在屏幕的真正中心,宽度和高度为-1 ... 1,但我真的不知道,因为我认为,该中心是在左上角...

另外一个问题...我读了很多C ++ code,他们打印使用像素。看来使用像素视频游戏真的necesary,因为需要的东西的确切位置,并用-1 ... 1我不能真的precise。如何使用像素,而不是-1 ... 1?

真是感谢和抱歉我的英文不好。谢谢

解决方案   

这是为什么全屏?是不是OpenGL的中心坐标在左上角(0,0)?为什么与vertexs平局是正确的?看来,该中心确实是在屏幕的真正中心,宽度和高度为-1 ... 1,但我真的不知道,因为我认为,该中心是在左上角...

在 iOS 中使用 OpenGL ES 实现绘画板

有3样东西撞在了一起。所谓的视的,即所谓的标准化设备坐标的(NDC),并从模型空间投射到眼位夹的空间NDC空间。

视口选择你的窗口的部分成NDC的范围

  [ -  1 ... 1]×[-1 ... 1]
 

映射到。该函数签名是 glViewport(X,Y​​,宽,高)。 OpenGL的假设坐标系与NDC的x坐标上升,因为去的权利和不断上升的NDC y坐标往上走。

所以,如果你调用 glViewport(0,0,window_width,window_height),这也是第一次绑定到一个窗口,一个OpenGL上下文后,默认情况下, NDC坐标(-1,-1)将在左下角和NDC的坐标(1,1)中的窗口的右上方的角

OpenGL的开始,所有的转换被设置为标识,这意味着,该顶点坐标你穿过的获得权利,通过NDC空间,而且PTED这样的跨$ P $。然而,大多数的你申请到后续的转换时间在OpenGL:

模型视图

预测

的模型视图变换用于世界各地的移动在前面的静止眼/相机(其总是位于(0,0,0))。放置一个摄像头只是意味着,将整个世界(图变换)的额外改造,这就是你如何将相机在世界上正好相反。 固定功能OpenGL调用这个的模型视图矩阵,如果矩阵模式已被设置为被访问的GL_MODELVIEW。

投影变换是一种OpenGL的镜头。您使用它,如果它是一个宽或小的角度(在立体的)或长方体(邻投影)或者甚至不同的东西的边缘设置。 固定功能OpenGL调用这个的投影矩阵,如果矩阵模式已被设置为被访问的GL_PROJECTION。

在投影原语被剪辑,然后在所谓的同质除法的应用,这是创建实际立体效果,如果一个透视投影已经应用

在这一点上的顶点已变成国家数据中心的空间,然后被映射到视口在一开始解释。

对于你的问题:你想要的是一个的预测的映射顶点坐标1:1到视像素。很容易:

  glViewport(0,0,宽度,高度);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
如果(origin_lower_left){
    glOrtho(0,宽度,高度,0,-1,1);
} 其他 {
    glOrtho(0,宽度,0,高度,-1,1);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
 

现在顶点坐标图,视口像素。

更新:由三角形绘制完整的视口纹理四:

的OpenGL 2.1和OpenGL ES-1

 无效fullscreenquad(INT宽度,高度INT,GLuint纹理)
{
    GLfloat VTCS [] = {
      0,0,
      1,0,
      1,1,
      0,1
    };

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    的glVertexPointer(2,GL_FLOAT,0,VTCS);
    glTexCoordPointer(2,GL_FLOAT,0,VTCS);

    glViewport(0,0,宽度,高度);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0,1,0,1,-1,1);

    glMatrixMode(GL_MODELVIEW);
    过glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,质地);
    (4,8 GL_TRIANGLE_FAN,0,4);
}
 

I'm starting to learn open GL in android (GL10) using java and I followed some tutorials to draw squares, triangles, etc.

Now I'm starting to draw some ideas I have but I'm really confused with the drawing vertexs of the screen. When I draw something using openGL ES, I have to specify the part of the screen I want to draw and the same for the texture...

So I started to make some tests and I printed a fullscreen texture with this vertexs:

(-1, -1, //top left
 -1, 1, //bottom left
 1, -1, //top right
 1, 1); //bottom right

Why is this fullscreen? Isn't the center of OpenGL coordinates at top left (0, 0)? Why with that vertexs the draw is correct? It seems that the center is really the real center of the screen and the width and height is from -1...1, but I dont really understand it because I thought that the center was at the top left...

Another question... I read a lot of c++ code where they print using pixels. It seems really necesary in videogames using pixels because needs the exact position of the things, and with -1...1 I cant be really precise. How can I use pixels instead of -1...1?

Really thanks and sorry about my poor english. Thanks

解决方案

Why is this fullscreen? Isn't the center of OpenGL coordinates at top left (0, 0)? Why with that vertexs the draw is correct? It seems that the center is really the real center of the screen and the width and height is from -1...1, but I dont really understand it because I thought that the center was at the top left...

There are 3 things coming together. The so called viewport, the so called normalized device coordinates (NDC), and the projection from model space to eye space to clip space to NDC space.

The viewport selects the portion of your window into which the NDC range

[-1…1]×[-1…1]

is mapped to. The function signature is glViewport(x, y, width, height). OpenGL assumes a coordinate system with rising NDC x-coordinates as going to the right and rising NDC y-coordinates going up.

So if you call glViewport(0, 0, window_width, window_height), which is also the default after a OpenGL context is bound the first time to a window, the NDC coordinate (-1, -1) will be in the lower left and the NDC coordinate (1,1) in the upper right corners of the window.

OpenGL starts with all transformations being set to identity, which means, that the vertex coordinates you pass through are getting right through to NDC space and are interpreted like this. However most of the time in OpenGL you're applying to successive transformations:

modelview

and

projection

The modelview transformation is used to move around the world in front of the stationary eye/camera (which is always located at (0,0,0)). Placing a camera just means, adding an additional transformation of the whole world (view transformation), that's the exact opposite of how you'd place the camera in the world. Fixed function OpenGL calls this the MODELVIEW matrix, being accessed if matrix mode has been set to GL_MODELVIEW.

The projection transformation is kind of the lens of OpenGL. You use it to set if it's a wide or small angle (in case of perspective) or the edges of a cuboid (ortho projection) or even something different. Fixed function OpenGL calls this the PROJECTION matrix, being accessed if matrix mode has been set to GL_PROJECTION.

After the projection primitives are clipped, and then the so called homogenous divide applied, which is creating the actual perspective effect, if a perspective projection has been applied.

At this point vertices have been transformed into NDC space, which then gets mapped to the viewport as explained in the beginning.

Regarding your problem: What you want is a projection that maps vertex coordinates 1:1 to viewport pixels. Easy enough:

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if( origin_lower_left ) {
    glOrtho(0, width, height, 0, -1, 1);
} else {
    glOrtho(0, width, 0, height, -1, 1);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Now vertex coordinates map to viewport pixels.

Update: Drawing a full viewport textured quad by triangles:

OpenGL-2.1 and OpenGL ES-1

void fullscreenquad(int width, int height, GLuint texture)
{
    GLfloat vtcs[] = {
      0, 0,
      1, 0,
      1, 1,
      0, 1
    };

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);

    glVertexPointer(2, GL_FLOAT, 0, vtcs);
    glTexCoordPointer(2, GL_FLOAT, 0, vtcs);

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1, 0, 1, -1, 1);

    glMatrixMode(GL_MODELVIEW);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texture);
    glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
}