Jump to content
  • Advertisement
Sign in to follow this  
junkyska

OpenGL UV problems

This topic is 2340 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi everyone,

I'm developing a home made 3d engine with opengl 4. I have problemes rendering 2d (GUI and Text). I know that i have to flip vertically the textures for opengl to work, and i do it (3d rendering works perfectly).
So I flip vertically the textures, i create a rectangle mesh, but when i render, the uv is upside down (Gui is a mess and text is flipped vertically). I'm really frustated because I spend many hours without success.
UV in opengl are: bottom left corner (0, 0) and top right (1, 1) right?
If I swap v1 and v2 the code works, but it's kind of hack and I don't like it.

If someone can help me I will really apreciated it.

Sorry for my English.

Here is my code, sorry for the mess.

The mesh:

void GL4RendererContext::create2dRect()
{
AtomicStaticMeshInfo mesh;
StaticVertex v;
// Bottom left
v.setPos(0.0f, 0.0f, 0.5f);
v.setUV(0.0f, 0.0f);
mesh.vertices.push_back(v);
// Bottom right
v.setPos(1.0f, 0.0f, 0.5f);
v.setUV(1.0f, 0.0f);
mesh.vertices.push_back(v),
// Top left
v.setPos(0.0f, 1.0f, 0.5f);
v.setUV(0.0f, 1.0f);
mesh.vertices.push_back(v);
// Top right
v.setPos(1.0f, 1.0f, 0.5f);
v.setUV(1.0f, 1.0f);
mesh.vertices.push_back(v);
// Indices
mesh.indices.push_back(0); mesh.indices.push_back(2); mesh.indices.push_back(3);
mesh.indices.push_back(0); mesh.indices.push_back(3); mesh.indices.push_back(1);
mesh.aabb.setMin(Core::vec3(0.0f, 0.0f, 0.0f));
mesh.aabb.setMax(Core::vec3(1.0f, 1.0f, 0.0f));
_2dRect = allocateAtomicStaticMesh(mesh);
}


Rendering 2d Textured Rectangle

void GL4RendererContext::draw2dTexturedRect(Texture::Ref texture, int x, int y, int w, int h, float32 startU, float32 startV, float32 endU, float32 endV)
{
if (getCurrentShader() != _2dShader)
{
bindShader(_2dShader);
}

/*
/// HACK HERE, UV SWPAED. WHY???? :_(
{
float32 aux = startV;
startV = endV;
endV = aux;
}
*/

Core::mat4 proj = Core::ortho(0.0f, (float)_renderWidth, (float)_renderHeight, 0.0f, 0.0f, 1.0f);
Core::mat4 trans = Core::translate(Core::mat4(1.0f), Core::vec3((float)x, (float)y, 0.0f));
Core::mat4 scale = Core::scale(Core::mat4(1.0f), Core::vec3((float)w, (float)h, 0.0f));
Core::mat4 MVP = proj * trans * scale;
GLuint fillModeIndex = glGetSubroutineIndex(_currentShader->getShaderID(), GL_FRAGMENT_SHADER, "textureMode");
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, 1, &fillModeIndex);
bindMVPMatrix(MVP);
glUniform2f(glGetUniformLocation(_currentShader->getShaderID(), "UVScale"), endU - startU, endV - startV);
glUniform2f(glGetUniformLocation(_currentShader->getShaderID(), "UVTrans"), startU, startV);
bindTexture(texture, "Texture", 0);
bindVAO(_2dRect->getMeshID());
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glDrawElements(GL_TRIANGLES, _2dRect->getNumTriangles()*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
unbindVAO();
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
CHECK_GL_ERRORS
}


Shader

void GL4RendererContext::create2dShader()
{
std::string vertexShader =
"#version 420 \n"
"\n"
"in vec3 in_Position; \n"
"in vec2 in_TexCoord; \n"
"\n"
"uniform mat4 MVP; \n"
"uniform vec2 UVScale; \n"
"uniform vec2 UVTrans; \n"
"\n"
"out vec2 pTexCoord; \n"
"\n"
"void main() \n"
"{ \n"
"gl_Position = MVP * vec4(in_Position, 1.0); \n"
"pTexCoord = vec2(in_TexCoord.x*UVScale.x + UVTrans.x, in_TexCoord.y*UVScale.y + UVTrans.y); \n"
//"pTexCoord = vec2((in_TexCoord.x+UVTrans.x)*UVScale.x, (in_TexCoord.y+UVTrans.y)*UVScale.y ); \n"
"}";
std::string fragShader =
"#version 420 \n"
"\n"
"subroutine vec4 fillModeType();\n"
"\n"
"out vec4 fragColor; \n"
"\n"
"in vec2 pTexCoord; \n"
"\n"
"uniform vec4 Color; \n"
"uniform sampler2D Texture; \n"
"\n"
"subroutine( fillModeType ) \n"
"vec4 colorMode() \n"
"{ return Color; } \n"
"\n"
"subroutine( fillModeType ) \n"
"vec4 textureMode() \n"
"{ \n"
"return textureLod(Texture, pTexCoord, 0); \n"
"}\n"
"\n"
"subroutine( fillModeType ) \n"
"vec4 textMode() \n"
"{ \n"
"return vec4(Color.rgb, textureLod(Texture, pTexCoord, 0).a); \n"
"}\n"
"subroutine uniform fillModeType fillMode;\n"
"\n"
"void main() \n"
"{ \n"
"fragColor = fillMode(); \n"
"}";
ShaderPartInfo::Ref vInfo = new ShaderPartInfo(vertexShader, Shader::SHADER_VERTEX);
ShaderPartInfo::Ref fInfo = new ShaderPartInfo(fragShader, Shader::SHADER_FRAGMENT);
ShaderInfo shaderInfo;
shaderInfo.appendShaderPart(vInfo);
shaderInfo.appendShaderPart(fInfo);
std::string error = "";
_2dShader = allocateShader(shaderInfo, error);
if (!_2dShader)
{
ERR("Unable to create debug shader, error: "+error);
FOSSIL_ASSERT(false, "Unable to create 2dShader shader");
}
}

Share this post


Link to post
Share on other sites
Advertisement
Maybe my post wouldn't be clear, but:
Look at this line (from your code):

Core::mat4 proj = Core::ortho(0.0f, (float)_renderWidth, (float)_renderHeight, 0.0f, 0.0f, 1.0f);

I'm not sure what parameters "Core::ortho" has but if they are equivalent to OpenGL's "glOrtho" then:
glOrtho(left,right,bottom,top,near,far);
as you see, _renderHeight is a "bottom" plane (not a "top").
If this true you need to swap "y" value of each vertex position (uv coordinates are right):


// Bottom left
v.setPos(0.0f, 1.0f, 0.5f);
v.setUV(0.0f, 0.0f);
mesh.vertices.push_back(v);
// Bottom right
v.setPos(1.0f, 1.0f, 0.5f);
v.setUV(1.0f, 0.0f);
mesh.vertices.push_back(v),
// Top left
v.setPos(0.0f, 0.0f, 0.5f);
v.setUV(0.0f, 1.0f);
mesh.vertices.push_back(v);
// Top right
v.setPos(1.0f, 0.0f, 0.5f);
v.setUV(1.0f, 1.0f);
mesh.vertices.push_back(v);


Best wishes, FXACE.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!