在OpenGL中实现Extrude 造型

在Opengl中实现Extrude功能,通过glu库实现,当前问题在启用材质的存在缺陷。继续努力中

<code begin>

/* Text3d by Robert J. Doyle, Jr., Naval Research Laboratory, Washington, DC. */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

#include <GL/glut.h>
 GLfloat dRotX = 0;
typedef enum {RESERVED, M_SIDE, M_EDGE, M_WHOLE, O_SIDE, O_EDGE, O_WHOLE,
T_SIDE, T_EDGE, T_WHOLE, H_SIDE, H_EDGE, H_WHOLE,REPEAT_SIDE, REPEAT_EDGE, REPEAT1,
REPEAT2_SIDE, REPEAT2_EDGE,REPEAT2, REPEAT3_SIDE, REPEAT3_EDGE,REPEAT3,
REPEAT4_SIDE, REPEAT4_EDGE,REPEAT4} displayLists;

GLfloat sideColor[] = {1.0, 0.0, 0.0, 1.0};
GLfloat edgeColor[] = {1.0, 0.0, 0.0, 1.0};
GLfloat shininess[] = {128.0};
GLfloat mat_specular[] = {1.0, 0.0, 0.0, 1.0};

GLfloat dHeight = 0.0;

GLfloat ProfileM[][3] =
{
    {-3.125000, 0.000000, 0.000000},
    {-3.125000, 6.208000, 0.000000},
    {-1.233000, 6.208000, 0.000000},
    {0.003000, 1.484000, 0.000000},
    {1.223000, 6.208000, 0.000000},
    {3.123000, 6.208000, 0.000000},
    {3.123000, 0.000000, 0.000000},
    {1.923000, 0.000000, 0.000000},
    {1.923000, 5.010000, 0.000000},
    {0.659000, 0.000000, 0.000000},
    {-0.649000, 0.000000, 0.000000},
    {-1.925000, 5.010000, 0.000000},
    {-1.925000, 0.000000, 0.000000}

};

GLfloat ProfileT[][3] =
{
 
 {0.0000, 0.0000,0.0000  },
  
 {10.0000, 0.0000, 0.0000  },
 
 {10.0000, 2.0000, 0.0000 },
 
 {6.0000, 2.0000, 0.0000 },
 
 {6.0000, 10.0000, 0.0000 },
 
 {10.0000, 10.0000, 0.0000 },
 
 {10.0000, 12.0000, 0.0000 },
 
 {0.0000, 12.0000, 0.0000 },
 
 {0.0000, 10.0000, 0.0000 },
 
 {4.0000, 10.0000, 0.0000 },
 
 {4.0000, 2.0000, 0.0000},
 
 {0.0000, 2.0000, 0.0000},
 
 {0.0000, 0.0000, 0.0000}
};
/*  Initialize light source and lighting.
 */


static void checkErrors(void)
{
  GLenum error;
  while ((error = glGetError()) != GL_NO_ERROR) {
    fprintf(stderr, "Error: %s/n", (char *) gluErrorString(error));
  }
}

void myinit(void)
{
    GLfloat light_ambient[] = { 1.0, 0.0, 0.0, 1.0 };
    GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
    GLfloat light_specular[] = { 1.0,  0.0, 0.0, 1.0 };
/* light_position is NOT default value */
    GLfloat light_position[] = { 10.0, 10.0, 0.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 */

}


/* Mark Kilgard's tessellation code from the "dino" demos. */
void 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_BEGIN, glBegin);
  gluTessCallback(tobj, GLU_VERTEX, glVertex3fv);
  gluTessCallback(tobj, GLU_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();
}

void Draw(GLfloat pt[][3])
{
 //nLen µÄÖµÐèÒª¼ÆËã
 int nLen = 13;//sizeof(pt)/(3*sizeof(GLfloat));
 int i = 0;
 GLfloat x0,y0,z0;
 glPointSize(2.0);

 x0 = pt[0][0];
 y0 = pt[0][1];
 z0 = pt[0][2];
 glColor3f(0,1,0);
 glBegin(GL_LINE_LOOP);//GL_LINES

 for(i = 0;i<nLen;i++)
 {
  glVertex3d(x0,y0,z0);

  glVertex3d(pt[i][0],pt[i][1],pt[i][2]);
  x0 = pt[i][0];
  y0 = pt[i][1];
  z0 = pt[i][2];
 }
 glEnd();
}

void display(void)
{
 //GLfloat xPos = -0.34;
 glLoadIdentity();
 glClearColor(1,1,1,1);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
 glTranslatef(0.0, 0.0, -0.0);
 glRotatef(dRotX,0.0,1.0,0.0);
 glRotatef(dRotX,1.0,0.0,0.0);
 glRotatef(45.0,0.0,1.0,0.0);

 dHeight = 3.0;
 extrudeSolidFromPolygon(ProfileM, sizeof(ProfileM), dHeight, M_SIDE,
  M_EDGE, M_WHOLE);
 glCallList(M_WHOLE);
 
 glTranslatef(-2.0,5.0,0.0);
 //glRotatef(45.0,0.0,1.0,0.0);
 
 glColor3f(1.0,0.0,0.0);
 Draw(ProfileM);

 glutSwapBuffers();
 glFlush();

}


void myReshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  //glFrustum(-7.0, 7.0, -7.0, 7.0, 6.0, 20.0);
    if (w <= h)
 glOrtho (-7.0, 7.0, -7.0*(GLfloat)h/(GLfloat)w,
     7.0*(GLfloat)h/(GLfloat)w, -20.0, 20.0);
    else
 glOrtho (-7.0*(GLfloat)w/(GLfloat)h,
     7.0*(GLfloat)w/(GLfloat)h, -7.0, 7.0, -20.0, 20.0); /**/
    glMatrixMode(GL_MODELVIEW);
}

void motionCB(int x, int y)
{
 dRotX = (GLfloat) x ;
 printf("the angle is &d/n",x);
 glutPostRedisplay();
}
/*  Main Loop
 *  Open window with initial window size, title bar,
 *  RGBA display mode, and handle input events.
 */
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutCreateWindow ("text3d");

/*glCullFace(GL_FRONT);*/
/*glEnable(GL_CULL_FACE);*/

    myinit();
    glutReshapeFunc (myReshape);
    glutDisplayFunc (display);
 glutMotionFunc(motionCB);
 
    glutMainLoop();
    return 0;             /* ANSI C requires main to return int. */
}

<code end>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值