c++ - OpenGL 渲染错误

标签 c++ opengl render

我现在使用 glut 之类的任务,我从 3ds max 的 obj 文件中导入所有顶点我现在使用的是立方体,但它呈现出所有奇怪的东西,很难解释,但它没有呈现为立方体我看起来更像一个三角形:

img

#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include <istream>
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>


using namespace std;

// structs for arrays
struct point3D
{
float x;
float y;
float z;
};
//camera structs
struct camera 
{
point3D pos;
point3D lookAt;
point3D up;

};

//global variables
camera cam = {0, 0, 5, 0, 0, 0, 0, 1, 0};

int mousex = 0;
int mousey = 0;
bool mouseDown=false;


point3D v[8] = {};
point3D vn[6] = {};
point3D vt[4] = {};
point3D f[12][3] = {};

void init()
{
glClearColor(0.5, 0.7, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 1000);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);


}

void display() 
{
    glClear(GL_COLOR_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW); 
glLoadIdentity(); // reset the matrix

gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
  cam.lookAt.x, cam.lookAt.y, cam.lookAt.z, 
  cam.up.x, cam.up.y, cam.up.z); // sets camera position.

glColor3f(1.0, 0.0, 0.0);
    glTranslatef(0.0f, 0.0f, -15.0f);


for(int i = 0; i < 8; i++)//reads coords for verts from global arrays.
{
    glBegin(GL_TRIANGLES);
        int one = f[i][0].x;
        int two = f[i][1].x;
        int three = f[i][2].x;

        glVertex3fv(&(v[one].x));
        glVertex3fv(&(v[two].x));
        glVertex3fv(&(v[three].x));
    glEnd();
}


glFlush();
}

void drawAxes() {
glBegin(GL_LINES);
    glVertex3f(0, 0, 0);
    glVertex3f(10, 0, 0);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 10, 0);
    glVertex3f(0, 0, 0);
    glVertex3f(0, 0, 10);
glEnd();// draws a axis to show scene
}

void passiveMouseFunc(int x, int y )
{
if (mouseDown == true)
{
if (x > mousex)
cam.lookAt.x+= 0.5;
if (x < mousex)
cam.lookAt.x-= 0.5;
if (y > mousey)
cam.lookAt.y-= 0.5;
if (y < mousey)
cam.lookAt.y+= 0.5;


mousex = x;
mousey = y;
glutPostRedisplay();
}
else
{
if (x > mousex)
cam.lookAt.x+= 0.0;
if (x < mousex)
cam.lookAt.x-= 0.0;
if (y > mousey)
cam.lookAt.y-= 0.0;
if (y < mousey)
cam.lookAt.y+= 0.0;


mousex = x;
mousey = y;
glutPostRedisplay(); //redraws scene

}
}
void glutMouseFunc(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON: //makes it so i have to click to move cam
     mouseDown = true;
     break;
case GLUT_RIGHT_BUTTON:
    mouseDown = false;
    break;
}
}


void specialKeyboard(int key, int x, int y) // methods used for moving cam
{
   switch (key)
   {
     case GLUT_KEY_RIGHT:
    cam.pos.x+=0.2;
    break;
    case GLUT_KEY_LEFT:
     cam.pos.x-=0.2;
    break;
   case GLUT_KEY_UP:
   cam.pos.y+=0.2;
  break;
  case GLUT_KEY_DOWN:
   cam.pos.y-=0.2;
   break;
  case GLUT_KEY_PAGE_UP:
   cam.pos.z+=0.2;
    break;
  case GLUT_KEY_PAGE_DOWN:
   cam.pos.z-=0.2;
    break;
}

   glutPostRedisplay();

}

void ObjloaderSpaceship () // loads a cube from .obj file
{
int numVert = 0;
int numNormals= 0;
int numcoords = 0;
int numFaces = 0;
int ammount = 0;
string test;
ifstream inputFile;
inputFile.open("cube.obj");

if (!inputFile.good())
    cout << "Problem with Input File";
else
{
    while(!inputFile.eof())
    {

        inputFile >> test;

        if (test == "v")
        {
            inputFile >> v[numVert].x;
            inputFile >> v[numVert].y;
            inputFile >> v[numVert].z;
            numVert++;
        }

        else if(test == "vn")
        {

            inputFile >> vn[numNormals].x;
            inputFile >> vn[numNormals].y;
            inputFile >> vn[numNormals].z;
            numNormals++;
        }
        else if(test == "vt")
        {
            inputFile >> vt[numcoords].x;
            inputFile >> vt[numcoords].y;
            inputFile >> vt[numcoords].z;
            numcoords++;
        }
        else if(test == "f")
        {


            string temp;

            for(int count = 0; count < 3; count++)
            {   

                inputFile >> temp;
                stringstream stream(temp);

                f[numFaces][count].x -= 1;

                getline(stream, temp, '/'); 
                f[numFaces][count].x = atof(temp.c_str());
                getline(stream, temp, '/'); 
                f[numFaces][count].y = atof(temp.c_str());
                getline(stream, temp, '/'); 
                f[numFaces][count].z = atof(temp.c_str());
            }

            numFaces++;
        }


    }


}
}

