在opengl中创建对象的正确方法?

发布于 2024-10-19 05:59:54 字数 2345 浏览 0 评论 0原文

我对 opengl 真的很陌生,现在正在学习基础知识。我有一个高层次的问题。 如果我想创建一个对象,比如说一列,其中包含所有选项?

  1. 从任何外部程序(如 3DStudio)导入 3D 网格并将其加载到 opengl 中
  2. 使用 glVertex3f 创建 6 个多边形
  3. 过剩调用? (我见过 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?

  1. Import a 3D mesh from any external program (like 3DStudio) and have it loaded in opengl
  2. Create 6 polygons using glVertex3f
  3. 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 技术交流群。

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

发布评论

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

评论(2

拒绝两难 2024-10-26 05:59:54

这取决于你对正确的定义。一一回答:

  1. 这是最正常的方式;然而 OpenGL 只是一个渲染库,您需要提供代码来加载您感兴趣的文件格式;
  2. glVertex3f 是一个糟糕的选择;参见下面的说明;
  3. GLUT 不是 OpenGL 的一部分,但是是的——有各种第三方库可以为您绘制原始对象。过剩就是其中之一。

回复: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:

  1. this would be the most normal way; however OpenGL is a rendering library only and it's up to you to provide code to load the file formats you are interested in;
  2. glVertex3f is a poor choice; see exposition below;
  3. GLUT is not part of OpenGL, but yes — there are a variety of third party libraries that can draw primitive objects for you. GLUT is 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.

深海里的那抹蓝 2024-10-26 05:59:54

渲染对象的方法只有一种,而实现该对象的方法有多种。一种方法是为对象的每个(可见)面描述一个多边形。

在 OpenGL 1.x 下,这意味着 glBeginglVertexglEnd

在 OpenGL 2.x 下,这意味着 glVertexPointerglDrawArrays (但 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.

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