ورود

View Full Version : کمک کنید : برنامه ای با opengl برای چرخش متن و ریزش حروف متن



reza_engineer
پنج شنبه 28 اردیبهشت 1391, 12:49 عصر
با سلام خدمت اساتید محترم
میشه راهنمایی کنید که چه جوری یک متن رو نمایش داد و چرخوند و سپس حروف متن ریزش کنن؟

من یک برنامه برای نمایش و چرخش نوشتم
اما برای ریزش نمی دونم چیکار کنم



#include "stdafx.h"
#include<glut.h>

#pragma comment(lib,"glut32.lib")
#pragma comment(lib,"opengl32.lib")

int width=480,hight=640;



void drawBitmapText(char *string,float x,float y,float z)
{
char *c;
glRasterPos3f(x, y,z);

for (c=string; *c != '\0'; C++‎)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, *c);
}
}

void drawStrokeText(char*string,int x,int y,int z)
{
char *c;
glPushMatrix();
glTranslatef(x, y+8,z);
glRotatef(40.0f,0,0,1.0f);
glScalef(0.09f,-0.08f,z);

for (c=string; *c != '\0'; C++‎)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN , *c);
}
glPopMatrix();
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
}


void reshape(int w,int h)
{

glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,w,h,0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

}


void render(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();

glColor3f(0,1,0);
drawStrokeText("Visual C++‎",200,200,0);

glutSwapBuffers();
}



int main(int argc, char* argv[])
{
glutInit(&argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);

glutInitWindowSize(500,500);

glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL Fonts");

glutDisplayFunc(render);
glutIdleFunc(render);
glutReshapeFunc(reshape);

glutMainLoop();
return 0;
}

reza_engineer
جمعه 29 اردیبهشت 1391, 19:09 عصر
از دوستان کسی نیست راهنمایی کنه؟

amin1softco
جمعه 29 اردیبهشت 1391, 19:56 عصر
این برنامه رو ببین

#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <GL/glut.h>

/* text.c */

/* Simple program demonstrates the use of text using GLUT and OpenGL
*
* Also demonstrates the use of reShape, mouse, and keyboard
* callback functions
*/


/* Jon McCormack, April 2003
* Updated 20 March 2004: added extra callbacks
* Updated 29 March 2004: removed platform dependencies
*/


#include <stdio.h>


#define MESSAGE "Underworld live 17.03.04"

/*
* local static variables: g_rotate used to keep track of
* global rotation
*
*/
static GLfloat g_rotate = 0;

/*
* drawText
*
* Draws the specified message in both
* raster and stroke text
*
*/
void drawText(const char * message)
{
/* raster pos sets the current raster position
* mapped via the modelview and projection matrices
*/
glRasterPos2f((GLfloat)0, (GLfloat)-400);

/*
* write using bitmap and stroke chars
*/
while (*message) {
Sleep(50);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *message);
glutStrokeCharacter(GLUT_STROKE_ROMAN,*message++);
}
}

/*
* display
*
* This function is called by the GLUT to display the graphics
* In this case it clears the screen then calls the function "drawText"
* which will draw some text using GLUT
*
*/
void display(void)
{
/* set matrix mode to modelview */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/* transformations to scale the stroke text -
* notice how these only change stroke and raster text
* differently...
*/
glScalef(0.001, 0.001, 0.001);
glRotatef(g_rotate, 0, 0, 1.0);
/* glTranslatef(-180, -180, 0.0); */

glClear( GL_COLOR_BUFFER_BIT );
drawText(MESSAGE);

glFlush(); /* force OpenGL output */
}


/*
* myReshape
*
* This function is called whenever the user (or OS) reshapes the
* OpenGL window. The GLUT sends the new window dimensions (x,y)
*
*/
void myReshape(int w, int h)
{
/* set viewport to new width and height */
/* note that this command does not change the CTM */
glViewport(0, 0, w, h);

/*
* set viewing window in world coordinates
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); /* init projection matrix */

if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -1.0, 1.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -1.0, 1.0);

/* set matrix mode to modelview */
glMatrixMode(GL_MODELVIEW);
}

