PDA

View Full Version : 2 Little problem in My Program..=/



Heavens
06-08-2011, 11:18 PM
well... Im really new in OpenGL, start by a class from university... but I had only 4 class and now an exam.. btw... I already write all program that teacher send to me... but still have 2 problems...

1- glutReshapeFunc() no work well when I resize the windows
he became a more small polygon and lose the position of click
but this isnt the really suxx problem..

2- the program already start with a draw line from 0,0.. this is really a problem.. =/


basic of program:

A white Painel... that with click of mouse made a point...
This points be a polygon when press F...
If need delete a point or line just press D... this working fine too

you can move the polygon pressing up/down/left/right and turn arround this with S and L, space makes fill.

I'm not asking for do job for me... was just some mistake in some command line that I cant see... or I dont know/learn =(

(sorry my english.. is already 2:00 am, and isnt my main language)

the program in C++/OpenGL


#include <gl/glut.h>
#include <math.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>

GLfloat angulo = 0.0f;
GLfloat escala = 1.0f;
bool fill = false;
GLfloat dx = 0.0f;
GLfloat dy = 0.0f;
int a1=0;
int a2=0;
int a3=0;
int numLines;


typedef struct point
{
int x;
int y;
}point;
point lines[256];


int gState=GLUT_DOWN;
bool lineisvalid=false;
int gHeight;


void drawlines(bool fill)
{
if (a1==0)
glBegin(GL_POINTS);
else
glBegin(fill ? GL_POLYGON : GL_LINE_LOOP);

for(int i=0; i<=numLines; i++)
{
glColor3f(a1, a2, a3);
glVertex2i(lines[i].x, gHeight-lines[i].y);
}
glEnd();
}

void display()
{

glClear(GL_COLOR_BUFFER_BIT);

glLoadIdentity();
glTranslatef(dx, dy, 0.0f);
glScalef(escala, escala, 0.0f);
glTranslatef(0.0f, -20.0f, 0.0f);
glRotatef(angulo, 0.0f, 0.0f, 1.0f);
glTranslatef(0.0f, 20.0f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f);
drawlines(fill);


glFlush();
glLoadIdentity();
glutSwapBuffers();
}


void init()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glMatrixMode(GL_PROJECTION);
glOrtho(-1, 1.0, -1, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
}



void mouseclick(int button, int state,int x, int y)
{

if(button==GLUT_LEFT_BUTTON &amp;&amp; state==GLUT_DOWN)
{
lines[numLines].x=x;
lines[numLines].y=y;
numLines++;
glutPostRedisplay();
}
glutPostRedisplay();
}


void redimensiona(int width, int height) // reshape something wrong in this
{
gHeight=height;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0,height);

glMatrixMode(GL_MODELVIEW);
}



void teclado(unsigned char tecla, int x, int y)
{

if (tecla == 'f' || tecla == 'F')
{
a1=1; a2=0; a3=0;
glutPostRedisplay();

}
if (tecla == 'd' || tecla == 'D')
{
numLines=numLines-1;
lines[numLines].x=0;
lines[numLines].y=0;
gState=GLUT_DOWN;

}
if (tecla == 's' || tecla == 'S')
angulo += 5;
else if (tecla == 'l' || tecla == 'L')
angulo -= 5;
else if (tecla == '+')
escala += 0.2;
else if (tecla == '-')
escala -= 0.2;
else if (tecla == ' ')
fill = ! fill;
if (angulo >= 360 || angulo <= -360)
angulo = 0;

glutPostRedisplay();


}

void tecladoEspecial(int tecla, int x, int y)
{
if (tecla == GLUT_KEY_UP)
dy += 2;
else if (tecla == GLUT_KEY_DOWN)
dy -= 2;
else if (tecla == GLUT_KEY_LEFT)
dx -= 2;
else if (tecla == GLUT_KEY_RIGHT)
dx += 2;

glutPostRedisplay();
}



int main(int argc, char ** argv)
{

glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("Poligonos");
glutReshapeFunc(redimensiona);
glutDisplayFunc(display);
glutMouseFunc(mouseclick);
glutKeyboardFunc(teclado);
glutSpecialFunc(tecladoEspecial);


init();
glutMainLoop();
return 0;
}

carsten neumann
06-09-2011, 04:48 PM
Please use [ code][ /code] (without space after '[') around code (see also suggested posting guides (http://www.opengl.org/discussion_boards/ubbthreads.php?ubb=showflat&amp;Number=298198#Post2981 98)).



2- the program already start with a draw line from 0,0.


I don't see where you initialize numLines (but you seem to get lucky and the compiler initializes it to 0 for you), however, if it is initially 0, this:



void drawlines(bool fill)
{
// ...

for(int i=0; i<=numLines; i++)
{
glColor3f(a1, a2, a3);
glVertex2i(lines[i].x, gHeight-lines[i].y);
}
}


will draw a line, because the "<=" test passes for i == 0; you probably meant to use "<" for the comparison operator.

I don't know what the other problem might be and frankly I don't quite understand what you are asking, can you try to rephrase the question?