PDA

View Full Version : 2d code only draws black screen - noob question...



newsb
12-23-2011, 05:33 PM
Hi

Using these lines:


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:



// 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!

newsb
12-23-2011, 06:16 PM
Aargh... sorry... Inserting:

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

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

LearningOGL
12-23-2011, 06:17 PM
Hmm, why do you have gluLookAt and gluPerspective commented out?

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

newsb
12-23-2011, 08:45 PM
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:


// 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...

newsb
12-24-2011, 11:00 AM
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!

uruk
12-25-2011, 05:00 AM
In your resize function:
move


glMatrixMode(GL_PROJECTION);
glLoadIdentity();

before the first "if".

newsb
12-26-2011, 06:44 PM
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) :-)