Search code examples
c++openglglutglfw

OpenGL drawing lines on non square screen


I'm using OpenGl in c++ to draw '+' symbols. My screen has a resolution of 1920x1080, but unfortunately with my method of drawing ("glBegin(GL_LINES)" ) only works on a 1080x1080 rectangle. For x > screen_height everything gets cut off. However, I can still locate text (using Glut) on the full 1920x1080 surface. Could you help me to find what setting is at fault?

I attached a code snippet. This may not compile, as I only included what is used for the drawing of the '+' symbol.

Much appreciated. Thank you in advance.

// Libraries needed for OpenGL and strings
#include <GL/glut.h>
#include <GLFW/glfw3.h>

// header file required for this cpp file
#include "window2.hxx" 

int main(int argc, char *argv[])
{
// Start GLFW as main OpenGL frontend
GLFWwindow* window;

if( !glfwInit() )
{
    fprintf( stderr, "Failed to initialize GLFW\n" );
    exit( EXIT_FAILURE );
}

// Later upstream GLut is used for text rendering: thus also initialize GLUT (freeglut3)
glutInit(&argc, argv); 

// check the resolution of the monitor and pass it to the create window function
const GLFWvidmode* mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
screen_width = mode->width;
screen_height = mode->height;
window = glfwCreateWindow(screen_width , screen_height, "LearnOpenGL", glfwGetPrimaryMonitor(), NULL );


if (!window)
{
    fprintf( stderr, "Failed to open GLFW window\n" );
    glfwTerminate();
    exit( EXIT_FAILURE );
}


glfwMakeContextCurrent(window);
glfwSwapInterval( 1 );

// set up view
glViewport( 0, 0, screen_height, screen_width );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();

// see https://www.opengl.org/sdk/docs/man2/xhtml/glOrtho.xml
glOrtho(0.0,screen_height,0.0,screen_width,0.0,1.0); // this creates a canvas for 2D drawing on

x_1 = 500;
y_1 = 100;

while( !glfwWindowShouldClose(window) ) {

    // Draw gears
    render_loop();

    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();

} // glfw while loop
}


//OpenGL Draw function
void render_loop()
{

// white background
glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPointSize(10);
glLineWidth(1);

// push matrix
glPushMatrix();

glColor3f(0.0, 0.0, 1.0);
glBegin(GL_LINES);  
glVertex2i(x_1-10,y_1);
glVertex2i(x_1+10,y_1);
glVertex2i(x_1,y_1-10);
glVertex2i(x_1,y_1+10);
glEnd();

// pop matrix
glPopMatrix();
}

Solution

  • I think you've got your dimensions the wrong way round:

    // set up view 
    glViewport( 0, 0, screen_height, screen_width );
    ...
    glOrtho(0.0,screen_height,0.0,screen_width,0.0,1.0);
    

    Try

    glViewport( 0, 0, screen_width, screen_height);
    ...
    glOrtho(0.0,screen_width,0.0,screen_height,0.0,1.0);