Jump to content
  • Advertisement
Sign in to follow this  
theHOUSE16

OpenGL Assimp index array problem

This topic is 917 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to get assimp to import a model into a vertex array and index array. I'm loading a cube but when i draw it on screen, this is what renders.

 

[attachment=30242:Screen Shot 2016-01-10 at 10.35.24 PM.png]

 

This is the same cube with the camera moved up a bit so you can get an idea of where all the faces are being drawn. 

 

[attachment=30243:Screen Shot 2016-01-11 at 10.21.33 PM.png]

 

I think its an issue with the index array because all the vertices appear to be in the correct place but i can't find the issue. I'm sure its something stupid i'm missing so hopefully another pair of eyes can help. Here's the full code. Everything relevant to assimp importing is in the Mesh constructor and each mesh(only one in this case) is drawn with the draw method in the Mesh class so i think the problem is in one of those.

 

Any ideas?

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"
#include "assimp/cimport.h"
#include "assimp/Importer.hpp"
#include "assimp/scene.h"
#include "assimp/postprocess.h"
#include <vector>
#include <iostream>
#include <SDL2/SDL.h>
#include <OpenGL/gl3.h>



SDL_Window* window;
SDL_GLContext gl_context;
SDL_DisplayMode current;
SDL_Event event;


int width = 800;
int height = 600;

glm::mat4 proj_matrix;
glm::mat4 view_matrix;

float cam_x = 0.0;
float cam_y = 0.0;
float cam_z = 5.0;
float tar_x = 0.0;
float tar_y = 0.0;
float tar_z = 0.0;
float up_x = 0.0;
float up_y = 1.0;
float up_z = 0.0;

unsigned int program;

const char* vs =
"#version 400\n"
"in vec3 pos;"
"uniform mat4 mvp;"
"void main(){"
"   gl_Position = mvp * vec4(pos, 1.0);"
"}";

const char* fs =
"#version 400\n"
"out vec4 frag_color;"
"void main(){"
"   frag_color = vec4(0.5, 0.0, 0.5, 1.0);"
"}";

class Mesh{
public:
    unsigned int vbo,ind, vao;
    int a_count;
    int f3_count;
    float* data_array;
    unsigned int* index_array;
    glm::mat4 model_matrix;
    Mesh(char* file_path){
        std::cout << "Loading model file " << file_path << "...";
        const aiScene* scene = aiImportFile(file_path, aiProcess_GenNormals | aiProcess_JoinIdenticalVertices);
        if(!scene)//if scene fails to load
            std::cout << "FAILED\n" << std::endl;
        else{
            std::cout << std::endl;
            
            std::cout << "\t" << scene->mNumAnimations << " animations" << std::endl;
            std::cout << "\t" << scene->mNumMaterials << " materials" << std::endl;
            std::cout << "\t" << scene->mNumMeshes << " meshes" << std::endl;
            std::cout << "\t" << scene->mNumTextures << " textures" << std::endl;
            
            const aiMesh* mesh = scene->mMeshes[0];
            std::cout << "\t" << mesh->mNumVertices << " vertices" << std::endl;
            float* vertex_array = new float[mesh->mNumVertices * 3 * sizeof(float)];
            float* normal_array = new float[mesh->mNumVertices * 3 * sizeof(float)];
            float* uv_array = new float[mesh->mNumVertices * 2 * sizeof(float)];
            
            int a,b,c,d;//counters for verts norms and uvs, d for indices
            a = b = c = d = 0;
            
            f3_count = mesh->mNumFaces * 3;
            
            for(unsigned int j = 0; j<mesh->mNumVertices; j++){
                const aiVector3D* vp = &(mesh->mVertices[j]);
                vertex_array[a++] = (vp->x);
                vertex_array[a++] = (vp->y);
                vertex_array[a++] = (vp->z);

                const aiVector3D* vn = &(mesh->mNormals[j]);
                normal_array[b++] = (vn->x);
                normal_array[b++] = (vn->y);
                normal_array[b++] = (vn->z);
                
                const aiVector3D* vt = &(mesh->mTextureCoords[0][j]);
                //uv_array[c++] = (vt->x);
                //uv_array[c++] = (vt->y);
                uv_array[c++] = 2.0f;
                uv_array[c++] = 2.0f;
            }
            
            a_count = (mesh->mNumVertices) * 8;
            //a_count = a+b+c;
            data_array = new float[a_count * sizeof(float)];
            a = b = c = 0;
            
            for(int i = 0; i<(mesh->mNumVertices * 8);){
                data_array[i++] = vertex_array[a++];
                data_array[i++] = vertex_array[a++];
                data_array[i++] = vertex_array[a++];
                data_array[i++] = normal_array[b++];
                data_array[i++] = normal_array[b++];
                data_array[i++] = normal_array[b++];
                data_array[i++] = uv_array[c++];
                data_array[i++] = uv_array[c++];
            }
            
            for(int i = 0; i<a+b+c; i++){
                if (i%8 == 0 && i != 0)
                    std::cout << std::endl;
                std::cout << data_array[i] << " ";
            }
            
            index_array = new unsigned int[f3_count * sizeof(unsigned int)];
            for(int i = 0; i<mesh->mNumFaces; i++){
                const struct aiFace* vf = &(mesh->mFaces[i]);
                index_array[d++] = (vf->mIndices[0]);
                index_array[d++] = (vf->mIndices[1]);
                index_array[d++] = (vf->mIndices[2]);
            }
            
            std::cout << std::endl;
            for(int i = 0; i<d; i++)
                std::cout << index_array[i] << " ";
            
            std::cout << std::endl;
            std::cout << "Acount:" << a_count << "(" << a_count/8 << ")" << " F3count:" << f3_count<< "(" << f3_count/3 << ")" << std::endl;
            std::cout << "DONE" << std::endl;
        }
        
        glGenBuffers(1, &vbo);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, a_count * sizeof(float), &data_array[0], GL_STATIC_DRAW);
        
