Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » simple model not showing on opengl via glDrawElements

Credits go to Edgar Reynaldo for helping out!
This thread is locked; no one can reply to it. rss feed Print
simple model not showing on opengl via glDrawElements
PCwizard200
Member #16,127
December 2015

Hello everyone it's been a long time since I been here.

I have an issue, In my main.cpp file I successfully load two very simple txt files, one with model vertices, normals, and uv coordinates and the other with faces. I have two void functions which when called load the two files and transfer the data to declared vectors. I then use the data stored in vectors to render the model via glDrawElements but not a single triangle is rendered. I tried translating model but I have a feeling I'm still missing something. I also made a test where I drew a simple point on screen and the point shows but not my model of course. also I did triangulated model in blender, I exported it to ply format then copy and pasted data to the two txt files I attached.

Thank you for your time and I only included my void functions since the rest is irrelavent.

EDIT: forgot to include one more function by mistake apologies
SECOND EDIT: minor mistake on face loader function and glDrawElements, made correction but still nothing...
THIRD EDIT!: ok I finally got something showing, but my model is all messed up. well I suppose I'll wait until next day... edited source code below.

#SelectExpand
1 2float rot = 1.0f; 3 4//model loading here 5//////////////////////////////////////////////////// 6//////////////////////////////////////////////////// 7struct PlyVertex{ 8 9float x, y, z; 10}Pv; 11 12struct PlyNormal{ 13 14float nx, ny, nz; 15}Pn; 16 17struct PlyUV{ 18 19float u, v; 20}Puv; 21 22struct PlyFace{ 23 24unsigned int f1, f2, f3, f4; 25}Pf; 26 27std::vector<PlyVertex> vertices; 28std::vector<PlyNormal> normals; 29std::vector<PlyUV> uv; 30std::vector<PlyFace> faces; 31 32void LoadTexPlyV(std::string filename){ 33 34std::string line; 35std::ifstream PlyFileV(filename); 36if(PlyFileV.is_open()){ 37 38 while(getline(PlyFileV, line)){ 39 40 PlyFileV >> Pv.x >> Pv.y >> Pv.z >> Pn.nx >> Pn.ny >> Pn.nz >> Puv.u >> Puv.v; 41 vertices.push_back({Pv.x, Pv.y, Pv.z}); 42 normals.push_back({Pn.nx, Pn.ny, Pn.nz}); 43 uv.push_back({Puv.u, Puv.v}); 44 } 45 PlyFileV.close(); 46} 47 48else std::cout << "Unable to open file."; 49} 50 51void LoadPlyF(std::string filename){ 52 53std::string line; 54std::ifstream PlyFileF(filename); 55if(PlyFileF.is_open()){ 56 57 while(getline(PlyFileF, line)){ 58 59 PlyFileF >> Pf.f1 >> Pf.f2 >> Pf.f3 >> Pf.f4; 60 faces.push_back({Pf.f2, Pf.f3, Pf.f4}); 61 } 62 PlyFileF.close(); 63} 64 65else std::cout << "Unable to open file."; 66} 67////////////////////////////////////////////////////// 68///////////////////////////////////////////////////// 69 70//main rendering functions here 71void display(){ 72 73glClearDepth(1); 74glEnable(GL_DEPTH_TEST); 75glEnable(GL_LIGHTING); 76glEnable(GL_COLOR_MATERIAL); 77glEnable(GL_LIGHT0); 78glEnableClientState(GL_VERTEX_ARRAY); 79glEnableClientState(GL_NORMAL_ARRAY); 80glClearColor(1.0f, 0.0f, 0.0f, 1.0f); 81glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 82glLoadIdentity(); 83 84LoadTexPlyV("C:/c++ projects/game/game/bin/Debug/data/cubem_v.txt"); 85LoadPlyF("C:/c++ projects/game/game/bin/Debug/data/cubem_f.txt"); 86 87glVertexPointer(3, GL_FLOAT, 0, vertices.data()); 88glNormalPointer(GL_FLOAT, 0, normals.data()); 89glTexCoordPointer(3, GL_FLOAT, 0, uv.data()); 90 91glTranslatef(0.0f, 0.0f, -10.0f); 92glRotatef(rot, 1.0f, 0.0f, 0.0f); 93glDrawElements(GL_TRIANGLE_FAN, faces.size(), GL_UNSIGNED_INT, faces.data()); 94 95rot = rot + 1; 96 97glFlush(); 98} 99 100void reshape(){ 101 102glViewport(0, 0, 800, 600); 103glMatrixMode(GL_PROJECTION); 104glLoadIdentity(); 105gluPerspective(60, 800 / 600, 1.0, 100.0); 106glMatrixMode(GL_MODELVIEW); 107glDisableClientState(GL_VERTEX_ARRAY); 108glDisableClientState(GL_NORMAL_ARRAY); 109}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

