📄 text3dview.cpp
字号:
//add down
Init(); //初始化OpenGL
//add up
return 0;
}
void CText3DView::OnDestroy()
{
//add down
HGLRC hrc;
hrc = ::wglGetCurrentContext();
::wglMakeCurrent(NULL, NULL);
if (hrc)
::wglDeleteContext(hrc);
if (m_pDC)
delete m_pDC;
//add up
CView::OnDestroy();
}
BOOL CText3DView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
}
void CText3DView::OnSize(UINT nType, int cx, int cy)
{
CView::OnSize(nType, cx, cy);
glViewport(0, 0, cx,cy);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-7.0, 7.0, -7.0, 7.0, 6.0, 20.0);
glMatrixMode(GL_MODELVIEW);
}
//add down
void CText3DView::Init()
{
PIXELFORMATDESCRIPTOR pfd;
int n;
HGLRC hrc;
m_pDC = new CClientDC(this);
ASSERT(m_pDC != NULL);
if (!bSetupPixelFormat())
return;
n = ::GetPixelFormat(m_pDC->GetSafeHdc());
::DescribePixelFormat(m_pDC->GetSafeHdc(), n, sizeof(pfd), &pfd);
hrc = wglCreateContext(m_pDC->GetSafeHdc());
wglMakeCurrent(m_pDC->GetSafeHdc(), hrc);
int count1 = sizeof(letterM) / (3 * sizeof(GLfloat));
int count2 = sizeof(letterO) / (3 * sizeof(GLfloat));
int count3 = sizeof(letterT) / (3 * sizeof(GLfloat));
int count4 = sizeof(letterH) / (3 * sizeof(GLfloat));
int i;
GLfloat light_ambient[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
/* light_position is NOT default value */
GLfloat light_position[] = { -1.0, -1.0, 1.0, 0.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glDrawBuffer(GL_FRONT_AND_BACK);
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glClear(GL_ACCUM_BUFFER_BIT);
glDrawBuffer(GL_BACK);
/* Zero position of text */
for(i = 0; i < count1; i++) {
letterM[i][1] = letterM[i][1] - 3.175;
}
for(i = 0; i < count2; i++) {
letterO[i][1] = letterO[i][1] - 3.175;
}
for(i = 0; i < count3; i++) {
letterT[i][1] = letterT[i][1] - 3.175;
}
for(i = 0; i < count4; i++) {
letterH[i][1] = letterH[i][1] - 3.175;
}
}
BOOL CText3DView::bSetupPixelFormat()
{
static PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd
1, // version number
PFD_DRAW_TO_WINDOW | // support window
PFD_SUPPORT_OPENGL | // support OpenGL
PFD_DOUBLEBUFFER, // double buffered
PFD_TYPE_RGBA, // RGBA type
24, // 24-bit color depth
0, 0, 0, 0, 0, 0, // color bits ignored
0, // no alpha buffer
0, // shift bit ignored
0, // no accumulation buffer
0, 0, 0, 0, // accum bits ignored
32, // 32-bit z-buffer
0, // no stencil buffer
0, // no auxiliary buffer
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0 // layer masks ignored
};
int pixelformat;
if ( (pixelformat = ChoosePixelFormat(m_pDC->GetSafeHdc(), &pfd)) == 0 )
{
MessageBox("ChoosePixelFormat failed");
return FALSE;
}
if (SetPixelFormat(m_pDC->GetSafeHdc(), pixelformat, &pfd) == FALSE)
{
MessageBox("SetPixelFormat failed");
return FALSE;
}
return TRUE;
}
void CText3DView::DrawScene(void)
{
int i, j;
GLfloat xPos = -0.34;
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTranslatef(0.0, 0.0, -10.0);
extrudeSolidFromPolygon(letterM, sizeof(letterM), width2, REPEAT_SIDE,
REPEAT_EDGE, REPEAT1);
extrudeSolidFromPolygon(letterO, sizeof(letterO), width2, REPEAT2_SIDE,
REPEAT2_EDGE, REPEAT2);
extrudeSolidFromPolygon(letterT, sizeof(letterT), width2, REPEAT3_SIDE,
REPEAT3_EDGE, REPEAT3);
extrudeSolidFromPolygon(letterH, sizeof(letterH), width2, REPEAT4_SIDE,
REPEAT4_EDGE, REPEAT4);
for(j = 1; j < 5; j++){
width = 0.0;
checkErrors();
for(i = 0; i < 10; i++){
glPushMatrix();
repeat(j);
glPopMatrix();
glPushMatrix();
glRotatef(90.0, 0.0, 1.0, 0.0);
if(j == 1){
extrudeSolidFromPolygon(letterM, sizeof(letterM), width, M_SIDE,
M_EDGE, M_WHOLE);
glCallList(M_WHOLE);
}
if(j == 2){
extrudeSolidFromPolygon(letterO, sizeof(letterO), width, O_SIDE,
O_EDGE, O_WHOLE);
glCallList(O_WHOLE);
}
if(j == 3){
extrudeSolidFromPolygon(letterT, sizeof(letterT), width, T_SIDE,
T_EDGE, T_WHOLE);
glCallList(T_WHOLE);
}
if(j == 4){
extrudeSolidFromPolygon(letterH, sizeof(letterH), width, H_SIDE,
H_EDGE, H_WHOLE);
glCallList(H_WHOLE);
}
glFinish();
SwapBuffers(wglGetCurrentDC());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
width = width + 0.2;
glPopMatrix();
}
for(i = 0; i < 45 ; i++){
glPushMatrix();
repeat(j);
glPopMatrix();
glPushMatrix();
glRotatef(90.0 - (2.0 * i), 0.0, 1.0, 0.0);
if(j == 1){
glCallList(M_WHOLE);
}
if(j == 2){
glCallList(O_WHOLE);
}
if(j == 3){
glCallList(T_WHOLE);
}
if(j == 4){
glCallList(H_WHOLE);
}
glFinish();
SwapBuffers(wglGetCurrentDC());
glPopMatrix();
}
for(i = 1; i < 32 ; i++){
glPushMatrix();
repeat(j);
glPopMatrix();
glPushMatrix();
glTranslatef(i * xPos, i * 0.3, i * -0.3);
if(j == 1){
glCallList(M_WHOLE);
}
if(j == 2){
glCallList(O_WHOLE);
}
if(j == 3){
glCallList(T_WHOLE);
}
if(j == 4){
glCallList(H_WHOLE);
}
glFinish();
SwapBuffers(wglGetCurrentDC());
glPopMatrix();
}
if(j == 1){
xPos = xPos + 0.25;
}
else{
xPos = xPos + 0.21;
}
}
}
/* Mark Kilgard's tessellation code from the "dino" demos. */
void CText3DView::extrudeSolidFromPolygon(GLfloat data[][3], unsigned int dataSize,
GLdouble thickness, GLuint side, GLuint edge, GLuint whole)
{
GLdouble vertex[3], dx, dy, len;
int i, k;
int flag = 0;
int count = dataSize / (3 * sizeof(GLfloat));
static GLUtriangulatorObj *tobj = NULL;
if (tobj == NULL) {
tobj = gluNewTess();
gluTessCallback(tobj, GLU_TESS_BEGIN, glBegin);
gluTessCallback(tobj, GLU_TESS_VERTEX, glVertex3fv);
gluTessCallback(tobj, GLU_TESS_END, glEnd);
}
glNewList(side, GL_COMPILE);
glShadeModel(GL_SMOOTH);
gluBeginPolygon(tobj);
for(i = 0; i < count; i++) {
/* This detects a new contour from a large number placed in
the unused z coordinate of the vertex where the new contour
starts. See the coordinates for letterO, above. The coordinate
must be reset below for additional calls. */
if (data[i][2] > 1000.0) {
data[i][2] = 0.0;
flag = 1; k = i;
gluNextContour(tobj, GLU_INTERIOR);
}
vertex[0] = data[i][0];
vertex[1] = data[i][1];
vertex[2] = 0.0;
gluTessVertex(tobj, vertex, data[i]);
}
gluEndPolygon(tobj);
glEndList();
/* Reset coordinate for new calls. */
if (flag == 1) {
data[k][2] = 10000.0;
flag = 0;
}
glNewList(edge, GL_COMPILE);
glBegin(GL_QUAD_STRIP);
for(i = 0; i <= count; i++) {
glVertex3f(data[i % count][0], data[i % count][1], 0.0);
glVertex3f(data[i % count][0], data[i % count][1], thickness);
/* Normals */
dx = data[(i+ 1) % count][1] - data[i % count][1];
dy = data[i % count][0] - data[(i + 1) % count][0];
len = sqrt(dx * dx + dy * dy);
glNormal3f(dx / len, dy / len, 0.0);
}
glEnd();
glEndList();
glNewList(whole, GL_COMPILE);
glFrontFace(GL_CW);
glMaterialfv(GL_FRONT, GL_DIFFUSE, edgeColor);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glCallList(edge);
glNormal3f(0.0, 0.0, -1.0);
glCallList(side);
glPushMatrix();
glTranslatef(0.0, 0.0, thickness);
glFrontFace(GL_CCW);
glNormal3f(0.0, 0.0, 1.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, sideColor);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glCallList(side);
glPopMatrix();
glEndList();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -