1. 程式人生 > 其它 >立方體三維觀察-模型變換

立方體三維觀察-模型變換

實驗一:

分析:

三個圖形的原型如下所示(所有的操作都必須先放到stack中,glPushMatrix () 和 glPopMatrix()):

1.以中心繪製原點,設定顏色為紅色,繪製單位立方體線框glutWireCube(1.0)
2.設定顏色為綠色, 設定線寬2.0 glLineWidth(2.0),講元立方體線框沿x軸正方向平移2.0這裡用到了平移函式glTranslatef(2.0f,0.0f,0.0f); 引數1 :x軸 引數2 :y軸 引數3 : z軸

3.跟2 差不多就不寫了

// 提示:在合適的地方修改或新增程式碼
#include <GL/freeglut.h>
#include<stdio.h>

// 評測程式碼所用標頭檔案-開始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 評測程式碼所用標頭檔案-結束

GLint winWidth = 400, winHeight =400 ; 	      //設定初始化視窗大小

/*觀察座標系引數設定*/
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;	   //設定觀察座標系原點 
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;	//設定觀察座標系參考點(視點) 
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;	   //設定觀察座標系向上向量(y軸) 

/*觀察體引數設定 */
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;//設定裁剪視窗座標範圍
GLfloat dnear = 1.5, dfar = 20.0;	      //設定遠、近裁剪面深度範圍

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    /*觀察變換*/
    gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);        //指定三維觀察引數

    // 請在此新增你的程式碼
    /********** Begin ********/
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glLineWidth(2.0);
    glTranslatef(2.0f,0.0f,0.0f);
    glutWireCube(1.0);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glLineWidth(2.0);
    glTranslatef(-2.0f,0.0f,0.0f);
    glutSolidCube(1.0);
    glPopMatrix();

    /********** End **********/
    glFlush();
}

void reshape(GLint newWidth, GLint newHeight)
{
    /*視口變換*/
    glViewport(0, 0, newWidth, newHeight);	//定義視口大小

    /*投影變換*/
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    /*透視投影,設定透視觀察體*/
	glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);

    /*模型變換*/
    glMatrixMode(GL_MODELVIEW);
    
    winWidth = newWidth;
    winHeight = newHeight;
}
int main(int argc, char* argv[])
{
    GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);//分配記憶體
    GLint viewport[4] = { 0 };
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize( 400 , 400 );        //設定初始化視窗大小
    glutCreateWindow("三維觀察");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoopEvent();





    /*************以下為評測程式碼,與本次實驗內容無關,請勿修改**************/
    glReadBuffer(GL_FRONT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glGetIntegerv(GL_VIEWPORT, viewport);
    glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
    cv::Mat img;
    std::vector<cv::Mat> imgPlanes;
    img.create(400, 400, CV_8UC3);
    cv::split(img, imgPlanes);

    for (int i = 0; i < 400; i++) {
        unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
        unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
        unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
        for (int j = 0; j < 400; j++) {
            int k = 3 * (i * 400 + j);
            plane2Ptr[j] = pPixelData[k];
            plane1Ptr[j] = pPixelData[k + 1];
            plane0Ptr[j] = pPixelData[k + 2];
        }
    }
    cv::merge(imgPlanes, img);
    cv::flip(img, img, 0);
    cv::namedWindow("openglGrab");
    cv::imshow("openglGrab", img);
    //cv::waitKey();
    cv::imwrite("../img_step1/test.jpg", img);
    return 0;
}

視點改為(1.0,1.5,8.0),觀察中心改為在(0, 0 ,0),向上向量改為(0, 1, 0);

實驗二:code;

// 提示:在合適的地方修改或新增程式碼
#include <GL/freeglut.h>
#include<stdio.h>

// 評測程式碼所用標頭檔案-開始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 評測程式碼所用標頭檔案-結束

GLint winWidth = 400, winHeight =400 ; 	      //設定初始化視窗大小

/*觀察座標系引數設定*/
GLfloat x0 = 1.0, yy = 1.5, z0 = 8.0;	   //設定觀察座標系原點 
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;	//設定觀察座標系參考點(視點) 
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;	   //設定觀察座標系向上向量(y軸) 

