OpenGL combat tutorial (13) - Twelfth Station: OBJ model library is loaded using the Assmip

1, Obj model data format

obj model file is a text file that consists of the following components:

; Model description file with a map, map files MTL file description information, including file name
mtllib xxx.mtl

; Apex coordinate data
V 1.000000 -1.000000 -1.000000
V 1.000000 -1.000000 1.000000
V -1.000000 -1.000000 1.000000
...
; texture coordinates defined
VT .748573 0.750412
VT 0.749279 0.501284
VT .501077 0.999110
...
; vertex normal vector
VN 0.000000 0.000000 -1.000000
VN -1.000000 -0.000000 -0.000000
VN -0.000000 -0.000000 1.000000
...

; Surface data: 1/2/8 surface using this format, represents the position of the index vector vertex / texture coordinates / method, where the front with the index v, vt, vn Note that data defined here is the index Obj 1 starting from instead 0
f 5/1/1 1/2/1 4/3/1
f 5/1/1 4/3/1 8/4/1
f 3/5/2 7/6/2 8/7 /2

2, source code demonstrate


#include "Model.h"
Model ourModel("model/nanosuit.obj");
GLint Step12_xAngle = 0;
GLint Step12_yAngle = 0;
GLint Step12_zAngle = 0;
GLint Step12_fScale = 90;
GLint Step12_lid = 0;

GLfloat Step12_noLight[4] = { 0.0f, 0.0f, 0.0f, 1.0f };
GLfloat Step12_ambientLight[4] = { 0.3f, 0.3f, 0.3f, 1.0f };
GLfloat Step12_diffuseLight[4] = { 0.7f, 0.7f, 0.7f, 1.0f };
GLfloat Step12_brightLight[4] = { 1.0f, 0.5f, 0.5f, 1.0f };
GLfloat Step12_lightPos[] = { 5.0f, 5.0f, 5.0f, 1.0f };//光的位置在右上角

void Step12_Display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // 清除屏幕及深度缓存
	glClearColor(0, 0, 0, 0);

	glLoadIdentity(); // 重置模型观察矩阵

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_COLOR_MATERIAL);
	glShadeModel(GL_SMOOTH);


	//开启颜色追踪
	glEnable(GL_COLOR_MATERIAL);
	glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);


	//设置光照环境
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, Step12_noLight);
	glLightfv(GL_LIGHT0, GL_AMBIENT, Step12_ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, Step12_diffuseLight);
	glLightfv(GL_LIGHT0, GL_SPECULAR, Step12_brightLight);
	glLightfv(GL_LIGHT0, GL_POSITION, Step12_lightPos);

	glRotatef(Step12_xAngle, 1.0, 0.0, 0.0);
	glRotatef(Step12_yAngle, 0.0, 1.0, 0.0);
	glRotatef(Step12_zAngle, 0.0, 0.0, 1.0);
	glScalef(0.5, 0.5, 0.5);
	glColor3f(1, 1, 0.6);

	ourModel.Draw();

	glutSwapBuffers();
}

Operating results presentation:

Here Insert Picture Description
Here Insert Picture Description

3, download project files

In the Debug x86 download compile successfully.

Download the source files

Published 33 original articles · won praise 9 · views 1157

Guess you like

Origin blog.csdn.net/x879014419/article/details/105244604