相关背景
代码展示
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;
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++)
{
for (iFace =
0; iFace <=
64; iFace++)
{
y = yMax * iSeg /
120.0;
theta =
2 * PI*iFace /
64.0;
r =
cos(y + b) + a;
rNext =
cos(y + dy + b) + a;
x = r*
cos(theta);
z = r*
sin(theta);
xNext = rNext *
cos(theta);
zNext = rNext *
sin(theta);
L =
sqrt(
pow(x,
2.0) +
pow(r*
sin(y + b),
2.0) +
pow(z,
2.0) );
LNext =
sqrt(
pow(xNext,
2) +
pow(rNext *
sin(y + dy + b),
2.0) +
pow(zNext,
2.0));
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();
delete win;
return 0;
}
GlutWin.h
#pragma once
#include <windows.h>
#include "freeglut.h"
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