/*觀察體引數設定 */
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;//設定裁剪視窗座標範圍
GLfloat dnear = 1.5, dfar = 20.0;	      //設定遠、近裁剪面深度範圍

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    /*觀察變換*/
    gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);        //指定三維觀察引數

    // 請在此新增你的程式碼
    /********** Begin ********/

    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glLineWidth(2.0);
    glTranslatef(2.0f,0.0f,0.0f);
    glutWireCube(1.0);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    glLineWidth(2.0);
    glTranslatef(-2.0f,0.0f,0.0f);
    glutSolidCube(1.0);
    glPopMatrix();
    /********** End **********/
    glFlush();
}

void reshape(GLint newWidth, GLint newHeight)
{
    /*視口變換*/
    glViewport(0, 0, newWidth, newHeight);	//定義視口大小

    /*投影變換*/
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    /*透視投影,設定透視觀察體*/
	 gluPerspective( 45, 1, 1, 100);

    /*模型變換*/
    glMatrixMode(GL_MODELVIEW);
    
    winWidth = newWidth;
    winHeight = newHeight;
}
int main(int argc, char* argv[])
{
    GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);//分配記憶體
    GLint viewport[4] = { 0 };
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize( 400 , 400 );        //設定初始化視窗大小
    glutCreateWindow("三維觀察");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoopEvent();





    /*************以下為評測程式碼,與本次實驗內容無關,請勿修改**************/
    glReadBuffer(GL_FRONT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glGetIntegerv(GL_VIEWPORT, viewport);
    glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
    cv::Mat img;
    std::vector<cv::Mat> imgPlanes;
    img.create(400, 400, CV_8UC3);
    cv::split(img, imgPlanes);

    for (int i = 0; i < 400; i++) {
        unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
        unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
        unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
        for (int j = 0; j < 400; j++) {
            int k = 3 * (i * 400 + j);
            plane2Ptr[j] = pPixelData[k];
            plane1Ptr[j] = pPixelData[k + 1];
            plane0Ptr[j] = pPixelData[k + 2];
        }
    }
    cv::merge(imgPlanes, img);
    cv::flip(img, img, 0);
    cv::namedWindow("openglGrab");
    cv::imshow("openglGrab", img);
    //cv::waitKey();
    cv::imwrite("../img_step2/test.jpg", img);
    return 0;
}

實驗三:

code:

// 提示:在合適的地方修改或新增程式碼
#include <GL/freeglut.h>
#include<stdio.h>

// 評測程式碼所用標頭檔案-開始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 評測程式碼所用標頭檔案-結束

GLint winWidth = 400, winHeight =400 ; 	      //設定初始化視窗大小

/*觀察座標系引數設定*/
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;	   //設定觀察座標系原點 
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;	//設定觀察座標系參考點(視點) 
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;	   //設定觀察座標系向上向量(y軸) 

/*觀察體引數設定 */
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;//設定裁剪視窗座標範圍
GLfloat dnear = 1.5, dfar = 20.0;	      //設定遠、近裁剪面深度範圍

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    /*觀察變換*/
    gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);        //指定三維觀察引數

    // 請在此新增你的程式碼
    /********** Begin ********/
    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glLineWidth(2.0);
    glTranslatef(2.0f,0.0f,0.0f);
    glRotatef(-30.0f,1.0f,0.0f,0.0f);
    glutWireCube(1.0);
    
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    // glLineWidth(2.0);
    glTranslatef(-2.0f,0.0f,0.0f);
    glutSolidCube(1.0);
    glPopMatrix();

    /********** End **********/
    glFlush();
}

