hi,
i’m new in the forum so i hope i’m not making mistakes posting this help request.
i wrote some code to load an OBJ file into my program but the result is very strange, here it is:
[ATTACH=CONFIG]688[/ATTACH]
and this is my code
main.cpp
#include <GL\glew.h>
#include <GL\freeglut.h>
#include <iostream>
#include "ShaderManager.h"
#include "ContentManager.h"
#include "DebugManager.h"
#pragma comment (lib, "glew32.lib")
const int WIDTH = 800;
const int HEIGHT = 600;
const char TITLE[] = "Spark Engine";
GLuint program;
ShaderManager shader;
DebugManager debug;
ContentManager content;
void init()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-2.0, 2.0, -2.0, 2.0, -2.0, 2.0);
debug.openGlInfoLog();
program = shader.loadFromStrings(); //load vertex and fragment shader from strings
debug.modelInfoLog("../cube.obj"); // print information about model that you want to load
content.load("../cube.obj");
}
void display()
{
// background color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1, 0, 0, 1);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
content.draw();
//glUseProgram(program);
glDrawArrays(GL_TRIANGLES, 0, 3);
//glRotatef(1.0, 0.0, 360.0, 0.0);
glutSwapBuffers();
}
void reshape(GLsizei width, GLsizei height)
{
}
void close()
{
//exit(1);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitContextVersion(4, 0);
glutInitContextFlags(GLUT_CORE_PROFILE | GLUT_DEBUG);
glutInitContextProfile(GLUT_FORWARD_COMPATIBLE);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(TITLE);
GLenum status = glewInit();
if (status != GLEW_OK)
{
std::cerr << "Glew Initialization failed: " << status << std::endl;
exit(1);
}
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutCloseFunc(close);
glutMainLoop();
return 0;
}
contentManager.h
#ifndef CONTENTMANAGER_H
#define CONTENTMANAGER_H
#include <string>
#include <vector>
typedef struct Model
{
int vertices;
int texels;
int normals;
int faces;
}Model;
typedef struct
{
float x;
float y;
float z;
}Vertex;
typedef struct
{
float x;
float y;
float z;
}Normal;
typedef struct
{
int vx;
int vy;
int vz;
int vnx;
int vny;
int vnz;
}Face;
class ContentManager
{
public:
ContentManager();
~ContentManager();
Model m_load(std::string file_path);
bool load(char *file_path);
void draw();
private:
std::vector <Vertex> vertices;
std::vector <Normal> normals;
std::vector <Face> faces;
};
#endif // CONTENTMANAGER_H
ContentManager.cpp
#include <GL\freeglut.h>
#include <fstream>
#include <iostream>
#include <string>
#include "ContentManager.h"
ContentManager::ContentManager()
{
}
ContentManager::~ContentManager()
{
}
Model ContentManager::m_load(std::string file_path)
{
Model model = { 0 };
std::ifstream file;
file.open(file_path);
if (!file.good())
{
std::cerr << "Error opening obj file" << std::endl;
exit(1);
}
while (!file.eof())
{
std::string line;
getline(file, line);
std::string type = line.substr(0, 2);
if (type.compare("v ") == 0)
model.vertices++;
else if (type.compare("vt") == 0)
model.texels++;
else if (type.compare("vn") == 0)
model.normals++;
else if (type.compare("f ") == 0)
model.faces++;
}
file.close();
return model;
}
bool ContentManager::load(char *file_path)
{
std::string line;
std::ifstream file(file_path);
if (file.fail())
{
std::cerr << "Could not open file " << file_path << " file is illegible or doesn' t exist." << std::endl;
return false;
}
int vertex_count = 0;
int normal_count = 0;
int face_count = 0;
float fill;
while (!file.eof())
{
std::getline(file, line);
if (line.c_str()[0] == 'v') {
if (line.c_str()[1] == ' ')
{
vertices.resize(vertex_count + 1);
sscanf(line.c_str(), "v %f %f %f", &vertices[vertex_count].x, &vertices[vertex_count].y, &vertices[vertex_count].z);
//std::cout << "vertex " << vertices[vertex_count].x << "," << vertices[vertex_count].y << "," << vertices[vertex_count].z << std::endl;
vertex_count += 1;
}
else if (line.c_str()[1] == 'n')
{
normals.resize(normal_count + 1);
sscanf(line.c_str(), "vn %f %f %f", &normals[normal_count].x, &normals[normal_count].y, &normals[normal_count].z);
//std::cout << "normals " << normals[normal_count].x << "," << normals[normal_count].y << "," << normals[normal_count].z << std::endl;
normal_count += 1;
}
}
else if (line.c_str()[0] == 'f')
{
faces.resize(face_count + 1);
sscanf(line.c_str(), "f %d//%d %d//%d %d//%d", &faces[face_count].vx, &faces[face_count].vnx, &faces[face_count].vy, &faces[face_count].vny, &faces[face_count].vz, &faces[face_count].vnz);
//std::cout << "faces " << faces[face_count].vx << "//" << faces[face_count].vnx << "," << faces[face_count].vy << "//" << faces[face_count].vny << "," << faces[face_count].vz << "//" << faces[face_count].vnz << std::endl;
face_count += 1;
}
}
return true;
}
void ContentManager::draw()
{
for (int i = 0; i < faces.size(); i++)
{
glBegin(GL_TRIANGLE_FAN);
glNormal3f(normals[faces[i].vnx - 1].x, normals[faces[i].vnx - 1].y, normals[faces[i].vnx - 1].z);
glVertex3f(vertices[faces[i].vx - 1].x, vertices[faces[i].vx - 1].y, vertices[faces[i].vx - 1].z);
glNormal3f(normals[faces[i].vny - 1].x, normals[faces[i].vny - 1].y, normals[faces[i].vny - 1].z);
glVertex3f(vertices[faces[i].vy - 1].x, vertices[faces[i].vy - 1].y, vertices[faces[i].vy - 1].z);
glNormal3f(normals[faces[i].vnz - 1].x, normals[faces[i].vnz - 1].y, normals[faces[i].vnz - 1].z);
glVertex3f(vertices[faces[i].vz - 1].x, vertices[faces[i].vz - 1].y, vertices[faces[i].vz - 1].z);
glEnd();
}
}
in your opinion why have i this strange result?