objloaderview.cpp
来自「用VC++实现OBJ文件读取,实现了强大的功能.」· C++ 代码 · 共 561 行 · 第 1/2 页
CPP
561 行
0.0, 0.0, -1.0);
setView();
glDrawBuffer(GL_BACK);
GLbitfield clearBits = GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT;
glClear(clearBits);
model->pointerMotion = 0;
register int i, j;
register int nextIndex;
register int currColor;
GLfloat lightpos[3];
GLfloat black4f[] = { 0.0f, 0.0f, 0.0f, 0.0f };
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glScaled(model->scaleObj, model->scaleObj, model->scaleObj);
if (model->needToUpdateViewMat)
{
glPushMatrix();
glLoadIdentity();
glRotated(model->rotX * 0.2, 0.0, 0.0, -1.0);
glMultMatrixd(model->rotMat);
glGetDoublev(GL_MODELVIEW_MATRIX, model->rotMat);
glPopMatrix();
model->needToUpdateViewMat = 0;
}
glMultMatrixd(model->rotMat);
lightpos[0] = (pos1[0] * model->rotMat[0]) + (pos1[2] * model->rotMat[1]);
lightpos[1] = pos1[1];
lightpos[2] = (pos1[0] * model->rotMat[4]) + (pos1[2] * model->rotMat[5]);
glRotated(rotAngle, rotAxis[0], rotAxis[1], rotAxis[2]);
//绘制对象
i = 0;
j = 0;
nextIndex = model->colorList[j].index;
if( m_objloaded )
{
if (drawSetting < LINE_INDEPENDENT)
{
if (model->haveNormals)
{
do
{
if (i == nextIndex)
{
setColor(&model->colorList[j].ra, &model->colorList[j].rd,
&model->colorList[j].rs, model->colorList[j].spec, 0);
j++;
nextIndex = model->colorList[j].index;
}
if (drawSetting <= LINE_POLYGON)
glBegin(GL_POLYGON);
else
glBegin(GL_LINE_LOOP);
glNormal3fv(&model->vertexList[i].nx);
glVertex3fv(&model->vertexList[i++].x);
while (model->vertexList[i].draw)
{
glNormal3fv(&model->vertexList[i].nx);
glVertex3fv(&model->vertexList[i++].x);
}
glEnd();
} while (i < model->vertexCount);
}
else do
{
if (i == nextIndex)
{
setColor(&model->colorList[j].ra, &model->colorList[j].rd,
&model->colorList[j].rs, model->colorList[j].spec, 0);
j++;
nextIndex = model->colorList[j].index;
}
if (drawSetting <= LINE_POLYGON)
glBegin(GL_POLYGON);
else
glBegin(GL_LINE_LOOP);
glVertex3fv(&model->vertexList[i++].x);
while (model->vertexList[i].draw)
{
glVertex3fv(&model->vertexList[i++].x);
}
glEnd();
}
while (i < model->vertexCount);
}
else
{
currColor = -1;
if (drawSetting == LINE_INDEPENDENT)
{
glBegin(GL_LINES);
do
{
if (model->lineList[i].colorIndex != currColor)
{
currColor = model->lineList[i].colorIndex;
setColor(&model->colorList[currColor].ra,
&model->colorList[currColor].rd,
&model->colorList[currColor].rs,
model->colorList[currColor].spec, 0);
}
glVertex3fv(&model->lineList[i++].x);
glVertex3fv(&model->lineList[i++].x);
}
while (i < model->lineCount);
glEnd();
}
if (drawSetting == LINE_STRIPS)
do
{
glBegin(GL_LINE_STRIP);
if (model->lineStripList[i].colorIndex != currColor)
{
currColor = model->lineStripList[i].colorIndex;
setColor(&model->colorList[currColor].ra,
&model->colorList[currColor].rd,
&model->colorList[currColor].rs,
model->colorList[currColor].spec, 0);
}
glVertex3fv(&model->lineStripList[i++].x);
while (model->lineStripList[i].draw)
{
glVertex3fv(&model->lineStripList[i++].x);
}
glEnd();
}
while (i < model->lineStripCount);
}
}
::SwapBuffers(m_pDC->GetSafeHdc()); //交互缓冲区
return TRUE;
}
void COBJLoaderView::setColor(const GLfloat *ambient,
const GLfloat *diffuse,
const GLfloat *specular,
GLfloat shininess,
GLboolean stereo)
{
GLfloat c;
GLfloat ca[4];
if (!stereo) {
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
glColor4fv(diffuse);
}
else {
c = 0.27 * ambient[0] + 0.59 * ambient[1] + 0.14 * ambient[2];
ca[0] = ca[1] = ca[2] = c;
glMaterialfv(GL_FRONT, GL_AMBIENT, ca);
ca[0] = ca[1] = ca[2] = c;
ca[3] = diffuse[3];
c = 0.27 * diffuse[0] + 0.59 * diffuse[1] + 0.14 * diffuse[2];
glMaterialfv(GL_FRONT, GL_DIFFUSE, ca);
glColor4f(c, c, c, diffuse[3]);
ca[0] = ca[1] = ca[2] = c;
c = 0.27 * specular[0] + 0.59 * specular[1] + 0.14 * specular[2];
glMaterialfv(GL_FRONT, GL_SPECULAR, ca);
glMaterialf(GL_FRONT, GL_SHININESS, shininess);
}
} /* End of setColor */
void COBJLoaderView::Init(GLvoid)
{
m_objloaded = false;
//分配模型的存储空间
model = (ModelContext *)calloc(1, sizeof(ModelContext));
if (model == NULL)
{
MessageBox("Not enough memory count be allocated for model data","错误",MB_OK);
exit (-1);
}
//初始化模型数据
model->scaleObj = 5.0;
model->needToUpdateViewMat = 0;
model->pointerMotion = 0;
model->windowWidth = 640;
model->windowHeight = 480;
model->minimumScale = MINIMUM_SCALE;
model->triangleFlag = 0;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glGetDoublev(GL_MODELVIEW_MATRIX, model->rotMat); /* Initial rotation */
glClearColor(0.2f, 0.4f, 0.7f, 1.0f);
glDisable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glDisable(GL_LINE_SMOOTH);
glDisable(GL_POINT_SMOOTH);
glDisable(GL_BLEND);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
setAlternateLights(1);
}
////////////////////////////////////////////////////////////////////////////////////
//设置合适的光源
void COBJLoaderView::setAlternateLights(int lights)
{
if (lights == 0) {
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
return;
}
else {
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light1);
glLightfv(GL_LIGHT0, GL_SPECULAR, light1);
glLightfv(GL_LIGHT0, GL_POSITION, pos1);
setColor(ambient, diffuse, specular, 50.0, 0);
glPopMatrix();
}
}
////////////////////////////////////////////////////////////////////////////////////
//设置合适的视点
void COBJLoaderView::setView(void)
{
GLdouble distanceAdjust;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
distanceAdjust = FRONT_PLANE / EYE_BACK;
distanceAdjust /= PIXELS_PER_INCH / FRONT_PLANE;
glFrustum(-model->windowWidth * distanceAdjust, model->windowWidth * distanceAdjust,
-model->windowHeight * distanceAdjust, model->windowHeight * distanceAdjust,
FRONT_PLANE, 1000.0);
}
BOOL COBJLoaderView::OpenFile(LPCTSTR lpszPathName)
{
char* file = new char[strlen(lpszPathName)];
strcpy(file, lpszPathName);
m_objRead->readObjData(file);
m_objloaded = TRUE;
return true;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?