void reshape(GLint newWidth, GLint newHeight)
{
    /*視口變換*/
    glViewport(0, 0, newWidth, newHeight);	//定義視口大小

    /*投影變換*/
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    /*透視投影,設定透視觀察體*/
	glFrustum(xwMin, xwMax, ywMin, ywMax, dnear, dfar);

    /*模型變換*/
    glMatrixMode(GL_MODELVIEW);
    
    winWidth = newWidth;
    winHeight = newHeight;
}
int main(int argc, char* argv[])
{
    GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);//分配記憶體
    GLint viewport[4] = { 0 };
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize( 400 , 400 );        //設定初始化視窗大小
    glutCreateWindow("三維觀察");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoopEvent();





    /*************以下為評測程式碼,與本次實驗內容無關,請勿修改**************/
    glReadBuffer(GL_FRONT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glGetIntegerv(GL_VIEWPORT, viewport);
    glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
    cv::Mat img;
    std::vector<cv::Mat> imgPlanes;
    img.create(400, 400, CV_8UC3);
    cv::split(img, imgPlanes);

    for (int i = 0; i < 400; i++) {
        unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
        unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
        unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
        for (int j = 0; j < 400; j++) {
            int k = 3 * (i * 400 + j);
            plane2Ptr[j] = pPixelData[k];
            plane1Ptr[j] = pPixelData[k + 1];
            plane0Ptr[j] = pPixelData[k + 2];
        }
    }
    cv::merge(imgPlanes, img);
    cv::flip(img, img, 0);
    cv::namedWindow("openglGrab");
    cv::imshow("openglGrab", img);
    //cv::waitKey();
    cv::imwrite("../img_step4/test.jpg", img);
    return 0;
}

實驗四:

code:

// 提示:在合適的地方修改或新增程式碼
#include <GL/freeglut.h>
#include<stdio.h>

// 評測程式碼所用標頭檔案-開始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 評測程式碼所用標頭檔案-結束

GLint winWidth = 400, winHeight =400 ; 	      //設定初始化視窗大小

/*觀察座標系引數設定*/
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;	   //設定觀察座標系原點 
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;	//設定觀察座標系參考點(視點) 
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;	   //設定觀察座標系向上向量(y軸) 

/*觀察體引數設定 */
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;//設定裁剪視窗座標範圍
GLfloat dnear = 1.5, dfar = 20.0;	      //設定遠、近裁剪面深度範圍

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    /*觀察變換*/
    gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);        //指定三維觀察引數

    // 請在此新增你的程式碼
    /********** Begin ********/

      glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glLineWidth(2.0);
    glTranslatef(2.0f,0.0f,0.0f);
    // glRotatef(-30.0f,1.0f,0.0f,0.0f);
    glutWireCube(1.0);
    
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    // glLineWidth(2.0);
    glTranslatef(-2.0f,0.0f,0.0f);
    glutSolidCube(1.0);
    glPopMatrix();

    /********** End **********/
    glFlush();
}

void reshape(GLint newWidth, GLint newHeight)
{
    /*視口變換*/
    glViewport(0, 0, newWidth, newHeight);	//定義視口大小

    /*投影變換*/
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    /*平行投影*/
    glOrtho(-3.0 ,3.0 ,-3.0 ,3.0 ,-100.0 ,100.0 );

    /*模型變換*/
    glMatrixMode(GL_MODELVIEW);
    
    winWidth = newWidth;
    winHeight = newHeight;
}
int main(int argc, char* argv[])
{
    GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);//分配記憶體
    GLint viewport[4] = { 0 };
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize( 400 , 400 );        //設定初始化視窗大小
    glutCreateWindow("三維觀察");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoopEvent();





    /*************以下為評測程式碼,與本次實驗內容無關,請勿修改**************/
    glReadBuffer(GL_FRONT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glGetIntegerv(GL_VIEWPORT, viewport);
    glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
    cv::Mat img;
    std::vector<cv::Mat> imgPlanes;
    img.create(400, 400, CV_8UC3);
    cv::split(img, imgPlanes);

    for (int i = 0; i < 400; i++) {
        unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
        unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
        unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
        for (int j = 0; j < 400; j++) {
            int k = 3 * (i * 400 + j);
            plane2Ptr[j] = pPixelData[k];
            plane1Ptr[j] = pPixelData[k + 1];
            plane0Ptr[j] = pPixelData[k + 2];
        }
    }
    cv::merge(imgPlanes, img);
    cv::flip(img, img, 0);
    cv::namedWindow("openglGrab");
    cv::imshow("openglGrab", img);
    //cv::waitKey();
    cv::imwrite("../img_step3/test.jpg", img);
    return 0;
}

實驗五:

code;

// 提示:在合適的地方修改或新增程式碼
#include <GL/freeglut.h>
#include<stdio.h>

