PDA

View Full Version : Display problems



Vlasko
04-16-2003, 07:44 AM
I have a problem with displaying elements on screen they are shown only once then disapper
The code:

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

typedef struct quad{
float x;
float y;
float r,g,b;
}QUADS;

#define MAX_OBJECTS 10

QUADS objects[MAX_OBJECTS];

float mouse_x;
float mouse_y;
float ortho_x=10;
float ortho_y=10;

void randomize()
{
int i;
for(i=0;i<MAX_OBJECTS;i++)
{
objects[i].x=(float)rand()/RAND_MAX*8+1;
objects[i].y=(float)rand()/RAND_MAX*8+1;
}
}



void InitObjects()
{
int i;
srand((unsigned int)time(NULL));
randomize();
for(i=0;i<MAX_OBJECTS;i++)
{
objects[i].b=1.0f;
objects[i].r=1.0f;
objects[i].g=1.0f;
}
}

void init()
{
glShadeModel(GL_SMOOTH);
glClearColor(0.0f,0.0f,0.0f,0.5f);
glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);
glMatrixMode(GL_PROJECTION);
glOrtho(0,ortho_x,0,ortho_y,1,-1);
glMatrixMode(GL_MODELVIEW);
InitObjects();
}

void display()
{
int i;
glClear(GL_COLOR_BUFFER_BIT);
for(i=0;i<MAX_OBJECTS;i++)
{
glLoadIdentity();
glColor3f(objects[i].r,objects[i].g,objects[i].b);
glTranslatef(objects[i].x,objects[i].y,-1.0f);
glBegin(GL_QUADS);
glVertex2f(0.0f,0.0f);
glVertex2f(0.0f,1.0f);
glVertex2f(1.0f,1.0f);
glVertex2f(1.0f,0.0f);
glEnd();
}
glFlush();
glutSwapBuffers();
}

void mousemove(int x,int y)
{
int i,window_x,window_y;
float real_x,real_y;
window_x=glutGet(GLUT_WINDOW_WIDTH);
window_y=glutGet(GLUT_WINDOW_HEIGHT);
real_x=x/window_x*ortho_x;
real_y=y/window_y*ortho_y;
for(i=0;i<MAX_OBJECTS;i++)
{
if(objects[i].b=0.0f)
{
objects[i].b=1.0f;
objects[i].g=1.0f;
}
}
i=0;
do
{
if(real_x<(objects[i].x+1.0f) && real_x>(objects[i].x-1.0f) && real_y>(objects[i].y-1.0f) && real_y<(objects[i].y+1.0f))
{
objects[i].b=0.0f;
objects[i].g=0.0f;
break;
}
i++;
}while(i!=MAX_OBJECTS);
glutPostRedisplay();
}

void reshape( int w, int h)
{
if(h==0)h=1;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(80,(float)w/h,1,200);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(400,400);
glutCreateWindow("Picking Object");
init();
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(display);
glutPassiveMotionFunc(mousemove);
glutMainLoop();
}

+quads are shown in yellow color

I have no idea whats wrong(if i would i wouldn't post the whole code :-)) someone help ?

shinpaughp
04-16-2003, 09:18 AM
In your reshape function you have gluPerspective. In your init function you have glOrtho. I replaced the gluPerspective with glOrtho and everything remains.

Vlasko
04-16-2003, 11:27 AM
Works now thanks but what about that yellow color ?It should be white(1,1,1)or red(1,0,0)

shinpaughp
04-16-2003, 12:04 PM
Your line in mousemove function:

if(objects[i].b=0.0f)

should be

if(objects[i].b==0.0f)

Otherwise it is setting blue to 0 and keeping red and green at 1 which I think is yellow, but I could be wrong.


[This message has been edited by shinpaughp (edited 04-16-2003).]

HS
04-16-2003, 12:35 PM
Its as shinpaughp said.