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(&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!