2023年6月1日 星期四

真week16

 week16總複習

旋轉茶壺

#include <stdio.h>
#include <GL/glut.h>
float teapotX = 0, teapotY = 0, angle = 0, oldX = 0, oldY = 0;
void mouse(int button, int state, int x, int y) {
    oldX = x;
    oldY = y;
}
void motion(int x, int y) {
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    angle += x - oldX;
    oldX = x;
    glutPostRedisplay();
    printf("  glTranslatef( %.2f, %.2f, 0 ); \n", teapotX, teapotY );
}
void display() {
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1,1,1);
        glTranslatef(-0.42, +0.06, 0 );
        glRotatef(angle, 0, 0, 1);
        glTranslatef( 0.42, -0.06, 0 );//glTranslatef(teapotX, teapotY, 0);
        glutSolidTeapot( 0.3 );
    glPopMatrix();

    glColor3f(0,1,0);
    glutSolidTeapot( 0.02 );
    glutSwapBuffers();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");

    glutDisplayFunc(display);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    glutMainLoop();
}







設定
complier的目錄要加
C:\OpenCV2.1\include
Linker:C:/C:\OpenCV2.1\lib




設定linker setting
    cv210
    cxcore210
    highgui210










 剛蛋

#include <opencv/cv.h>
#include <GL/glut.h>
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;
}
#include <GL/glut.h>
#include "glm.h"///把source.zip裡的glm.h放在同目錄
GLMmodel * pmodel =NULL;
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(pmodel==NULL){
pmodel = glmReadOBJ("Gundam.obj");
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL | GLM_TEXTURE);
///glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char**argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("week08");
myTexture("Diffuse.jpg");
glutDisplayFunc(display);
glutMainLoop();
}







期末作業:


#include <stdio.h>

#include <GL/glut.h>

#include "glm.h"


GLMmodel * head = NULL;

GLMmodel * body = NULL;

GLMmodel * uparm = NULL;

GLMmodel * downarm = NULL;

GLMmodel * cap = NULL;

GLMmodel * eye = NULL;

GLMmodel * nose = NULL;

GLMmodel * downleg = NULL;

GLMmodel * upleg = NULL;


int show[7] = {1,1,1,1,1,1,1};

int ID = 6;

float teapotX =0, teapotY = 0;

float angle[20] = {};

FILE * fout = NULL;

FILE * fin = NULL;

void keyboard(unsigned char key, int x,int y) {

    if(key=='0') ID = 0;

    if(key=='1') ID = 1;

    if(key=='2') ID = 2;

    if(key=='3') ID = 3;

    if(key=='4') ID = 4;

    if(key=='5') ID = 5;

    if(key=='6') ID = 6;

    if(key=='7') ID = 7;

    if(key=='8') ID = 8;

    glutPostRedisplay();

}

void display()

{

    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    if(head==NULL){

        head = glmReadOBJ("model/head.obj");

        body = glmReadOBJ("model/body.obj");

        uparm = glmReadOBJ("model/uparm.obj");

        downarm = glmReadOBJ("model/downarm.obj");

        cap = glmReadOBJ("model/cap.obj");

        eye = glmReadOBJ("model/eye.obj");

        nose = glmReadOBJ("model/nose.obj");

    }

glPushMatrix();

    glScalef(0.2, 0.2, 0.2);


    glPushMatrix();

        ///glTranslatef(teapotX, teapotY, 0);


        glColor3f(1, 1, 1);

        if (show[0]) glmDraw(head, GLM_MATERIAL);

    glPopMatrix();


    if (ID == 1) glColor3f(1, 0, 0);

    else glColor3f(1, 1, 1);

    if (show[1]) glmDraw(body, GLM_MATERIAL);


    glPushMatrix();

        glTranslatef(-1.360000, +0.360000, 0);

        glRotatef(angle[2], 0, 0, 1);

        glTranslatef(1.360000, -0.360000, 0);


        if (ID == 2) glColor3f(1, 0, 0);

        else glColor3f(1, 1, 1);

        if (show[2]) glmDraw(uparm, GLM_MATERIAL);

    glPopMatrix();



    glPushMatrix();


        glTranslatef(teapotX, teapotY, 0);



        if (ID == 3) glColor3f(1, 0, 0);

        else glColor3f(1, 1, 1);

        if (show[3]) glmDraw(downarm, GLM_MATERIAL);

    glPopMatrix();




    glPushMatrix();

        if (ID == 4) glColor3f(1, 0, 0);

        else glColor3f(1, 0, 0);

        if (show[4]) glmDraw(cap, GLM_MATERIAL);

    glPopMatrix();


    glPushMatrix();

        if (ID == 5) glColor3f(0, 0, 0);

        else glColor3f(0, 0, 0);

        if (show[5]) glmDraw(eye, GLM_MATERIAL);

    glPopMatrix();


    glPushMatrix();

        if (ID == 6) glColor3f(0, 0, 1);

        else glColor3f(0, 0, 1);

        if (show[6]) glmDraw(nose, GLM_MATERIAL);

    glPopMatrix();


glPopMatrix();



    glPopMatrix();

    glColor3f(0,1,0);

    glutSolidTeapot( 0.02 );

    glutSwapBuffers();

}

int oldX=0, oldY=0;

void mouse(int button, int state, int x, int y)

{

    if(state==GLUT_DOWN){

        oldX = x;

        oldY = y;

    }

}

void motion(int x, int y){

    teapotX = (x-oldX)/150.0;

    teapotY = (y-oldY)/150.0;

    angle+=x-oldX;

    angle+=x-oldX;

    printf("glTranslatef(%f, %f, 0);\n", teapotX, teapotY);

    angle[ID] += x-oldX;

    oldX = x;

    oldY = y;

    glutPostRedisplay();

}

int main(int argc, char** argv)

{

    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("week14");


    glutMotionFunc(motion);

    glutDisplayFunc(display);

    glutKeyboardFunc(keyboard);

    glutMouseFunc(mouse);


    glutMainLoop();


}

沒有留言:

張貼留言