Black screen of DOOM!

Started by
5 comments, last by ibranext 9 years, 9 months ago

Hi again! It's me and my second post smile.png

Anyways, I would Like some help to resolve a little problem, Infact It's a bug not an error.

See the problem is that my code can compile/build and run but no 3D Blender object is shown.


#include "stdafx.h" //VC++ Must have this ELSE error.

#include <stdarg.h>
#include <math.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <GL/glut.h>


    using namespace std;

    void display();
    void specialKeys();
    float stringToDouble(string& s);
    vector<string> explode(const string& str,const char& ch);


    double rotate_y=0; 
    double rotate_x=0;


    void display(){

      float x,y,z;
      string v;
      string a,b,c;

      glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
      glLoadIdentity();

    /**perform object rotation using keyboard arrows in specialKeys function**/
      glRotatef( rotate_x, 1.0, 0.0, 0.0 );
      glRotatef( rotate_y, 0.0, 1.0, 0.0 );


      ifstream myfile("test.obj"); // open file
       if (myfile.is_open())
      {

        while ( myfile.good())
        {
          string line;
          getline (myfile,line); // get current line
          vector<string> subs;
          subs=explode(line,' '); //explode line by space
          v=subs[0];
          if (v == "v") { // This = True

          /***draw polygon***/
          glBegin(GL_POLYGON); 
          a=subs[1];
          b=subs[2];
          c=subs[3];
           x=stringToDouble(a);
           y=stringToDouble(b);
           z=stringToDouble(c);
           /****draw 3D vertex*****/
           glVertex3f(x,y,z); 
           glEnd();

           }

        }

      }
      myfile.close(); //close file

    glFlush();
    glutSwapBuffers();


    }



    /**** special key***/
    void specialKeys( int key, int x, int y ) {


      if (key == GLUT_KEY_RIGHT)
        rotate_y += 5;


      else if (key == GLUT_KEY_LEFT)
        rotate_y -= 5;

      else if (key == GLUT_KEY_UP)
        rotate_x += 5;

      else if (key == GLUT_KEY_DOWN)
        rotate_x -= 5;

      glutPostRedisplay();

    }



/*** main***/
    int main(int argc, char* argv[]){

      glutInit(&argc,argv);
      glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
      glutCreateWindow("Object");
      glEnable(GL_DEPTH_TEST);
      glutDisplayFunc(display);
      glutSpecialFunc(specialKeys);

      glutMainLoop();

      return 0;

    }



/**converts string to double***/
    float stringToDouble(string& s )
     {
       std::istringstream i(s);
       float x;
       if (!(i >> x))
         return 0;
       return x;
     } 




/***Explode : exploding strings***/
      vector<string> explode(const string& str, const char& ch) {
        string next;
        vector<string> result;

        // For each character in the string
        for (string::const_iterator it = str.begin(); it != str.end(); it++) {
            // If we've hit the terminal character
            if (*it == ch) {
                // If we have some characters accumulated
                if (!next.empty()) {
                    // Add them to the result vector
                    result.push_back(next);
                    next.clear();
                }
            } else {

                next += *it;
            }
        }
        if (!next.empty())
             result.push_back(next);
        return result;
    }

By the way I think this test.obj file is important too...

