在此code我正在尝试绘制简单的奥运环和旋转......下面做工精细,但我不能转动戒指..帮我解决这个problme ...
无效myReshape(INT宽度,高度INT)
{
glViewport(0,0,宽度,高度);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-5,105,-5,105);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
的glTranslatef(0.375,0.375,0.0);
}
INT主(INT ARGC,字符** argv的)
{
glutInit(安培; ARGC,ARGV);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(110 * PIXEL_SIZE,110 * PIXEL_SIZE);
glutCreateWindow(Olymipc戒指||旋转);
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(PIXEL_SIZE);
glShadeModel(GL_FLAT);
glutDisplayFunc(显示);
glutReshapeFunc(myReshape);
glutMainLoop();
返回0;
}
解决方案
试试这个:
的#include< stdlib.h中>
#包括< stdio.h中>
#包括< GL / glut.h>
#包括< GL / gl.h>
#包括<文件math.h>
#定义PIXEL_SIZE 3
#定义消息世界,你好!
无效draw_circle(INT X,INT Y,INT读);
INT ring_radius = 19;
INT颜色[5] [3] = {{0,0,1},{0,0,0},{1,0,0},{1,1,0},{0,1,0}} ;
INT中心[5] [2] = {{15,60},{50,60},{85,60},{33,45},{68,45}};
// ================================================ =========
无效的drawText(为const char *消息)
{
glRasterPos2f((GLfloat)0,(GLfloat)-400);
而(*消息)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12,*邮件);
glutStrokeCharacter(GLUT_STROKE_ROMAN,*消息++);
}
}
无效的显示(无效)
{
诠释毫秒= glutGet(GLUT_ELAPSED_TIME);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-5,105,-5,105);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
的glTranslatef(0.375,0.375,0.0);
glClear(GL_COLOR_BUFFER_BIT);
在glBegin(GL_LINE_LOOP);
glVertex2i(-1,-1);
glVertex2i(100,-1);
glVertex2i(100,100);
glVertex2i(-1100);
glEnd();
常量浮deg_per_sec = 60.0f;
浮动角= deg_per_sec *((浮点)MS / 1000.0f);
的for(int i = 0;我小于5;我++)
{
glColor3f(彩色[I] [0],彩色[I] [1],彩色[I] [2]);
glPushMatrix();
的glTranslatef(中心[I] [0],中心[I] [1],0);
glRotatef(角度,0,0,1);
的glTranslatef(-center [I] [0],-center [I] [1],0);
draw_circle(中心[I] [0],中心[I] [1],ring_radius);
glPopMatrix();
}
glScalef(0.001,0.001,0.001);
的drawText(消息);
glFlush();
glutSwapBuffers();
}
无效draw_circle(INT center_x,诠释center_y,INT半径)
{
INT R =半径;
INT H = 1 - R的; /*初始化 */
INT X = 0;
INTÿ= R;
INT DU = 3;
INT DD = 5 - 2 * R;
INT I = center_x;
INT J = center_y;
glPointSize(6);
在glBegin(GL_POINTS);
而(Y> X)
{
如果(H&小于0)
{
DU = dU的+ 2;
H = H +杜,
X = X + 1;
DD = DD + 2;
}
其他
{
DD = 2 *(X-Y)+ 5;
H = H +的dD;
X = X + 1;
Y = Y - 1;
DU = dU的+ 2;
DD = DD + 4;
}
glVertex2i(X + I,Y + J);
glVertex2i(-x + I,Y + J);
glVertex2i(X + I,-y + J);
glVertex2i(-x + I,-y + J);
glVertex2i(Y + I,X + J);
glVertex2i(Y + I,-x + J);
glVertex2i(-y + I,X + J);
glVertex2i(-y + I,-x + J);
}
glEnd();
}
无效myReshape(INT宽度,高度INT)
{
glViewport(0,0,宽度,高度);
}
无效闲置()
{
glutPostRedisplay();
}
INT主(INT ARGC,字符** argv的)
{
glutInit(安培; ARGC,ARGV);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(110 * PIXEL_SIZE,110 * PIXEL_SIZE);
glutCreateWindow(Olymipc戒指||旋转);
glClearColor(1.0,1.0,1.0,0.0);
glPointSize(PIXEL_SIZE);
glShadeModel(GL_FLAT);
glutDisplayFunc(显示);
glutReshapeFunc(myReshape);
glutIdleFunc(空闲);
glutMainLoop();
返回0;
}
in this code i'm try to draw simple olympic ring and rotate it... the below work fine but i can't rotate the rings.. help me to solve this problme...
void myReshape (int width, int height)
{
glViewport (0, 0, width, height);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (-5, 105, -5, 105);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.375, 0.375, 0.0);
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE);
glutCreateWindow ("Olymipc Rings || rotation ");
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(PIXEL_SIZE);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutMainLoop();
return 0;
}
解决方案
Try this:
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <math.h>
#define PIXEL_SIZE 3
#define MESSAGE "hello world !"
void draw_circle(int x, int y, int r);
int ring_radius = 19;
int color[5][3]={{0,0,1}, {0,0,0},{1,0,0}, {1,1,0},{0,1,0}};
int center[5][2]={{15,60},{50,60},{85,60},{33,45},{68,45}};
//=========================================================
void drawText(const char * message)
{
glRasterPos2f((GLfloat)0, (GLfloat)-400);
while (*message)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, *message);
glutStrokeCharacter(GLUT_STROKE_ROMAN,*message++);
}
}
void display (void)
{
int ms = glutGet(GLUT_ELAPSED_TIME);
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (-5, 105, -5, 105);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0.375, 0.375, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_LINE_LOOP);
glVertex2i(-1,-1);
glVertex2i(100,-1);
glVertex2i(100,100);
glVertex2i(-1,100);
glEnd();
const float deg_per_sec = 60.0f;
float angle = deg_per_sec * ( (float)ms / 1000.0f );
for(int i = 0 ; i <5; i++)
{
glColor3f(color[i][0],color[i][1],color[i][2]);
glPushMatrix();
glTranslatef( center[i][0], center[i][1], 0 );
glRotatef(angle, 0, 0, 1);
glTranslatef( -center[i][0], -center[i][1], 0 );
draw_circle(center[i][0],center[i][1],ring_radius);
glPopMatrix();
}
glScalef(0.001, 0.001, 0.001);
drawText(MESSAGE);
glFlush();
glutSwapBuffers();
}
void draw_circle(int center_x, int center_y , int radius)
{
int r = radius;
int h = 1 - r ; /*initialization */
int x = 0;
int y = r;
int dU=3;
int dD = 5 - 2*r;
int i = center_x;
int j = center_y;
glPointSize(6);
glBegin(GL_POINTS);
while( y > x )
{
if (h<0)
{
dU= dU + 2;
h = h + dU;
x = x + 1;
dD = dD + 2;
}
else
{
dD = 2*(x-y) + 5;
h = h + dD;
x = x + 1;
y = y - 1;
dU = dU + 2;
dD = dD + 4;
}
glVertex2i(x+i, y+j);
glVertex2i(-x+i, y+j);
glVertex2i(x+i, -y+j);
glVertex2i(-x+i,-y+j);
glVertex2i(y+i, x+j);
glVertex2i(y+i, -x+j);
glVertex2i(-y+i, x+j);
glVertex2i(-y+i, -x+j);
}
glEnd();
}
void myReshape (int width, int height)
{
glViewport (0, 0, width, height);
}
void idle()
{
glutPostRedisplay();
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100,100);
glutInitWindowSize(110*PIXEL_SIZE, 110*PIXEL_SIZE);
glutCreateWindow ("Olymipc Rings || rotation ");
glClearColor(1.0, 1.0, 1.0, 0.0);
glPointSize(PIXEL_SIZE);
glShadeModel (GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutIdleFunc(idle);
glutMainLoop();
return 0;
}