2D in Opengl 3.2?

Started by
12 comments, last by Murdocki 12 years, 10 months ago

in a shader u would transform your vertex positions as normal with the projection matrix.

also I would recomment NOT to use your screen size for creating your matrix. Just think about how many world-units the player should see in one direction and calculate the size of the other with your screen ratio.

for example you want the player to see 10 in width u have to set a height of 7.5 (ratio: 4/3), I think this is importandt because you wouldn't want the player to see more of the game depeding on their screen resolution.

Another thing I like to do is to set the center of the screen in the center of the projection, so instead of
ortho(0, 10, 0, 7.5 ...

I use
ortho(-5, 5, -3.75, 3.75 ...


Oh but Im doing 2D so I want to map vertices to pixel coordinates

with this I have no geometry being rendered (if I toggle the ortho projection it displays the polygon tho)



GLint _uniform_location_matrix = -1;

glm::mat4 _projection(1.0f);
_projection = glm::ortho(0, 800, 600, 0, -100, 100);

CreateVBO();

Shader _basic_shader("..\\Resources\\Shaders\\Basic.v.glsl", "..\\Resources\\Shaders\\Basic.f.glsl");
_basic_shader.Compile(); if ( _basic_shader.GetProgramId() == NULL ) return;

glUseProgram(_basic_shader.GetProgramId());

_uniform_location_matrix = glGetUniformLocation(_basic_shader.GetProgramId(), "mvpMatrix");

glUniformMatrix4fv(_uniform_location_matrix, 1, GL_FALSE, glm::value_ptr(_projection));

while(true)Render();



#version 400

uniform mat4 mvpMatrix;

layout(location=0) in vec4 in_Position;
layout(location=1) in vec4 in_Color;
out vec4 ex_Color;

void main(void)
{
gl_Position = mvpMatrix * in_Position;
ex_Color = in_Color;
}



#version 400

in vec4 ex_Color;
out vec4 out_Color;

void main(void)
{
out_Color = ex_Color;
}
Advertisement
why would u want to map vertices to single pixels?


just assume u would want to do some pixel art where textures have to align perfectly to pixels so u won't get flickering with gl_nearest.
In this case i would take care of this in the vertex shader. So you don't have to handle this View(Render) feature in your Controller(Game logic)

why would u want to map vertices to single pixels?


just assume u would want to do some pixel art where textures have to align perfectly to pixels so u won't get flickering with gl_nearest.
In this case i would take care of this in the vertex shader. So you don't have to handle this View(Render) feature in your Controller(Game logic)


I though I was doing just that :S
You could just fill a vertexbuffer with one quad (or more) like so:



Vertex vertices[] =
{
{ Vector3f(), Vector2f(), }, //Top-left
{ Vector3f( (float)width, 0, 0 ), Vector2f( 1.0f, 0.0f ), }, //Top-right
{ Vector3f( 0.0f, (float)height, 0.0f ), Vector2f( 0.0f, 1.0f), }, //Bottom-left
{ Vector3f( (float)width, (float)height, 0.0f ), Vector2f( 1.0f, 1.0f), }, //Bottom-right
};


where Vertex would be three floats for position and two for uv. Texture coordinates are flipped on the y-axis here (DirectX convention).
Then in your vertex shader you could place the quad like so:



uniform ivec2 ScreenSize;
uniform vec2 translation;
uniform vec2 scale = vec2( 1.0, 1.0 );
out vec2 texCoord;
void main()
{
vec4 vPos;
vPos.x = (gl_Vertex.x * scale.x + translation.x) / (ScreenSize.x/2.0f) - 1.0f;
vPos.y = 1.0f - (gl_Vertex.y * scale.y + translation.y) / (ScreenSize.y/2.0f);
vPos.z = 0.0f;
vPos.w = 1.0f;
gl_Position = vPos;
texCoord = vec2( gl_MultiTexCoord0.x, -gl_MultiTexCoord0.y ); //Flip back uv's y axis
}


This code is for OpenGL 2.1 so you might want to update it a bit but it gives the basic idea.

This topic is closed to new replies.

Advertisement