在opengl中创建对象的正确方法?
我对 opengl 真的很陌生,现在正在学习基础知识。我有一个高层次的问题。 如果我想创建一个对象,比如说一列,其中包含所有选项?
- 从任何外部程序(如 3DStudio)导入 3D 网格并将其加载到 opengl 中
- 使用 glVertex3f 创建 6 个多边形
- 过剩调用? (我见过 glutSolidCube,但没有见过像 glutSolidColumn 那样的东西)
我的问题是:
A)还有其他方法吗?
B)哪种是创建普通对象/动画对象的正确方法opengl,如果您不使用 3DStudio 网格?
C) 此代码是否正确地在 2) 中创建列?(如果是,我将尝试使用 x 对其进行参数化, y,z 变量对于给定高度的给定地砖具有一列)
//Column
//Wall
glBegin(GL_POLYGON);
glVertex3f(150.0f, 250.0f,50); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f,50); // x2, y1 - top-right corner
glVertex3f(50.0f, 50.0f,50); // x2, y2 - bottom-right corner
glVertex3f(150.0f, 50.0f,50); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f,0); // x1, y1 - top-left corner
glVertex3f(150.0f, 250.0f,0); // x2, y1 - top-right corner
glVertex3f(150.0f, 50.0f,0); // x2, y2 - bottom-right corner
glVertex3f(50.0f, 50.0f,0); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(150.0f, 250.0f,0); // x1, y1 - top-left corner
glVertex3f(150.0f, 250.0f,50.0f); // x2, y1 - top-right corner
glVertex3f(150.0f, 50.0f,50.0f); // x2, y2 - bottom-right corner
glVertex3f(150.0f, 50.0f,0); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f,50); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f,0); // x2, y1 - top-right corner
glVertex3f(50.0f, 50.0f,0); // x2, y2 - bottom-right corner
glVertex3f(50.0f, 50.0f,50); // x1, y2 - bottom-left corner
glEnd( );
//Floor
glBegin(GL_POLYGON);
glVertex3f(50.0f, 50.0f, 0); // x1, y1 - top-left corner
glVertex3f(50.0f, 50.0f, 50); // x2, y1 - top-right corner
glVertex3f(0, 50.0f, 50); // x2, y2 - bottom-right corner
glVertex3f(0, 50.0f, 0); // x1, y2 - bottom-left corner
glEnd( );
//Ceiling
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f, 0); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f, 50); // x2, y1 - top-right corner
glVertex3f(0, 250.0f, 50); // x2, y2 - bottom-right corner
glVertex3f(0, 250.0f, 0); // x1, y2 - bottom-left corner
glEnd( );
I am really really new to opengl, and I am learning basics now. I have a high level question.
If I want to create an object, say a column which are all the options?
- Import a 3D mesh from any external program (like 3DStudio) and have it loaded in opengl
- Create 6 polygons using glVertex3f
- A glut call? (I have seen glutSolidCube but nothing like glutSolidColumn)
My questions are:
A) Is there any other way of doing it?
B) Which is the correct way of creating normal objects/ animated objects in opengl if you do NOT use a 3DStudio mesh?
C) Is this code correct to create a column in 2)? (In case it is, I will try to parametrize it using x,y,z variables to have a column for a given floor tile with a given height)
//Column
//Wall
glBegin(GL_POLYGON);
glVertex3f(150.0f, 250.0f,50); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f,50); // x2, y1 - top-right corner
glVertex3f(50.0f, 50.0f,50); // x2, y2 - bottom-right corner
glVertex3f(150.0f, 50.0f,50); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f,0); // x1, y1 - top-left corner
glVertex3f(150.0f, 250.0f,0); // x2, y1 - top-right corner
glVertex3f(150.0f, 50.0f,0); // x2, y2 - bottom-right corner
glVertex3f(50.0f, 50.0f,0); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(150.0f, 250.0f,0); // x1, y1 - top-left corner
glVertex3f(150.0f, 250.0f,50.0f); // x2, y1 - top-right corner
glVertex3f(150.0f, 50.0f,50.0f); // x2, y2 - bottom-right corner
glVertex3f(150.0f, 50.0f,0); // x1, y2 - bottom-left corner
glEnd( );
//Wall
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f,50); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f,0); // x2, y1 - top-right corner
glVertex3f(50.0f, 50.0f,0); // x2, y2 - bottom-right corner
glVertex3f(50.0f, 50.0f,50); // x1, y2 - bottom-left corner
glEnd( );
//Floor
glBegin(GL_POLYGON);
glVertex3f(50.0f, 50.0f, 0); // x1, y1 - top-left corner
glVertex3f(50.0f, 50.0f, 50); // x2, y1 - top-right corner
glVertex3f(0, 50.0f, 50); // x2, y2 - bottom-right corner
glVertex3f(0, 50.0f, 0); // x1, y2 - bottom-left corner
glEnd( );
//Ceiling
glBegin(GL_POLYGON);
glVertex3f(50.0f, 250.0f, 0); // x1, y1 - top-left corner
glVertex3f(50.0f, 250.0f, 50); // x2, y1 - top-right corner
glVertex3f(0, 250.0f, 50); // x2, y2 - bottom-right corner
glVertex3f(0, 250.0f, 0); // x1, y2 - bottom-left corner
glEnd( );
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于你对正确的定义。一一回答:
回复:glVertex3f 和 glBegin/glEnd 调用。目前,当您学习时,它们很好,可以作为一种简单的方法让您研究渲染管道的其他部分。然而,它们已被弃用并且不是最理想的,因此您不希望永久绑定它们。因此,它们不会在 OpenGL ES 或 WebGL 中复制。
CPU 和 GPU 之间的任何通信都会产生相关成本。通信中传输的数据越多,成本就越大。然而,由于两个异步并行设备之间需要临时同步,即使是小调用也会花费相当多的成本。
因此,glBegin/glVertex3f/glEnd 很糟糕,因为所有数据都位于 CPU 上,并通过大量调用零散地传输到驱动程序。作为第一步,您可能希望切换到使用 glVertexPointer(在一个步骤中提供所有顶点数据)和 glDrawArrays 或 glDrawElements(在一个步骤中执行与所有 glBegin/glEnd 调用等效的操作)。从这里开始,您可以通过顶点数组对象将数据存储在 GPU 上,而不是 CPU 上。
It depends on your definition of correct. To answer one by one:
Re: glVertex3f and glBegin/glEnd calls. They're fine, at present, when you're learning, as a simple means to let you investigate other parts of the rendering pipeline. However, they're deprecated and suboptimal so aren't something you'd want to become permanently bound to. Because of this, they aren't replicated in OpenGL ES or WebGL.
There is a cost associated with any communication between the CPU and GPU. The more data you transfer in the communication, the greater the cost. However, even small calls cost quite a lot due to the temporary synchronisation required between the two asynchronous and parallel devices.
glBegin/glVertex3f/glEnd are therefore bad because all data sits on the CPU and is transferred to the driver piecemeal, through lots of calls. As a first step, you probably want to switch to using glVertexPointer (to supply all vertex data in one step) and either glDrawArrays or glDrawElements (to perform the equivalent of all glBegin/glEnd calls in one step). From there you can move to storing the data on the GPU rather than the CPU via vertex array objects.
渲染对象的方法只有一种,而实现该对象的方法有多种。一种方法是为对象的每个(可见)面描述一个多边形。
在 OpenGL 1.x 下,这意味着
glBegin
、glVertex
、glEnd
。在 OpenGL 2.x 下,这意味着
glVertexPointer
、glDrawArrays
(但 1.x 方式仍然有效)在 OpenGL 3.x 下,这意味着创建一个顶点缓冲区对象、绑定它和glDrawArrays(1.x 和2.x 方法仅在兼容模式下工作)。
诸如 glutSolidCube 之类的辅助函数或用于加载模型网格的库在内部使用这些相同的方法。
There's only one way to render an object, and several ways to get there. The one way is to describe a polygon for each (visible) face of the object.
Under OpenGL 1.x, that means
glBegin
,glVertex
,glEnd
.Under OpenGL 2.x, that means
glVertexPointer
,glDrawArrays
(but the 1.x way still works)Under OpenGL 3.x, that means making a vertex buffer object, binding it, and
glDrawArrays
(1.x and 2.x methods work only in compatibility mode).Helper functions like
glutSolidCube
or a library to load a model mesh use these same methods internally.