// 評測程式碼所用標頭檔案-開始
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/imgproc/imgproc.hpp>
// 評測程式碼所用標頭檔案-結束

GLint winWidth = 800, winHeight = 400; 	      //設定初始化視窗大小

/*觀察座標系引數設定*/
GLfloat x0 = 0.0, yy = 0.0, z0 = 5.0;	   //設定觀察座標系原點 
GLfloat xref = 0.0, yref = 0.0, zref = 0.0;	//設定觀察座標系參考點(視點) 
GLfloat Vx = 0.0, Vy = 1.0, Vz = 0.0;	   //設定觀察座標系向上向量(y軸) 

/*觀察體引數設定 */
GLfloat xwMin = -1.0, ywMin = -1.0, xwMax = 1.0, ywMax = 1.0;//設定裁剪視窗座標範圍
GLfloat dnear = 1.5, dfar = 20.0;	      //設定遠、近裁剪面深度範圍

void init(void)
{
    glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glLoadIdentity();
    /*觀察變換*/
    gluLookAt(x0, yy, z0, xref, yref, zref, Vx, Vy, Vz);        //指定三維觀察引數

    // 請在此新增你的程式碼
    /********** Begin ********/

    glPushMatrix();
    glColor3f(1.0, 0.0, 0.0);
    glutWireCube(1.0);
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 1.0, 0.0);
    glLineWidth(2.0);
    glTranslatef(2.0f,0.0f,0.0f);
    // glRotatef(-30.0f,1.0f,0.0f,0.0f);
    glutWireCube(1.0);
    
    glPopMatrix();

    glPushMatrix();
    glColor3f(0.0, 0.0, 1.0);
    // glLineWidth(2.0);
    glTranslatef(-2.0f,0.0f,0.0f);
    glutSolidCube(1.0);
    glPopMatrix();





    /********** End **********/
    glFlush();
}

void reshape(GLint newWidth, GLint newHeight)
{
    /*視口變換*/
    glViewport(0, 0, newWidth, newHeight);	//定義視口大小

    /*投影變換*/
    glMatrixMode(GL_PROJECTION);

    glLoadIdentity();

    /*透視投影,設定透視觀察體*/
    gluPerspective( 45, 2, 1, 100);

    /*模型變換*/
    glMatrixMode(GL_MODELVIEW);
    
    winWidth = newWidth;
    winHeight = newHeight;
}
int main(int argc, char* argv[])
{
    GLubyte* pPixelData = (GLubyte*)malloc(800 * 400 * 3);//分配記憶體
    GLint viewport[4] = { 0 };
    glutInit(&argc, argv);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize( winWidth , winHeight );        //設定初始化視窗大小
    glutCreateWindow("三維觀察");
    init();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoopEvent();





    /*************以下為評測程式碼,與本次實驗內容無關,請勿修改**************/
    glReadBuffer(GL_FRONT);
    glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
    glGetIntegerv(GL_VIEWPORT, viewport);
    glReadPixels(viewport[0], viewport[1], viewport[2], viewport[3], GL_RGB, GL_UNSIGNED_BYTE, pPixelData);
    cv::Mat img;
    std::vector<cv::Mat> imgPlanes;
    img.create(400, 800, CV_8UC3);
    cv::split(img, imgPlanes);

    for (int i = 0; i < 400; i++) {
        unsigned char* plane0Ptr = imgPlanes[0].ptr<unsigned char>(i);
        unsigned char* plane1Ptr = imgPlanes[1].ptr<unsigned char>(i);
        unsigned char* plane2Ptr = imgPlanes[2].ptr<unsigned char>(i);
        for (int j = 0; j < 800; j++) {
            int k = 3 * (i * 800 + j);
            plane2Ptr[j] = pPixelData[k];
            plane1Ptr[j] = pPixelData[k + 1];
            plane0Ptr[j] = pPixelData[k + 2];
        }
    }
    cv::merge(imgPlanes, img);
    cv::flip(img, img, 0);
    cv::namedWindow("openglGrab");
    cv::imshow("openglGrab", img);
    //cv::waitKey();
    cv::imwrite("../img_step5/test.jpg", img);
    return 0;
}