//void normalKeyboard(unsigned char key, int x, int y) {
//   switch (key)
//   {

//   case 'w' :
//     cam.lookAt.y+=0.5;
//     break;
//   case 'a' :
//     cam.lookAt.y-=0.5;
//     break;
//   case 's' :
//     cam.lookAt.x-=0.5;
//     break;
//   case 'd' :
//     cam.lookAt.x+=0.5;
//     break;
//   }
    //  
//   glutPostRedisplay();

//}




int main(int argc, char* argv[]) 
{

    glutInit(&argc, argv);
    glutCreateWindow("C++ Assignment");
    drawAxes();
    glutMouseFunc(glutMouseFunc);
    glutPassiveMotionFunc(passiveMouseFunc);
    ObjloaderSpaceship();
    glutDisplayFunc(display);
    glutSpecialFunc(specialKeyboard);
//  glutKeyboardFunc(normalKeyboard);

    init();
    glutMainLoop();


}

这是 obj 文件。

# 3ds Max Wavefront OBJ Exporter v0.97b - (c)2007 guruware
# File Created: 23.04.2013 13:50:59

mtllib cube.mtl

#
# object Box001
#

v  2.2289 0.0000 -1.5936
v  2.2289 0.0000 -30.3830
v  31.1072 0.0000 -30.3830
v  31.1072 0.0000 -1.5936
v  2.2289 29.4346 -1.5936
v  31.1072 29.4346 -1.5936
v  31.1072 29.4346 -30.3830
v  2.2289 29.4346 -30.3830
# 8 vertices

vn 0.0000 -1.0000 -0.0000
vn 0.0000 1.0000 -0.0000
vn 0.0000 0.0000 1.0000
vn 1.0000 0.0000 -0.0000
vn 0.0000 0.0000 -1.0000
vn -1.0000 0.0000 -0.0000
# 6 vertex normals

vt 1.0000 0.0000 0.0000
vt 1.0000 1.0000 0.0000
vt 0.0000 1.0000 0.0000
vt 0.0000 0.0000 0.0000
# 4 texture coords

g Box001
usemtl wire_006135006
f 1/1/1 2/2/1 3/3/1 
f 3/3/1 4/4/1 1/1/1 
f 5/4/2 6/1/2 7/2/2 
f 7/2/2 8/3/2 5/4/2 
f 1/4/3 4/1/3 6/2/3 
f 6/2/3 5/3/3 1/4/3 
f 4/4/4 3/1/4 7/2/4 
f 7/2/4 6/3/4 4/4/4 
f 3/4/5 2/1/5 8/2/5 
f 8/2/5 7/3/5 3/4/5 
f 2/4/6 1/1/6 5/2/6 
f 5/2/6 8/3/6 2/4/6 
# 12 faces

最佳答案

这段代码很粗糙,并且有很多基于对输入数据的假设的陷阱。但这是你要解决的问题,而不是问题的一部分。

您可能错过的重要信息是 OBJ 格式指定任何具有基于 1 的索引。但在 C/C++ 中,索引是基于 0 的。您可以在读取例程或渲染例程中修复它。 (我会去阅读,因为这使您能够阅读其他格式而不更改呈现代码。)

所以在第 219 行,您将代码更改为:

    else if(test == "f")
    {
        string temp;
        for(int count = 0; count < 3; count++)
        {   
            inputFile >> temp;
            stringstream stream(temp);

            getline(stream, temp, '/'); 
            f[numFaces][count].x = atoi(temp.c_str()) - 1;
            getline(stream, temp, '/'); 
            f[numFaces][count].y = atoi(temp.c_str()) - 1;
            getline(stream, temp, '/'); 
            f[numFaces][count].z = atoi(temp.c_str()) - 1;
        }

        numFaces++;
    }

您还肯定希望将您的面部格式更改为整数而不是 float 。我完全可以想象值(value)观会发生变化。

您还需要捕获以下情况:

  • 只有一个顶点(1)
  • 顶点和纹理线(1/1)
  • 顶点和法线(1//1)
  • 脸不是三角形(超过3个顶点)

关于c++ - OpenGL 渲染错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16170410/

相关文章:

c++ - 使用 dlsym 访问类符号

c++ - 作为第一个参数的可变参数模板

opengl - 在 OpenGL 中结合顶点数组和纹理

html - 有条件地渲染 .hbs 模板 emberjs

css - margin bleeding 是 WebKit 中的错误吗?

c++ - 使用 boost::spirit 解析时如何将结构 "summand"放入 vector 中?

c++ - 如何编写安全且用户友好的 c/c++ #define 宏

c++ - 我可以有多个 GL_ARRAY_BUFFER 缓冲区吗?

c++ - openGL修复相机?

flutter - 在渲染上更新小部件值?