openGL 用于矩阵堆栈

发布于 2024-12-09 01:25:30 字数 317 浏览 1 评论 0原文

我有一个 win32 应用程序,我想在其中使用 openGL 仅用于其矩阵堆栈,而不是用于任何渲染。也就是说,我想使用 openGL 来指定相机、视口等,这样我就不必再次进行数学计算。创建场景时,我只想使用 gluProject 投影点并使用它。投影点被传递到另一个库,该库为我创建场景,所有窗口句柄都是由库本身创建的,我无权访问它。

问题是,Windows 需要设备上下文来进行初始化。但是,由于我没有使用 openGL 进行任何渲染,有没有办法在没有任何窗口句柄的情况下使用 openGL?

如果没有任何显式初始化,当我使用 glGet 读回矩阵时,它会返回垃圾。有想过如何解决它吗?

I have a win32 application, in which I want to use openGL just for its matrix stack not for any rendering. That is, I want to use openGL to specify the camera, viewport etc so that I dont have to do the maths again. While creating the scene, I just want to project the points using gluProject and use it. The projected points are passed to another library which creates the scene for me, all the windows handles are created by library itself and I dont have access to that.

The problem is, windows needs a device context for initialization. But, since I am not using openGL for any rendering, is there a way to use openGL without any Window handle at all?

Without any explicit initialization, when I read back the matrices using glGet, it returns a garbage. Any thought on how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

顾铮苏瑾 2024-12-16 01:25:30

我想使用 openGL 只是为了它的矩阵堆栈,而不是为了任何渲染。

这不是 OpenGL 的目的。 OpenGL 是一个绘图/渲染 API,不是一个数学库。实际上,出于这个原因,整个矩阵数学内容已从最新的 OpenGL 版本(OpenGL-3 核心及更高版本)中剥离。

而且,做这个矩阵数学的事情非常简单,你可以用不到 1k 行的 C 代码把它写下来。为此滥用 OpenGL 绝对没有任何好处。

I want to use openGL just for its matrix stack not for any rendering.

That's not what OpenGL is meant for. OpenGL is a drawing/rendering API, not a math library. Actually the whole matrix math stuff has been stripped away from the latest OpenGL versions (OpenGL-3 core and later), for that very reason.

Also doing this matrix math stuff is so simple, you can write it down in less than 1k lines of C code. There's absolutely no benefit in abusing OpenGL for this.

沉鱼一梦 2024-12-16 01:25:30

在您的实现中,Matrix 堆栈可能存在于图形硬件上。因此,OpenGL 坚持要求您拥有 OpenGL 上下文以便能够使用此类功能,这是相当合理的。这是因为创建上下文的行为可能包括设置存储矩阵堆栈所需的必要实现机制。

即使在纯粹基于软件的 OpenGL 实现中,人们仍然期望创建上下文的行为来调用与 malloc 等效的函数来保护堆栈的存储空间。如果您碰巧发现不需要创建上下文的 OpenGL 实现,我仍然会避免依赖该行为,因为它很可能是未定义的,并且可能会在该实现的下一个版本中被破坏。

如果是 C++,如果您没有将 OpenGL 用于除此以外的任何用途,我只会将 std::stack 与您最喜欢的线性代数包中的 Matrix 类一起使用。

The Matrix stack could potentially live on graphics hardware in your implementation. OpenGL is quite reasonable therefore in insisting you have an OpenGL context in order to be able to use such functions. This is because the act of creating a context probably includes setting up the necessary implementation mechanics required to store the matrix stack.

Even in a purely software based OpenGL implementation one would still expect the act of creating a context to call some equivalent to malloc to secure the storage space for the stack. If you happened to find an OpenGL implementation where creating a context wasn't necessary I'd still keep clear of relying on that behavior since it's most likely undefined and could be broken in the next release of that implementation.

If it's C++ I'd just use std::stack with the Matrix class from your favorite linear algebra package if you're not using OpenGL for anything other than that.

泅渡 2024-12-16 01:25:30

我向您展示我完整的(开源)矩阵类。享受。

https://github.com/TheBuzzSaw/paroxysm/blob/master /newsource/CGE/Matrix4x4.h

I present to you my complete (open source) matrix class. Enjoy.

https://github.com/TheBuzzSaw/paroxysm/blob/master/newsource/CGE/Matrix4x4.h

星光不落少年眉 2024-12-16 01:25:30

