PDA

View Full Version : Text and paint problem



valonj
05-18-2012, 04:26 PM
i didn't know if i should 've post it here or somewhere else i', new so sorry if this is in the wrong place.
My problem is that the code that it is shown below is supposed to show a text in the screen and simply draw on the screen with different colors, but it doesn't when i change the color the only thing that changes is the text color and i cant draw on the screen, so please help someone who understands this better thank you.

#include <iostream>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <GL/glut.h>
using namespace std;
void display(void);

/* mos harro mi fshi
void polygon(int a, int b, int c , int d);
void DrawCube();
*/



int screenHeight=600;
int screenWidth=800;

GLint brush=7.0;
float gColor[3]={1.0,1.0,1.0};


void Init()
{
glClearColor(0.0,0.0,0.0,0.0);glClear(GL_COLOR_BUF FER_BIT);
gluOrtho2D(0.0,(GLdouble)screenWidth,0.0,(GLdouble )screenHeight);
glViewport(0,0,screenWidth,screenHeight);
}


void keyboard(unsigned char i,int w, int q)
{
switch (i)
{
case 'z':// me shtypjen e tastit "z" ne tastier brusha vizaton me ngjyre te zeze dhe mund te perdoret si gome
gColor[0]=0.0;
gColor[1]=0.0;
gColor[2]=0.0;
break;


case 'w':// me shtypjen e tastit "w" ne tastier brusha vizaton me ngjyre te bardhe
gColor[0]=1.0;
gColor[1]=1.0;
gColor[2]=1.0;
break;


case 'r'://me shtypjen e tastit "r" ne tastier brusha vizaton me ngjyre te kuqe
gColor[0]=0.0;
gColor[0]=1.0;
gColor[1]=0.0;
gColor[2]=0.0;
break;

case 'g'://me shtypjen e tastit "g" ne tastier brusha vizaton me ngjyre te gjelbert
gColor[0]=0.0;
gColor[1]=1.0;
gColor[2]=0.0;
break;


case 'b'://me shtypjen e tastit "b" ne tastier brusha vizaton me ngjyre te kalter
gColor[0]=0.0;
gColor[1]=0.0;
gColor[2]=1.0;
break;

case 'y'://me shtypjen e tastit "y" ne tastier brusha vizaton me ngjyre te verdh
gColor[0]=1.0;
gColor[1]=1.0;
gColor[2]=0.0;
break;

case '>'://me shtypjen e tastit "shift" + ">" ne tastier brusha rritet
brush++;
break;

case '<'://me shtypjen e tastit "shift" + "<" ne tastier brusha zvogelohet
brush--;
if (brush<1)
{
brush=1;
}
break;

case 'c'://me shtypjen e tastit "c" ne tastier ekrani pastrohet
glClear(GL_COLOR_BUFFER_BIT);
break;
case 'q'://me shtypjen e tastit "q" ne tastier mbyllet programi
exit(0);
break;
}
}

void printtext(int x, int y, string String)
{
//(x,y) is from the bottom left of the window
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0, screenWidth, 0, screenHeight, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glPushAttrib(GL_DEPTH_TEST);
glDisable(GL_DEPTH_TEST);
glRasterPos2i(x,y);
for (int i=0; i<String.size(); i++)
{
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, String[i]);
}
glPopAttrib();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}


void mouse_motion(int x, int y)
{
glRecti(x,screenHeight-y,x+brush,(screenHeight-y)+brush);
glFlush();
}

void mouse(int button,int state,int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state ==GLUT_DOWN)
{
glBegin(GL_POINTS);
glColor3fv(gColor);
glRecti(x,screenHeight-y,x+brush,(screenHeight-y)+brush);
glEnd();
}
}



void Display2(void)
{
glEnd();
glColor3fv(gColor);
glPointSize(brush);
glFlush();
}

int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100, 100);

glutCreateWindow("PaintBrush");

glutKeyboardFunc(keyboard);
glutMotionFunc(mouse_motion);
glutMouseFunc(mouse);

glutDisplayFunc(Display2);
glutDisplayFunc(display);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, 1, 1, 100);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

gluLookAt(2, 2, 10, 2, 0, 0, 0, 1, 0);
glutMainLoop();
return 0;

}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_DEPTH_TEST);

char string[64],string2[64],string3[64];
sprintf(string, "colors: r=red, g=green, w=white, y=yellow, b=blue");
sprintf(string2, "functions: z=erase, c=clear, q=quit ");
sprintf(string3,"for mouse zoom press shift + '<' '>'");
printtext(10,30,string);
printtext(10,10,string2);
printtext(10,50,string3);
glutSwapBuffers();

//
}

V-man
05-19-2012, 05:51 AM
My problem is that the code that it is shown below is supposed to show a text in the screen and simply draw on the screen with different colors, but it doesn't

I traced the code. You never call glColor so it uses whatever GL's default is, which is white.

If I put a call to glColor, it renders all the text in red

glColor4f(1.0, 0.0, 0.0, 1.0);
printtext(10,30,string);
printtext(10,10,string2);
printtext(10,50,string3);
glutSwapBuffers();

valonj
05-19-2012, 11:23 AM
I traced the code. You never call glColor so it uses whatever GL's default is, which is white.

If I put a call to glColor, it renders all the text in red

glColor4f(1.0, 0.0, 0.0, 1.0);
printtext(10,30,string);
printtext(10,10,string2);
printtext(10,50,string3);
glutSwapBuffers();

Thank you for the correction but this still doesn't solve the problem, the mouse is supposed to draw when you click it.
When i separate the codes they both work perfectly but when i put them together they don't :(

V-man
05-19-2012, 08:45 PM
Thank you for the correction but this still doesn't solve the problem, the mouse is supposed to draw when you click it.
When i separate the codes they both work perfectly but when i put them together they don't :(

You should do all your drawing operations in your display() function. If you do a draw operation in your mouse function, it just renders to the backbuffer and when display() is called, it clears the backbuffer.