        glGenBuffers(1, &ind);
        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ind);
        glBufferData( GL_ELEMENT_ARRAY_BUFFER, f3_count * sizeof(unsigned int), &index_array[0], GL_STATIC_DRAW );
        
        unsigned int normal_offset = 3 * sizeof(float);
        unsigned int uv_offset = 6 * sizeof(float);
        unsigned int stride = 8 * sizeof(float);
        
        glGenVertexArrays(1, &vao);
        glBindVertexArray(vao);
        glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ind);
        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, stride, (void*)normal_offset);
        glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, stride, (void*)uv_offset);
        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);
        glEnableVertexAttribArray(2);
        
        
        
        
    };
    void draw(){
        glBindVertexArray(vao);
        glUseProgram(program);
        
        model_matrix = glm::mat4(1.0f);
        view_matrix = glm::lookAt(glm::vec3(cam_x,cam_y,cam_z), glm::vec3(tar_x,tar_y,tar_z), glm::vec3(up_x,up_y,up_z));
        proj_matrix = glm::perspective(70.0f, (float)width/height, 0.1f, 50.0f);
        glm::mat4 mvp = proj_matrix * view_matrix * model_matrix;
        
        glUniformMatrix4fv(glGetUniformLocation(program,"mvp"), 1, GL_FALSE, &mvp[0][0]);
        glDrawElements(GL_TRIANGLES, f3_count, GL_UNSIGNED_SHORT, (void*)0);
    }
};

std::vector<Mesh> meshes;

unsigned int compile_shader(const char* v_source, const char* f_source){
    int vs_status = -1;
    int fs_status = -1;
    
    unsigned int vs = glCreateShader (GL_VERTEX_SHADER);
    glShaderSource (vs, 1, &v_source, NULL);
    glCompileShader (vs);
    glGetShaderiv(vs, GL_COMPILE_STATUS, &vs_status);
    if(vs_status != GL_TRUE){
        int length;
        char buffer[1000];
        glGetShaderInfoLog(vs, sizeof(buffer), &length, buffer);
        std::cout <<"Vertex Shader ID:"<< vs << " OpenGL Shader Compile Error at " << buffer << std::endl;
    }
    unsigned int fs = glCreateShader (GL_FRAGMENT_SHADER);
    glShaderSource (fs, 1, &f_source, NULL);
    glCompileShader (fs);
    glGetShaderiv(fs, GL_COMPILE_STATUS, &fs_status);
    if(fs_status != GL_TRUE){
        int length;
        char buffer[1000];
        glGetShaderInfoLog(fs, sizeof(buffer), &length, buffer);
        std::cout << "Fragmemt Shader ID: " << fs << "OpenGL Shader Compile Error at " << buffer << std::endl;
        
    }
    unsigned int program = glCreateProgram ();
    glAttachShader (program, fs);
    glAttachShader (program, vs);
    glLinkProgram (program);
    
    glDeleteShader(vs);
    glDeleteShader(fs);
    
    return program;
}