/*
* myKey
*
* responds to key presses from the user
*/
void myKey(unsigned char k, int x, int y)
{
switch (k) {
case 'q':
case 'Q': exit(0);
break;
default:
printf("Unknown keyboard command \'%c\'.\n", k);
break;
}
}


/*
* myMouse
*
* function called by the GLUT when the user presses a mouse button
*
* Here we increment the global rotation with each press - left to do a
* positive increment, right for negative, middle to reset
*/
void myMouse(int btn, int state, int x, int y)
{

if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) g_rotate += 20.0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) g_rotate = 0.0;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) g_rotate -= 20.0;

/* force redisplay */
glutPostRedisplay();
}



/*
* main
*
* Initialization and sets graphics callbacks
*
*/
int main(int argc, char **argv)
{
/* glutInit MUST be called before any other GLUT/OpenGL calls */
glutInit(&argc, argv);

/* need both double buffering and z buffer */

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Jon Mc Text Test");

/* set callback functions */
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(NULL);
glutKeyboardFunc(myKey);
glutMouseFunc(myMouse);

/* set clear colour */
glClearColor(1.0, 1.0, 1.0, 1.0);

/* set current colour to black */
glColor3f(0.0, 0.0, 0.0);

glutMainLoop();

return 0;
}



برای اینکه این کار رو انجام بدی باید بشینی یک الگوریتم طراحی کنی
مثلاً نوشته ها به صورت کارکتر به کارکتر دارای موقعیت x,y باشند بعد وقتی صفحه لود شد یک مختصات تصادفی داشته باشند و به مرور زمان به موقعیت اصلی خودشون نزدیک بشند .
نمونه هاش با جا وا اسکریپت زیاد طراحی می شه می تونه از فکرشون استفاده کنی مثلاً اینجا http://rainbow.arch.scriptmania.com/scripts/bg/hearts_fall_1.html

reza_engineer
شنبه 30 اردیبهشت 1391, 22:11 عصر
میشه کمکم کنی که بنویسمش
آخه تجربه زیادی توش ندارم
می خوام متنی که نشون داده میشه بعد حروفش به سمته پایین ریخته بشه باید یک ریزش مثه برگ درخت

amin1softco
یک شنبه 31 اردیبهشت 1391, 00:06 صبح
خوب آخه شما یک کدی چیزی خودت بنویس هر جاش به مشکل برخوردی من و بقیه برای کمک در خدمتیم.

reza_engineer
یک شنبه 31 اردیبهشت 1391, 10:14 صبح
آخه مشکل اینجاس من اصلا opengl کار نکردم و اینو استادم پروژه ازم خواسته
نمی دونم برای نمایش کارکتراها که بتونن ریزش کنن چه جوری عمل کنم

amin1softco
دوشنبه 01 خرداد 1391, 16:52 عصر
خوب در این مواقع شما باید برید با یک نرم افزار طراحی سه بعدی مثل مکس طراحی خودتون رو انجام بدید و انیمشن حاصل رو ازش خروجی بگیرید برای c++ که یک پلاگین به نام پلی ترانس این کار رو انجام میده.
اگر احیاناً خواستید خودتون کد بنویسید می تونید از لیست پیوندی استفاده کنید و یک کلاس تعریف کنید تا کارکتر و موقعیت x,y اون رو ذخیره کنه و بعد با استفاده از یک الگوریتم, افکت رو پیاده سازی کنید.
من بودم با یک همچین کدی شروع می کردم :

class word{
public:
word(char ch="",int xx=0,int yy=0,word* p=0):chare(ch),x(xx),y(yy),next(p){}
char chare;
int x;
int y;
word* next;
};

int main(){
char * message= " my name is amin";
int n,x,y;
word* p;
word* q=0;
while (*message) {

p = new word(*message++,nrandom(200),nrandom(200),q);
q=p;
}
for (;p->next;p=p->next)
cout<<p->chare<<" \n x:"<<p->x<<" \n y:"<<p->y<<endl;

system("PAUSE");
return EXIT_SUCCESS;
}

برای مثال مختصات تصادفی حروف نمایش داده می شه اینجوری

