2)其次,你需要声明一个类,这个类能将一个特定的模型文件中的所有信息读入内存,用一定的方式存储起来
3)一般一个模型被分解成若干个独立的“面”,存储在内存中。例如一个正方体模型,其实就是8个独立的“面”组成的。
4)一个“面”,有以下4方面的数据:顶点数组、法向量数组、纹理坐标数组、纹理图片。以一个最简单的三角形面来说,顶点数组中存储了3个点;法向量数组中存储了3个点处的法向量(这些法向量其实可以根据顶点数组计算出来,但opengl希望你计算好存储着,它直接画就不用临时计算了。法向量是用来做光照效果的);纹理坐标数组则是描述纹理图片是以什么样的方式(拉伸、重复、反转等等)贴在这个三角形上的;纹理图片则就是一个图片。
5)以上就把一个模型设置好了。你安排场景、位置、光照等等,然后把这个模型画出来就好了。
可以通过红宝书学习opengl。
我自己学qt时的笔记,供参考
qt下构建opengl开发环境
首先在工程文件下加入环境配置 .pro
qt += qt3support //支持qt3支持
qt += opengl //opengl库支持
nehewidget.h文件
#include
#include
class nehewidget : public qglwidget //任何opengl的窗体都要从qglwidget类中派生
{
q_object //宏定义只有加入了q_object,你才能使用qt中的signal和slot机制
public:
nehewidget( qwidget* parent = 0, const char* name = 0, bool fs = false );
~nehewidget();
protected:
void initializegl(); //初始化窗口
void paintgl(); //画窗口
void resizegl( int width, int height ); //重置窗口
void keypressevent( qkeyevent *e ); //按钮事件
protected:
bool fullscreen; //全屏事件
};
源文件
nehewidget::nehewidget( qwidget* parent, const char* name, bool fs )
: qglwidget( parent, name ) //二个qglwidget 需要的构造参数
{
fullscreen = fs;
setgeometry( 0, 0, 640, 480 ); //设置大小
setcaption( "nehe's opengl framework" );
if ( fullscreen )
showfullscreen(); //设置全局
}
nehewidget::~nehewidget()
{
}
void nehewidget::initializegl()
{
glshademodel( gl_smooth );
glclearcolor( 0.0, 0.0, 0.0, 0.0 );
glcleardepth( 1.0 );
glenable( gl_depth_test );
gldepthfunc( gl_lequal );
glhint( gl_perspective_correction_hint, gl_nicest );
}
void nehewidget::paintgl()
{
glclear( gl_color_buffer_bit | gl_depth_buffer_bit );
glloadidentity();
}
void nehewidget::resizegl( int width, int height )
{
if ( height == 0 )
{
height = 1;
}
glviewport( 0, 0, (glint)width, (glint)height );
glmatrixmode( gl_projection );
glloadidentity();
gluperspective( 45.0, (glfloat)width/(glfloat)height, 0.1, 100.0 );
glmatrixmode( gl_modelview );
glloadidentity();
}
void nehewidget::keypressevent( qkeyevent *e )
{
switch ( e->key() )
{
case qt::key_f2:
fullscreen = !fullscreen;
if ( fullscreen )
{
showfullscreen();
}
else
{
shownormal();
setgeometry( 0, 0, 640, 480 );
}
updategl();
break;
case qt::key_escape:
close();
}
}
main.cpp
#include
#include
#include "nehewidget.h"
int main( int argc, char **argv )
{
bool fs = false;
qapplication a(argc,argv);
switch( qmessagebox::information( 0,
"start fullscreen?",
"would you like to run in fullscreen mode?",
qmessagebox::yes,
qmessagebox::no | qmessagebox::default ) )
{
case qmessagebox::yes:
fs = true;
break;
case qmessagebox::no:
fs = false;
break;
}
nehewidget w( 0, 0, fs );
a.setmainwidget( &w );
w.show();
return a.exec();
}
//qt中用opengl画图时,记得用update(),更新数据