Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 5 of 5

Thread: Reading 3D Model

  1. #1
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    4

    Reading 3D Model

    Hi Experts,

    I am completely new at Computer Graphics and has been exploring a lot in OpenGL, i am able to render basic primitives, translate and rotate them.

    I am having trouble reading an input model file.

    Object name = SQUARE
    # triangles = 2
    Material count = 1
    ambient color 0.694 0.580 0.459
    diffuse color 0.992 0.941 0.863
    specular color 1.000 1.000 1.000
    material shine 0.250
    -- 3*[pos(x,y,z) normal(x,y,z) color_index] face_normal(x,y,z)
    v0 -1.0 -1.0 -2.0 0.0 0.0 1.0 0
    v1 1.0 -1.0 -2.0 0.0 0.0 1.0 0
    v2 1.0 1.0 -2.0 0.0 0.0 1.0 0
    face normal 0.0 0.0 1.0
    v0 1.0 1.0 -2.0 0.0 0.0 1.0 0
    v1 -1.0 1.0 -2.0 0.0 0.0 1.0 0
    v2 -1.0 -1.0 -2.0 0.0 0.0 1.0 0
    face normal 0.0 0.0 1.0

    I tried this normal way using file operations

    #include<GL/freeglut.h>
    #include<GL/gl.h>
    #include<math.h>
    #include<fstream>
    #include<iostream>
    #include<string>
    #include<vector>

    using namespace std;

    int main(){
    char line[256];
    int tri,mat;
    vector<double> ambient,diffuse,specular;

    float material_shine;

    FILE* infile;

    infile=fopen("/home/ameya/Documents/CG/square.in","r");

    series of fscanf operations to read float and integer values.

    return 0;
    }

    1)Am getting garbage values.
    2)How should i store the vertices of the sqaure.
    3)Should i define a vector class to store each vector something like that.
    4) or just store vertices in a normal array.

    Actually i am completely clueless, in need of some direction.

    Kindly help.
    Regards,
    Ameya Joshi

  2. #2
    Senior Member OpenGL Pro BionicBytes's Avatar
    Join Date
    Mar 2009
    Location
    UK, London
    Posts
    1,171

    Re: Reading 3D Model

    What you are trying to load is a OBJ model file.
    Personally I think this may be a little beyond you at the moment unless you are very familiar with vertex arrays.
    The data you get from this OBJ model can not be directly used without first restructuring it into multiple arrays (position, normal, texture coordinate, etc). This process is complicated enough without the additional complication of not knowing what the end result is expected to look like.

  3. #3
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    4

    Re: Reading 3D Model

    Hi ,

    Even i guessed , can you please provide me with any reference tutorial for this

    it will be of great help.

    Regards,
    Ameya
    Regards,
    Ameya Joshi

  4. #4

  5. #5
    Junior Member Newbie
    Join Date
    Feb 2012
    Posts
    4

    Re: Reading 3D Model


    Hi,

    I wrote a program to read the square model

    i defined structs for the vertices , color etc

    struct color{
    GLfloat x,y,z;
    };
    struct vertex{
    GLfloat x,y,z;
    };

    struct normal{
    GLfloat x,y,z;
    };

    #include<GL/freeglut.h>
    #include<GL/gl.h>
    #include<stdio.h>
    #include<iostream>
    #include<string>
    #include<vector>
    #include "read.h"

    using namespace std;

    vector<vertex> v;
    vector<normal> n;
    vector<facenormal> face;

    void fileread(FILE *file){


    vertex vec;
    normal nor;
    facenormal f;

    char line[256];

    int tri,mat;

    color c;

    vector<color> ambient,diffuse,specular;


    float material_shine;




    if(file == NULL)
    cout << "Cant open file";

    while(1){

    int r = fscanf(file,"%s",line);//read first line
    if(r==EOF)
    break;

    //cout << line;
    if(line[0]=='#'){

    fscanf(file," triangles = %d",&amp;tri);

    }
    if(line[0] == 'M'){
    //cout << line;
    fscanf(file," count = %d",&amp;mat);
    }

    if(line[0]=='a')
    {cout << "yay";
    cout << fscanf(file," color %f %f %f",&amp;c.x,&amp;c.y,&amp;c.z);
    ambient.push_back(c);
    }
    if(line[0]=='d'){
    cout << fscanf(file," color %f %f %f",&amp;c.x,&amp;c.y,&amp;c.z);
    diffuse.push_back(c);
    }

    if(line[0]=='s'){
    cout << fscanf(file," color %f %f %f",&amp;c.x,&amp;c.y,&amp;c.z);
    specular.push_back(c);
    }
    if(line[0] == 'm')
    cout<<fscanf(file," shine %f",&amp;material_shine);

    if(line[0] == 'v')
    { cout << fscanf(file," %f %f %f %f %f %f",&amp;vec.x,&amp;vec.y,&amp;vec.z,&amp;nor.x,&a mp;nor.y,&amp;nor.z);
    v.push_back(vec);
    n.push_back(nor);

    }

    if(line[0] == 'f')
    { cout<<fscanf(file," normal %f %f %f",&amp;f.x,&amp;f.y,&amp;f.z);
    face.push_back(f);
    }

    }

    //cout << endl<<"debug = "<<tri<<" "<<mat<< " "<<material_shine;

    return;
    }

    i am getting error while passing the vector vertex V to glVertex3fv , cant convert const *float to const GLfloat

    Kindly help
    Regards,
    Ameya Joshi

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •