- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 GLFW 上下文中使用 GLUT 绘制了一个球体。我使用的代码是:
void drawSphere(std::vector<GLfloat> color, std::vector<GLdouble> position, float radius) {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glColor3f(color[0], color[1], color[2]);
glPushMatrix();
glTranslated(position[0], position[1], position[2]);
glutSolidSphere(radius, 50, 50);
glPopMatrix();
}
我在我的 main()
函数循环中调用它,看起来像这样:
while(!glfwWindowShouldClose(window)) {
// other stuff...
drawSphere({(GLfloat)1.0, (GLfloat)1.0, (GLfloat)1.0}, {(GLdouble)2.0, (GLdouble)2.0, (GLdouble)2.0}, 1.0);
// other stuff...
glfwSwapBuffers(window);
glfwPollEvents();
}
但是,无论我指定什么颜色或什么位置,我都会得到一个很棒的、以 (0, 0, 0) 为中心的红色球体。尽管如果我修改半径,球体会改变其尺寸。
我已经多次检查了代码,几乎所有关于如何使用 GLUT 绘制实体的结果都在 Google 上得到了检查,但找不到我的问题的答案。此外,我尝试通过两次调用该函数来绘制两个球体,给出两个不同的位置和两种不同的颜色。绘制了两个球体,但它们都是红色的并且以 (0, 0, 0) 为中心,如您在该屏幕截图中所见:
因为我还使用带有顶点和片段着色器的 VAO、VBO 和 EBO 绘制其他东西,这会不会是个问题?这可能是 GLUT 和 GLFW 之间的兼容性问题吗?我该怎么做才能解决这个问题?
编辑:感谢@Rabbid76,我已经设法使用 glUseProgram(0)
解决了位置问题,而 glBindVertexArray(0)
似乎没有影响程序行为。不过,现在我面临另一个问题:因为我使用着色器绘制所有内容,所以我还有一个模型矩阵、一个 View 矩阵和一个投影矩阵要处理。当我使用 gl 管道绘制球体时,它显示在相机视觉的一角:
无论我用相机做什么 Action (使用鼠标和键盘),球体都将保持在那个位置(在相机 POV 中)。我认为这是因为我用来绘制对象的方式不同,但我不知道如何使用 gl 管道命令将球体设置到正确的位置。我能做些什么?是否有某种有用的指南或教程可以让事情顺利进行?
最佳答案
[...] I've also got a model matrix, a view matrix and a projection matrix to deal with. [...] I don't know how to set the sphere to the right position using the gl pipeline commands. [...]
在Legacy OpenGL , glVertexPointer
指定的固定函数坐标属性(分别由 glVertex
设置的顶点坐标)由当前模型 View 矩阵(GL_MODELVIEW
)和当前投影矩阵(GL_PROJECTION
)转换。
如果你已经设置了一个 4x 投影和(模型) View 矩阵,这个矩阵可以通过 glLoadMatrix
加载到当前矩阵。 .矩阵必须由指向 16 个连续值的指针提供,这些值用作 4x4 列主矩阵的元素。
当前矩阵(类型)可以通过glMatrixMode
来选择.
在通过 glutSolidSphere
绘制几何之前加载投影和(模型) View 矩阵。例如:
const GLfloat *projection = ...; // pointer to 4x4 matrix (16 consecutive floats)
const GLfloat *view = ...; // pointer to 4x4 matrix (16 consecutive floats)
// load projection matrix
glPushMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projection);
// load view matrix
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(view);
// concatenate model matrix to view matrix
glTranslated(position[0], position[1], position[2]);
// draw sphere
glutSolidSphere(radius, 50, 50);
// restore modelview matrix (the mode is still GL_MODELVIEW)
glPopMatrix();
// restore projection matrix
glMatrixMode(GL_PROJECTION);
glPopMatrix();
// set GL_MODELVIEW matrix mode (possibly this is not necessary)
glMatrixMode(GL_MODELVIEW);
关于c++ - 为什么 glColor3f() 和 glTranslated() 不影响对象创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58061683/
这是一个非常笼统的问题,我希望我能答对。 我正在研究 SSL/TLS 重新协商并已阅读了一些内容。这是我从阅读中了解到的内容: 从 SSL/TLS 重新协商的角度来看,客户端分为两个主要组,打补丁的和
第一个屏幕是艺术的细节。当我向上滚动时,标题将是 alpha。我点击另一个“艺术”到另一个细节 UI,然后按回到 Previous UI。之前的UI标题是黑色的,怎么变透明了。 布局:
想知道 mv 对基表的影响。它会减慢基表的速度吗?它什么时候开始写入 mv,就像同时写入基表和 mv 一样? 如果我有 local_quorum 的 CL 且 RF=3,客户端是否必须等到写入 mv
似乎在任何地方都找不到太多关于此问题的帮助,所以我想我会在这里尝试。 我正在尝试制作一个简单的 for 循环,当我将鼠标悬停在 html 卡上时,它会隐藏卡中的一些文本。该卡有一个简单的名字和姓氏,我
我有一个程序每帧运行 tick() 方法。我希望一个对象根据设定的重力常数下落,因此我创建了一个 Ball 对象,该对象会将其位置更新为前一帧的位置减去 y 速度。每个刻度 y 速度都会减少重力常数。
我的 KeyHandler 在这里: private void KeyHandler(java.awt.event.KeyEvent evt) {
我有一个方法,其中使用了很多其他类,包括链接列表、队列和堆栈。在我的方法中,我有一个 for 循环,我想在其中弹出堆栈(方便地命名为 s)并将队列(方便地命名为 q)出队到 s1 和 q1。由于某种原
我有一个 JTree 节点数组和另一个自定义对象的相应数组。 我想要什么:当选择 JTree 的节点时,相应对象(其数组中索引与节点数组中所选节点索引相同的对象)的字段填充 JLabels。 我被困在
我知道浏览器完成了处理客户端脚本(Javascript、JQuery 等)的所有工作,但想知道在性能方面是否还有其他重要因素(网络速度、客户端计算机速度、服务器环境) 如果它完全依赖于浏览器(类型和版
我有一个 Android 服务在后台运行,它将使用以下代码: while(true) { ServerSocket server = new ServerSocket(1234); Socke
对JQM有以下疑惑: 1.如果我们在单独的 html 文件中使用重复的 id,对 jquery mobile 有什么影响。 假设我们在单独的 html 文件中有重复的 id,但如果我们不使用该 id
我正在尝试更新两个(inventory、sold)MySQL 表的表库存。 假设我们正在处理的 sku 是 BT888-16 UPDATE inventory JOIN sold ON invento
我使用这种方法来更改我的表格单元格值, 它在 jtable 上改变但在文本文件上没有改变! public class user_AllBooks extends AbstractTableModel
我想在向表中插入数据时创建一个 MYSQL 存储过程,数据也会被插入到其他服务器表中。 我知道这在 ORACLE 数据库中是可能的,但我不知道它是否适用于 MYSQL。 有什么办法吗? 最佳答案 是的
我在 css 方面非常糟糕,只能靠 SO 答案来解决 - 但是我找不到针对这个特定问题的任何解释。 我有一个表单,其中包含一个 textarea 和一个 button(input/submit),仅此
我在一个元素上有动画,但它的移动也会影响 sibling 。如何在不影响兄弟元素的情况下仅在元素上使用动画? 问题示例: function animateSearch() { $('.glyph
我试图在我的 ViewController 中的 UIView 的所有四个边上建立一个阴影 — 在我通过 Xcode 向 UIView 添加约束之前,它工作得很好。我怎样才能使 UIView 的阴影显
自从我使用 JavaScript 以来已经有一段时间了 - 在获得证书之后我开始学习 Perl 并从那时起就一直使用它。我只是想重新开始使用 JS,我已经写了这个,我想说的是,这是一个简单的小脚本,可
我正在处理一个 HTML 元素,我添加了一个复选框,选中后会高亮显示所有文本输入字段。唯一的问题是一些输入字段在表格内,出于某种原因我无法用我的代码影响它们。任何帮助将不胜感激。 相关代码: HTML
我为 String 类创建了一个小扩展,以便方便地从中删除字符。这是它的样子: mutating func drop(characters chars: [String]) { for c i
我是一名优秀的程序员,十分优秀!