#include <stdlib.h>
#include <math.h>
#include <windows.h>
#include <time.h>
#include <GL/glut.h>

#include <stdio.h>


#define MESSAGE "my passage in the program"

/*
* local static variables: g_rotate used to keep track of
* global rotation
*
*/
static GLfloat g_rotate = 0;

/*
* drawText
*
* Draws the specified message in both
* raster and stroke text
*
*/





int nrandom(int max)
{ //srand(time(NULL));
return rand() % max;
}


class word{
public:
word(char ch="",int xx=0,int yy=0,word* p=0):chare(ch),x(xx),y(yy),next(p){}
char chare;
int x;
int y;
word* next;
};



void drawText(const char * message)
{
/* raster pos sets the current raster position
* mapped via the modelview and projection matrices
*/
// glRasterPos2f((GLfloat)x, (GLfloat)y-400);

/*
* write using bitmap and stroke chars
*/

int n;
word* p;
word* q=0;
while (*message) {

p = new word(*message++,nrandom(500),nrandom(500),q);
q=p;
}
for (;p->next;p=p->next){

glRasterPos2f((GLfloat)p->x, (GLfloat)p->y-400);
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, p->chare);
}
//cout<<<<" \n x:"<<p->x<<" \n y:"<<<<endl;





// glutStrokeCharacter(GLUT_STROKE_ROMAN,p->chare);


}

/*
* display
*
* This function is called by the GLUT to display the graphics
* In this case it clears the screen then calls the function "drawText"
* which will draw some text using GLUT
*
*/
void display(void)
{
/* set matrix mode to modelview */
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

/* transformations to scale the stroke text -
* notice how these only change stroke and raster text
* differently...
*/
glScalef(0.001, 0.001, 0.001);
glRotatef(g_rotate, 0, 0, 1.0);
/* glTranslatef(-180, -180, 0.0); */

glClear( GL_COLOR_BUFFER_BIT );

drawText(MESSAGE);

glFlush(); /* force OpenGL output */
}


/*
* myReshape
*
* This function is called whenever the user (or OS) reshapes the
* OpenGL window. The GLUT sends the new window dimensions (x,y)
*
*/
void myReshape(int w, int h)
{
/* set viewport to new width and height */
/* note that this command does not change the CTM */
glViewport(0, 0, w, h);

/*
* set viewing window in world coordinates
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); /* init projection matrix */

if (w <= h)
glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,
2.0 * (GLfloat) h / (GLfloat) w, -1.0, 1.0);
else
glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,
2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -1.0, 1.0);

/* set matrix mode to modelview */
glMatrixMode(GL_MODELVIEW);
}

/*
* myKey
*
* responds to key presses from the user
*/
void myKey(unsigned char k, int x, int y)
{
switch (k) {
case 'q':
case 'Q': exit(0);
break;
default:
printf("Unknown keyboard command \'%c\'.\n", k);
break;
}
}


/*
* myMouse
*
* function called by the GLUT when the user presses a mouse button
*
* Here we increment the global rotation with each press - left to do a
* positive increment, right for negative, middle to reset
*/
void myMouse(int btn, int state, int x, int y)
{

if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) g_rotate += 20.0;
if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) g_rotate = 0.0;
if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) g_rotate -= 20.0;

/* force redisplay */
glutPostRedisplay();
}



/*
* main
*
* Initialization and sets graphics callbacks
*
*/
int main(int argc, char **argv)
{
/* glutInit MUST be called before any other GLUT/OpenGL calls */
glutInit(&argc, argv);

/* need both double buffering and z buffer */

glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("Jon Mc Text Test");

/* set callback functions */
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(NULL);
glutKeyboardFunc(myKey);
glutMouseFunc(myMouse);

/* set clear colour */
glClearColor(1.0, 1.0, 1.0, 1.0);

/* set current colour to black */
glColor3f(0.0, 0.0, 0.0);

glutMainLoop();

return 0;
}

reza_engineer
چهارشنبه 03 خرداد 1391, 18:47 عصر
بسیار ممنونم دوست خوبم