#pragma once
#include "vector"
#include "iostream"
#include "string"
#include "fstream"
#include "sstream"
#include "algorithm"
#include "assert.h"
#include
#include
#include
#include
#include
#pragma comment(lib, "glut.lib")
using namespace std;
struct vec3
{
double x, y, z;
};
struct vec2
{
double x, y;
};
class Vertex
{
public:
int vertIndex;
int normIndex;
int textIndex;
};
class Face
{
public:
vector<Vertex> vertex;
Face(){
}
~Face(){
}
};
class Mesh
{
private:
vector<vec3> vVertex;
vector<vec2> vText;
vector<vec3> vNorm;
vector<Face> vFace;
public:
Mesh(){
};
~Mesh(){
};
bool readFile(char* path);
void drawMesh();
};
bool Mesh::readFile(char* path)
{
ifstream file(path);
if (!file)
{
cerr << "Error::ObjLoader, could not open obj file:"
<< path << " for reading." << std::endl;
return false;
}
else
{
qDebug() << "qweq";
}
string line;
while (getline(file, line))
{
if (line.substr(0, 2) == "vt")
{
istringstream s(line.substr(2));
vec2 v;
s >> v.x; s >> v.y;
v.y = -v.y;
vText.push_back(v);
}
else if (line.substr(0, 2) == "vn")
{
istringstream s(line.substr(2));
vec3 v;
s >> v.x; s >> v.y; s >> v.z;
vNorm.push_back(v);
}
else if (line.substr(0, 1) == "v")
{
istringstream s(line.substr(1));
vec3 v;
s >> v.x; s >> v.y; s >> v.z;
vVertex.push_back(v);
}
else if (line.substr(0, 1) == "f")
{
Face face;
istringstream vtns(line.substr(1));
string vtn;
while (vtns >> vtn)
{
Vertex vertex;
replace(vtn.begin(), vtn.end(), '/', ' ');
istringstream ivtn(vtn);
if (vtn.find(" ") != string::npos)
{
ivtn >> vertex.vertIndex
>> vertex.normIndex;
vertex.vertIndex--;
vertex.normIndex--;
}
else
{
ivtn >> vertex.vertIndex
>> vertex.textIndex
>> vertex.normIndex;
vertex.vertIndex--;
vertex.textIndex--;
vertex.normIndex--;
}
face.vertex.push_back(vertex);
}
vFace.push_back(face);
}
else if (line[0] == '#')
{
}
else
{
}
}
return true;
}
void Mesh::drawMesh()
{
if (vFace.empty())
return;
if (vText.size() > 0)
{
for (int f = 0; f < vFace.size(); f++)
{
int n = vFace[f].vertex.size();
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
for (int v = 0; v < n; v++)
{
int it = vFace[f].vertex[v].textIndex;
glTexCoord2f(vText[it].x, vText[it].y);
int in = vFace[f].vertex[v].normIndex;
glNormal3f(vNorm[in].x, vNorm[in].y, vNorm[in].z);
int iv = vFace[f].vertex[v].vertIndex;
glVertex3f(vVertex[iv].x, vVertex[iv].y, vVertex[iv].z);
}
glEnd();
glBegin(GL_LINE_LOOP);
glColor3f(1, 0, 0);
for (int v = 0; v < n; v++)
{
int it = vFace[f].vertex[v].textIndex;
glTexCoord2f(vText[it].x, vText[it].y);
int in = vFace[f].vertex[v].normIndex;
glNormal3f(vNorm[in].x, vNorm[in].y, vNorm[in].z);
int iv = vFace[f].vertex[v].vertIndex;
glVertex3f(vVertex[iv].x, vVertex[iv].y, vVertex[iv].z);
}
glEnd();
}
}
else
{
for (int f = 0; f < vFace.size(); f++)
{
int n = vFace[f].vertex.size();
glBegin(GL_TRIANGLES);
for (int v = 0; v < n; v++)
{
int in = vFace[f].vertex[v].normIndex;
glNormal3f(vNorm[in].x, vNorm[in].y, vNorm[in].z);
int iv = vFace[f].vertex[v].vertIndex;
glVertex3f(vVertex[iv].x, vVertex[iv].y, vVertex[iv].z);
}
glEnd();
}
}
}
#ifndef THIRDWINDOW_H
#define THIRDWINDOW_H
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "vector"
#include "assert.h"
#include
#include
class ThirdWindow : public QOpenGLWidget
{
Q_OBJECT
public:
ThirdWindow(QWidget *parent);
~ThirdWindow();
typedef struct b
{
GLdouble y;
GLdouble vy;
} Ball;
Ball ball;
GLdouble dt;
bool direction;
double G;
GLuint *tex0;
QString bmpfile;
QPoint m_last;
double dist, horizontal, vertical;
void initializeGL();
void paintGL();
void loadTexture(QString filepath, GLuint *texture);
void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
private:
};
#endif
#include "thirdwindow.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include
#include "mesh.h"
#pragma comment(lib, "glut32.lib")
const int windowWidth = 1080;
const int windowHeight = 1080;
Mesh mesh;
double PI = 3.14159265358979323846;
ThirdWindow::ThirdWindow(QWidget *parent)
: QOpenGLWidget(parent)
{
glViewport(0, 0, windowWidth, windowHeight);
dt = 0.02;
ball.y = 10;
ball.vy = 0;
direction = true;
G = 9.8;
bmpfile = "://1.jpg";
tex0 = new GLuint;
dist = 2.0;
vertical = 45.0;
horizontal = 45.0;
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(update()));
timer->start(10);
}
ThirdWindow::~ThirdWindow()
{
}
void ThirdWindow::initializeGL(){
setGeometry(2, 5, windowWidth, windowHeight);
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_AUTO_NORMAL);
glEnable(GL_NORMALIZE);
glMaterialf(GL_FRONT, GL_SHININESS, 64.0);
mesh.readFile("../JuneThirdOne/3.obj");
}
void ThirdWindow::paintGL(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0, 0, 0, 0);
glLoadIdentity();
glOrtho(-20, 20, -20, 20, -20, 10);
glDisable(GL_TEXTURE_2D);
glPushMatrix();
glTranslated(0, ball.y, 0.0);
mesh.drawMesh();
if (ball.vy<1 && ball.y<-2.0){
ball.vy = 0;
ball.y = -2.0;
G = 0;
}
if (direction){
G = 9.8;
if (ball.y - (ball.vy*dt + 0.5*G*dt*dt) <= -2){
direction = false;
G += 6.0;
}
ball.y = ball.y - (ball.vy*dt + 0.5*G*dt*dt);
ball.vy = ball.vy + G*dt;
}
else{
if (ball.y >-2 && ball.vy<0){
direction = true;
}
ball.y = ball.y + (ball.vy*dt - 0.5*G*dt*dt);
ball.vy = ball.vy - G*dt;
}
glPopMatrix();
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(10.0, -4.0, 0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(0.0, -4.0, -6.0);
glTexCoord2f(1.0, 1.0); glVertex3f(-6.0, -4.0, 0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(0.0, -4.0, 10.0);
glEnd();
glPopMatrix();
glFlush();
}
void ThirdWindow::loadTexture(QString filepath, GLuint *texture){
QImage tex, buf;
if (!buf.load(filepath))
{
qDebug() << "Error: failed to load image!";
exit(1);
}
tex = QGLWidget::convertToGLFormat(buf);
glGenTextures(1, texture);
glBindTexture(GL_TEXTURE_2D, *texture);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, tex.width(), tex.height(), GL_RGBA, GL_UNSIGNED_BYTE, tex.bits());
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
}
void ThirdWindow::mousePressEvent(QMouseEvent *e)
{
m_last = e->pos();
}
void ThirdWindow::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() & Qt::LeftButton)
{
QPoint m_now = e->pos();
float disx = m_now.x() - m_last.x();
float disy = m_now.y() - m_last.y();
horizontal -= disx / 25;
vertical += disy / 25;
m_last = m_now;
update();
}
}