Skip to main content
gamedev.net
🔒 Locked

Problems with a simple program in OpenGL

Started by Himura-San Nov 15, 2004 at 7:10 AM 2 replies 400+ views
Original Post
Himura-San
Himura-San
Hi, i´m extremely noob in OpenGL API and i got a big problem in coordinates.Well, i wanna make a program that highlight a square if the user clicks on it and set the default color if he clicks out.The problem in my code is : if the user click on the top of square it doesn´t highlight!!! Here is the code, please tell what´s wrong.

#include <GL\glut.h>

void Draw()
  {
      //Aplica as alterações ao modelo      
      glMatrixMode(GL_MODELVIEW);
      glLoadIdentity();
      
      glClear(GL_COLOR_BUFFER_BIT);
     
      
      glBegin(GL_POLYGON);
        {
            //glColor3f(1.0f,1.0f,1.0f);
            glVertex2i(100,150);
            glVertex2i(100,100);
            glVertex2i(150,100);
            glVertex2i(150,150);
        }
       glEnd();
       glFlush();
   }    

void Mouse(int button , int state, int x , int y)
  {
      if (button == GLUT_LEFT_BUTTON)
         if (state == GLUT_DOWN)
            if ( (x >= 100) && (x <= 150) && (y >= 100) && (y <= 150) ) glColor3f(1.0f,0.0f,0.0f);
            else glColor3f(1.0f,1.0f,1.0f);
         //if (state == GLUT_UP) glColor3f(1.0f,1.0f,1.0f);      
      glutPostRedisplay();      
  }    

void Reshape(GLint w, GLint h)
  {
      glViewport(0,0,(GLsizei)w,(GLsizei)h);
      glMatrixMode(GL_PROJECTION);
      glLoadIdentity();
      gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);
  }


int main()
  {
      //Cria a Janela
      glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
      glutInitWindowSize(320,240);
      glutInitWindowPosition(10,10);
      glutCreateWindow("My Program :p");
      
      //Seta a cor de fundo como preta
      glClearColor(0.0,0.0,0.0,0.0);
      
      glutDisplayFunc(Draw);      
      glutReshapeFunc(Reshape);
      glutMouseFunc(Mouse);
      glutMainLoop();
       
   }


[Edited by - Himura-San on November 15, 2004 3:24:26 PM]
Roboguy
Roboguy
works fine for me, but I had to fix you main function, it should look like this:
int main(int argc, char* argv[]){//Cria a JanelaglutInit(&argc, argv);glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);glutInitWindowSize(320,240);glutInitWindowPosition(10,10);glutCreateWindow("My Program :p");//Seta a cor de fundo como pretaglClearColor(0.0,0.0,0.0,0.0);glutDisplayFunc(Draw);glutReshapeFunc(Reshape);glutMouseFunc(Mouse);glutMainLoop();}
Himura-San
Himura-San
Thanks Roboguy , but it doesn´t work fine.Just try to click on top of square, the color doesn´t change!!!

Is this a coordinate problem?Is there any difference between passing arguments in the main function main and passing void?

Roboguy
Roboguy
the arguments describe the arguments passed to the program, for example, if you ran:
C:\path\to\myOpenGLProg.exe this is a test

the arguments in main would be setup like this:
argc: 5(I think)
contents of argv:
argv[0] = "C:\path\to\myOpenGLProg.exe"
argv[1] = "this"
argv[2] = "is"
argv[3] = "a"
argv[4] = "test"

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.