我发现this code sample可以完成我想用现代opengl做的事情。
我正在试图弄清楚如何使用我的代码执行此操作,但我遇到了非常困难的时间。
/*
* GL03Viewport.cpp: Clipping-area and Viewport
* Implementing reshape to ensure same aspect ratio between the
* clipping-area and the viewport.
*/
#include <GL/glut.h> // GLUT, include glu.h and gl.h
/* Initialize OpenGL Graphics */
void initGL() {
// Set "clearing" or background color
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}
void display() {
glClear(GL_COLOR_BUFFER_BIT); // Clear the color buffer with current clearing color
// Define shapes enclosed within a pair of glBegin and glEnd
glBegin(GL_QUADS); // Each set of 4 vertices form a quad
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(-0.8f, 0.1f); // Define vertices in counter-clockwise (CCW) order
glVertex2f(-0.2f, 0.1f); // so that the normal (front-face) is facing you
glVertex2f(-0.2f, 0.7f);
glVertex2f(-0.8f, 0.7f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f(-0.7f, -0.6f);
glVertex2f(-0.1f, -0.6f);
glVertex2f(-0.1f, 0.0f);
glVertex2f(-0.7f, 0.0f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.9f, -0.7f);
glColor3f(1.0f, 1.0f, 1.0f); // White
glVertex2f(-0.5f, -0.7f);
glColor3f(0.2f, 0.2f, 0.2f); // Dark Gray
glVertex2f(-0.5f, -0.3f);
glColor3f(1.0f, 1.0f, 1.0f); // White
glVertex2f(-0.9f, -0.3f);
glEnd();
glBegin(GL_TRIANGLES); // Each set of 3 vertices form a triangle
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(0.1f, -0.6f);
glVertex2f(0.7f, -0.6f);
glVertex2f(0.4f, -0.1f);
glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex2f(0.3f, -0.4f);
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex2f(0.9f, -0.4f);
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex2f(0.6f, -0.9f);
glEnd();
glBegin(GL_POLYGON); // These vertices form a closed polygon
glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex2f(0.4f, 0.2f);
glVertex2f(0.6f, 0.2f);
glVertex2f(0.7f, 0.4f);
glVertex2f(0.6f, 0.6f);
glVertex2f(0.4f, 0.6f);
glVertex2f(0.3f, 0.4f);
glEnd();
glFlush(); // Render now
}
/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;
// Set the viewport to cover the new window
glViewport(0, 0, width, height);
// Set the aspect ratio of the clipping area to match the viewport
glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset the projection matrix
if (width >= height) {
// aspect >= 1, set the height from -1 to 1, with larger width
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
} else {
// aspect < 1, set the width to -1 to 1, with larger height
gluOrtho2D(-1.0, 1.0, -1.0 / aspect, 1.0 / aspect);
}
}
/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv); // Initialize GLUT
//glutInitWindowSize(640, 480); // Set the window's initial width & height - non-square
glutInitWindowSize(1024, 720); // Set the window's initial width & height - non-square
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow("Viewport Transform"); // Create window with the given title
glutDisplayFunc(display); // Register callback handler for window re-paint event
glutReshapeFunc(reshape); // Register callback handler for window re-size event
initGL(); // Our own OpenGL initialization
glutMainLoop(); // Enter the infinite event-processing loop
return 0;
}
由于两个原因,我对代码进行了一些机会。 1)我希望0,0是左下角,宽度,高度是右上角。
2)第二个也是更令人困惑的部分是如何保持我的像素坐标。示例代码使用-1.0到1.0范围内的点,但我希望能够说我的四边形是50px乘50px。
目前我创建了这样的视口和投影。
point3 eye, center;
vec3 up;
vmathP3MakeFromElems(&eye, 0, 0, 0);
vmathP3MakeFromElems(¢er, 0, 0, -1);
vmathV3MakeFromElems(&up, 0, 1, 0);
vmathM4MakeLookAt(&v_mat, &eye, ¢er, &up);
vec3 trans;
vmathV3MakeFromElems(&trans, 0, 0, -20);
vmathM4MakeTranslation(&v_mat, &trans);
glViewport(0, 0, width, height);
vmathM4MakeOrthographic(&p_mat, 0, width, 0, height, 1, 100);
我目前正在绘制1个四边形,其定义如下:
//3 position, 4 color, 2 texture coordinates
float v_data[] = {-1.0f, -1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
-1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f,
1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f};
short i_data[] = {0, 1, 2, 0, 2, 3};
我设置了这样的模型矩阵:
vmathV3MakeFromElems(&out->scale, texture->width * 0.5f, texture->height * 0.5f, 1);
vmathM4SetElem(&out->model_mat, 0, 0, out->scale.x);
vmathM4SetElem(&out->model_mat, 1, 1, out->scale.y);
vmathM4SetElem(&out->model_mat, 2, 2, 1);
vmathM4SetElem(&out->model_mat, 3, 3, 1);
vmathM4SetElem(&out->model_mat, 3, 0, (out->scale.x) - (x));
vmathM4SetElem(&out->model_mat, 3, 1, (out->scale.y) + (y));
那么如何在示例代码中实现视口缩放,但能够使用像素[这可能是错误的方法,我不确定]。
顶部的示例代码可以使用此命令进行复制粘贴和构建。
gcc -lglut -lGLU -lGL -o glsample glsample.c && ./glsample
答案 0 :(得分:0)
vmathM4MakeOrthographic(&p_mat, 0, width, 0, height, 1, 100);
让我们说,在此行p_mat
之后将保留这样的矩阵:
|1/width | -> p_mat[0][0]
| 1/height | -> p_mat[1][1]
| .... |
| |
这应该确保,如果宽度和高度以像素为单位,您将保持像素完美的比例。 但是,您提供的第一个样本使用
gluOrtho2D(-1.0 * aspect, 1.0 * aspect, -1.0, 1.0);
不考虑屏幕宽度和高度(仅它们之间的宽高比),并且您将具有统一的单位比例。
基本上,通过在投影矩阵(p_mat[0][0] & p_mat[1][1]
)中将这两个组件(通过相同的数字保持方面)相除或相乘,您可以缩放视口。要获得像素完美的比例,只需将它们设置为1 /宽度和1 /高度。