OpenGL绘制满屏
OpenGL draw full of screen
我正在尝试完整地绘制一个 2D 足球场 window。但是,它在左侧和右侧出现黑条:
这是我的代码:
#include <OpenGL/gl.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>
#include <math.h>
void Soccer_Field();
int main() {
// start GL context and O/S window using the GLFW helper library
if (!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
return 1;
}
// uncomment these lines if on Apple OS X
/*glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/
GLFWwindow* window = glfwCreateWindow(640, 480, "Soccer field", NULL, NULL);
if (!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
// get version info
const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
const GLubyte* version = glGetString(GL_VERSION); // version as a string
printf("Renderer: %s\n", renderer);
printf("OpenGL version supported %s\n", version);
/* OTHER STUFF GOES HERE NEXT */
while(!glfwWindowShouldClose(window)) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, 1, 0.0, 1, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
Soccer_Field();
// update other events like input handling
glfwPollEvents();
// put the stuff we've been drawing onto the display
glfwSwapBuffers(window);
}
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}
void Soccer_Field (void)
{
float x, y, ang, radius = 0.05; // Not sure what the radius of the center circle should be?
static float RAD_DEG = 57.296;
glBegin (GL_QUADS);
glColor3f (0.20, 0.60, 0.20); // GreenYard
glVertex2f (0.10, 0.10); glVertex2f (0.90, 0.10);
glVertex2f (0.90, 0.90); glVertex2f (0.10, 0.90);
glColor3f (1.0, 1.0, 1.0);
glVertex2f (0.90, 0.35); glVertex2f (0.83, 0.35); // Inner White Quad - Right
glVertex2f (0.83, 0.65); glVertex2f (0.90, 0.65);
glVertex2f (0.10, 0.35); glVertex2f (0.17, 0.35); // Inner White Quad - Left
glVertex2f (0.17, 0.65); glVertex2f (0.10, 0.65);
glEnd ();
glColor3f (0.0, 0.0, 0.0); // Change color to black
glBegin (GL_LINES);
glVertex2f (0.50, 0.10); glVertex2f (0.50, 0.90); // Mid Line
// Left side of the Ground
glVertex2f (0.25, 0.25); glVertex2f (0.25, 0.75); // Goal keeper front line
glVertex2f (0.10, 0.75); glVertex2f (0.25, 0.75); // Goal keeper left line
glVertex2f (0.10, 0.25); glVertex2f (0.25, 0.25); // Goal keeper right line
// Right Side of the Ground
glVertex2f (0.75, 0.25); glVertex2f (0.75, 0.75); // Goal keeper front line
glVertex2f (0.75, 0.25); glVertex2f (0.90, 0.25); // Goal keeper left Line
glVertex2f (0.75, 0.75); glVertex2f (0.90, 0.75); // Goal keeper right line
glEnd ();
glBegin (GL_LINE_LOOP); // Circle at center of field
for (ang = 0.0; ang < 360.0; ang += 10.0) {
x = radius * cos(ang/RAD_DEG) + 1.0;
y = radius * sin(ang/RAD_DEG) + 0.5;
glVertex2f (x/2.0, y);
}
glEnd ();
}
我该如何解决这个问题?我已尝试设置视口和其他设置,但无法正常工作。
希望有人让我直截了当。
谢谢
投影矩阵描述了从场景的 3D 点到视口的 2D 点的映射。投影矩阵从viewspace变换到clipspace,clipspace中的坐标变换为范围为(-1,-1)的归一化设备坐标(NDC) , -1) 到 (1, 1, 1) 除以剪辑坐标的 w
分量。
在正交投影中,眼睛中的坐标 space 线性映射到归一化设备坐标。
正射投影矩阵由glOrtho
定义。使用此功能,您可以定义场景的视野。
由于你所有的几何图形都在(0.1, 0.1) 到(0.9, 0.9) 的范围内,你必须将其更改为:
GLdouble left = 0.1;
GLdouble right = 0.9;
GLdouble bottom = 0.1;
GLdouble top = 0.9;
glOrtho(left, right, bottom, top, -1.0, 1.0);
第二种可能性是通过模型视图矩阵缩放几何体。你必须缩放 (glScale
) the geometry from [(0.1, 0.1), (0.9, 0.9)] to the range which which is set up by the orthographic projection [(0.0, 0.0), (1.0, 1.0)]. Note, in this case you have to "re-center" the geometry (glTranslate
):
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLdouble scaleX = (1.0 - 0.0) / (0.9 - 0.1);
GLdouble scaleY = (1.0 - 0.0) / (0.9 - 0.1);
glScaled( scaleX, scaleY, 1.0 );
glTranslated( -0.1, -0.1, 0.0 );
我正在尝试完整地绘制一个 2D 足球场 window。但是,它在左侧和右侧出现黑条:
这是我的代码:
#include <OpenGL/gl.h> // include GLEW and new version of GL on Windows
#include <GLFW/glfw3.h> // GLFW helper library
#include <stdio.h>
#include <math.h>
void Soccer_Field();
int main() {
// start GL context and O/S window using the GLFW helper library
if (!glfwInit()) {
fprintf(stderr, "ERROR: could not start GLFW3\n");
return 1;
}
// uncomment these lines if on Apple OS X
/*glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);*/
GLFWwindow* window = glfwCreateWindow(640, 480, "Soccer field", NULL, NULL);
if (!window) {
fprintf(stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate();
return 1;
}
glfwMakeContextCurrent(window);
// get version info
const GLubyte* renderer = glGetString(GL_RENDERER); // get renderer string
const GLubyte* version = glGetString(GL_VERSION); // version as a string
printf("Renderer: %s\n", renderer);
printf("OpenGL version supported %s\n", version);
/* OTHER STUFF GOES HERE NEXT */
while(!glfwWindowShouldClose(window)) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, 1, 0.0, 1, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
Soccer_Field();
// update other events like input handling
glfwPollEvents();
// put the stuff we've been drawing onto the display
glfwSwapBuffers(window);
}
// close GL context and any other GLFW resources
glfwTerminate();
return 0;
}
void Soccer_Field (void)
{
float x, y, ang, radius = 0.05; // Not sure what the radius of the center circle should be?
static float RAD_DEG = 57.296;
glBegin (GL_QUADS);
glColor3f (0.20, 0.60, 0.20); // GreenYard
glVertex2f (0.10, 0.10); glVertex2f (0.90, 0.10);
glVertex2f (0.90, 0.90); glVertex2f (0.10, 0.90);
glColor3f (1.0, 1.0, 1.0);
glVertex2f (0.90, 0.35); glVertex2f (0.83, 0.35); // Inner White Quad - Right
glVertex2f (0.83, 0.65); glVertex2f (0.90, 0.65);
glVertex2f (0.10, 0.35); glVertex2f (0.17, 0.35); // Inner White Quad - Left
glVertex2f (0.17, 0.65); glVertex2f (0.10, 0.65);
glEnd ();
glColor3f (0.0, 0.0, 0.0); // Change color to black
glBegin (GL_LINES);
glVertex2f (0.50, 0.10); glVertex2f (0.50, 0.90); // Mid Line
// Left side of the Ground
glVertex2f (0.25, 0.25); glVertex2f (0.25, 0.75); // Goal keeper front line
glVertex2f (0.10, 0.75); glVertex2f (0.25, 0.75); // Goal keeper left line
glVertex2f (0.10, 0.25); glVertex2f (0.25, 0.25); // Goal keeper right line
// Right Side of the Ground
glVertex2f (0.75, 0.25); glVertex2f (0.75, 0.75); // Goal keeper front line
glVertex2f (0.75, 0.25); glVertex2f (0.90, 0.25); // Goal keeper left Line
glVertex2f (0.75, 0.75); glVertex2f (0.90, 0.75); // Goal keeper right line
glEnd ();
glBegin (GL_LINE_LOOP); // Circle at center of field
for (ang = 0.0; ang < 360.0; ang += 10.0) {
x = radius * cos(ang/RAD_DEG) + 1.0;
y = radius * sin(ang/RAD_DEG) + 0.5;
glVertex2f (x/2.0, y);
}
glEnd ();
}
我该如何解决这个问题?我已尝试设置视口和其他设置,但无法正常工作。
希望有人让我直截了当。
谢谢
投影矩阵描述了从场景的 3D 点到视口的 2D 点的映射。投影矩阵从viewspace变换到clipspace,clipspace中的坐标变换为范围为(-1,-1)的归一化设备坐标(NDC) , -1) 到 (1, 1, 1) 除以剪辑坐标的 w
分量。
在正交投影中,眼睛中的坐标 space 线性映射到归一化设备坐标。
正射投影矩阵由glOrtho
定义。使用此功能,您可以定义场景的视野。
由于你所有的几何图形都在(0.1, 0.1) 到(0.9, 0.9) 的范围内,你必须将其更改为:
GLdouble left = 0.1;
GLdouble right = 0.9;
GLdouble bottom = 0.1;
GLdouble top = 0.9;
glOrtho(left, right, bottom, top, -1.0, 1.0);
第二种可能性是通过模型视图矩阵缩放几何体。你必须缩放 (glScale
) the geometry from [(0.1, 0.1), (0.9, 0.9)] to the range which which is set up by the orthographic projection [(0.0, 0.0), (1.0, 1.0)]. Note, in this case you have to "re-center" the geometry (glTranslate
):
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLdouble scaleX = (1.0 - 0.0) / (0.9 - 0.1);
GLdouble scaleY = (1.0 - 0.0) / (0.9 - 0.1);
glScaled( scaleX, scaleY, 1.0 );
glTranslated( -0.1, -0.1, 0.0 );