|

楼主 |
发表于 2009-11-2 21:28:00
|
显示全部楼层
Re: 这个OBJ FILE如何用OPENGL绘制
我用的基本代码如下:
- #include <math.h>
- #include <stdlib.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <windows.h>
- #include <stdio.h>
- #include <data.h>
- #define false 0
- #define true 1
- int i;
- void myReshape(int w, int h)
- {
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-0.5,0.5,-0.5,0.5,-0.5,0.5);
- glMatrixMode(GL_MODELVIEW);
- }
- void colorcube(void)
- {
- static GLfloat position [] = {0.0,0.0,1.0,1.0};
- static GLfloat color [] = {0.5,0.25,0.0,1.0};
- glLightfv(GL_LIGHT0,GL_POSITION,position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
- glMaterialfv(GL_FRONT_AND_BACK,GL_AMBIENT_AND_DIFFUSE,color);
- glDrawElements(GL_TRIANGLES ,3*NUMTRIANGLES,GL_UNSIGNED_BYTE,index);
- }
- void display(void)
- {
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- colorcube();
- glutSwapBuffers();
- }
- void main(int argc, char **argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(500, 500);
- glutCreateWindow("Mesh Grid");
- glutReshapeFunc(myReshape);
- glutDisplayFunc(display);
- glEnable(GL_DEPTH_TEST);
- glEnableClientState(GL_VERTEX_ARRAY);
- glEnableClientState(GL_INDEX_ARRAY);
- glEnableClientState(GL_COLOR_ARRAY);
- glVertexPointer(3,GL_FLOAT,0,vertex);
- glIndexPointer(GL_FLOAT,0,index);
- glColorPointer(3,GL_FLOAT,0,color);
- glutMainLoop();
- }
- data of (vertex color index ) in the objfile
复制代码 |
|