void init(){
    if(SDL_Init(SDL_INIT_VIDEO) != 0)
        std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
    std::cout << "SDL initialized." << std::endl;
    
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE );
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
    window = SDL_CreateWindow("Warmachine ;)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
    SDL_SetWindowPosition(window, 50, 50);
    
    
    gl_context = SDL_GL_CreateContext(window);
    if (gl_context == NULL)
        std::cout << "There was an error creating the OpenGL context." << std::endl;
    SDL_GL_MakeCurrent(window, gl_context);
    SDL_GetWindowDisplayMode(window, &current);
    std::cout << "OpenGL VERSION: " << glGetString(GL_VERSION) << std::endl;
    std::cout << "OpenGL RENDERER: " << glGetString(GL_RENDERER) << std::endl;
    std::cout << "GLSL VERSION: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
    std::cout << "OpenGL VENDOR: " << glGetString(GL_VENDOR) << std::endl;
    
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    //glEnable(GL_CULL_FACE);
    //glCullFace(GL_BACK);
    //glFrontFace(GL_CCW);
    
    
    program = compile_shader(vs, fs);
    
    meshes.push_back(Mesh("terrain.obj"));
    
    glClearColor(0.2f, 0.2f, 0.2f, 0.0f);
    
}

int input(){
    int exit = 1;
    while(SDL_PollEvent(&event)){
        if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE){
            exit = 0;
        }
        if (event.type == SDL_MOUSEWHEEL){
            if(event.wheel.x > 0)
                cam_x += 0.5f;
            if(event.wheel.x < 0)
                cam_x -= 0.5f;
            if(event.wheel.y > 0)
                cam_y += 0.5f;
            if(event.wheel.y < 0)
                cam_y -= 0.5f;
            
        }
    }
    return exit;
}

int main(){
    
    char cwd[1024];
    if (getcwd(cwd, sizeof(cwd)) != NULL)
        fprintf(stdout, "Current working dir: %s\n", cwd);
    
    init();
    
    while(input()){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        for(int i = 0; i<meshes.size(); i++){
            meshes[i].draw();
        }
        SDL_GL_SwapWindow(window);
    }
    
    return 0;
}

Share this post


Link to post
Share on other sites
Advertisement

I forgot to post the output in case you wanted to look at the vertex and index data.

Current working dir: /Users/user/Documents/warmachine/warmachine
SDL initialized.
OpenGL VERSION: 4.1 INTEL-10.12.13
OpenGL RENDERER: Intel HD Graphics 4000 OpenGL Engine
GLSL VERSION: 4.10
OpenGL VENDOR: Intel Inc.
Loading model file terrain.obj...
	0 animations
	1 materials
	1 meshes
	0 textures
	24 vertices
-1 1 -1 -1 0 0 2 2 
-1 -1 -1 -1 0 0 2 2 
-1 -1 1 -1 0 0 2 2 
1 1 -1 0 0 -1 2 2 
1 -1 -1 0 0 -1 2 2 
-1 -1 -1 0 0 -1 2 2 
1 1 1 1 0 0 2 2 
1 -1 1 1 0 0 2 2 
1 -1 -1 1 0 0 2 2 
-1 1 1 0 0 1 2 2 
-1 -1 1 0 0 1 2 2 
1 -1 1 0 0 1 2 2 
-1 -1 -1 0 -1 0 2 2 
1 -1 -1 0 -1 0 2 2 
1 -1 1 0 -1 0 2 2 
1 1 -1 0 1 0 2 2 
-1 1 -1 0 1 0 2 2 
-1 1 1 0 1 0 2 2 
-1 1 1 -1 0 0 2 2 
-1 1 -1 0 0 -1 2 2 
1 1 -1 1 0 0 2 2 
1 1 1 0 0 1 2 2 
-1 -1 1 0 -1 0 2 2 
1 1 1 0 1 0 2 2 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 0 2 19 3 5 20 6 8 21 9 11 22 12 14 23 15 17 
Acount:192(24) F3count:36(12)
DONE
Program ended with exit code: 0

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!