opengl 画矩形,窗口不显示内容,改如何解决 10
代码如下:#include<GL/glut.h>#include<gl/GL.h>#include<string>#include<fstream>#include<ve...
代码如下:
#include<GL/glut.h>
#include<gl/GL.h>
#include<string>
#include<fstream>
#include<vector>
#include<io.h>
#include <sstream>
using namespace std;
struct v_xyz{ //定义了顶点的坐标
GLfloat x ;
GLfloat y ;
GLfloat z ;
v_xyz(GLfloat xtemp , GLfloat ytemp , GLfloat ztemp)
{
x = xtemp ;
y = ytemp ;
z = ztemp ;
}
};
struct f_poicom{
int f1 ;
int f2 ;
int f3 ;
f_poicom(int f1p , int f2p , int f3p){ //用于定义面是由哪三个顶点组成
f1 = f1p ;
f2 = f2p ;
f3 = f3p ;
}
};
vector<v_xyz> v_num; //记录单个点的位置
vector<f_poicom> fArr; //记录面片的顶点组合
void DrawGLScene(GLvoid){ //此处进行绘制
glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); //清楚屏幕和深度缓存
//glLoadIdentity(); //重置当前的模型观察矩阵
glScalef(0.1f , 0.1f , 0.1f);
glColor3f(1.0f, 0.0f, 0.0f);
gluLookAt (-2.0,2.0,2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
for (int i=0;i < fArr.size() ; i++)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
glVertex3f(v_num[fArr[i].f1-1].x, v_num[fArr[i].f1-1].y , v_num[fArr[i].f1-1].z);
glVertex3f(v_num[fArr[i].f2-1].x, v_num[fArr[i].f2-1].y , v_num[fArr[i].f2-1].z);
glVertex3f(v_num[fArr[i].f3-1].x, v_num[fArr[i].f3-1].y , v_num[fArr[i].f3-1].z);
glEnd();
}
glFlush();
}
void getData (vector<v_xyz> & v_num , vector<f_poicom> & fArr ,const string & Path){
ifstream in(Path);
string line ;
while(getline(in,line)){
//cout<<line<<endl;
if(line[0] == 'v'){
if(line[1] != 'n' && line[1] != 't'){
line = line.substr(2);
stringstream ss(line);
GLfloat x , y , z ;
ss>>x>>y>>z ;
//cout<<x<<" "<<y<<" "<<z<<endl;
v_num.push_back(v_xyz(x,y,z));
}
}
else if(line[0] == 'f'){
vector<string> temp ;
int place ; //储存空格的位置
line = line.substr(2) ;
for(int j = 0 ; j < 2 ; j++){
place = line.find(" ");
temp.push_back(line.substr(0,place));
line.erase(0 , place+1);
if(j == 1){
temp.push_back(line);
}
}
vector<int> placetag ; //记录下标
for(int i = 0 ; i < temp.size() ; i++){
stringstream ss2(temp[i]);
int p1 , p2 , p3 ;
ss2>>p1>>p2>>p3 ;
//cout<<p1<<" "<<p2<<" "<<p3<<endl;
placetag.push_back(p1);
}
fArr.push_back(f_poicom(placetag[0] , placetag[1] , placetag[2]));
}
}
}
int main(int argc , char * argv[]){
string path = "cube.obj" ;
getData(v_num , fArr , path);
glutInit(& argc , argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("Show");
glutDisplayFunc(&DrawGLScene);
glutMainLoop();
return 0 ;
} 展开
#include<GL/glut.h>
#include<gl/GL.h>
#include<string>
#include<fstream>
#include<vector>
#include<io.h>
#include <sstream>
using namespace std;
struct v_xyz{ //定义了顶点的坐标
GLfloat x ;
GLfloat y ;
GLfloat z ;
v_xyz(GLfloat xtemp , GLfloat ytemp , GLfloat ztemp)
{
x = xtemp ;
y = ytemp ;
z = ztemp ;
}
};
struct f_poicom{
int f1 ;
int f2 ;
int f3 ;
f_poicom(int f1p , int f2p , int f3p){ //用于定义面是由哪三个顶点组成
f1 = f1p ;
f2 = f2p ;
f3 = f3p ;
}
};
vector<v_xyz> v_num; //记录单个点的位置
vector<f_poicom> fArr; //记录面片的顶点组合
void DrawGLScene(GLvoid){ //此处进行绘制
glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); //清楚屏幕和深度缓存
//glLoadIdentity(); //重置当前的模型观察矩阵
glScalef(0.1f , 0.1f , 0.1f);
glColor3f(1.0f, 0.0f, 0.0f);
gluLookAt (-2.0,2.0,2.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
for (int i=0;i < fArr.size() ; i++)
{
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
glVertex3f(v_num[fArr[i].f1-1].x, v_num[fArr[i].f1-1].y , v_num[fArr[i].f1-1].z);
glVertex3f(v_num[fArr[i].f2-1].x, v_num[fArr[i].f2-1].y , v_num[fArr[i].f2-1].z);
glVertex3f(v_num[fArr[i].f3-1].x, v_num[fArr[i].f3-1].y , v_num[fArr[i].f3-1].z);
glEnd();
}
glFlush();
}
void getData (vector<v_xyz> & v_num , vector<f_poicom> & fArr ,const string & Path){
ifstream in(Path);
string line ;
while(getline(in,line)){
//cout<<line<<endl;
if(line[0] == 'v'){
if(line[1] != 'n' && line[1] != 't'){
line = line.substr(2);
stringstream ss(line);
GLfloat x , y , z ;
ss>>x>>y>>z ;
//cout<<x<<" "<<y<<" "<<z<<endl;
v_num.push_back(v_xyz(x,y,z));
}
}
else if(line[0] == 'f'){
vector<string> temp ;
int place ; //储存空格的位置
line = line.substr(2) ;
for(int j = 0 ; j < 2 ; j++){
place = line.find(" ");
temp.push_back(line.substr(0,place));
line.erase(0 , place+1);
if(j == 1){
temp.push_back(line);
}
}
vector<int> placetag ; //记录下标
for(int i = 0 ; i < temp.size() ; i++){
stringstream ss2(temp[i]);
int p1 , p2 , p3 ;
ss2>>p1>>p2>>p3 ;
//cout<<p1<<" "<<p2<<" "<<p3<<endl;
placetag.push_back(p1);
}
fArr.push_back(f_poicom(placetag[0] , placetag[1] , placetag[2]));
}
}
}
int main(int argc , char * argv[]){
string path = "cube.obj" ;
getData(v_num , fArr , path);
glutInit(& argc , argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("Show");
glutDisplayFunc(&DrawGLScene);
glutMainLoop();
return 0 ;
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励10(财富值+成长值)+提问者悬赏10(财富值+成长值)
1个回答
展开全部
你的数据文件呢,上传一下。
更多追问追答
追问
#
v -1 -1 -1
v -1 -1 1
v -1 1 -1
v -1 1 1
v 1 -1 -1
v 1 -1 1
v 1 1 -1
v 1 1 1
#
f 2 4 8
f 8 6 2
f 1 3 4
f 4 2 1
f 8 4 3
f 3 7 8
f 8 7 5
f 5 6 8
f 2 6 5
f 5 1 2
f 5 7 3
f 3 1 5
这是我的数据。麻烦你了。
追答
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询