Hi I am a OpenGL beginner here, this code suppose to draw a yellow square at bottom right but I dont know why nothing happens when I click the mouse (left button), here’s the code:
#include <stdio.h>
#include <math.h>
#include “glut.h”
void sideGlutDisplay( void );
void myGlutMouse(int button, int state, int x, int y );
//void myGlutPassiveMotion(int x, int y);
void drawSquare();
int side_window;
void redraw_func(int id) {
glutSetWindow(side_window);
glutPostRedisplay();
}
/***************************************** myGlutIdle() ***********/
void myGlutIdle( void )
{
}
void myGlutDisplay( void )
{
glClearColor(0,0,0,1);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,1.0,0.0,1.0);
// left, right, bottom, top
glViewport(0,0,600,400);
// startx, starty, xsize, ysize
// coordinates begin from lower left corner of window
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//drawSquare();
glutSwapBuffers();
}
void drawSquare()
{
glColor3f(1,1,0);
glBegin(GL_QUADS);
glVertex3f(0.83,0.05,0);
glVertex3f(0.91,0.05,0);
glVertex3f(0.91,0.17,0);
glVertex3f(0.83,0.17,0);
glEnd();
}
/***************************************** myGlutMouse() **********/
void myGlutMouse(int button, int state, int x, int y )
{
switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
drawSquare();
}
break;
}
glutPostRedisplay();
}
void myGlutPassiveMotion(int x, int y)
{
glutPostRedisplay();
}
/***************************************** myGlutMotion() **********/
void myGlutMotion(int x, int y )
{
glutPostRedisplay();
}
/**************************************** myGlutReshape() *************/
void myGlutReshape( int x, int y )
{
glutPostRedisplay();
}
/********************* myGlutKeyboard() **********/
void myGlutKeyboard(unsigned char Key, int x, int y)
{
switch(Key)
{
case 27:
case ‘q’:
exit(0);
break;
};
glutPostRedisplay();
}
/**************************************** main() ********************/
int main(int argc, char* argv[])
{
glutInitDisplayMode( GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH );
side_window = glutCreateWindow( "Simple 2D Graphics Editor" );
glutPositionWindow( 10, 30 );
glutReshapeWindow( 600, 400 );
glutDisplayFunc( myGlutDisplay );
glutReshapeFunc( myGlutReshape );
glutKeyboardFunc( myGlutKeyboard );
glutMouseFunc( myGlutMouse );
//glutMotionFunc( myGlutMotion );
//glutPassiveMotionFunc( myGlutPassiveMotion );
glutMainLoop();
return 0;
}