OpenGL coordinates start in the middle?

Programming, for all ages and all languages.
Post Reply
computertrick
Member
Member
Posts: 71
Joined: Wed May 29, 2013 1:07 pm

OpenGL coordinates start in the middle?

Post by computertrick »

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
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: OpenGL coordinates start in the middle?

Post by bluemoon »

simply do a transform in shader
h0bby1
Member
Member
Posts: 240
Joined: Wed Aug 21, 2013 7:08 am

Re: OpenGL coordinates start in the middle?

Post by h0bby1 »

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
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: OpenGL coordinates start in the middle?

Post by bluemoon »

h0bby1 wrote:you should use glFrustum or glOrtho to setup the coordinate system...
Fixed pipeline has become less supported or emulated in software drivers. You should use shader and do the transformation there.
h0bby1
Member
Member
Posts: 240
Joined: Wed Aug 21, 2013 7:08 am

Re: OpenGL coordinates start in the middle?

Post by h0bby1 »

bluemoon wrote:
h0bby1 wrote:you should use glFrustum or glOrtho to setup the coordinate system...
Fixed pipeline has become less supported or emulated in software drivers. You should use shader and do the transformation there.
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 transformed

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.
User avatar
bluemoon
Member
Member
Posts: 1761
Joined: Wed Dec 01, 2010 3:41 am
Location: Hong Kong

Re: OpenGL coordinates start in the middle?

Post by bluemoon »

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:

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);
}
Or, for OpenGL 2.0 ES:

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);
}
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].
h0bby1
Member
Member
Posts: 240
Joined: Wed Aug 21, 2013 7:08 am

Re: OpenGL coordinates start in the middle?

Post by h0bby1 »

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

Image
Image

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
Post Reply