(I'm not quite sure how to use it ph34r.png) ... Here it is.....


# Blender v2.71 (sub 0) OBJ File: 'untitled.blend'
# www.blender.org
mtllib test.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8

Maby you can see that it is a simple cube wink.png .

Anyways, I hope you can help, thanks!

Advertisement

I would forget about the object for the moment. Try to get the window cleared to a color your want. Then try getting a single hand-crafted triangle rendered. After that we can maybe talk about loading models.

That said, loading the model every time display is called is going to be a horrible mess. Load the data once, then render that data on every frame. I cannot really say what is wrong, but the first sore point I notice is that you do not set a proper projection matrix anywhere.

yes possiblycalling meth0ods between glbegin glend fails everything. if you want to parse model eveytime you call drawing you should do a simplier method, anyway if you dont need such task, load model once and draw it., anyway try to lower z_near value maybe you have set it to 1

hah but wait.


       glBegin(GL_POLYGON); 
          a=subs[1];
          b=subs[2];
          c=subs[3];
           x=stringToDouble(a);
           y=stringToDouble(b);
           z=stringToDouble(c);
           /****draw 3D vertex*****/
           glVertex3f(x,y,z); 
           glEnd();

What sort of dark magic is this

when you call glBegin(something) glENd it requires at least 1 glvertex3f for a point, 2 glvertex3f for a line, and at least 3 glvertex3f for a poly,

i see that you put only one glvertex3f betweeen glbegin(GL_POLYGON); glend(); where you should put at least three of them

Hi again! It's me and my second post smile.png

Anyways, I would Like some help to resolve a little problem, Infact It's a bug not an error.

See the problem is that my code can compile/build and run but no 3D Blender object is shown.




      ifstream myfile("test.obj"); // open file
       if (myfile.is_open())
      {

        while ( myfile.good())
        {
          string line;
          getline (myfile,line); // get current line
          vector<string> subs;
          subs=explode(line,' '); //explode line by space
          v=subs[0];
          if (v == "v") { // This = True

          /***draw polygon***/
          glBegin(GL_POLYGON); 
          a=subs[1];
          b=subs[2];
          c=subs[3];
           x=stringToDouble(a);
           y=stringToDouble(b);
           z=stringToDouble(c);
           /****draw 3D vertex*****/
           glVertex3f(x,y,z); 
           glEnd();

           }

        }

      }
      myfile.close(); //close file

    glFlush();
    glutSwapBuffers();


    }

A: do NOT open a file at run-time to read model data, this is so insanely inefficient, read the file at least into a vector of strings, and then parse those strings, but do not open and read a file during your main loop.

B: you specify a single vertex between glBegin and glEnd, a glPolygon requires at least 3 vertex's in a single draw statement to draw something.

C:

an .obj file is an index based file, this means that each vertex is not specefied in order of drawing, but are all unique vertices in the model.

in order to generate a triangle, or face to draw, you have to take the indices specified by the "f" lines, as the vertices used to draw the corresponding face.

for example:

v -1.0 -1.0 0.0
v  1.0 -1.0 0.0
v -1.0  1.0 0.0
v  1.0  1.0 0.0
 
f 1 2 4
f 1 4 3

this file says: There are 4 unique vertices in the model, to draw the first face, use vertices 1, 2, and 4. to draw the second face, use vertices 1 4 and 3. please note that .obj starts indices at 1, not 0, so if you store the vertices you need to subtract 1 from the face indices.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

Thanks for the help smile.png But I don't think I will be using this code anymore due to the advice you guys game me. I now find that this is not a correctly formed code, any links on how to make a correct one? All I found is a good video but it was on linux, I also found some totorials but they were incompleet or unsatisfactory.

I've found these to be useful. But, I should note I'm very new to this as well, so can't speak to whether there are better out there or not. But, they're fairly complete, in terms of teaching the basics. And, they seem to be relatively well regarded as far as modern opengl tutorials go. Though, I do wish the Loading Objects tutorial was a little more in-depth. But, they provide an example you can parse apart if you want to create your own object loader.

The only other advice I can safely give (as a beginner myself), is to be careful when looking up opengl tutorials online, as there's a marked shift around opengl version 3.3 (i think, perhaps it was earlier). Many of the tutorials before this use (now) deprecated functions and the methods of rendering objects have changed dramatically. The newer methods strike me as decidedly less user friendly, though I'm sure there are solid reasons for them. That isn't to say you can't use the older methods, just be aware that there is a sort of before/after in terms of how things are drawn. If you're getting your information from multiple sources and tutorials, just make sure you're aware of which method you're using. I think as a general rule, if you see "glbegin/glend" you're looking at the older method (though, someone may correct me on this, again, I'm new myself tongue.png). But, it can be confusing at first if you're getting your information from multiple sources that seem to contradict each other.

Beginner here <- please take any opinions with grain of salt

Wow, thanks; I think that link will be helpfull. I found some new things on it.

This topic is closed to new replies.

Advertisement