gpt4 book ai didi

c++ - 非方形屏幕上的 OpenGL 绘图线

转载 作者:行者123 更新时间:2023-11-27 23:50:05 25 4
gpt4 key购买 nike

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

最佳答案

我认为你把尺寸搞错了:

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

关于c++ - 非方形屏幕上的 OpenGL 绘图线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47177943/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com