2018年1月13日 星期六
2017年12月14日 星期四
LYC的計圖學習筆記-WEEK14
畫幾何圖形的另一方法
size(600,600,P3D);background(255);
beginShape(TRIANGLE);
fill(255,0,0);vertex(300,100,0);
fill(255,0,255);vertex(100,100,0);
fill(255,255,0);vertex(100,500,0);
endShape();
p.s.fill (r,g,b); //以RGB模式決定填的顏色
vertex(x,y,z); //畫(x,y,z)的點
2017年12月7日 星期四
LYC的計圖學習筆記-WEEK13
色彩系統
colorMode(RGB/HSB,255);
for(int x=0;x<255;x++)
{
for(int y=0;y<255;y++)
{
stroke(_,_,_);
point(x,y);
}
}
p.s. RGB(Red,Green,Blue) (加法混色法) //光三原色的比例
HSB/HSV(Hue,Saturation,Brightness/Value) //色相,飽和度,亮度
HSL(Hue,Saturation,Lightness) //色相,飽和度,明度
CMYK(Cyan,Magenta,Yellow,Black) (減法混色法)
//青色(藍+綠),洋紅(紅+藍),黃色(紅+綠),黑色(都沒有)
for(int x=0;x<255;x++)
{
for(int y=0;y<255;y++)
{
stroke(_,_,_);
point(x,y);
}
}
p.s. RGB(Red,Green,Blue) (加法混色法) //光三原色的比例
HSB/HSV(Hue,Saturation,Brightness/Value) //色相,飽和度,亮度
HSL(Hue,Saturation,Lightness) //色相,飽和度,明度
CMYK(Cyan,Magenta,Yellow,Black) (減法混色法)
//青色(藍+綠),洋紅(紅+藍),黃色(紅+綠),黑色(都沒有)
2017年11月30日 星期四
LYC的計圖學習筆記-WEEK12
GLUT可以同時開好幾個視窗
#include <GL/glut.h>
#include <stdio.h>
#include <math.h>
float rrr=0;
GLUquadric* quadFrustrum=NULL;
float rot=0;
float rotX=30, rotY=-30;
float bigger=2;
bool bOrtho=false, bShow1=true, bShow2=true;
int winW=600, winH=400, winTitleH=30;
int winID[4];
float blendPercent=0.5;
float K1x=200,K1y=50,K1z=200, K2x=-50,K2y=50,K2z=50;
float K1cx=-200,K1cy=-200,K1cz=500, K2cx=0,K2cy=-200,K2cz=500;
void frustrum(float r, float g, float b, float a);
float humanX=0, humanY=0;
void showHuman1();
void showHuman2();
typedef struct _myPoint{
float x, y, z;
} myPoint;
myPoint human1[15]={//我這座標是亂設的, 請即時地把 OpenNI 量到的值塞進來即可
{0,0,1.5}, {0,0.3,1.5}, {0,0.9,1.3},
{0.2, 0.3, 1.5}, {0.4, 0.3, 1.5}, {0.8, 0.3,1.5},
{-0.2,0.3, 1.5}, {-0.4,0.3, 1.5}, {-0.8,0.3,1.5},
{0.2, -0.2,1.5}, {0.2,-0.6, 1.5}, {0.2,-1,1.5},
{-0.2,-0.2,1.5}, {-0.2,-0.6,1.5}, {-0.2,-1,1.5}};
myPoint human2[15]={//我這座標是亂設的, 請即時地把 OpenNI 量到的值塞進來即可
{0,0,1.5}, {0,0.3,1.5}, {0,0.9,1.3},
{0.2, 0.3, 1.5}, {0.6, 0.3, 1.5}, {0.8, 0.3,1.5},
{-0.2,0.3, 1.5}, {-0.6,0.3, 1.5}, {-0.8,0.3,1.5},
{0.2, -0.2,1.5}, {0.2,-0.6, 1.5}, {0.2,-1,1.5},
{-0.2,-0.2,1.5}, {-0.2,-0.6,1.5}, {-0.2,-1,1.5}};
void showHuman1()
{
//TO Show Human, you can use human1[] 將座標 放到陣列中,即可自動秀出
glColor3f(0.5,0.5,0);
for(int i=0;i<15;i++){//小心,關節數量要正確,這裡是15
glPushMatrix();
glTranslatef(human1[i].x, human1[i].y, -1*human1[i].z);//小心,z為負
glutSolidSphere(0.1,10,10);
glPopMatrix();
}
}
void showHuman2()
{
//TO Show Human, you can use human2[] 將座標 放到陣列中,即可自動秀出
glColor3f(0,0.5,0);
for(int i=0;i<15;i++){
glPushMatrix();
glTranslatef(human2[i].x, human2[i].y, -1*human2[i].z);//小心,z為負
glutSolidSphere(0.1,10,10);
glPopMatrix();
}
}
void NormalizeVector(float v[3])
{
float len=sqrt(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
v[0]/=len;
v[1]/=len;
v[2]/=len;
}
void ComputeNormalOfPlane(float ans[3], float v1[3], float v2[3])
{
ans[0]=v1[1]*v2[2]-v1[2]*v2[1];
ans[1]=v1[2]*v2[0]-v1[0]*v2[2];
ans[2]=v1[0]*v2[1]-v1[1]*v2[0];
}
void myLookAtRotate(float ex, float ey, float ez, float cx, float cy, float cz, float upx, float upy, float upz)
{
float forward[3], side[3], up[3];
float matrix2[16], resultMatrix[16];
//------------------
forward[0] = cx-ex;
forward[1] = cy-ey;
forward[2] = cz-ez;
NormalizeVector(forward);
up[0]=upx; up[1]=upy; up[2]=upz;
//------------------
//Side = forward x up
ComputeNormalOfPlane(side, forward, up);
NormalizeVector(side);
//------------------
//Recompute up as: up = side x forward
ComputeNormalOfPlane(up, side, forward);
//------------------
matrix2[0] = side[0];
matrix2[4] = side[1];
matrix2[8] = side[2];
matrix2[12] = 0.0;
//------------------
matrix2[1] = up[0];
matrix2[5] = up[1];
matrix2[9] = up[2];
matrix2[13] = 0.0;
//------------------
matrix2[2] = -forward[0];
matrix2[6] = -forward[1];
matrix2[10] = -forward[2];
matrix2[14] = 0.0;
//------------------
matrix2[3] = matrix2[7] = matrix2[11] = 0.0;
matrix2[15] = 1.0;
//下面是我亂試的!
matrix2[0] = side[0];
matrix2[1] = side[1];
matrix2[2] = side[2];
matrix2[3] = 0.0;
//------------------
matrix2[4] = up[0];
matrix2[5] = up[1];
matrix2[6] = up[2];
matrix2[7] = 0.0;
//------------------
matrix2[8] = -forward[0];
matrix2[9] = -forward[1];
matrix2[10] = -forward[2];
matrix2[11] = 0.0;
//------------------
matrix2[12] = matrix2[13] = matrix2[14] = 0.0;
glMultMatrixf(matrix2);
}
void room()
{
glEnable(GL_DEPTH_TEST);
glPushMatrix();
glScalef(400,400,500);
glTranslatef(0,0,0.5);//鍾老師的坐標系統是左手標系統(類kinect)會有問題
glColor4f(0.9,0.9,0.9,0.9);
glutSolidCube(1);//The blending room
glDisable(GL_DEPTH_TEST);
glEnable(GL_LINE_STIPPLE);//dashed line
glColor3f(0,0,0);
glutWireCube(1);//The edge of the room (Hidden line, black)
glDisable(GL_LINE_STIPPLE);
glEnable(GL_DEPTH_TEST);
glColor3f(1,0,0);
glutWireCube(1.005);//The edge of the room (Visible line, red)
glTranslatef(0,-0.52,0);
glScalef(1.5,0.01,1.5);
glColor3f(1,1,1);
glutSolidCube(1);//The big floor
glDisable(GL_DEPTH_TEST);
glEnable(GL_LINE_STIPPLE);//dashed line
glColor3f(0,0,0);
glutWireCube(1);//The edge of the big floor (Hidden line, black)
glDisable(GL_LINE_STIPPLE);
glEnable(GL_DEPTH_TEST);
glColor3f(1,0,0);
glutWireCube(1);//The edge of the big floor (Visible line, red)
glPopMatrix();
glDisable(GL_DEPTH_TEST);
glColor3f(0,0,0);
glEnable(GL_LINE_STIPPLE);//dashed line
glBegin(GL_LINE_LOOP);//Window
glVertex3f(180,150,500);
glVertex3f(180,-150,500);
glVertex3f(-50,-150,500);
glVertex3f(-50,150,500);
glEnd();
glDisable(GL_LINE_STIPPLE);
glEnable(GL_DEPTH_TEST);
glColor3f(1,0,0);
glBegin(GL_LINE_LOOP);//Window
glVertex3f(180,150,502);
glVertex3f(180,-150,502);
glVertex3f(-50,-150,502);
glVertex3f(-50,150,502);
glEnd();
glClear(GL_DEPTH_BUFFER_BIT);
if(bShow1){
glPushMatrix();
glTranslatef(K1x,K1y,K1z);
glColor3f(0.9,0.5,0);//dark yellow
glutSolidSphere(5,10,10);//EYE 1
glScalef(100,100,100);
myLookAtRotate(K1x,K1y,K1z, K1cx,K1cy,K1cz, 0,1,0);
frustrum(1,1,0,blendPercent);//Kinect 1 (yellow)
glPopMatrix();
glPushMatrix();
glTranslatef(K1cx,K1cy,K1cz);
glutSolidSphere(5,10,10);//Kinect 1 看的目標 -2,-2,5
glPopMatrix();
}else{
// glDisable(GL_DEPTH_TEST);
// glPushMatrix();
// glTranslatef(K1x,K1y,K1z);
// glScalef(100,100,100);
// myLookAtRotate(K1x,K1y,K1z, K1cx,K1cy,K1cz, 0,1,0);
// showHuman1();//將第1台Kinect裡的使用者秀出來
// glPopMatrix();
// glEnable(GL_DEPTH_TEST);
}
glClear(GL_DEPTH_BUFFER_BIT);
if(bShow2){
glPushMatrix();
glTranslatef(K2x,K2y,K2z);
glColor3f(0,0.5,0);//dark green
glutSolidSphere(5,10,10);//EYE 2
glScalef(100,100,100);
myLookAtRotate(K2x,K2y,K2z, K2cx,K2cy,K2cz, sin(rrr),cos(rrr),0);//2012-12-17 rotating the camera by the UP vector
frustrum(0.5,1,0.5,blendPercent);//Kinect 2 (green)
glPopMatrix();
glPushMatrix();
glTranslatef(K2cx,K2cy,K2cz);
glutSolidSphere(5,10,10);//Kinect 2看的目標 0,-2,5
glPopMatrix();
}else{
// glPushMatrix();
// glTranslatef(K2x,K2y,K2z);
// glScalef(100,100,100);
// myLookAtRotate(K2x,K2y,K2z, K2cx,K2cy,K2cz, 0,1,0);//2012-12-17 don't rotate the camera UP vector for the small green man
// glDisable(GL_DEPTH_TEST);
// showHuman2();//將第2台Kinect裡的使用者秀出來
// glEnable(GL_DEPTH_TEST);
// glPopMatrix();
}
// glColor3f(0,0,0);
// glutSolidSphere(2,10,10);//original center
glDisable(GL_DEPTH_TEST);
glPushMatrix();
glTranslatef(humanX,-100,humanY);
glScalef(100,100,-100);
showHuman1();
glPopMatrix();
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);//special floor
glColor3f(0.5,0.5,0.5);
glBegin(GL_POLYGON);
glScalef(400,400,500);
glVertex3f(300,-210,625);
glVertex3f(300,-210,-125);
glVertex3f(-300,-210,-125);
glVertex3f(-300,-210,625);
glEnd();
glDisable(GL_CULL_FACE);
}
void frustrum(float r, float g, float b, float a)
{
glPushMatrix();
glScalef(3,3,3);
//http://stackoverflow.com/questions/7696436/precision-of-the-kinect-depth-camera
//62.0 vs. 48.6 (color) and 58.5 vs. 45.6 (depth)
//old kinect spec: 57 vs. 43
glScalef(tan(58.5*M_PI/180/2.0),tan(45.6*M_PI/180/2.0),1);
glScalef(1,1,1);
glTranslatef(0,0,-1);
glRotatef(45,0,0,1);
glColor4f(1,0,0,0.5);
gluQuadricDrawStyle(quadFrustrum,GLU_LINE);
gluCylinder(quadFrustrum, sqrt(2), 0, 1, 4, 1);
glColor4f(r,g,b,a);
gluQuadricDrawStyle(quadFrustrum,GLU_FILL);
gluCylinder(quadFrustrum, sqrt(2), 0, 1, 4, 1);
glBegin(GL_POLYGON);
glVertex3f(sqrt(2),0, 0);
glVertex3f(0,sqrt(2), 0);
glVertex3f(-sqrt(2),0,0);
glVertex3f(0,-sqrt(2),0);
glEnd();
glPopMatrix();
}
void displayOrtho()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glPushMatrix();
glScalef(bigger, bigger, bigger);
glScalef(0.001, 0.001, 0.001);
glTranslatef(0,0,-250);//(400,350,500);
glScalef(-1,1,1);//GL的右手座標系統,與Kinect左手座標系統不同
room();//之後就都用Kinect左手座標系統沒關係,反正z都會被我轉成負的
glPopMatrix();
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glPushMatrix();
glScalef(bigger, bigger, bigger);
glRotatef(rotY, 1,0,0);
glRotatef(rotX, 0,1,0);
glScalef(0.001, 0.001, 0.001);
glTranslatef(0,0,-250);//(400,350,500);
glScalef(-1,1,1);//GL的右手座標系統,與Kinect左手座標系統不同
room();//之後就都用Kinect左手座標系統沒關係,反正z都會被我轉成負的
glPopMatrix();
glDisable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glutSwapBuffers();
}
void resizeTop(int w, int h)
{
winW=w;winH=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1.0*h/w,1.0*h/w,-10,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,2,0,0,0,0,0,0,1);
}
void resizeFront(int w, int h)
{
winW=w;winH=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1.0*h/w,1.0*h/w,-10,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-2,0,0,0,0,1,0);
}
void resizeSide(int w, int h)
{
winW=w;winH=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1,1,-1.0*h/w,1.0*h/w,-10,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2,0,0,0,0,0,0,1,0);
}
void resize(int w, int h)
{
winW=w;winH=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(bOrtho){
glOrtho(-1,1,-1.0*h/w,1.0*h/w,-10,10);
}else{
gluPerspective(57, w/(float)h, 0.1f, 3000.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-2,0,0,0,0,1,0);
}
void timer(int t)
{
glutTimerFunc(33,timer, t+1);
for(int i=0;i<4;i++){
glutSetWindow(winID[i]);
glutPostRedisplay();
}
}
int oldx=0,oldy=0;
void mouse(int button, int state, int x, int y)
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_DOWN){
oldx=x;oldy=y;
}else if(button==GLUT_LEFT_BUTTON && state==GLUT_UP){
}
}
void motion(int x, int y)
{
rotX+=(x-oldx)/2;
rotY-=(y-oldy)/2;
oldx=x;oldy=y;
}
void keyboard(unsigned char key, int x, int y)
{
if(key==27){
exit(0);
}else if(key=='+' || key=='='){
bigger*=1.1;
}else if(key=='-'){
bigger*=0.9;
}else if(key=='o' || key=='O'){
bOrtho= !bOrtho; //true
resize(winW, winH);
}else if(key=='p' || key=='P'){
bOrtho= !bOrtho; //false;
resize(winW, winH);
}else if(key=='1'){
bShow1=!bShow1;
}else if(key=='2'){
bShow2=!bShow2;
}else if(key=='9'){
rrr+=0.1;
}else if(key=='a' || key=='A'){
humanX--;
}else if(key=='d' || key=='D'){
humanX++;
}else if(key=='w' || key=='W'){
humanY++;
}else if(key=='s' || key=='S'){
humanY--;
}else if(key=='b' || key=='B'){
if(blendPercent>0.5) blendPercent=0.5;
else blendPercent=1;
}
glutPostRedisplay();
}
void special(int key, int x, int y)
{
if(key==GLUT_KEY_LEFT)rotX+=2;
if(key==GLUT_KEY_RIGHT) rotX-=2;
if(key==GLUT_KEY_UP) rotY-=2;
if(key==GLUT_KEY_DOWN) rotY+=2;
}
void mySetCallback()
{
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glClearColor(1,1,1,1);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void initGL()
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize(winW, winH);
winID[0]=glutCreateWindow("Top View");
mySetCallback();
glutPositionWindow(winW+30,0+winTitleH);
glutDisplayFunc(displayOrtho);
glutReshapeFunc(resizeTop);
winID[1]=glutCreateWindow("Side View");
mySetCallback();
glutPositionWindow(0+10,winH+winTitleH*2);
glutDisplayFunc(displayOrtho);
glutReshapeFunc(resizeSide);
winID[2]=glutCreateWindow("Front View");
mySetCallback();
glutPositionWindow(winW+30,winH+winTitleH*2);
glutDisplayFunc(displayOrtho);
glutReshapeFunc(resizeFront);
winID[3]=glutCreateWindow("3D展示程式-鍾聖倫老師 v2.2, (c)2012 by jsyeh 2012-12-21");
glutPositionWindow(0+10,0+winTitleH);
mySetCallback();
glutDisplayFunc(display);
glutReshapeFunc(resize);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutTimerFunc(0, timer, 0);
quadFrustrum=gluNewQuadric();
glLineStipple(1,0x0101);
}
void readKinectCFG()
{
FILE * fin=fopen("myGL_Frustrum.txt", "r");
if(fin!=NULL){
fscanf(fin, "%f %f %f", &K1x, &K1y, &K1z);
fscanf(fin, "%f %f %f", &K1cx, &K1cy, &K1cz);
fscanf(fin, "%f %f %f", &K2x, &K2y, &K2z);
fscanf(fin, "%f %f %f", &K2cx, &K2cy, &K2cz);
}
fclose(fin);
}
int main(int argc,char *argv[])
{
glutInit(&argc,argv);
printf("Usage\n"
"(1) Mouse Drag to rotate\n"
"(2) Key UP/DOWN/LEFT/RIGHT to rotate\n"
"(3) Key '+'/'-' to resize\n"
"(4) Key 'o' for Orthogonal Projection\n"
"(5) Key 'p' for Perspective Projection\n"
"(6) Key '1'(red ball) and '2'(green ball) to switch to Kinect Frustrum\n"
"(7) Key ASDW for (humanX, humanY) movememt\n"
"It will read myGL_Frustrum.txt for Kinect Positions and Targets");
readKinectCFG();
initGL();
glutMainLoop();
return 0;
}
用樹狀結構以關節為基畫出人體動作
float [][]frames;
int frameN;
float frameTime;
class Bone{
ArrayList<Bone> childs;
PVector offset;
int r0;//address/index of Zrotation Xrotation Yrotatioin
int cN;//channels number
String name;
Bone(String _name, PVector _offset, int _r0, int _cN){
childs=new ArrayList<Bone>();
name=_name;
offset=_offset;
r0=_r0;
cN=_cN;
}
void showTree(){
println(name);
println("offset:"+offset);
println("r0:"+r0);
println("{");
for(Bone b:childs){
b.showTree();
}
println("}");
}
void draw(int i){
if(name=="End Site"){
pushMatrix();
beginShape(LINES);
vertex(0,0,0);
vertex(offset.x, offset.y, offset.z);
endShape();
popMatrix();
return;
}
pushMatrix();
rotateZ(radians(frames[i][r0]));
rotateX(radians(frames[i][r0+1]));
rotateY(radians(frames[i][r0+2]));
ellipse(0,0,10,10);
beginShape(LINES);
vertex(0,0,0);
vertex(offset.x, offset.y, offset.z);
endShape();
translate(offset.x, offset.y, offset.z);
for(Bone b: childs){
b.draw(i);
}
popMatrix();
}
}
Bone root;
void setup() {
size(600, 400, P3D);
loadBVH("watch.bvh");
root.showTree();
println("frameN"+frameN);
}
String getName(String line){
String [] words=splitTokens(line);
return words[1];
}
PVector getOffset(String line){
String [] words=splitTokens(line);
PVector now=new PVector(float(words[1]), float(words[2]), float(words[3]) );
return now;
}
int getChannelsN(String line){
String [] words=splitTokens(line);
return int(words[1]);
}
int parseNode(String [] lines, int i, Bone parent){
if(level==0)return i;
while(true){
if (lines[i].indexOf("JOINT")!=-1) {
String name = getName(lines[i]);
PVector offset = getOffset(lines[i+2]);
int cN=getChannelsN(lines[i+3]);
Bone now=new Bone(name, offset, channelN, cN);
parent.childs.add(now);
channelN+=cN;
i+=3;
level++;
println("JOINT "+name);
i=parseNode(lines, i+1, now);
} else if (lines[i].indexOf("End Site")!=-1) {
String name="End Site";
PVector offset = getOffset(lines[i+2]);
Bone now=new Bone(name, offset, 0, 0);
parent.childs.add(now);
i+=4;
println("End Site");
} else if (lines[i].indexOf("}")!=-1) {
level--;
return i+1;
}else println("Error:"+lines[i]);
}
}
int level=0, channelN=0;
void loadBVH(String filename) {
int motionI=0;
String [] lines=loadStrings(filename);
for (int i=0; i<lines.length; i++) {
println(lines[i]);
if ( lines[i].equals("HIERARCHY") ){
i++;
if (lines[i].indexOf("ROOT")!=-1) {
String name = getName(lines[i]);
PVector offset = getOffset(lines[i+2]);
int cN=getChannelsN(lines[i+3]);
root=new Bone(name, offset, channelN, cN);
channelN+=cN;
i+=3;
level++;
println("ROOT "+name);
i=parseNode(lines, i+1, root)-1;
}
}else if (lines[i].equals("MOTION") ){
println(lines[i+1].substring(12));
frameN=int(lines[i+1].substring(12));
frameTime=float(lines[i+1].substring(12));
motionI=i+3;
println(motionI);
break;
}
}
frames = new float[frameN][channelN];
for(int i=0;i<frameN;i++){
String [] v=splitTokens(lines[motionI+i]);
for(int j=0;j<channelN;j++){
frames[i][j]=float(v[j]);
}
}
}
void draw() {
background(255);
translate(width/2, height/2);
rotateX(PI/2);
int i=800+frameCount%(frameN-800);
translate(frames[i][0], frames[i][1], frames[i][2]);
root.draw(i);
println(i);
}
2017年11月23日 星期四
LYC的計圖學習筆記-WEEK11
聲音
#include<mmsystem.h> //多媒體,聲音的函式庫
int main()
{
PlaySoundA("檔名.wav",NULL,SND_ASYNC/SND_SYNC);
return 0;
}
p.s.1 PlaySoundA("檔名.wav",NULL,SND_ASYNC/SND_SYNC); 只能放wav檔
p.s.2 SND_ASYNC等同步 SND_SYNC不等下一步
如何放mp3檔音樂
1.下載CMP3_MCI.h
2.放入project中
#include <stdio.h>
#include "CMP3_MCI.h" //使用函式庫
CMP3_MCI mp3; //宣告變數
int main()
{
mp3.Load("file.mp3"); //讀入 mp3檔
mp3.Play(); //Play播放mp3檔
printf("現在等待輸入a\n");
int a;
scanf("%d", &a); //等待輸入時,程式還沒結束
}
2017年11月16日 星期四
LYC的計圖學習筆記-WEEK10
processing3
void setup(){fullScreen();
}
float ballX=250,ballY=450, ballVX=1.3, ballVY=-10;
float ball2X=250, ball2Y=50;
void draw(){
if( dist(ballX, ballY, ball2X, ball2Y)<100 ){
float cx=(ballX+ball2X)/2, cy=(ballY+ball2Y)/2;
float nx=ballX-ball2X, ny=ballY-ball2Y;
PVector N=new PVector(nx,ny);
N.normalize();
PVector v0=new PVector(ballVX, ballVY);
float len= -N.dot(v0);
v0.add(N.mult(len*2));
ellipse(cx, cy, 10,10);
stroke(0); line(ballX, ballY, ball2X, ball2Y);
stroke(255,128,0); line(cx,cy, cx+5*ballVX, cy+5*ballVY);
stroke(255,128,0); line(cx,cy, cx+5*v0.x, cy+5*v0.y);
stroke(255,0,0); line(cx,cy, cx+N.x, cy+N.y);
//return;
ballVX=v0.x; ballVY=v0.y;
}
background(255);
ballX+=ballVX; ballY+=ballVY;
ellipse(ballX, ballY, 100,100);
ellipse(ball2X, ball2Y, 100,100);
2017年11月9日 星期四
LYC的計圖學習筆記-WEEK09
3D Bump Mapping
https://www.openprocessing.org/sketch/249457Sketch
PImage img=loadImage("dora.png"); //定義圖片
size(600,600); //視窗大小
image(img,0,0,600,600); //讀圖片
fill(#26DCEA); //填顏色
rect(0,0,10,10); //開長方形
PImage img;
void setup()
{
size(1200,800);
img=loadImage("error.png");
}
void draw()
{
image(img,mouseX,mouseY);
}
//滑鼠移動時會自動重畫
PImage imgBrick,imgAzuka;
int azukaX,azukaY;
void setup()
{
size(600,600);
imgBrick=loadImage("brick.jpg");
imgAzuka=loadImage("azuka.png");
}
void draw()
{
background(255);
for(int i=0;i<=10;i+=1)
{
image(imgBrick,50*i,500,100,100);
}
image(imgAzuka,azukaX,azukaY,100,100);
azukaY+=5;
if(azukaY>=400){azukaY=400;}
}
void keyPressed()
{
if(keyCode==UP&&azukaY==400)
{azukaY-=100;}
if(keyCode==RIGHT)
{if(azukaX>=500){azukaX=0;}azukaX+=100;}
if(keyCode==LEFT)
{if(azukaX<=0){azukaX=600;}azukaX-=100;}
}
2017年11月2日 星期四
LYC的計圖學習筆記-WEEK08
OpenCv
目的:可以讀圖檔
程式碼#include <opencv/highgui.h>
using namespace std;
int main()
{
IplImage * img=cvLoadImage("earth.jpg");
cvNamedWindow("hello");
cvShowImage("hello", img);
cvWaitKey(0);
return 0;
}
轉動的地球
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
GLUquadric * quad;
GLuint id;
float angle=0;
void display()
{ glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();///自動轉很帥
glRotatef(90, 1,0,0);
glRotatef(angle, 0,0,1);///自動轉很帥
gluQuadricTexture(quad, 1);
gluSphere(quad, 1, 30, 30);///glutSolidTeapot(0.3);
glPopMatrix();///自動轉很帥
glFlush();
}
void timer(int t)
{
glutTimerFunc(10, timer, 0);/// 1000 msec 50fps:20msec
angle+=1;///自動轉很帥
glutPostRedisplay();
}
int myTexture(char *filename)
{
IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
return id;
}
void myInit()
{ quad = gluNewQuadric();
id = myTexture("earth.jpg");
}
int main(int argc, char**argv)
{ glutInit(&argc, argv);
glutCreateWindow("3D");
glutDisplayFunc(display); ///顯示
glutTimerFunc(0, timer, 0);
myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
glutMainLoop();
}
2017年10月26日 星期四
LYC的計圖學習筆記-WEEK07
光影
GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
物體材質
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
自動旋轉
glutIdleFunc(idle); //閒置時能自動旋轉
改編長寬比時不會歪掉
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
2017年10月19日 星期四
LYC的計圖學習筆記-WEEK06
打光
GLfloat pos[] = { 0.0, 0.0, -1.0, 0.0 };int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week05");
glutDisplayFunc(display);
glEnable(GL_LIGHT0); //打開打光功能
glEnable(GL_LIGHTING); //打開打光功能
glLightfv(GL_LIGHT0, GL_POSITION, pos);
glEnable(GL_DEPTH_TEST); //深度測試
glutMainLoop();
}
p.s. glLightfv(光源,光的座標,顏色);
加入keyboard,mouse
int RotateX=0,RotateY=0,RotateZ=0;
int oldX=0,oldY=0;
void keyboard(unsigned char key,int x,int y)
{
printf("now: %c (%d %d)\n",key,x,y);
if(key=='1')
{
RotateX+=1;
}
else if(key=='2')
{
RotateY+=1;
}
else if(key=='3')
{
RotateZ+=1;
}
glutPostRedisplay();
}
void mouse(int button,int state,int x,int y)
{
printf("now: %d %d (%d %d)\n",button,state,x,y);
if(state==GLUT_DOWN)
{
oldX=x;
oldY=y;
}
}
void motion(int x,int y)
{
RotateY+=-(x-oldX); //y軸旋轉是左右旋轉
RotateX+=-(y-oldY); //x軸旋轉是上下旋轉
oldX=x;
oldY=y;
glutPostRedisplay();
}
LYC的計圖學習筆記-WEEK05
上週複習
1.開啟jsyeh.org/3dcg10下載Examples的data,win32, Other examples的glut32.dll
2.全部解壓縮後放一起
舊程式
3.執行原本在win32(windows)的Transformation.exe
新程式
4.執行原本在win32(windows)的Projection.exe

5.按右鍵選glOrtho
6.按右鍵選glFrustum
gluPerspective(視野角度,視野,最近視野,最遠視野)
gluLookAt(眼睛看的角度轉x軸,眼睛看的角度轉y軸,眼睛看的角度轉z軸,
視野中心轉x軸,視野中心轉y軸,視野中心轉z軸
與視線垂直的面轉x軸,與視線垂直的面轉y軸,與視線垂直的面轉z軸)
glOrtho(左面的視野平移,右面的視野平移,上面的視野平移,
下面的視野平移,前面的視野平移,後面的視野平移)
glFrustum(整個視野的左面平移,整個視野的右面平移,整個視野的上面平移,
整個視野的下面平移,整個視野的前面平移,整個視野的後面平移,)
2017年10月5日 星期四
LYC的計圖學習筆記-WEEK04
旋轉範例
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidTeapot(1);
glPopMatrix();

p.s. glutSolidIcosahedron();

glutSolidOctahedron();

glutSolidDodecahedron();

分開製作圖形
glPushMatrix(); //備份矩陣
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix(); //還原矩陣
移動
glPushMatrix();
glTranslated(-2.4,1.2,-6);
//glRotated(60,1,0,0);
//glRotated(a,0,0,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s.glTranslated(移動x座標,移動y座標,移動z座標);
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
//glRotated(a,0,0,1);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s. glRotated(轉動角度,x座標,y座標,z座標);
以(x座標,y座標,z座標)為軸轉動

旋轉(隨時間改變)
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;const double a = t*90.0;
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);//一毫秒轉90度
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s. glRotated(轉動角度,x座標,y座標,z座標);
以(x座標,y座標,z座標)為軸轉動
放大縮小
glPushMatrix();
glTranslated(-2.4,1.2,-6);
glRotated(60,1,0,0);
glRotated(a,0,0,1);
glScalef(0.5,0.5,0.5);
glutSolidSphere(1,slices,stacks);
glPopMatrix();
p.s. glScalef(x軸的大小乘以倍數,y軸的大小乘以倍數,z軸的大小乘以倍數); //倍數小於1即縮小
float dx=0,dy=0,dz=0;//要畫的3D位置
int oldx=0,oldy=0,oldz=0;//舊的x,y,z位置
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); //設為單位矩陣
glTranslatef(dx,dy,dz);
glColor3d(1,0,0);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void motion(int x,int y)//用來拖曳移動
{
dx+= (x-oldx)/150.0;
dy+= -(y-oldy)/150.0;
oldx=x;
oldy=y;
glutPostRedisplay(); //貼便利貼,請電腦要重畫畫面
}
void mouse(int button,int state,int x,int y) //按下去時記錄在哪裡開始
{
if(state==GLUT_DOWN)
{
oldx=x;
oldy=y;
}
//GLUT_DOWN--->滑鼠按下去時為1
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glClearColor(1,1,1,1);
glutMainLoop();
return 0;
}
2017年9月28日 星期四
LYC的計圖學習筆記-WEEK03
讀檔
txt檔要放入freeglut->bin中
3D模型
1.從Examples->data下載
2.拖到3D exploring中
3.另存新檔並將存檔類型改為.cpp
4.將export type改為Sample APP
5.放入main.cpp並執行
2017年9月21日 星期四
LYC的計圖學習筆記-WEEK02
重點一:Notepad++將檔名更改.cpp即可改變文字顏色
重點二:用5-10行寫出Glut的基礎內容
#include <GL/glut.h> //呼叫涵式庫windows自己下載;moc自己就有
int main(int argc, char *argv[]) //主程式;argc參數數量;*argv[]參數內容
{
glutInit(&argc, argv); //初始化
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH); //初始化設定模式
glutCreateWindow("GLUT Shapes"); //創造視窗
glutMainLoop(); //無限迴圈
return 0; //結束
}
p.s. glutInitDisplayMode(顏色,雙緩存,3D(深度));
p.s. glutInitDisplayMode(顏色,雙緩存,3D(深度));
重點三:寫出Glut使3D視窗能出現點,線,面,顏色
1.畫出茶壺
#include <GL/glut.h>
void display(void)
{
glClearColor(1,0,0,0); //設定背景顏色(目前為紅色)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //清 顏色 | 3D(深度) 的緩存
glColor3f(0,1,1); //賦與物體顏色(目前為靛色);f為float
glutSolidTeapot(0.3); //畫出茶壺
glutSwapBuffers(); //雙緩衝區互換
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display); //註冊display涵式
glutMainLoop();
return 0;
}
p.s.1 glClearColor(紅,綠,藍,透明度);
p.s.2. glColor3f(紅,綠,藍);
p.s.3 為何要用glutSwapBuffers();
2.以點,線畫出面
#include <GL/glut.h>
void display(void)
{
glClearColor(1,0,0,1000000);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,1);
glBegin(GL_POLYGON); //開始畵polygon(多邊形)
glColor3f(0,1,1); //從點開始擴散顏色
glVertex3f(0,0.5,0); //決定頂點之一;f為float
glColor3f(1,0,1); //從點開始擴散顏色
glVertex3f(-0.707,-0.707,0); //決定頂點之一;f為float
glColor3f(1,1,0); //從點開始擴散顏色
glVertex3f(0.707,-0.707,0); //決定頂點之一;f為float
glEnd(); //畫圖結束
glutSwapBuffers();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
p.s.1 glVertex3f(); 將整個視窗視為從-1~1
p.s.2 glVertex3f(x座標,y座標,z座標);
2017年9月14日 星期四
LYC的計圖學習筆記-WEEK01
訂閱:
文章 (Atom)

































