2023年6月1日 星期四

咻碰閃week16

 week16

好想放假

1 clone 2023graphicsb資料夾,開新專案week16_all,總複習

step01 點線面色彩

#include <GL/glut.h>
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    glColor3f(1,1,0);
    glBegin(GL_POLYGON);
        glVertex2f(0.5,0.5);
        glVertex2f(-0.5,0.5);
        glVertex2f(-0.5,-0.5);
        glVertex2f(0.5,-0.5);
    glEnd();

    glColor3f(0,1,0);
    glutSolidTeapot(0.3);
    glutSwapBuffers();
}
int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");

    glutDisplayFunc(display);

    glutMainLoop();
}

step02 滑鼠拖曳物件移動

#include <GL/glut.h>
float teapotX = 0, teapotY = 0;
void motion(int x,int y)
{
    teapotX = (x-150)/150.0;
    teapotY = (150-y)/150.0;
    glutPostRedisplay();

}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glTranslatef(teapotX, teapotY, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");

    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();
}


step03 滑鼠拖曳旋轉


 











#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();

}
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glRotatef(angle,0,0,1);
        ///glTranslatef(teapotX, teapotY, 0);
        glutSolidTeapot(0.3);
    glPopMatrix();
    glutSwapBuffers();
}
int main(int argc,char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
    glutCreateWindow("week16");

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

    glutMainLoop();
}

step04 位置

#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);
        //glRotatef(angle, 0, 0, 1);
        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();
}


step05 複製位置,定錨點,讓茶壺旋轉














void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    glPushMatrix();
        glColor3f(1,1,1);
        glRotatef(angle,0,0,1);
        ///glTranslatef( 0.45, -0.05 ,0);
        glTranslatef( 0.45, -0.05 ,0);
        glutSolidTeapot(0.3);
    glPopMatrix();

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

step06 






 












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();
}

step07 更改路徑




















複製freeglut.dll到資料夾內
無法執行














從week10複製glm.cpp和glm.h到week16資料夾















新增glm.cpp,加程式碼







模型太大













縮小、加顏色










複製week7-2_myTexture程式碼














安裝OpenCV 選第二個path的選項















Setting>Compiler>Search directories>Complier>C:\OpenCV2.1\include
                                                      >Linker>C:\OpenCV2.1\lib

 
                           

Setting>Compiler>Linker setting>cv210、cxcore210、highgu210

                  














重開Codeblocks

#include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#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 <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * gundam = NULL;
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);
        glScalef(0.04, 0.04, 0.04);
        glRotatef(angle, 0, 1, 0);
        glmDraw(gundam, GLM_MATERIAL|GLM_TEXTURE);
    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);

    gundam = glmReadOBJ("model/Gundam.obj");
    myTexture("model/Diffuse.jpg");
    glEnable(GL_DEPTH_TEST);

    glutMainLoop();
}






沒有留言:

張貼留言