I can only guess that you haven't correctly set up your projection matrix.

All I see is load_identity. That could be either matrix as you haven't specified I don't know what the default is.

GL_MODELVIEW_MATRIX
GL_PROJECTION_MATRIX

You have to set up both.

PCwizard200
Member #16,127
December 2015

Hello!

I edited my reshape function as so:

#SelectExpand
1 2void reshape(){ 3 4glViewport(0, 0, 800, 600); 5glMatrixMode(GL_MODELVIEW_MATRIX); 6glMatrixMode(GL_PROJECTION_MATRIX); 7glLoadIdentity(); 8gluPerspective(60, 800 / 600, 1.0, 100.0); 9 10glDisableClientState(GL_VERTEX_ARRAY); 11glDisableClientState(GL_NORMAL_ARRAY); 12}

However now nothing shows at all. So I'm thinking perhaps its my model data files? I basically copied vertex, normals, and uv to one txt file and the faces to another from a .PLY file I made in blender.

Before your reply the cube was showing but faces were messed up, I attached an image in this reply and in my first post the model data files in txt form. Maybey I am reading my model wrong? not sure yet...

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

The MODELVIEW matrix should be identity, unless you're moving things around.

The PROJECTION matrix needs to be set with an orthographic or perspective projection.

The order doesn't matter, but as soon as you do;

glMatrixMode(GL_PROJECTION);

It starts affecting the projection matrix.

Similarly, as soon as you do;

glMatrixMode(GL_MODELVIEW);

It starts affecting the model view matrix.

So it looks like your projection matrix was fine, but I think your coordinates were clipped by the near clipping plane. Try translating with the modelview matrix about 15 forward down -Z.

EDIT
Double check your cube coordinates, and the winding order. Both of those could have affected what you see.

PCwizard200
Member #16,127
December 2015

I tried translating it on -z but it did not fix it. I also did a test by rendering only the points and the vertices looked to be in the right place.

How do you think I should go about checking the winding order?

btw I use these two tutorials to teach myself, this is not for school just hobby:

http://www.swiftless.com/opengltuts.html
http://www.songho.ca/opengl/

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Winding order is simply vertex order. gl allows you to specify either front or back.

By default, GL winds vertices counter clockwise. So looking down from the top of your triangle face, wind the vertices counter clockwise.

EDIT
If your vertices are in the right place, then the problem is either the winding order, or the glFrontFace value specified in the call. Default is top is front.

PCwizard200
Member #16,127
December 2015

my new code so far using GL_CW... but still the same result. Could it be the way blender exported the original poly? would using .x or obj format be a wiser choice perhaps? It must be something im not doing right...

EDIT: In my main program loop I call function display THEN function reshape. That probably does not matter though...

SECOND EDIT: I set GL_LIGHT0 to GL_LIGHT1 and the model appears to be much better! could it be an issue with lighting GL_LIGHT0??? I attached an image to explain this result.

