非方形屏幕上的OpenGL绘图线

时间:2017-11-08 10:59:06

标签: c++ opengl glut glfw

我在c ++中使用OpenGl绘制' +'符号。我的屏幕分辨率为1920x1080,但遗憾的是我的绘图方法(" glBegin(GL_LINES)" )仅适用于1080x1080矩形。对于x> screen_height一切都被切断了。但是,我仍然可以在完整的1920x1080表面上找到文本(使用Glut)。你能帮我找一下有什么设置吗?

我附上了一段代码。这可能无法编译,因为我只包括用于绘制' +'符号。

非常感谢。提前谢谢。

// 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();
}

1 个答案:

答案 0 :(得分:3)

我认为你的尺寸方向错误:

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

尝试

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