• Advertisement
  • Popular Tags

  • Popular Now

  • Advertisement
  • Similar Content

    • By codelyoko373
      I wasn't sure if this would be the right place for a topic like this so sorry if it isn't.
      I'm currently working on a project for Uni using FreeGLUT to make a simple solar system simulation. I've got to the point where I've implemented all the planets and have used a Scene Graph to link them all together. The issue I'm having with now though is basically the planets and moons orbit correctly at their own orbit speeds.
      I'm not really experienced with using matrices for stuff like this so It's likely why I can't figure out how exactly to get it working. This is where I'm applying the transformation matrices, as well as pushing and popping them. This is within the Render function that every planet including the sun and moons will have and run.
      if (tag != "Sun") { glRotatef(orbitAngle, orbitRotation.X, orbitRotation.Y, orbitRotation.Z); } glPushMatrix(); glTranslatef(position.X, position.Y, position.Z); glRotatef(rotationAngle, rotation.X, rotation.Y, rotation.Z); glScalef(scale.X, scale.Y, scale.Z); glDrawElements(GL_TRIANGLES, mesh->indiceCount, GL_UNSIGNED_SHORT, mesh->indices); if (tag != "Sun") { glPopMatrix(); } The "If(tag != "Sun")" parts are my attempts are getting the planets to orbit correctly though it likely isn't the way I'm meant to be doing it. So I was wondering if someone would be able to help me? As I really don't have an idea on what I would do to get it working. Using the if statement is truthfully the closest I've got to it working but there are still weird effects like the planets orbiting faster then they should depending on the number of planets actually be updated/rendered.
    • By Jens Eckervogt
      Hello everyone, 
      I have problem with texture
      using System; using OpenTK; using OpenTK.Input; using OpenTK.Graphics; using OpenTK.Graphics.OpenGL4; using System.Drawing; using System.Reflection; namespace Tutorial_05 { class Game : GameWindow { private static int WIDTH = 1200; private static int HEIGHT = 720; private static KeyboardState keyState; private int vaoID; private int vboID; private int iboID; private Vector3[] vertices = { new Vector3(-0.5f, 0.5f, 0.0f), // V0 new Vector3(-0.5f, -0.5f, 0.0f), // V1 new Vector3(0.5f, -0.5f, 0.0f), // V2 new Vector3(0.5f, 0.5f, 0.0f) // V3 }; private Vector2[] texcoords = { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) }; private int[] indices = { 0, 1, 3, 3, 1, 2 }; private string vertsrc = @"#version 450 core in vec3 position; in vec2 textureCoords; out vec2 pass_textureCoords; void main(void) { gl_Position = vec4(position, 1.0); pass_textureCoords = textureCoords; }"; private string fragsrc = @"#version 450 core in vec2 pass_textureCoords; out vec4 out_color; uniform sampler2D textureSampler; void main(void) { out_color = texture(textureSampler, pass_textureCoords); }"; private int programID; private int vertexShaderID; private int fragmentShaderID; private int textureID; private Bitmap texsrc; public Game() : base(WIDTH, HEIGHT, GraphicsMode.Default, "Tutorial 05 - Texturing", GameWindowFlags.Default, DisplayDevice.Default, 4, 5, GraphicsContextFlags.Default) { } protected override void OnLoad(EventArgs e) { base.OnLoad(e); CursorVisible = true; GL.GenVertexArrays(1, out vaoID); GL.BindVertexArray(vaoID); GL.GenBuffers(1, out vboID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)(vertices.Length * Vector3.SizeInBytes), vertices, BufferUsageHint.StaticDraw); GL.GenBuffers(1, out iboID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)(indices.Length * sizeof(int)), indices, BufferUsageHint.StaticDraw); vertexShaderID = GL.CreateShader(ShaderType.VertexShader); GL.ShaderSource(vertexShaderID, vertsrc); GL.CompileShader(vertexShaderID); fragmentShaderID = GL.CreateShader(ShaderType.FragmentShader); GL.ShaderSource(fragmentShaderID, fragsrc); GL.CompileShader(fragmentShaderID); programID = GL.CreateProgram(); GL.AttachShader(programID, vertexShaderID); GL.AttachShader(programID, fragmentShaderID); GL.LinkProgram(programID); // Loading texture from embedded resource texsrc = new Bitmap(Assembly.GetEntryAssembly().GetManifestResourceStream("Tutorial_05.example.png")); textureID = GL.GenTexture(); GL.BindTexture(TextureTarget.Texture2D, textureID); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)All.Linear); GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)All.Linear); GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba, texsrc.Width, texsrc.Height, 0, PixelFormat.Bgra, PixelType.UnsignedByte, IntPtr.Zero); System.Drawing.Imaging.BitmapData bitmap_data = texsrc.LockBits(new Rectangle(0, 0, texsrc.Width, texsrc.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb); GL.TexSubImage2D(TextureTarget.Texture2D, 0, 0, 0, texsrc.Width, texsrc.Height, PixelFormat.Bgra, PixelType.UnsignedByte, bitmap_data.Scan0); texsrc.UnlockBits(bitmap_data); GL.Enable(EnableCap.Texture2D); GL.BufferData(BufferTarget.TextureBuffer, (IntPtr)(texcoords.Length * Vector2.SizeInBytes), texcoords, BufferUsageHint.StaticDraw); GL.BindAttribLocation(programID, 0, "position"); GL.BindAttribLocation(programID, 1, "textureCoords"); } protected override void OnResize(EventArgs e) { base.OnResize(e); GL.Viewport(0, 0, ClientRectangle.Width, ClientRectangle.Height); } protected override void OnUpdateFrame(FrameEventArgs e) { base.OnUpdateFrame(e); keyState = Keyboard.GetState(); if (keyState.IsKeyDown(Key.Escape)) { Exit(); } } protected override void OnRenderFrame(FrameEventArgs e) { base.OnRenderFrame(e); // Prepare for background GL.Clear(ClearBufferMask.ColorBufferBit); GL.ClearColor(Color4.Red); // Draw traingles GL.EnableVertexAttribArray(0); GL.EnableVertexAttribArray(1); GL.BindVertexArray(vaoID); GL.UseProgram(programID); GL.BindBuffer(BufferTarget.ArrayBuffer, vboID); GL.VertexAttribPointer(0, 3, VertexAttribPointerType.Float, false, 0, IntPtr.Zero); GL.ActiveTexture(TextureUnit.Texture0); GL.BindTexture(TextureTarget.Texture3D, textureID); GL.BindBuffer(BufferTarget.ElementArrayBuffer, iboID); GL.DrawElements(BeginMode.Triangles, indices.Length, DrawElementsType.UnsignedInt, 0); GL.DisableVertexAttribArray(0); GL.DisableVertexAttribArray(1); SwapBuffers(); } protected override void OnClosed(EventArgs e) { base.OnClosed(e); GL.DeleteVertexArray(vaoID); GL.DeleteBuffer(vboID); } } } I can not remember where do I add GL.Uniform2();
    • By Jens Eckervogt
      Hello everyone
      For @80bserver8 nice job - I have found Google search. How did you port from Javascript WebGL to C# OpenTK.?
      I have been searched Google but it shows f***ing Unity 3D. I really want know how do I understand I want start with OpenTK But I want know where is porting of Javascript and C#?
       
      Thanks!
    • By mike44
      Hi
      I draw in a OpenGL framebuffer. All is fine but it eats FPS (frames per second), hence I wonder if I could execute the framebuffer drawing only every 5-10th loop or so?
      Many thanks
       
    • By cebugdev
      hi all,
      how to implement this type of effect ? 
      Also what is this effect called? this is considered volumetric lighting?
      what are the options of doing this? 
      a. billboard? but i want this to have the 3D effect that when we rotate the camera we can still have that 3d feel.
      b. a transparent 3d mesh? and we can animate it as well?
      need your expert advise.
       
      additional:
      2. how to implement things like fireball projectile (shot from a monster) (billboard texture or a 3d mesh)?
       
      Note: im using OpenGL ES 2.0 on mobile. 
       
      thanks!
  • Advertisement
  • Advertisement
Sign in to follow this  

OpenGL Assimp index array problem

This topic is 828 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