#SelectExpand
1 2float rot = 1.0f; 3 4//model loading here 5//////////////////////////////////////////////////// 6//////////////////////////////////////////////////// 7struct PlyVertex{ 8 9GLfloat x, y, z; 10}Pv; 11 12struct PlyNormal{ 13 14GLfloat nx, ny, nz; 15}Pn; 16 17struct PlyUV{ 18 19GLfloat u, v; 20}Puv; 21 22struct PlyFace{ 23 24GLuint f1, f2, f3, f4; 25}Pf; 26 27std::vector<PlyVertex> vertices; 28std::vector<PlyNormal> normals; 29std::vector<PlyUV> uv; 30std::vector<PlyFace> faces; 31 32void LoadTexPlyV(std::string filename){ 33 34std::string line; 35std::ifstream PlyFileV(filename); 36if(PlyFileV.is_open()){ 37 38 while(getline(PlyFileV, line)){ 39 40 PlyFileV >> Pv.x >> Pv.y >> Pv.z >> Pn.nx >> Pn.ny >> Pn.nz >> Puv.u >> Puv.v; 41 vertices.push_back({Pv.x, Pv.y, Pv.z}); 42 normals.push_back({Pn.nx, Pn.ny, Pn.nz}); 43 uv.push_back({Puv.u, Puv.v}); 44 } 45 PlyFileV.close(); 46} 47 48else std::cout << "Unable to open file."; 49} 50 51void LoadPlyF(std::string filename){ 52 53std::string line; 54std::ifstream PlyFileF(filename); 55if(PlyFileF.is_open()){ 56 57 while(getline(PlyFileF, line)){ 58 59 PlyFileF >> Pf.f1 >> Pf.f2 >> Pf.f3 >> Pf.f4; 60 faces.push_back({Pf.f2, Pf.f3, Pf.f4}); 61 } 62 PlyFileF.close(); 63} 64 65else std::cout << "Unable to open file."; 66} 67////////////////////////////////////////////////////// 68///////////////////////////////////////////////////// 69 70//main rendering functions here 71void display(){ 72 73glClearDepth(1); 74glEnable(GL_DEPTH_TEST); 75glEnable(GL_LIGHTING); 76glEnable(GL_COLOR_MATERIAL); 77glEnable(GL_LIGHT0); 78glEnableClientState(GL_VERTEX_ARRAY); 79glEnableClientState(GL_NORMAL_ARRAY); 80glFrontFace(GL_CW); 81glClearColor(1.0f, 0.0f, 0.0f, 1.0f); 82glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 83glLoadIdentity(); 84 85LoadTexPlyV("C:/c++ projects/game/game/bin/Debug/data/cubem_v.txt"); 86LoadPlyF("C:/c++ projects/game/game/bin/Debug/data/cubem_f.txt"); 87 88glVertexPointer(3, GL_FLOAT, 0, vertices.data()); 89glNormalPointer(GL_FLOAT, 0, normals.data()); 90glTexCoordPointer(3, GL_FLOAT, 0, uv.data()); 91 92glTranslatef(0.0f, 0.0f, -10.0f); 93glRotatef(rot, 1.0f, 0.0f, 0.0f); 94glDrawElements(GL_TRIANGLE_FAN, faces.size(), GL_UNSIGNED_INT, faces.data()); 95 96rot = rot + 1; 97 98glFlush(); 99} 100 101void reshape(){ 102 103glViewport(0, 0, 800, 600); 104glMatrixMode(GL_PROJECTION); 105glLoadIdentity(); 106gluPerspective(60, 800 / 600, 1.0, 100.0); 107glMatrixMode(GL_MODELVIEW); 108glLoadIdentity(); 109 110 111glDisableClientState(GL_VERTEX_ARRAY); 112glDisableClientState(GL_NORMAL_ARRAY); 113}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

The loop order would only matter on the first frame. After that, it would 'catch-up'.

If changing the light affects it, you might take a look at your normal vectors. They should all be multiples of .25 from 0.0 to 1.0, and they should be 3 wide, signifying normal x,y,z. I'm not sure what else to check.

EDIT
They should be multiples of .25PI in radians I mean.

MikiZX
Member #17,092
June 2019

I have not tried this (only copying it here after a Google search) but you might want to try adding these just a line before doing the actual drawing:

glDisable(GL_CULL_FACE);
glDisable(GL_LIGHTING);
glDisable(GL_TEXTURE_2D);

Might help getting the initial drawing to work.

Also, if I understand correctly you are drawing something that was exported by Blender3d yet you are drawing the elements using GL_TRIANGLE_FAN - I am not sure that Blender3d exports triangle fans (?!) though I could be wrong. Possibly try using GL_TRIANGLES ?

EDIT: Also, before exporting from blender try to triangulate your model.

PCwizard200
Member #16,127
December 2015

Hi! thank you. I just used a different format from blender which is obj that I then edited to my liking and then I fixed the way I was reading vertices. I got it to work correctly.

Go to: