Results 1 to 7 of 7

Thread: 2d code only draws black screen - noob question...

  1. #1
    Member Contributor
    Join Date
    Aug 2011
    Posts
    75

    2d code only draws black screen - noob question...

    Hi

    Using these lines:
    Code :
      glBegin(GL_LINES);
      glVertex3f(10, 10, 0);
      glVertex3f(500, 500, 0);
      glEnd();

    I want to try to plot some 2D code (make a straight line in this example)... However, all I get is a black screen - I'm trying to modify the below code:

    Code :
    //    g++ openGL.cpp  -lGL -lglut -lGLU -lm && ./a.out
     
    #ifdef __APPLE_CC__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
     
    static float x = 0, y = 0;
     
    // Handles the keyboard event: the left and right arrows bend the elbow, the
    // up and down keys bend the shoulder.
    void special(int key, int, int)
    {
      switch (key) {
        case GLUT_KEY_LEFT: (x -= 1); break;
        case GLUT_KEY_RIGHT: (x += 1); break;
        case GLUT_KEY_UP: (y += 1); break;
        case GLUT_KEY_DOWN: (y -= 1); break;
        default: return;
      }
      glutPostRedisplay();
    }
     
     
    void display()
    {
      glClear(GL_COLOR_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      //==================
      glPushMatrix();
      glTranslatef(x, y, 0.0);
     
      glBegin(GL_LINES);
      glVertex3f(10, 10, 0);
      glVertex3f(500, 500, 0);
      glEnd();
     
      glPopMatrix();
      //==================
      glFlush();
    }
     
    // Handles the reshape event by setting the viewport so that it takes up the
    // whole visible region, then sets the projection matrix to something reason-
    // able that maintains proper aspect ratio.
    void reshape(GLint w, GLint h)
    {
      glViewport(0, 0, w, h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      /*  gluPerspective(65.0, // fovy: field of angle view (degrees), in y direction
    				 GLfloat(w)/GLfloat(h), // ratio of x (width) to y (height)
    				 1.0, // distance from the viewer to the near clipping plane
    				 20.0); // distance from the viewer to the far clipping plane
      */
    }
     
     
    // Initializes GLUT, the display mode, and main window; registers callbacks;
    // does application initialization; enters the main event loop.
    int main(int argc, char** argv)
    {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(800, 600);
      glutCreateWindow("Testing...");
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutSpecialFunc(special);
     
      //-----------------
      glShadeModel(GL_FLAT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      /*  gluLookAt(
    			1,2,8, // eyeX, eyeY, eyeZ
    			0,0,0, // centerX, centerY, centerZ
    			0,1,0); // upX, upY, upZ
      */
      //-----------------
     
      glutMainLoop();
    }

    Please advice, thank you very much!

  2. #2
    Member Contributor
    Join Date
    Aug 2011
    Posts
    75

    Re: 2d code only draws black screen - noob question...

    Aargh... sorry... Inserting:

    gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);

    in reshape(..) seems to fix the problem... Merry christmas...

  3. #3
    Junior Member Newbie
    Join Date
    Dec 2011
    Posts
    4

    Re: 2d code only draws black screen - noob question...

    Hmm, why do you have gluLookAt and gluPerspective commented out?

    Try removing the /* */ and let me know what happens!

  4. #4
    Member Contributor
    Join Date
    Aug 2011
    Posts
    75

    Re: 2d code only draws black screen - noob question...

    Hi LearningOGL,

    The problem was solved with:

    gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);

    in reshape(..)... Answer to your question: gluLookAt and gluPerspective is commented out because the original code was 3d, I then changed it into 2D-code...

    I only have one tiny/simple question:
    Code :
    //    g++ openGL.cpp  -lGL -lglut -lGLU -lm &amp;&amp; ./a.out
     
    #ifdef __APPLE_CC__
    #include <GLUT/glut.h>
    #else
    #include <GL/glut.h>
    #endif
     
    static float x = 0, y = 0;
     
    // Handles the keyboard event: the left and right arrows bend the elbow, the
    // up and down keys bend the shoulder.
    void special(int key, int, int)
    {
      switch (key) {
        case GLUT_KEY_LEFT: (x -= 5); break;
        case GLUT_KEY_RIGHT: (x += 5); break;
        case GLUT_KEY_UP: (y += 5); break;
        case GLUT_KEY_DOWN: (y -= 5); break;
        default: return;
      }
      glutPostRedisplay();
    }
     
     
    void display()
    {
      glClear(GL_COLOR_BUFFER_BIT);
      glMatrixMode(GL_MODELVIEW);
      //==================
      glPushMatrix();
      glTranslatef(x, y, 0.0);
     
      glColor3f(1.0, 1.0, 1.0);
      glBegin(GL_LINES);
      glVertex2f(-50, -50);
      glVertex2f(50, 50);
      glVertex2f(-50, 50);
      glVertex2f(50, -50);
      glEnd();
     
      glPopMatrix();
      //==================
      glutSwapBuffers(); // also does glFlush();
    }
     
    // Handles the reshape event by setting the viewport so that it takes up the
    // whole visible region, then sets the projection matrix to something reason-
    // able that maintains proper aspect ratio.
    void reshape(GLint w, GLint h)
    {
      GLdouble myLgt = 100;
      bool aspect = false;
      //  bool aspect = true;
     
      if (!aspect)
    	{
    	  glViewport(0, 0, // x, y
    				 (GLsizei) w, (GLsizei) h); // width, height
    	  gluOrtho2D(-myLgt, myLgt, // left, right
    				 -myLgt, myLgt); // bottom, top
    	}
     
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
     
      if (aspect)
    	{
    	  glViewport(0, 0, // x, y
    				 (GLsizei) w, (GLsizei) h); // width, height
     
    	  if (w <= h) // tall
    		gluOrtho2D( -myLgt, myLgt,
    				   -myLgt*(GLfloat)h/(GLfloat)w, myLgt*(GLfloat)h/(GLfloat)w );
    	  else // wide
    		gluOrtho2D( -myLgt*(GLfloat)w/(GLfloat)h, myLgt*(GLfloat)w/(GLfloat)h,
    				   -myLgt, myLgt);
    	}
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
    }
     
     
    void keyboard(unsigned char key, int x, int y)
    {
       switch (key) {
          case 27:
             exit(0);
             break;
       }
    }
     
     
    // Initializes GLUT, the display mode, and main window; registers callbacks;
    // does application initialization; enters the main event loop.
    int main(int argc, char** argv)
    {
      glutInit(&amp;argc, argv);
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(800, 600);
      glutCreateWindow("Testing...");
      //-----------------
      glShadeModel(GL_FLAT);
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      //-----------------
      glutDisplayFunc(display);
      glutReshapeFunc(reshape);
      glutSpecialFunc(special);
      glutKeyboardFunc(keyboard);
     
      glutMainLoop();
      return 0;
    }

    When I run this, why does the cross move completely out of the picture when I press any arrow key (note the difference, when "bool aspect" value is negated => this is the behaviour that I would expect in any case) ???

    I don't understand that/why the current cross moves out of the screen by a single arrow-key-press...

  5. #5
    Member Contributor
    Join Date
    Aug 2011
    Posts
    75

    Re: 2d code only draws black screen - noob question...

    hmm... No answers...

    Please allow me to rephrase: Copy/paste the code into a file and type (or press F5 with MS Visual studio):

    g++ openGL.cpp -lGL -lglut -lGLU -lm &amp;&amp; ./a.out

    Then single-type any of the 4 arrow keys. Notice/please explain to me the difference between using:

    bool aspect = false;
    bool aspect = true;

    I cannot understand, why the image moves "out-of-the-screen" by a single key-press (I don't like this)...???

    Any explanation for this behaviour is greatly appreciated!

  6. #6
    Member Contributor uruk's Avatar
    Join Date
    Dec 2004
    Location
    acasa :) sau la munca :(
    Posts
    66

    Re: 2d code only draws black screen - noob question...

    In your resize function:
    move
    Code :
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
    before the first "if".
    Stat rosa pristina nomine, nomina nuda tenemus.

  7. #7
    Member Contributor
    Join Date
    Aug 2011
    Posts
    75

    Re: 2d code only draws black screen - noob question...

    Aah, that was stupid of me -

    THANK you very much, uruk!!!

    I'm very grateful for your time (even though I should have seen it myselt)

Similar Threads

  1. I have this code, that draws a cloud, house, female and male
    By jasmine in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 10-18-2016, 08:11 AM
  2. Black screen after porting code to GL 3.3
    By Zylzyl in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 01-12-2014, 06:25 AM
  3. noob cannot figure out what is wrong - code crash.
    By newsb in forum OpenGL: Basic Coding
    Replies: 14
    Last Post: 01-23-2012, 05:02 AM
  4. inline code draws, function call doesn't
    By godofskies8 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-20-2004, 09:46 AM
  5. Replies: 0
    Last Post: 09-30-2003, 08:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean