Vertically flip text - c++

I'm using Fontstash to load and render my text. However when I draw, the text is vertically flipped:
flipped text
I cannot change the origin so I think I have to rotate the texture here, however I'm not sure how to do it or if it's the proper way.
I set the matrices like this:
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
glViewport(0, 0, viewport[2], viewport[3]);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, viewport[2], viewport[3], 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

Wyck pointed out that I had face culling enabled, disabling it resolved the issue just fine when flipping the orthographic matrix.

Related

Texture not appearing on the screen Opengl

I want to load an image on the background, I have written following code to load the texture as background by texturing the Rectangle. My image is in power of 2 (512x512). I am not getting why it is not showing anything on the screen,
please help me in this.
I am pasting my code to load the texture and to draw the rect.
Also, I have checked whether the image is loaded properly or not by using SOIL_last_result() and also checked whether there is any error in OpenGL by using glGetError()
Code to load the image as a texture using SOIL
void loadTexture(GLuint* texture, char* path){
*texture = SOIL_load_OGL_texture(path,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_MULTIPLY_ALPHA
);
cout<<*texture<<endl;
if(*texture == NULL){
printf("Failed to load %s", path);
}
}
Following code is to draw texture and rectangle,
void drawRect(int x, int y, int w, int h, GLuint texture){
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_FALSE);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);
glTexCoord2i(0,0);
glVertex2i(x,y);
glTexCoord2i(1,0);
glVertex2i(x+w,y);
glTexCoord2i(0,1);
glVertex2i(x,y+h);
glTexCoord2i(1,1);
glVertex2i(x+w,y+h);
glEnd();
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDisable(GL_BLEND);
}
And I am calling it using init mehod,
void initGL(void)
{
glClearDepth(1.0);
glClearColor(0,0,0, 1.0f);
glColor3f(0.0, 0.0, 0.0);
glLineWidth(1);
glDepthFunc(GL_LESS);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
GLuint texture;
loadTexture(&texture, "image.png");
drawRect(0,0,screen_width-100,screen_height-100,texture);
glfwSwapBuffers(window);
}
Please help me and let me know if you need more information.
Thanks in advance!!
The projection matrix describes the mapping from 3D points of a scene, to 2D points of the viewport. It transforms from eye space to the clip space, and the coordinates in the clip space are transformed to the normalized device coordinates (NDC) by dividing with the w component of the clip coordinates. The NDC are in range (-1,-1,-1) to (1,1,1). Every geometry which is out of the clippspace is clipped.
Since you do not set up a projection matrix, you're projection matrix is the identity matrix. This means you will only "see" the geometry, whose coordinates are in the range -1 to 1.
I recommend to setup a orthographic projection matrix (glOrtho). At Orthographic Projection the coordinates in the eye space are linearly mapped to normalized device coordinates and the clip sapce coordinates are equal the normalized device coordiantes.
Set up the projection somehow like this:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, screen_width, 0, screen_height, -1, 1);

OpenGL immediate mode in window coordinates [duplicate]

I am having trouble setting the openGL origin to the upper left corner of the view. So, in my window resize handler, I do something as;
// ox and oy are some offsets and width and height are the
// required viewport width and height
glViewport(ox, oy, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
This keeps the origin at bottom left and I can render my texture as:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
glEnd();
As far as I can tell from reading the pages here, to flip the origin I simply need to replace the glOrtho call with
glOrtho(0, width, height, 0, -1, 1);
However, doing this and using the render code above does not render my texture anymore and I just see a blank screen.
By flipping around the y-axis you flipped the chirality of the world space. Which means that the winding of your faces comes out differently. CCW becomes CW and vice versa. Most likely you have face culling enabled, so to account for the chirality flip you have to swap CCW for CW face culling.

opengl: Change the origin to upper left corner

I am having trouble setting the openGL origin to the upper left corner of the view. So, in my window resize handler, I do something as;
// ox and oy are some offsets and width and height are the
// required viewport width and height
glViewport(ox, oy, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
This keeps the origin at bottom left and I can render my texture as:
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(0, 0);
glTexCoord2f(1, 0); glVertex2f(width, 0);
glTexCoord2f(1, 1); glVertex2f(width, height);
glTexCoord2f(0, 1); glVertex2f(0, height);
glEnd();
As far as I can tell from reading the pages here, to flip the origin I simply need to replace the glOrtho call with
glOrtho(0, width, height, 0, -1, 1);
However, doing this and using the render code above does not render my texture anymore and I just see a blank screen.
By flipping around the y-axis you flipped the chirality of the world space. Which means that the winding of your faces comes out differently. CCW becomes CW and vice versa. Most likely you have face culling enabled, so to account for the chirality flip you have to swap CCW for CW face culling.

switching between 2D and 3D with Picking

So I'm trying to make a menu , and when I select an option I want it to draw the scene and to be able to pick from the scene in order to run the game. The game is checkers. I can do this without the 2D Menu, I perform the picking and the game is fine. But when I add the menu and try to switch between both, either I get no image (all black) or I set a perspective view and the Picking is no longer available in the same range.
to setup the 2D I make:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 600, 600, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
//draw 2D
to setup the 3D I use
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluPerspective(45.0, (GLdouble)600/(GLdouble)600, 1.0, 200.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//draw 3D
With this I'm no longer able to pick objects from the scene, but I can pass from 2D to 3D and watch the entire scene.
To perform the picking:
void PickInterface::performPicking(int x, int y) {
glSelectBuffer (BUFSIZE, selectBuf);
glRenderMode(GL_SELECT);
glInitNames();
glMatrixMode(GL_PROJECTION);
glPushMatrix ();
GLfloat projmat[16];
glGetFloatv(GL_PROJECTION_MATRIX,projmat);
glLoadIdentity();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
gluPickMatrix ((GLdouble) x, (GLdouble) (CGFapplication::height - y), 5.0, 5.0, viewport);
glMultMatrixf(projmat);
((PickScene*)scene)->selectMode=true;
scene->display();
((PickScene*)scene)->selectMode=false;
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush();
GLint hits;
hits = glRenderMode(GL_RENDER);
processHits(hits, selectBuf);
}

OpenGL fill frustum with quad

I'm trying to write an OpenGL/GLSL app that will use GLSL for image processing. I've done some research and come to the conclusion that the right approach is to render to a framebuffer object and then retrieve the image from the gpu. Unfortunately I can't figure out how to set up the frustum and render the quadrilateral so that it fills it properly. Does anyone know how to do this?
You need to render with an orthogonal projection matrix.
glPushMatrix(GL_WORLDVIEW);
glLoadIdentity();
glPushMatrix(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, height, 0, 0, 1);
glBegin(GL_QUADS);
glVertex2i(0, 0);
glVertex2i(width, 0);
glVertex2i(width, height);
glVertex2i(0, height);
glEnd();
glPopMatrix();
glMatrixMode(GL_WORLDVIEW);
glPopMatrix();
Width and height are the dimensions of your FBO. Of course they could be both one if you don't need to address special parts of your FBO by drawing quads at pixel positions.