OpenGL coordinates start in the middle?
-
- Member
- Posts: 71
- Joined: Wed May 29, 2013 1:07 pm
OpenGL coordinates start in the middle?
Hi is their anyway to make the coordinates of OpenGL start at the left rather than the middle of the screen? or would I have to hard code some math instead?
1100110100010011
Re: OpenGL coordinates start in the middle?
simply do a transform in shader
Re: OpenGL coordinates start in the middle?
normally screen coordinate has to do with the projection matrix and the viewport, you should use glFrustum or glOrtho to setup the coordinate system which determine how vertex will be projected in the screen space, but with frustum and perspective, i'm not sure it's really a good idea to have the origin of the space in a corner, if you just use it for 2D rendering without perspective you can use glOrtho to setup the projection matrix to fit the viewport coordinate if you want a pixel-like coordinate system
Re: OpenGL coordinates start in the middle?
Fixed pipeline has become less supported or emulated in software drivers. You should use shader and do the transformation there.h0bby1 wrote:you should use glFrustum or glOrtho to setup the coordinate system...
Re: OpenGL coordinates start in the middle?
but you need to setup the matrix you will use for the transformation in order to do the transformation in the shader, the modelview/projection matrix are also handled in the GLSL, and the default pipeline of the shader should already use the projection matrix to do the transformation to screen space and it not efficient and not recommanded to do operation on matrix in shader, because it will be computed for each vertex whereas it will be constant for all vertices to be transformedbluemoon wrote:Fixed pipeline has become less supported or emulated in software drivers. You should use shader and do the transformation there.h0bby1 wrote:you should use glFrustum or glOrtho to setup the coordinate system...
the GLSL would just look like position= vertex*modelview*projection, and the projection matrix still need to be setup before to execute the shader
even the most broken opengl implementation should still handle the modelview/projection matrix transformation correctly without the need to write a custom shader, even if a shader is used, the projection matrix still need to be setup with either glOrtho/glFrustum, or either computed manually and either loaded with a loadmatrix or passed as an uniform to the shader
Last edited by h0bby1 on Sat Aug 31, 2013 2:00 am, edited 1 time in total.
Re: OpenGL coordinates start in the middle?
Yes, but not with glFrustum or glOrtho.
Fir a simplified 2D shader (I use in my simple UI shader), it looks like this in OpenGL 2.1:
Or, for OpenGL 2.0 ES:
To setup screen coordinate system, you set custom attribute (u_ScreenHalf in the above example) instead of the 2 fixed pipeline functions.
OpenGL itself would not care much on the maths, and it only take final result which is bounded to [-1,1].
Fir a simplified 2D shader (I use in my simple UI shader), it looks like this in OpenGL 2.1:
Code: Select all
uniform vec2 u_ScreenHalf;
varying vec4 v_Color;
varying vec2 v_TexCoordinate;
void main() {
v_Color = gl_Color;
v_TexCoordinate = gl_MultiTexCoord0.st;
gl_Position = vec4(gl_Vertex.x/u_ScreenHalf.x - 1.0, gl_Vertex.y/u_ScreenHalf.y - 1.0, 0.0, 1.0);
}
Code: Select all
precision mediump float;
uniform vec2 u_ScreenHalf;
attribute vec2 in_Position;
attribute vec4 in_Color;
attribute vec2 in_Tex;
varying vec4 v_Color;
varying vec2 v_TexCoordinate;
void main() {
v_Color = in_Color;
v_TexCoordinate = in_Tex;
gl_Position = vec4(in_Position.x/u_ScreenHalf.x - 1.0, in_Position.y/u_ScreenHalf.y - 1.0, 0.0, 1.0);
}
OpenGL itself would not care much on the maths, and it only take final result which is bounded to [-1,1].
Re: OpenGL coordinates start in the middle?
if you use it only for 2D rendering without perspective or depth buffer it should work, but the actual math of transforming the vertex using orthogonal projection matrix would come to the same computation
the orthogonal matrix should look like that
and multiplying the vertex by it should give similar operation to what you do, and it should really be handled correctly with any opengl pipeline if this matrix is loaded into the projection matrix unless it's really broken
but it can be just easier to just compute cheaply vertex position like you do as well, if it only has to handle the simplest case of pure 2D rendering with coordinate mapped to screen coordinate, but even if the plateform or the opengl doesn't support projection/modelview matrix, you can pass it as an uniform to the shader, but in the case it can be simpler to do with your method if you just handle 2D coordinate
the orthogonal matrix should look like that
and multiplying the vertex by it should give similar operation to what you do, and it should really be handled correctly with any opengl pipeline if this matrix is loaded into the projection matrix unless it's really broken
but it can be just easier to just compute cheaply vertex position like you do as well, if it only has to handle the simplest case of pure 2D rendering with coordinate mapped to screen coordinate, but even if the plateform or the opengl doesn't support projection/modelview matrix, you can pass it as an uniform to the shader, but in the case it can be simpler to do with your method if you just handle 2D coordinate