0

我使用 Opengl 库在 C++ 中编写了下面的代码,它在屏幕上给了我一个简单的三角形 (2d)

如何更改渲染函数中的代码以编写 3d 框或原始对象?

代码如下:

#include <iostream>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>

void render(void);
int main(int argc, char ** argv)

{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH);
    glutInitWindowPosition(100,100);
    glutInitWindowSize(800,600);
    glutCreateWindow("Welcome");

    glutDisplayFunc(render);
    glutMainLoop();

    return 0;
}

void render(void)

{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glBegin(GL_POLYGON);
    glColor3f(0.0,1.0,0.0);
    glVertex2f(-0.5, -0.5);
    glColor3f(1.0,0.0,0.0);
    glVertex2f(0.5,0.0);
    glColor3f(0.0,0.0,1.0);
    glVertex2f(0.0,0.5);


    glEnd();
    glutSwapBuffers();
}
4

2 回答 2

4

As a follow-up to Denis's answer...

If you are willing to learn a bit more OpenGL (perhaps a tall order if you just want to draw a cube...) here is the code that I reuse when doing small demos. It uses vertex array+buffer objects and creates a textured rectangular prism centered at (0, 0, 0). You'll need to provide your own texture.

The code to generate the rectangular prism is:

void GLmakeRect(GLuint vao, GLuint vbo[2], float w, float h, float d, GLuint storageHint) {
  /*
         8|---|9
         0|BAC|1
    10|---|---|---|12
      |LEF|TOP|RIG|
    11|---|---|---|13
         3|FRO|2
         4|---|5
          |BOT|
         6|---|7
  */
  float verts[] = {
    -0.5*w, 0.5*h, -0.5*d,   1.0/3.0, 0.25,
    0.5*w, 0.5*h, -0.5*d,    2.0/3.0, 0.25,
    0.5*w, 0.5*h, 0.5*d,     2.0/3.0, 0.5,
    -0.5*w, 0.5*h, 0.5*d,    1.0/3.0, 0.5,
    -0.5*w, -0.5*h, 0.5*d,   1.0/3.0, 0.75,
    0.5*w, -0.5*h, 0.5*d,    2.0/3.0, 0.75,
    -0.5*w, -0.5*h, -0.5*d,  1.0/3.0, 1.0,
    0.5*w, -0.5*h, -0.5*d,   2.0/3.0, 1.0,
    -0.5*w, -0.5*h, -0.5*d,  1.0/3.0, 0.0,
    0.5*w, -0.5*h, -0.5*d,   2.0/3.0, 0.0,
    -0.5*w, -0.5*h, -0.5*d,  0.0, 0.25,
    -0.5*w, -0.5*h, 0.5*d,   0.0, 0.5,
    0.5*w, -0.5*h, -0.5*d,   1.0, 0.25,
    0.5*w, -0.5*h, 0.5*d,    1.0, 0.5
  };
  int polys[] = {
    0, 3, 1, 1, 3, 2,    // TOP
    3, 4, 2, 2, 4, 5,    // FRONT
    4, 6, 5, 5, 6, 7,    // BOTTOM
    8, 0, 9, 9, 0, 1,    // BACK
    10, 11, 0, 0, 11, 3, // LEFT
    1, 2, 12, 12, 2, 13  // RIGHT
  };
  glBindVertexArray(vao);
  glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
  glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts[0], storageHint);
  glEnableVertexAttribArray(0);
  glEnableVertexAttribArray(1);
  glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), 0);
  glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
  glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[1]);
  glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(polys), &polys[0], storageHint);
}

You can call the function like this during initialization:

GLuint vao, vbo[2]
glGenVertexArrays(1, &vao);
glGenBuffers(2, &vbo[0]);
GLmakeRect(vao, vbo, 1, 1, 1, GL_STATIC_DRAW);

To draw the cube in your render loop, do:

glBindTexture(GL_TEXTURE_2D, yourTextureHandleHere);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0);
于 2012-09-30T15:45:20.720 回答
3

如果您只想显示一个简单的立方体,请查看“glutSolidCube”(也许还有其他“glutSolidABC”函数):glutSolidCube

但是,当显示 3d 对象时,您必须设置一个投影矩阵、一个世界矩阵(“查看器”或“相机”的转换),如果您想转换您的对象,还需要为每个对象设置一个模型矩阵。

可以在lighthouse3d.com的“tutorials -> glut”部分找到一些不错的 kickstart 课程。

如果您想创建自己的网格(立方体、球体等),您必须编写包含顶点和索引的网格或表面类。当一个渲染调用出现时,这个类必须绘制它的所有顶点和三角形——你的立方体/球体/等等。

于 2012-09-30T12:44:47.040 回答