我建议您尝试自己实现这些调用。我曾为我编写的 Palm 应用程序 tinyGL 做过一次。我了解到,文档基本上以纯文本形式告诉您做了什么。

tglFrustumtglOrth 的逐字代码是(请注意,我使用定点数学来获得一些性能)

void tglFrustum(fix_t w, fix_t h, fix_t n, fix_t f) {
  matrix_t fm, m;
  fix_t f_sub_n;
  f_sub_n = sub_fix_t(f,n);
  fm[0][0] = mult_fix_t(_two_,div_fix_t(n,w));
  fm[0][1] = 0;
  fm[0][2] = 0;
  fm[0][3] = 0;
  fm[1][0] = 0;
  fm[1][1] = mult_fix_t(_two_,div_fix_t(n,h));
  fm[1][2] = 0;
  fm[1][3] = 0;
  fm[2][0] = 0;
  fm[2][1] = 0;
  fm[2][2] = inv_fix_t(div_fix_t(add_fix_t(f,n),f_sub_n));
  f = mult_fix_t(_two_,f);
  fm[2][3] = inv_fix_t(div_fix_t(mult_fix_t(f,n),f_sub_n));
  fm[3][0] = 0;
  fm[3][1] = 0;
  fm[3][2] = _minus_one_;
  fm[3][3] = 0;
  set_matrix_t(m,_matrix_stack[_toms]);
  mult_matrix_t(_matrix_stack[_toms],m,fm);  
}

void tglOrtho(fix_t w, fix_t h, fix_t n, fix_t f) {
  matrix_t om, m;
  fix_t f_sub_n;
  f_sub_n = sub_fix_t(f,n);
  MemSet(om,sizeof(matrix_t),0);
  om[0][0] = div_fix_t(_two_,w);
  om[1][1] = div_fix_t(_two_,h);
  om[2][2] = div_fix_t(inv_fix_t(_two_),f_sub_n);
  om[2][3] = inv_fix_t(div_fix_t(add_fix_t(f,n),f_sub_n));
  om[3][3] = _one_;
  set_matrix_t(m,_matrix_stack[_toms]);
  mult_matrix_t(_matrix_stack[_toms],m,om);  
}

将它们与 glFrustumglOrtho

I can recommend trying to implement those calls yourself. I did that once for a Palm app I wrote, tinyGL. What I learnt was that the documentation basically tells you in plain text what is done.

i.e the verbatim code for tglFrustum and tglOrth are (note that I was using fix point math to get some performance)

void tglFrustum(fix_t w, fix_t h, fix_t n, fix_t f) {
  matrix_t fm, m;
  fix_t f_sub_n;
  f_sub_n = sub_fix_t(f,n);
  fm[0][0] = mult_fix_t(_two_,div_fix_t(n,w));
  fm[0][1] = 0;
  fm[0][2] = 0;
  fm[0][3] = 0;
  fm[1][0] = 0;
  fm[1][1] = mult_fix_t(_two_,div_fix_t(n,h));
  fm[1][2] = 0;
  fm[1][3] = 0;
  fm[2][0] = 0;
  fm[2][1] = 0;
  fm[2][2] = inv_fix_t(div_fix_t(add_fix_t(f,n),f_sub_n));
  f = mult_fix_t(_two_,f);
  fm[2][3] = inv_fix_t(div_fix_t(mult_fix_t(f,n),f_sub_n));
  fm[3][0] = 0;
  fm[3][1] = 0;
  fm[3][2] = _minus_one_;
  fm[3][3] = 0;
  set_matrix_t(m,_matrix_stack[_toms]);
  mult_matrix_t(_matrix_stack[_toms],m,fm);  
}

void tglOrtho(fix_t w, fix_t h, fix_t n, fix_t f) {
  matrix_t om, m;
  fix_t f_sub_n;
  f_sub_n = sub_fix_t(f,n);
  MemSet(om,sizeof(matrix_t),0);
  om[0][0] = div_fix_t(_two_,w);
  om[1][1] = div_fix_t(_two_,h);
  om[2][2] = div_fix_t(inv_fix_t(_two_),f_sub_n);
  om[2][3] = inv_fix_t(div_fix_t(add_fix_t(f,n),f_sub_n));
  om[3][3] = _one_;
  set_matrix_t(m,_matrix_stack[_toms]);
  mult_matrix_t(_matrix_stack[_toms],m,om);  
}

Compare those with the man pages for glFrustum and glOrtho

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文