
translate移動
rotate旋轉
scale放大縮小

glTranslatef(x,y,z);
glRotatef(角度,x,y,z);
glScalef(x,y,z);
2.用glutMouseFunc(mouse);配合自己的滑鼠函式
void mouse (int button, int state,int x,int y)
{
if(state==GLUT_DOWN) printf("%d %d\n", x, y);
}

#include <GL/glut.h>
#include <stdio.h>
void mouse (int button, int state,int x,int y)
{
if(state==GLUT_DOWN) printf("%d %d\n", x, y);
}
void display ()
{
glClearColor(150/255.0,170/255.0,55/255.0,1);
glClear(GL_COLOR_BUFFER_BIT);
glutSwapBuffers();
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}

glColor3ub(175, 193, 81); ///顏色
glBegin(GL_POLYGON);
glVertex2f( (158-150)/150.0, (46-150)/150.0); ///頂點
glVertex2f( (52-150)/150.0, (156-150)/150.0);
glVertex2f( (163-150)/150.0, (231-150)/150.0);
glVertex2f( (251-150)/150.0, (146-150)/150.0);
glEnd();
3.畫圓,使用cos()及sin()

#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
void mouse (int button, int state,int x,int y)
{
if(state==GLUT_DOWN) printf("%d %d\n", x, y);
}
void display ()
{
glClearColor(150/255.0,170/255.0,55/255.0,1);
glClear(GL_COLOR_BUFFER_BIT);
glColor3ub(175,193,81);
glBegin(GL_POLYGON);
for(float angle=0; angle<3.14159265357989 *2; angle+=0.1)
{
glVertex2f( cos(angle), sin(angle));
}
glEnd();
glutSwapBuffers();
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("hello3D");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}

glColor3ub(175,193,81);
glPushMatrix();
glTranslatef(0.5,0,0);
glBegin(GL_POLYGON);
for(float angle=0; angle<3.14159265357989 *2; angle+=0.1)
{
glVertex2f( 0.2*cos(angle), 0.2*sin(angle));
}
glEnd();
glTranslatef(0,0.5,0);
glBegin(GL_POLYGON);
for(float angle=0; angle<3.14159265357989 *2; angle+=0.1)
{
glVertex2f( 0.2*cos(angle), 0.2*sin(angle));
}
glEnd();
glTranslatef(-0.5,0,0);
glBegin(GL_POLYGON);
for(float angle=0; angle<3.14159265357989 *2; angle+=0.1)
{
glVertex2f( 0.2*cos(angle), 0.2*sin(angle));
}
glEnd();
glPopMatrix();
沒有留言:
張貼留言