用鍵盤a, b控制茶壺旋轉角度~
開小黑視窗,顯示出滑鼠游標在茶壺視窗的座標位置。
加入打光程式碼,呈現山寨三低感
將鍵盤控制住解掉,改為滑鼠拖曳控制
#include <GL/glut.h>
#include <stdio.h>
float angleX=0, startX=0;
/*void keyboard(unsigned char key, int x, int y)//unsigned 代表沒有正負號,int x/y是坐標軸
{
printf("%c %d %d\n", key, x, y);
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}*/
void mouse(int button, int state, int x, int y)//012左鍵中鑑右鍵 , 01放開按下去, xy座標
{
printf("%d %d %d %d\n", button, state, x, y);
startX=x;
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
angleX= x - startX;
glutPostRedisplay();
}
void myLight()//打光,開新專案從範例程式碼找有關鍵字light, 抄六行過來
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void display()
{
glEnable(GL_DEPTH_TEST);//打光要開深度測試
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,1,0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
對x和y軸做旋轉
#include <GL/glut.h>
#include <stdio.h>
float angleX=0, startX=0;//對X軸做轉動
float angleY=0, startY=0;//對Y軸做轉動
/*void keyboard(unsigned char key, int x, int y)//unsigned 代表沒有正負號,int x/y是坐標軸
{
printf("%c %d %d\n", key, x, y);
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}*/
void mouse(int button, int state, int x, int y)//012左鍵中鑑右鍵 , 01放開按下去, xy座標
{
printf("%d %d %d %d\n", button, state, x, y);
startX=x;
startY=y;
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
angleX+= x - startX; startX=x;//多加這行, startX=x;一定要和 angleX+= x - startX;寫同一行
angleY+= y - startY; startY=y;//多加這行, 改用+= 不要用=, 而且startX會更新
glutPostRedisplay();
}
void myLight()//打光,開新專案從範例程式碼找有關鍵字light, 抄六行過來
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void display()
{
glEnable(GL_DEPTH_TEST);//打光要開深度測試
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angleX, 0,-1,0);
glRotatef(angleY, -1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float angleX=0, startX=0;//對X軸做轉動
float angleY=0, startY=0;//對Y軸做轉動
float m[16]= {1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};//旋轉矩陣Rotation Matrix, 注意逗號
/*void keyboard(unsigned char key, int x, int y)//unsigned 代表沒有正負號,int x/y是坐標軸
{
printf("%c %d %d\n", key, x, y);
if(key=='a') angleX+=3;
if(key=='b') angleX-=3;
glutPostRedisplay();
}*/
void mouse(int button, int state, int x, int y)//012左鍵中鑑右鍵 , 01放開按下去, xy座標
{
printf("%d %d %d %d\n", button, state, x, y);
startX=x;
startY=y;
}
void motion(int x, int y)
{
printf("%d %d\n", x, y);
angleX= x - startX; startX=x;//多加這行, startX=x;一定要和 angleX+= x - startX;寫同一行
angleY= y - startY; startY=y;//多加這行, 改用+= 不要用=, 而且startX會更新 .
//再把+=改成=, 因為之後下面程式碼要把轉動累積起來了呃!
glutPostRedisplay();
}
void myLight()//打光,開新專案從範例程式碼找有關鍵字light, 抄六行過來
{
const GLfloat light_position[] = { 2.0f, 5.0f, -5.0f, 0.0f };
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
}
void display()
{
glEnable(GL_DEPTH_TEST);//打光要開深度測試
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
float dist = sqrt(angleX*angleX+ angleY*angleY);//sqrt引入math.h
glRotatef(dist, -angleY, -angleX, 0);
glMultMatrixf(m);
glGetFloatv(GL_MODELVIEW_MATRIX, m);//累積轉動
//glRotatef(angleX, 0,-1,0);
//glRotatef(angleY, -1,0,0);
glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
int main()
{
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week10");
glutDisplayFunc(display);
//glutKeyboardFunc(keyboard);
glutMouseFunc(mouse);
glutMotionFunc(motion);
myLight();
glutMainLoop();
}
沒有留言:
張貼留言