Jump to content
  • Advertisement
Sign in to follow this  
Himura-San

OpenGL Problems with a simple program in OpenGL

This topic is 5024 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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 Janela
glutInit(&argc, argv);
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();

}

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!