OpenGL解析法绘制旋转曲面--青花瓷

    xiaoxiao2021-04-17  38

    相关背景

    代码展示

    Test.cpp #include "freeglut.h" #include "GlutWin.h" #include <math.h> #include <iostream> #pragma comment(lib,"freeglut.lib") #define PI 3.1415926 using namespace std; void myDisplay(void) { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); cout << "正在绘制。。" << endl; /*static double theta = 0; theta += 0.05; glColor3d(1, 0.5, 1); glRotated(theta, 0, 1, 0);*/ int iSeg = 0; int iFace = 0; GLfloat b = 1.0; GLfloat a = PI/2.0f; GLfloat y, x, z,r, rNext,xNext,zNext,L,LNext, theta; GLfloat dy = 0.5f; GLfloat yMax = 12.0f; glBegin(GL_TRIANGLE_STRIP); for (iSeg = 0; iSeg < 120; iSeg++)//y方向切割 { for (iFace = 0; iFace <= 64; iFace++)//切割面以扇形分割 { y = yMax * iSeg / 120.0; theta = 2 * PI*iFace / 64.0; r = cos(y + b) + a; //2.11109853 rNext = cos(y + dy + b) + a;// 1.64153349 x = r*cos(theta); //2.11109853 z = r*sin(theta); //0 xNext = rNext * cos(theta); //1.64153349 zNext = rNext * sin(theta); //0.000000000 L = sqrt( pow(x, 2.0) + pow(r*sin(y + b), 2.0) + pow(z, 2.0) ); //2.75906396 LNext = sqrt( pow(xNext, 2) + pow(rNext * sin(y + dy + b), 2.0) + pow(zNext, 2.0)); //2.31857324 glNormal3f(x / L, r*sin(y + b) / L, z / L); glVertex3f(x,y,z); glNormal3f(xNext/LNext,rNext*sin(y+dy+b)/LNext,zNext/LNext); glVertex3f(xNext,y+dy,zNext); } } glEnd(); glFlush(); glutSwapBuffers(); } void myReshape(GLsizei width, GLsizei height) { const float ar = (float)width / (GLfloat)height; if (height == 0) { height = 1; } //投影->视口变换 glViewport(0, 0, width, height); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑 glMatrixMode(GL_PROJECTION); glLoadIdentity(); #if 0 glOrtho(-1.0, 1.0, -1.0 / ar, 1.0 / ar, -1.0, 1.0); #else gluPerspective(45.0, ar, 0.1, 100.0); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); #endif // 透视投影、平行投影 glScalef(10.0, 10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); } static void myidle() { glutPostRedisplay(); } int main(int argc, char *argv[]) { GlutWin *win = NULL; win = new GlutWin(600, 800, 100, 100, GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH, "青花瓷"); glutDisplayFunc(myDisplay); glutReshapeFunc(myReshape); glutIdleFunc(myidle); glutMainLoop(); //glEnable(GL_DEPTH_TEST); //glDepthFunc(GL_LESS); //如果输入的深度值小于参考值,则通过 //glEnable(GL_LIGHTING); //glEnable(GL_LIGHT0); //glEnable(GL_COLOR_MATERIAL); //开启颜色追踪 delete win; return 0; } GlutWin.h #pragma once #include <windows.h> #include "freeglut.h" //Glut初始化 class GlutWin { public: GlutWin(int windowHeight, int windowWidth,int windowPosX, int windowPosY, unsigned int displayMode,const char * windowTitle); ~GlutWin() {}; private: const char * windowTitle; int windowHeight, windowWidth; int windowPosX, windowPosY; int windowID; unsigned int displayMode; bool fullScreen; }; Glutwin.cpp #include "GlutWin.h" GlutWin::GlutWin(int windowHeight, int windowWidth,int windowPosX, int windowPosY, unsigned int displayMode,const char * windowTitle) { windowTitle = windowTitle; windowHeight = windowHeight; windowWidth = windowWidth; windowPosX = windowPosX; windowPosY = windowPosY; displayMode = displayMode; fullScreen = false; char cmd_line[8]; char * argv[1]; argv[0] = cmd_line; int argc = 1; glutInit(&argc, argv); glutInitWindowSize(windowWidth, windowHeight); glutInitWindowPosition(windowPosX, windowPosY); glutInitDisplayMode(displayMode); windowID = glutCreateWindow(windowTitle); const float ar = (float)windowWidth / (GLfloat)windowHeight; #if 0 glOrtho(-1.0, 1.0, -1.0 / ar, 1.0 / ar, -1.0, 1.0); #else gluPerspective(45.0, ar, 0.1, 100.0); gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); #endif // 透视投影、平行投影 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // 黑 glViewport(0, 0, windowWidth, windowHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glScalef(10.0, 10.0, 10.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity() ; }
    转载请注明原文地址: https://ju.6miu.com/read-673571.html

    最新回复(0)