그래픽스 스터디
2022. 7. 12. 18:36ㆍopengl
728x90
#include "Viewer.h"
#include "GL/freeglut.h"
#include <fstream>
#include <QKeyEvent>
#include <iostream>
#include <sstream>
#include <fstream>
void pares(const std::string& str, std::vector<std::string>& values, std::string& delimiter) {
std::string::size_type Fpos = str.find_first_not_of(delimiter, 0);
std::string::size_type Lpos = str.find_first_of(delimiter, Fpos);
while (std::string::npos != Fpos || std::string::npos != Lpos) {
values.push_back(str.substr(Fpos, Lpos - Fpos));
Fpos = str.find_first_not_of(delimiter, Lpos);
Lpos = str.find_first_of(delimiter, Fpos);
}
}
Viewer::Viewer(QWidget* parent)
{
setParent(parent);
}
void Viewer::initializeGL()
{
GLfloat ambientLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat diffuseLight[] = { 0.9f, 0.9f, 0.9f, 1.0f };
GLfloat lightPos[] = { -100.0f, 130.0f, 150.0f, 1.0f };
GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CCW);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glEnable(GL_LIGHT0);
camera()->setSceneRadius(1.0);
LoadFils("C:\\Users\\syino\\Desktop\\LightTest\\data\\bunny_stanford.obj");
LoadObjFiles("C:\\Users\\syino\\Desktop\\LightTest\\data\\bunny_stanford.obj");
query = { 0.1,0.1,0.1 };
}
void Viewer::draw()
{
printf("arrownum0 %d, arrowNum1 %d\n", arrowNum0, arrowNum1);
//DrawCube();
//Drawpts();
for (auto& f : mesh.face)
{
glBegin(GL_TRIANGLES);
for (int i = 0; i < 3; i++)
{
auto p = mesh.vertex[f.v[i]];
auto n = mesh.normal[f.vn[i]];
glNormal3d(n[0], n[1], n[2]);
glVertex3d(p[0], p[1], p[2]);
}
glEnd();
}
}
void Viewer::keyPressEvent(QKeyEvent* e)
{
int key = e->key();
if (key == Qt::Key_N)
{
isAnimated ^= true;
if (isAnimated)
startAnimation();
else
stopAnimation();
}
else if (key == Qt::Key_Up)
{
arrowNum0++;
}
else if (key == Qt::Key_Down)
{
if (arrowNum0 != 0) {
arrowNum0--;
}
}
else if (key == Qt::Key_Left)
{
arrowNum1--;
}
else if (key == Qt::Key_Right)
{
arrowNum1++;
}
else
QGLViewerOrtho::keyPressEvent(e);
double theta = double(arrowNum0 % 360) / 360.0 * PI20;
query[0] = 0.1 * cos(theta);
query[2] = 0.1 * sin(theta);
updateGL();
}
void Viewer::mousePressEvent(QMouseEvent* e)
{
QGLViewerOrtho::mousePressEvent(e);
}
void Viewer::mouseMoveEvent(QMouseEvent* e)
{
QGLViewerOrtho::mouseMoveEvent(e);
}
void Viewer::mouseReleaseEvent(QMouseEvent* e)
{
QGLViewerOrtho::mouseReleaseEvent(e);
}
void Viewer::light0SpecularChanged(int value)
{
float val = value / 100.0;
GLfloat specular[4] = { val,val,val,1.0 };
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
updateGL();
}
void Viewer::light0AmbientChanged(int value)
{
float val = value / 100.0;
GLfloat ambient[4] = { val,val,val,1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
updateGL();
}
void Viewer::light0DiffuseChanged(int value)
{
float val = value / 100.0;
GLfloat diffuse[4] = { val,val,val,1.0 };
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
updateGL();
}
void Viewer::DrawCube()
{
/*glBegin(GL_QUADS);
glNormal3f(0.0f, 0.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glNormal3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glNormal3f(0.0f, 0.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glNormal3f(-1.0f, 0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glNormal3f(0.0f, -1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glNormal3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
glEnd();*/
/*for (double i = -1.0; i < 1; i += 0.1) {
for (double j = -1.0; j < 1; j += 0.1) {
for (double k = -1.0; k < 1; k += 0.1) {
glBegin(GL_POINTS);
glVertex3d(i, j, k);
glEnd();
}
}
}*/
}
void Viewer::LoadFils(std::string fileName) {
std::ifstream ifs;
ifs.open(fileName);
std::string str;
while (std::getline(ifs, str)) {
if (str[0] == '#')
continue;
else if (str[0] == 'f')
{
std::vector<std::string> paresData;
std::string delimiter = " ";
pares(str, paresData, delimiter);
Face f;
for (int i = 1; i < 4; i++) {
std::vector<std::string> paresData2;
std::string delimiter = "//";
pares(paresData[i], paresData2, delimiter);
f.v[i - 1] = std::stoi(paresData2[0]) - 1;
f.vn[i - 1] = std::stoi(paresData2[1]) - 1;
//mesh.face.push_back(pt);
}
mesh.face.push_back(f);
}
else if (str[0] == 'v')
{
if (str[1] == 'n') {
std::vector<std::string> paresData;
std::string delimiter = " ";
pares(str, paresData, delimiter);
Vec3 pt;
pt[0] = std::stod(paresData[1]);
pt[1] = std::stod(paresData[2]);
pt[2] = std::stod(paresData[3]);
mesh.normal.push_back(pt);
}
else {
std::vector<std::string> paresData;
std::string delimiter = " ";
pares(str, paresData, delimiter);
Vec3 pt;
pt[0] = std::stod(paresData[1]);
pt[1] = std::stod(paresData[2]);
pt[2] = std::stod(paresData[3]);
mesh.vertex.push_back(pt);
}
}
}
ifs.close();
ifs.clear();
}
void Viewer::LoadObjFiles(std::string fileName) {
}
void Viewer::Drawpts() {
glDisable(GL_LIGHTING);
glPointSize(1.0);
glColor3f(1, 1, 1);
glBegin(GL_POINTS);
for (auto& pt : pts)
glVertex3f(pt[0], pt[1], pt[2]);
glEnd();
//double minDist = 1E+10;
//Vec3 minPt;
//for (auto& pt : pts)
// if (pt.dist(query) < minDist) {
// minDist = pt.dist(query);
// minPt = pt;
// }
//glColor3f(1, 0, 0);
//glPointSize(20.0);
//glBegin(GL_POINTS);
//glVertex3f(minPt[0], minPt[1], minPt[2]);
//glVertex3d(query[0], query[1], query[2]);
//glEnd();
//glLineWidth(2.0);
//glColor3f(0, 1, 0);
//glBegin(GL_LINES);
//glVertex3d(query[0], query[1], query[2]);
//glVertex3d(minPt[0], minPt[1], minPt[2]);
//glEnd();
}
/*
glPointSize(20.0);
glBegin(GL_POINTS);
glColor3f(0, 1, 0);
glVertex3d(0.1, 0.1, 0.1);
glVertex3d(min_point[0], min_point[1], min_point[0]);
glEnd();
glLineWidth(2.0);
glColor3f(0, 1, 0);
glBegin(GL_LINES);
glVertex3d(0.1, 0.1, 0.1);
glVertex3d(min_point[0], min_point[1], min_point[0]);
glEnd();
auto pt = pts[arrowNum0];
printf("pt %f %f %f\n", pt[0], pt[1], pt[2]);
glColor3f(1, 0, 0);
glPointSize(20.0);
glBegin(GL_POINTS);
glVertex3d(pt[0], pt[1], pt[2]);
glEnd();*/
void Viewer::animate()
{
printf("animate called\n");
animatedIdx++;
double radian = (animatedIdx % 360) * PI / 180.0;
}
#pragma once
#include "QGLViewer/manipulatedFrame.h"
#include "QGLViewer/qglviewerortho.h"
#include "Utils.h"
#include <chrono>
#include <iostream>
#include<fstream>
class Viewer : public QGLViewerOrtho
{
Q_OBJECT
public:
Viewer(QWidget* parent);
virtual void initializeGL() override;
virtual void draw() override;
virtual void keyPressEvent(QKeyEvent* e) override;
virtual void mousePressEvent(QMouseEvent* e) override;
virtual void mouseMoveEvent(QMouseEvent* e) override;
virtual void mouseReleaseEvent(QMouseEvent* e) override;
virtual void animate();
void DrawCube();
void Drawpts();
void LoadFils(std::string filename);
void LoadObjFiles(std::string filename);
bool isAnimated = false;
int animatedIdx = 0;
int arrowNum0 = 0, arrowNum1 = 0;
std::vector<Vec3> pts;
struct Face
{
int v[3];
int vn[3];
};
struct Mesh{
std::vector <Face>face;
std::vector <Vec3> vertex;
std::vector <Vec3> normal;
};
Mesh mesh;
double min_point[3];
double min_ragne = 10;
Vec3 query;
private slots:
void light0AmbientChanged(int value);
void light0DiffuseChanged(int value);
void light0SpecularChanged(int value);
};
728x90
'opengl' 카테고리의 다른 글
<자료구조> 큐(queue),덱(deque) (0) | 2023.06.01 |
---|---|
[OpenGL] LearnOpenGL 정리본 6~7 (0) | 2022.07.30 |
[OpenGL] LearnOpenGL 정리본 4~5 (0) | 2022.07.23 |
[OpenGL] LearnOpenGL 정리 본 (1~4) (0) | 2022.07.18 |