顯示具有 02161061_陳侑增 標籤的文章。 顯示所有文章
顯示具有 02161061_陳侑增 標籤的文章。 顯示所有文章

2015年6月10日 星期三


#include <GL/glut.h>
float angle1=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(0.1,0.1,1);
        glutSolidTeapot(0.3);
            glPushMatrix();
                glTranslatef(0.5, 0.15, 0);
                glRotatef(angle1,0,0,1);
                glTranslatef(0.3,0,0);
                glScalef(1,0.3,0.3);
                glColor3f(1,0,0);
                glutSolidSphere(0.3,30,30);
            glPopMatrix();
    glPopMatrix();
    glFlush();
}
void motion(int x, int y)
{
    angle1=x;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("dfkhiodfklghdfmhkltuighsdrklgyuwjopmhgfhim,");
    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}
#include <GL/glut.h>
float angle=0,oldX=0,oldY=0;
void drawBody(){
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    {
    glColor3ub(100,100,100);
    GLfloat pos[]={0,0,-1,0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glTranslatef(0.5, 0, 0);
        glRotatef(angle, 0,0,1);
        glTranslatef(0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle, 0,0,1);
            glTranslatef(0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);
        glRotatef(-angle, 0,0,1);
        glTranslatef(-0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(-angle, 0,0,1);
            glTranslatef(-0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void motion(int x, int y)
{   angle -= y - oldY;
    oldY=y; oldX=x;
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("dfnhodfnhdfgnkldfmjkghsdmgkladrtbmhiosuisgjfomhfji");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}
#include <stdio.h>
#include <GL/glut.h>
FILE *fout=NULL;
FILE *fin=NULL;
float oldX=0, oldY=0,angle[10]={};
int angleID=1;
void drawBody(){
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    {
    glColor3ub(100,100,100);
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glTranslatef(0.5, 0, 0);
        glRotatef(angle[1], 0,0,1);
        glTranslatef(0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);
        glRotatef(-angle[3], 0,0,1);
        glTranslatef(-0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(-angle[4], 0,0,1);
            glTranslatef(-0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}

void motion(int x, int y)
{   angle[angleID]-= y - oldY;
    oldY=y; oldX=x;

    if(fout==NULL)
        fout=fopen("a.txt","w+");
    for(int i=0;i<10;i++)
    {
        printf("%f ",angle[i]);
        fprintf(fout,"%f ",angle[i]);
    }
    printf("\n");
    fprintf(fout,"\n");
}
void timerPlay(int t)
{
    glutTimerFunc(500,timerPlay,t+1);
    if(fin==NULL)
        fin=fopen("a.txt","r");
    for(int i=0;i<10;i++)
    {
        fscanf(fin,"%f",&angle[i]);
        printf("%f ",angle[i]);
    }
    glutPostRedisplay();

}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    glutPostRedisplay();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1')angleID=1;
    if(key=='2')angleID=2;
    if(key=='3')angleID=3;
    if(key=='4')angleID=4;
    if(key=='p') glutTimerFunc(10,timerPlay,0);
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("trhokoprkghopkdfopgkopdfjgjkiopqwejtioj");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

2015年6月3日 星期三

#include <GL/glut.h>
#include <stdio.h>
float x1=-0.4, y1=0, x2=0, y2=0, angle=0;
void display()
{
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(x1, y1, 0);//要掛在哪裡?
        glRotatef(angle, 0,0,1);
        glTranslatef(-0.25, -0.11, 0);
        glColor3f(1,1,0);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glPushMatrix();
        glTranslatef(x2, y2, 0);
        glColor3f(1,0,0);
        glutSolidTeapot(0.2);
    glPopMatrix();
    glFlush();///先不用 glutSwapBuffers(); 配合 glutInitDisplayMode(GLUT_DOUBLE);
}
void keyboard(unsigned char key, int x, int y)
{
    if(key=='1') x1+=0.1;
    if(key=='2') x1-=0.1;
    if(key=='3') y1+=0.1;
    if(key=='4') y1-=0.1;
    if(key=='5') angle+=10;
    if(key=='6') angle-=10;
    printf("%f %f\n", x1, y1);
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("Robot3D");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}
手臂茶壺,可以使用1,2,3,4,5,6來控制茶壺手臂。
#include <GL/glut.h>
float oldX=0,oldY=0;
float angle[10]={0,0,0,0,0,0,0,0,0,0};
int now;
void motion(int x,int y)
{
    angle[now]-=y-oldY;
    oldY=y;
    oldX=x;
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') now=1;
    if(key=='2') now=2;
    if(key=='3') now=3;
    if(key=='4') now=4;
    glutPostRedisplay();
}
void drawBody(){
    glPushMatrix();
    glScalef(1.1, 0.55, 0.55);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm(){
    glPushMatrix();
    glScalef(0.66, 0.33, 0.33);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    { ///Lighting
    glColor3ub(100,130,100);
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }
    glPushMatrix();
        glTranslatef(0.5, 0, 0);
        glRotatef(angle[1], 0,0,1);
        glTranslatef(0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(0.25, 0, 0);
            glRotatef(angle[2], 0,0,1);
            glTranslatef(0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0);
        glRotatef(-angle[3], 0,0,1);
        glTranslatef(-0.25, 0,0);
        drawArm();
        glPushMatrix();
            glTranslatef(-0.25, 0, 0);
            glRotatef(-angle[4], 0,0,1);
            glTranslatef(-0.25, 0,0);
            drawArm();
        glPopMatrix();
    glPopMatrix();
    drawBody();
    glFlush();
}
void mouse(int button, int state, int x, int y){
    if(state==GLUT_DOWN) { oldX=x; oldY=y; }
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    //angle++;
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("WTF");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

2015年5月27日 星期三

#include <GL/glut.h>
float angle=0;
void display()
{
    GLfloat pos[]={0,0,-1,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);
    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0.6,0.2,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.3,0,0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSolidTeapot(0.3);
    glFlush();
}
void timer(int t)
{
    glutTimerFunc(20,timer,t+1);
    angle++;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("WTF");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMainLoop();
}
大茶壺像機械手臂一樣旋轉!!

#include <GL/glut.h>
float angle=0;
void drawBody()
{
    glPushMatrix();
    glScalef(1,0.5,0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm()
{
    glPushMatrix();
    glScalef(0.6,0.3,0.3);
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(0.5,0,0);
        glRotatef(angle,0,0,1);
        glTranslatef(0.3,0,0);
        drawArm();
    glPopMatrix();
    drawBody();
    glFlush();
}
void timer(int t)
{
    glutTimerFunc(20,timer,t+1);
    angle++;
    glutPostRedisplay();
}
int main(int argc,char**argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("WTF");
    glutDisplayFunc(display);
    glutTimerFunc(20,timer,0);
    glutMainLoop();
}
方塊像機械手臂一樣旋轉!!
#include <GL/glut.h>
float angle=0, oldX=0, oldY;

void drawBody()
{
    glPushMatrix();
    glScalef(1, 0.5, 0.5);
    glutSolidCube(1);
    glPopMatrix();
}
void drawArm()
{
    glPushMatrix();
    glScalef(0.6, 0.3, 0.3); ///手臂角度
    glutSolidCube(1);
    glPopMatrix();
}
void display()
{
    GLfloat pos[]={0, 0, -1, 0};
    glLightfv(GL_LIGHT0, GL_POSITION, pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glPushMatrix();
        glTranslatef(0.5, 0, 0); ///旋轉軸
        glRotatef(angle, 0,0,1); ///角度
        glTranslatef(0.25, 0,0);
        drawArm();
        //glutSolidTeapot(0.3);
    glPushMatrix();
        glTranslatef(0.25, 0, 0); ///旋轉軸
        glRotatef(angle, 0,0,1); ///角度
        glTranslatef(0.25, 0,0);
        drawArm();
    glPopMatrix();
glPopMatrix();
    glPushMatrix();
        glTranslatef(-0.5, 0, 0); ///旋轉軸
        glRotatef(-angle, 0,0,1); ///角度
        glTranslatef(-0.25, 0,0);
        drawArm();
    glPushMatrix();
        glTranslatef(-0.25, 0, 0); ///旋轉軸
        glRotatef(-angle, 0,0,1); ///角度
        glTranslatef(-0.25, 0,0);
        drawArm();
    glPopMatrix();
    glPopMatrix();
    drawBody();

    glFlush();
}
void motion(int x, int y)
{
    angle -= y - oldY;
    oldY=y; oldX=x;
}
void mouse(int button, int state, int x, int y)
{
    if(state==GLUT_DOWN) {oldX=x; oldY=y;}
}
void timer(int t)
{
    glutTimerFunc(20, timer, t+1);
    ///angle++; 要自己動,要刪掉
    glutPostRedisplay();
}
int main(int argc, char**argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("WTF");
    glutDisplayFunc(display);
    glutTimerFunc(20, timer, 0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);

    glutMainLoop();
}
類似展現肌肉一樣動作,用方塊來表示。

2015年5月20日 星期三


#include <GL/glut.h>
void display()
{
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glFlush();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glViewport(0,0,800,600);
    glMatrixMode(GL_PROJECTION);
    glOrtho(-400,400,-300,300,-1000,1000);
    glMatrixMode(GL_MODELVIEW);
    glutMainLoop();
}
一個大茶壺!!!!!但是有缺陷,這茶壺會依視窗大小而變化,實在不太符合現實世界,
這一定是bug!!!
#include <GL/glut.h>
void display()
{
    glPushMatrix();
        glutSolidTeapot(100);
    glPopMatrix();
    glFlush();
}
void reshape(int w,int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w/2,w/2,-h/2,h/2,-1000,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
}
另一個大茶壺!!!終於不會一視窗大小變化,看來帥哥終於把bug修好了!!~
#include <GL/glut.h>
float eyeX=-100,eyeY=80,eyeZ=-200;
void display()
{
    GLfloat pos[]={0,0,200,0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHT0);
    glEnable(GL_NORMALIZE);
    glEnable(GL_LIGHTING);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        gluLookAt(eyeX,eyeY,eyeZ,0,0,0,0,1,0);
        glPushMatrix();
            glutSolidTeapot(100);
        glPopMatrix();
    glPopMatrix();
    glFlush();
}
void motion (int x,int y)
{
    eyeX=x-400;
    eyeY=300-y;
    glutPostRedisplay();
}
void reshape(int w,int h)
{
    glViewport(0,0,w,h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-w/2,w/2,-h/2,h/2,-1000,1000);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
void timer(int t)
{
    glutTimerFunc(20,timer,t+1);
    eyeX+=2;
    if(eyeX>300)eyeY+=4;
    glutPostRedisplay();
}
int main (int argc,char**argv)
{
    glutInit(&argc,argv);
    glutInitWindowSize(800,600);
    glutCreateWindow("3D");
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMotionFunc(motion);
    glutTimerFunc(0,timer,0);
    glutMainLoop();
}

又是大茶壺,會依時間來調整視角,各種角度來觀察這個大茶壺!!

2015年5月13日 星期三

0.0


#include "glm.h"
GLMmodel * pmodel = NULL;
void display()
{
    GLfloat pos[] = {0.0,0.0,-1.0,0.0};
    glLightfv(GL_LIGHT0,GL_POSITION,pos);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

    glEnable(GL_DEPTH_TEST);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL);
    glFlush();
}
int main(int argc, char**argv)
{
    pmodel = glmReadOBJ("porsche.obj");
    glmUnitize(pmodel);
    glmFacetNormals(pmodel);
    glmVertexNormals(pmodel,90.0);

    glutInit(&argc, argv);
    glutCreateWindow("22315651276518734237785");
    glutDisplayFunc(display);
    glutMainLoop();
}
匯入3d模型+上打光,感覺好棒棒。

2015年5月6日 星期三

0.0+

#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
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, 0.5, 30, 30); ///glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
    angle+=1;///自動轉很帥
}
void myInit()
{
    quad = gluNewQuadric();
    IplImage * img = cvLoadImage("dog.jpg"); ///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);
}   ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec   50fps:20msec
    glutTimerFunc(20, timer, 0);
    //printf("timer now\n");
    glutPostRedisplay();
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    //glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    glutTimerFunc(0, timer, 0);
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}


讓地球無論大小,都能等速運轉,不會大得很慢,小的很快 @@"
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
float angle=0; ///自動轉很帥
GLUquadric * quad;
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,1,0);///自動轉很帥
        glutSolidTeapot(0.3);
        //gluQuadricTexture(quad, 1);
        //gluSphere(quad, 0.5, 30, 30); ///glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
    angle+=1;///自動轉很帥
}
void myInit()
{
    quad = gluNewQuadric();
    IplImage * img = cvLoadImage("dog.jpg"); ///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);
}   ///最後一行最難/最重要, 所貼圖影像的資料都設定好
void timer(int t)
{/// 1000 msec   50fps:20msec
    glutTimerFunc(20, timer, 0);
    //printf("timer now\n");
    glutPostRedisplay();
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    //glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    glutTimerFunc(0, timer, 0);

    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}

自動水平轉動的地球茶壺!!~  史上最潮的茶壺 @@"  
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
#include <stdio.h>
#include <math.h>
float angle=0, potX=0, potY=0, potVX=0, potVY=0;
int potState=0;
void timer(int t)
{
    glutTimerFunc(10,timer, t+1);
    angle+=1;
    if(potState==0) potY=0.5*sin(angle/180.0*3.14);
    if(potState==2){
        potX+=potVX;
        potY+=potVY;
    }
    glutPostRedisplay();
}
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glutSolidTeapot(0.02);
    glPushMatrix();///自動轉很帥
        glTranslatef(potX,potY,0);
        glRotatef(angle, 0,1,0);///自動轉很帥
        glutSolidTeapot(0.03);
    glPopMatrix();///自動轉很帥
    glFlush();
}
void mouse(int button, int state,int x, int y )
{
    if(state==GLUT_DOWN) potState=1;
    else if(state==GLUT_UP){
                potState=2;
                potVX=-(x-350)/3500.0;
                potVY=(y-350)/3500.0;
    }
}
void motion(int x,int y)
{
    if(potState==1){
        potX=(x-350)/350.0;
        potY=-(y-350)/350.0;
    }
}
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("4D");
    glutReshapeWindow(700,700);
    glutDisplayFunc(display);
    glutTimerFunc(10,timer,0);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutMainLoop();
}
憤怒的茶壺,拖曳茶壺能像憤怒鳥一樣射出!!!!!!!!潮到都出水了。

2015年4月29日 星期三

0.0


#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h> ///3D glut
float angle=0; ///自動轉很帥
void display()
{   glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
    glClear(GL_COLOR_BUFFER_BIT  | GL_DEPTH_BUFFER_BIT);
    glPushMatrix();///自動轉很帥
        glRotatef(angle, 0,1,0);///自動轉很帥
        glutSolidTeapot(0.3);
    glPopMatrix();///自動轉很帥
    glFlush();
    angle+=0.1;///自動轉很帥
}
void myInit()
{   IplImage * img = cvLoadImage("image.jpg"); ///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);
}   ///最後一行最難/最重要, 所有貼圖影像的資料都設定好
int main(int argc, char**argv)
{   glutInit(&argc, argv);
    glutCreateWindow("3D");
    glutDisplayFunc(display); ///顯示
    glutIdleFunc(display);///自動轉很帥 (閒閒沒事幹, 就重畫)
    myInit(); ///我的 init 初始化 把貼圖準備好 前面OpenCV 2行, 後面 OpenGL 9行
    glutMainLoop();
}

2015年4月15日 星期三

02161061



嘗試製作第一張圖的遊戲,慢慢達成了,有個會移動的茶壺標靶給我們打,打擊有聲音,打到也有另一種聲音。
#include <stdio.h>
#include <GL/glut.h>
#include <mmsystem.h>
float potX=-1,potY=0;
void display()
 {
     glClearColor(0.5,0.5,0.3,0);
     glClear(GL_COLOR_BUFFER_BIT);
     glPushMatrix();
        glTranslatef(potX,potY,0);
        glutSolidTeapot(0.07);
     glPopMatrix();
     glFlush();
     potX+=0.01;
     if(potX>1.1)potX=-1.1;
 }
 void keyboard(unsigned char key,int x,int y)
 {
    exit(0);
 }
 void mouse(int button,int state,int x,int y)
 {
     if(state==GLUT_DOWN){
         PlaySound("Data/Shot.wav", NULL, SND_ASYNC);
         printf("Shot!!\n");
    }
 }
 int main(int argc, char**argv)
 {
     glutInit(&argc, argv);
     glutCreateWindow("02161061");
     glutFullScreen();
     glutDisplayFunc(display);
     glutIdleFunc(display);
     glutKeyboardFunc(keyboard);
     glutMouseFunc(mouse);
     glutMainLoop();
 }

2015年4月8日 星期三

#include <GL/glut.h>
#include <stdio.h>
void display()
{
    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a'){printf("AAA\n");}
    else if(key=='b'){printf("BBB\n");}
}
int main (int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("vgsdghosdgjlof");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
}

畫一個簡單的茶壺和寫出一些指令,如果輸入a,就會輸出AAA,如果輸入b,就會輸出BBB。
#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void display()
{
    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='a'){printf("AAA\n");}
    else if(key=='b'){printf("BBB\n");}
}
void mouse (int button,int state,int x,int y)
{
    printf("now playing sound");
    PlaySound("bubbs.wav",NULL,SND_ASYNC);
}
int main (int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("vgsdghosdgjlof");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();
}

這次多插入個滑鼠指令,案圖片後會撥放我所設定的音效檔。
音效檔要要放在程式資料夾內,bin->Debug裡面,才會有聲音。

#include <GL/glut.h>
#include <mmsystem.h>
#include <stdio.h>
void display()
{
    glutSolidTeapot(0.3);
    glFlush();
}
void keyboard(unsigned char key,int x,int y)
{
    if(key=='1') PlaySound("Do.wav",NULL,SND_ASYNC);
    if(key=='2') PlaySound("RE.wav",NULL,SND_ASYNC);
    if(key=='3') PlaySound("Mi.wav",NULL,SND_ASYNC);
    if(key=='4') PlaySound("Fa.wav",NULL,SND_ASYNC);
    if(key=='5') PlaySound("Sol.wav",NULL,SND_ASYNC);
    if(key=='6') PlaySound("La.wav",NULL,SND_ASYNC);
    if(key=='7') PlaySound("Si.wav",NULL,SND_ASYNC);
}
void mouse (int button,int state,int x,int y)
{
    printf("now playing sound");
    PlaySound("bubbs.wav",NULL,SND_ASYNC);
}
int main (int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("vgsdghosdgjlof");
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMouseFunc(mouse);
    glutMainLoop();

用鍵盤控制想發啥聲音!!~

2015年3月25日 星期三

02161061_陳侑增



#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,1,0.5);
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,0.8);
        glEnd();

        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            glVertex3f(0.1,0.1,-0.8);
            glVertex3f(-0.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();

        glBegin(GL_QUAD_STRIP); =====>把方形連在一起變長條形
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(0.1,0.1,-0.8);
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();
        glPopMatrix();
        angle+=0.01;
    glFlush();
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02161061");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMainLoop();
}
一步一步從面到會轉動的長方形。
#include <GL/glut.h>
float angle=0;
void display()
{
    glClear(GL_COLOR_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,1,0.5);
        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,0.8);
        glEnd();

        glBegin(GL_POLYGON);
            glColor3f(1,0,0);
            glVertex3f(0.1,0.1,-0.8);
            glVertex3f(-0.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();

        glBegin(GL_QUAD_STRIP);
            glVertex3f(0.1,0.1,0.8);
            glVertex3f(0.1,0.1,-0.8);
            glVertex3f(-0.1,0.1,0.8);
            glVertex3f(-0.1,0.1,-0.8);
            glVertex3f(-0.1,-0.1,0.8);
            glVertex3f(-0.1,-0.1,-0.8);
            glVertex3f(0.1,-0.1,0.8);
            glVertex3f(0.1,-0.1,-0.8);
        glEnd();
        glPopMatrix();
        angle+=0.01;
    glFlush();
}
int oldX=0;
void motion(int x,int y)
{
    angle+=(x-oldX);
    oldX=x;
}
int main(int argc,char **argv)
{
    glutInit(&argc,argv);
    glutCreateWindow("02161061");
    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutMotionFunc(motion);
    glutMainLoop();
}

多了幾行 就可以用滑鼠轉圖形了 = =+

2015年3月4日 星期三

week02 hw02

10行
0行
0行

學習一些輕鬆的3d程式寫法,讓我對這門課有一些基礎的了解 ^.^