diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/mainwindow.h b/hw04/mainwindow.h new file mode 100644 index 0000000..b3aab25 --- /dev/null +++ b/hw04/mainwindow.h @@ -0,0 +1,25 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/mainwindow.h b/hw04/mainwindow.h new file mode 100644 index 0000000..b3aab25 --- /dev/null +++ b/hw04/mainwindow.h @@ -0,0 +1,25 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/hw04/mainwindow.ui b/hw04/mainwindow.ui new file mode 100644 index 0000000..5e1745e --- /dev/null +++ b/hw04/mainwindow.ui @@ -0,0 +1,64 @@ + + + MainWindow + + + + 0 + 0 + 548 + 415 + + + + MainWindow + + + + + + + + 0 + 0 + + + + + + + + + + 0 + 0 + 548 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + OGLWidget + QOpenGLWidget +
oglwidget.h
+ + setParamA(double) + setParamB(double) + +
+
+ + +
diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/mainwindow.h b/hw04/mainwindow.h new file mode 100644 index 0000000..b3aab25 --- /dev/null +++ b/hw04/mainwindow.h @@ -0,0 +1,25 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/hw04/mainwindow.ui b/hw04/mainwindow.ui new file mode 100644 index 0000000..5e1745e --- /dev/null +++ b/hw04/mainwindow.ui @@ -0,0 +1,64 @@ + + + MainWindow + + + + 0 + 0 + 548 + 415 + + + + MainWindow + + + + + + + + 0 + 0 + + + + + + + + + + 0 + 0 + 548 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + OGLWidget + QOpenGLWidget +
oglwidget.h
+ + setParamA(double) + setParamB(double) + +
+
+ + +
diff --git a/hw04/mesh1.obj b/hw04/mesh1.obj new file mode 100644 index 0000000..0af35e5 --- /dev/null +++ b/hw04/mesh1.obj @@ -0,0 +1,8 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +f 1 2 4 +f 2 3 4 +f 3 1 4 +f 3 2 1 \ No newline at end of file diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/mainwindow.h b/hw04/mainwindow.h new file mode 100644 index 0000000..b3aab25 --- /dev/null +++ b/hw04/mainwindow.h @@ -0,0 +1,25 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/hw04/mainwindow.ui b/hw04/mainwindow.ui new file mode 100644 index 0000000..5e1745e --- /dev/null +++ b/hw04/mainwindow.ui @@ -0,0 +1,64 @@ + + + MainWindow + + + + 0 + 0 + 548 + 415 + + + + MainWindow + + + + + + + + 0 + 0 + + + + + + + + + + 0 + 0 + 548 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + OGLWidget + QOpenGLWidget +
oglwidget.h
+ + setParamA(double) + setParamB(double) + +
+
+ + +
diff --git a/hw04/mesh1.obj b/hw04/mesh1.obj new file mode 100644 index 0000000..0af35e5 --- /dev/null +++ b/hw04/mesh1.obj @@ -0,0 +1,8 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +f 1 2 4 +f 2 3 4 +f 3 1 4 +f 3 2 1 \ No newline at end of file diff --git a/hw04/oglwidget.cpp b/hw04/oglwidget.cpp new file mode 100644 index 0000000..3be1f28 --- /dev/null +++ b/hw04/oglwidget.cpp @@ -0,0 +1,404 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "oglwidget.h" +#include +#include +#include +#include +#include +#include + +#define PI 3.14159265358979323846 +using namespace std; + +static double alpha = 45.0; // rotation angle + +class Vertex{ +public: + float p[3]; // coordinates + + Vertex(); + Vertex( float point[3]); + Vertex( float x, float y, float z); + void Print() { + cout << p[0] << " " << p[1] << " " << p[2] << endl; + } + // print coordinates on screen + +}; + +Vertex::Vertex(){}; // empty constructor + +Vertex::Vertex(float x, float y, float z){ // constructor with initialization + p[0] = x; + p[1] = y; + p[2] = z; +} + +Vertex::Vertex(float *point) { + copy_n(point, 3, p); +} + +Vertex operator+( Vertex a, Vertex b) // add pts or vectors +{ + return *new Vertex(a.p[0] + b.p[0], a.p[1] + b.p[1],a.p[2] + b.p[2]); +} +Vertex operator-( Vertex a, Vertex b)// subtract pts or vectors +{ + return *new Vertex(a.p[0] - b.p[0], a.p[1] - b.p[1],a.p[2] - b.p[2]); +} +Vertex operator*( float a, Vertex b) // product between scalar and vector +{ + return *new Vertex(a*b.p[0], a*b.p[1], a*b.p[2]); +} +float operator*( Vertex a, Vertex b) // scalar product +{ + return a.p[0] * b.p[0] + a.p[1] * b.p[1] + a.p[2] * b.p[2]; +} +Vertex operator%( Vertex a, Vertex b) // cross product +{ + return *new Vertex(a.p[1]*b.p[2] - a.p[2]*b.p[1], a.p[2]*b.p[0] - a.p[0]*b.p[2], a.p[0]*b.p[1] - a.p[1]*b.p[0]); +} +bool operator==(Vertex a, Vertex b) // check if coords are the same +{ + return (a.p[0] == b.p[0]) && (a.p[1] == b.p[1]) && (a.p[2] == b.p[2]); +} + +class Tri +{ +public: + int iv[3]; // vertex indices + int it[3]; // adjacent triangle indices + int ie[3]; // edge vertex indices + Tri(); + Tri( int i[3]); + Tri( int i, int j, int k); + void Print(); +}; + +void subDivEdgeMidpoint(); + +void saveData(); + +Tri::Tri() { + it[0] = -1; + it[1] = -1; + it[2] = -1; +} + +Tri::Tri(int *i) { + new (this) Tri(); + +} + +Tri::Tri(int i, int j, int k) { + new (this) Tri(); + + iv[0] = i; + iv[1] = j; + iv[2] = k; +} + + +vector pts; +vector valences; // valence list (no. of triangles for every point +vector tris; + +void Tri::Print() { + cout << "Tri:" << endl << " neighbour: " << it[0] << " " << it[1] << " " << it[2] << endl << " - "; + cout << valences[iv[0]] << "x "; + pts[iv[0]].Print(); + cout << " - " << valences[iv[1]] << "x "; + pts[iv[1]].Print(); + cout << " - " << valences[iv[2]] << "x "; + pts[iv[2]].Print(); + cout << endl; +} + + +void loadData() +{ + string fname = R"(C:\CLionProjects\cg\hw04\mesh1.obj)"; + ifstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + string key; + while( file){ + //getline( file, line); + file >> key; + if (key == "v") { + float x, y, z; + file >> x >> y >> z; + Vertex pVertex = *new Vertex(x, y, z); + pts.push_back(pVertex); + valences.push_back(0); + } else if (key == "f") { + int a, b, c; + file >> a >> b >> c; + Tri pTriangle = *new Tri(a-1, b-1, c-1); + tris.push_back(pTriangle); + } + } + file.close(); + + tris.pop_back(); + + subDivEdgeMidpoint(); + subDivEdgeMidpoint(); + + // Connectivity Algorithm + for (int i = 0; i < tris.size(); ++i) { + Tri triag = tris[i]; + int n = 0; + // Search for neighbour + for (int ti = 0; ti < tris.size(); ++ti) { + if (i == ti) continue; + Tri t = tris[ti]; + int count = 0; + for (int x : triag.iv) { + for (int y : t.iv) { + if (x == y) + count++; + } + } + // if two points are the same, they are neighbours + if (count == 2) + triag.it[n++] = ti; + } + + // increase valence for each vertex of triangle + valences[triag.iv[0]]++; + valences[triag.iv[1]]++; + valences[triag.iv[2]]++; + + tris[i] = triag; + } + + for (auto triag : tris) { + triag.Print(); + } + + +// saveData(); +} + +void saveData() { + string fname = R"(C:\CLionProjects\cg\hw04\test2.obj)"; + ofstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + + for (auto vert : pts) { + file << "v\t" << vert.p[0] << "\t" << vert.p[1] << "\t" << vert.p[2] << endl; + } + + for (auto triag : tris) { + file << "f\t" << triag.iv[0]+1 << "\t" << triag.iv[1]+1 << "\t" << triag.iv[2]+1 << endl; + } + + file.close(); +} + + +void subDivEdgeMidpoint() { + const unsigned long long int amountToSubDiv = tris.size(); + for (int i = 0; i < amountToSubDiv; ++i) { + Tri triag = tris[i]; + + Vertex a = pts[triag.iv[0]]; + Vertex b = pts[triag.iv[1]]; + Vertex c = pts[triag.iv[2]]; + +// int e = pts.size(); + int ei0 = -1; + int ei1 = -1; + int ei2 = -1; + Vertex e0 = 0.5f * (a+b); + Vertex e1 = 0.5f * (c+b); + Vertex e2 = 0.5f * (a+c); + for (int j = 0; j < pts.size(); ++j) { + if (pts[j] == e0) + ei0 = j; + if (pts[j] == e1) + ei1 = j; + if (pts[j] == e2) + ei2 = j; + } + if (ei0 == -1) { + ei0 = pts.size(); + pts.push_back(e0); + valences.push_back(0); + } + if (ei1 == -1) { + ei1 = pts.size(); + pts.push_back(e1); + valences.push_back(0); + } + if (ei2 == -1) { + ei2 = pts.size(); + pts.push_back(e2); + valences.push_back(0); + } + + tris.push_back(*new Tri(triag.iv[2], ei2, ei1)); + tris.push_back(*new Tri(triag.iv[1], ei0, ei1)); + tris.push_back(*new Tri(ei0, ei1, ei2)); + + + triag.iv[1] = ei0; + triag.iv[2] = ei2; + tris[i] = triag; + } +} + +void DrawTriag() { + + glPolygonMode(GL_FRONT_AND_BACK , GL_LINE); + glBegin( GL_TRIANGLES); + for (auto tri : tris) { + Vertex a = pts[tri.iv[0]]; + Vertex b = pts[tri.iv[1]]; + Vertex c = pts[tri.iv[2]]; + Vertex nvec = (b-a)%(c-a); + + glNormal3fv(nvec.p); + + glVertex3fv(a.p); + glVertex3fv(c.p); + glVertex3fv(b.p); + + } + glEnd(); +} + +// initialize Open GL lighting and projection matrix +void InitLightingAndProjection() // to be executed once before drawing +{ + // light positions and colors + GLfloat LightPosition1[4] = { 10, 5, 10, 0}; + GLfloat LightPosition2[4] = { -5, 5, -10, 0}; + GLfloat ColorRedish[4] = { 1.0, .8, .8, 1}; // white with a little bit of red + GLfloat ColorBlueish[4] = { .8, .8, 1.0, 1};// white with a little bit of blue + + glEnable( GL_DEPTH_TEST); // switch on z-buffer + glDepthFunc( GL_LESS); + + glShadeModel( GL_SMOOTH); // Gouraud shading + //glShadeModel( GL_FLAT); + + glEnable( GL_LIGHTING); // use lighting + glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1); // draw both sides + + // define and switch on light 0 + glLightfv( GL_LIGHT0, GL_POSITION, LightPosition1); + glLightfv( GL_LIGHT0, GL_DIFFUSE, ColorRedish); + glLightfv( GL_LIGHT0, GL_SPECULAR, ColorRedish); + glEnable( GL_LIGHT0); + + // define and switch on light 1 + glLightfv( GL_LIGHT1, GL_POSITION, LightPosition2); + glLightfv( GL_LIGHT1, GL_DIFFUSE, ColorBlueish); + glLightfv( GL_LIGHT1, GL_SPECULAR, ColorBlueish); + glEnable( GL_LIGHT1); + + glMatrixMode( GL_PROJECTION); // define camera projection + glLoadIdentity(); // reset matrix to identity (otherwise existing matrix will be multiplied with) + glOrtho( -15, 15, -10, 10, -50, 50); // orthogonal projection (xmin xmax ymin ymax zmin zmax) + //glFrustum( -10, 10, -8, 8, 2, 20); // perspective projektion +} + +// define material color properties for front and back side +void SetMaterialColor( int side, float r, float g, float b){ + float amb[4], dif[4], spe[4]; + int i, mat; + + dif[0] = r; // diffuse color as defined by r,g, and b + dif[1] = g; + dif[2] = b; + for( i=0; i<3; i++){ + amb[i] = .1 * dif[i]; // ambient color is 10 percent of diffuse + spe[i] = .5; // specular color is just white / gray + } + amb[3] = dif[3] = spe[3] = 1.0; // alpha component is always 1 + switch( side){ + case 1: mat = GL_FRONT; break; + case 2: mat = GL_BACK; break; + default: mat = GL_FRONT_AND_BACK; break; + } + glMaterialfv( mat, GL_AMBIENT, amb); // define ambient, diffuse and specular components + glMaterialfv( mat, GL_DIFFUSE, dif); + glMaterialfv( mat, GL_SPECULAR, spe); + glMaterialf( mat, GL_SHININESS, 50.0); // Phong constant for the size of highlights +} + + +OGLWidget::OGLWidget(QWidget *parent) // constructor + : QOpenGLWidget(parent) +{ + // Setup the animation timer to fire every x msec + animtimer = new QTimer(this); + animtimer->start( 50 ); + + // Everytime the timer fires, the animation is going one step forward + connect(animtimer, SIGNAL(timeout()), this, SLOT(stepAnimation())); + + animstep = 0; +} + +OGLWidget::~OGLWidget() // destructor +{ +} + +void OGLWidget::stepAnimation() +{ + animstep++; // Increase animation steps + update(); // Trigger redraw of scene with paintGL +} + +void OGLWidget::initializeGL() // initializations to be called once +{ + initializeOpenGLFunctions(); + + InitLightingAndProjection(); // define light sources and projection + + loadData(); +} + +void OGLWidget::paintGL() // draw everything, to be called repeatedly +{ + glEnable(GL_NORMALIZE); // this is necessary when using glScale (keep normals to unit length) + + // set background color + glClearColor(0.8, 0.8, 1.0, 1.0); // bright blue + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // draw the scene + glMatrixMode( GL_MODELVIEW); + glLoadIdentity(); // Reset The Current Modelview Matrix + glTranslated( 0 ,0 ,-10.0); // Move 10 units backwards in z, since camera is at origin + glScaled( 2.0, 2.0, 2.0); // scale objects + glRotated( alpha, 0, 3, 1); // continuous rotation + alpha += 2; + + // define color: 1=front, 2=back, 3=both, followed by r, g, and b + SetMaterialColor( 1, 0, .2, .2); // front color is red + SetMaterialColor( 2, 0.2, 0.2, 1.0); // back color is blue + + // draw a cylinder with default resolution + DrawTriag(); + + // make it appear (before this, it's hidden in the rear buffer) + glFlush(); +} + +void OGLWidget::resizeGL(int w, int h) // called when window size is changed +{ + // adjust viewport transform + glViewport(0,0,w,h); +} + diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/mainwindow.h b/hw04/mainwindow.h new file mode 100644 index 0000000..b3aab25 --- /dev/null +++ b/hw04/mainwindow.h @@ -0,0 +1,25 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/hw04/mainwindow.ui b/hw04/mainwindow.ui new file mode 100644 index 0000000..5e1745e --- /dev/null +++ b/hw04/mainwindow.ui @@ -0,0 +1,64 @@ + + + MainWindow + + + + 0 + 0 + 548 + 415 + + + + MainWindow + + + + + + + + 0 + 0 + + + + + + + + + + 0 + 0 + 548 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + OGLWidget + QOpenGLWidget +
oglwidget.h
+ + setParamA(double) + setParamB(double) + +
+
+ + +
diff --git a/hw04/mesh1.obj b/hw04/mesh1.obj new file mode 100644 index 0000000..0af35e5 --- /dev/null +++ b/hw04/mesh1.obj @@ -0,0 +1,8 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +f 1 2 4 +f 2 3 4 +f 3 1 4 +f 3 2 1 \ No newline at end of file diff --git a/hw04/oglwidget.cpp b/hw04/oglwidget.cpp new file mode 100644 index 0000000..3be1f28 --- /dev/null +++ b/hw04/oglwidget.cpp @@ -0,0 +1,404 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "oglwidget.h" +#include +#include +#include +#include +#include +#include + +#define PI 3.14159265358979323846 +using namespace std; + +static double alpha = 45.0; // rotation angle + +class Vertex{ +public: + float p[3]; // coordinates + + Vertex(); + Vertex( float point[3]); + Vertex( float x, float y, float z); + void Print() { + cout << p[0] << " " << p[1] << " " << p[2] << endl; + } + // print coordinates on screen + +}; + +Vertex::Vertex(){}; // empty constructor + +Vertex::Vertex(float x, float y, float z){ // constructor with initialization + p[0] = x; + p[1] = y; + p[2] = z; +} + +Vertex::Vertex(float *point) { + copy_n(point, 3, p); +} + +Vertex operator+( Vertex a, Vertex b) // add pts or vectors +{ + return *new Vertex(a.p[0] + b.p[0], a.p[1] + b.p[1],a.p[2] + b.p[2]); +} +Vertex operator-( Vertex a, Vertex b)// subtract pts or vectors +{ + return *new Vertex(a.p[0] - b.p[0], a.p[1] - b.p[1],a.p[2] - b.p[2]); +} +Vertex operator*( float a, Vertex b) // product between scalar and vector +{ + return *new Vertex(a*b.p[0], a*b.p[1], a*b.p[2]); +} +float operator*( Vertex a, Vertex b) // scalar product +{ + return a.p[0] * b.p[0] + a.p[1] * b.p[1] + a.p[2] * b.p[2]; +} +Vertex operator%( Vertex a, Vertex b) // cross product +{ + return *new Vertex(a.p[1]*b.p[2] - a.p[2]*b.p[1], a.p[2]*b.p[0] - a.p[0]*b.p[2], a.p[0]*b.p[1] - a.p[1]*b.p[0]); +} +bool operator==(Vertex a, Vertex b) // check if coords are the same +{ + return (a.p[0] == b.p[0]) && (a.p[1] == b.p[1]) && (a.p[2] == b.p[2]); +} + +class Tri +{ +public: + int iv[3]; // vertex indices + int it[3]; // adjacent triangle indices + int ie[3]; // edge vertex indices + Tri(); + Tri( int i[3]); + Tri( int i, int j, int k); + void Print(); +}; + +void subDivEdgeMidpoint(); + +void saveData(); + +Tri::Tri() { + it[0] = -1; + it[1] = -1; + it[2] = -1; +} + +Tri::Tri(int *i) { + new (this) Tri(); + +} + +Tri::Tri(int i, int j, int k) { + new (this) Tri(); + + iv[0] = i; + iv[1] = j; + iv[2] = k; +} + + +vector pts; +vector valences; // valence list (no. of triangles for every point +vector tris; + +void Tri::Print() { + cout << "Tri:" << endl << " neighbour: " << it[0] << " " << it[1] << " " << it[2] << endl << " - "; + cout << valences[iv[0]] << "x "; + pts[iv[0]].Print(); + cout << " - " << valences[iv[1]] << "x "; + pts[iv[1]].Print(); + cout << " - " << valences[iv[2]] << "x "; + pts[iv[2]].Print(); + cout << endl; +} + + +void loadData() +{ + string fname = R"(C:\CLionProjects\cg\hw04\mesh1.obj)"; + ifstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + string key; + while( file){ + //getline( file, line); + file >> key; + if (key == "v") { + float x, y, z; + file >> x >> y >> z; + Vertex pVertex = *new Vertex(x, y, z); + pts.push_back(pVertex); + valences.push_back(0); + } else if (key == "f") { + int a, b, c; + file >> a >> b >> c; + Tri pTriangle = *new Tri(a-1, b-1, c-1); + tris.push_back(pTriangle); + } + } + file.close(); + + tris.pop_back(); + + subDivEdgeMidpoint(); + subDivEdgeMidpoint(); + + // Connectivity Algorithm + for (int i = 0; i < tris.size(); ++i) { + Tri triag = tris[i]; + int n = 0; + // Search for neighbour + for (int ti = 0; ti < tris.size(); ++ti) { + if (i == ti) continue; + Tri t = tris[ti]; + int count = 0; + for (int x : triag.iv) { + for (int y : t.iv) { + if (x == y) + count++; + } + } + // if two points are the same, they are neighbours + if (count == 2) + triag.it[n++] = ti; + } + + // increase valence for each vertex of triangle + valences[triag.iv[0]]++; + valences[triag.iv[1]]++; + valences[triag.iv[2]]++; + + tris[i] = triag; + } + + for (auto triag : tris) { + triag.Print(); + } + + +// saveData(); +} + +void saveData() { + string fname = R"(C:\CLionProjects\cg\hw04\test2.obj)"; + ofstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + + for (auto vert : pts) { + file << "v\t" << vert.p[0] << "\t" << vert.p[1] << "\t" << vert.p[2] << endl; + } + + for (auto triag : tris) { + file << "f\t" << triag.iv[0]+1 << "\t" << triag.iv[1]+1 << "\t" << triag.iv[2]+1 << endl; + } + + file.close(); +} + + +void subDivEdgeMidpoint() { + const unsigned long long int amountToSubDiv = tris.size(); + for (int i = 0; i < amountToSubDiv; ++i) { + Tri triag = tris[i]; + + Vertex a = pts[triag.iv[0]]; + Vertex b = pts[triag.iv[1]]; + Vertex c = pts[triag.iv[2]]; + +// int e = pts.size(); + int ei0 = -1; + int ei1 = -1; + int ei2 = -1; + Vertex e0 = 0.5f * (a+b); + Vertex e1 = 0.5f * (c+b); + Vertex e2 = 0.5f * (a+c); + for (int j = 0; j < pts.size(); ++j) { + if (pts[j] == e0) + ei0 = j; + if (pts[j] == e1) + ei1 = j; + if (pts[j] == e2) + ei2 = j; + } + if (ei0 == -1) { + ei0 = pts.size(); + pts.push_back(e0); + valences.push_back(0); + } + if (ei1 == -1) { + ei1 = pts.size(); + pts.push_back(e1); + valences.push_back(0); + } + if (ei2 == -1) { + ei2 = pts.size(); + pts.push_back(e2); + valences.push_back(0); + } + + tris.push_back(*new Tri(triag.iv[2], ei2, ei1)); + tris.push_back(*new Tri(triag.iv[1], ei0, ei1)); + tris.push_back(*new Tri(ei0, ei1, ei2)); + + + triag.iv[1] = ei0; + triag.iv[2] = ei2; + tris[i] = triag; + } +} + +void DrawTriag() { + + glPolygonMode(GL_FRONT_AND_BACK , GL_LINE); + glBegin( GL_TRIANGLES); + for (auto tri : tris) { + Vertex a = pts[tri.iv[0]]; + Vertex b = pts[tri.iv[1]]; + Vertex c = pts[tri.iv[2]]; + Vertex nvec = (b-a)%(c-a); + + glNormal3fv(nvec.p); + + glVertex3fv(a.p); + glVertex3fv(c.p); + glVertex3fv(b.p); + + } + glEnd(); +} + +// initialize Open GL lighting and projection matrix +void InitLightingAndProjection() // to be executed once before drawing +{ + // light positions and colors + GLfloat LightPosition1[4] = { 10, 5, 10, 0}; + GLfloat LightPosition2[4] = { -5, 5, -10, 0}; + GLfloat ColorRedish[4] = { 1.0, .8, .8, 1}; // white with a little bit of red + GLfloat ColorBlueish[4] = { .8, .8, 1.0, 1};// white with a little bit of blue + + glEnable( GL_DEPTH_TEST); // switch on z-buffer + glDepthFunc( GL_LESS); + + glShadeModel( GL_SMOOTH); // Gouraud shading + //glShadeModel( GL_FLAT); + + glEnable( GL_LIGHTING); // use lighting + glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1); // draw both sides + + // define and switch on light 0 + glLightfv( GL_LIGHT0, GL_POSITION, LightPosition1); + glLightfv( GL_LIGHT0, GL_DIFFUSE, ColorRedish); + glLightfv( GL_LIGHT0, GL_SPECULAR, ColorRedish); + glEnable( GL_LIGHT0); + + // define and switch on light 1 + glLightfv( GL_LIGHT1, GL_POSITION, LightPosition2); + glLightfv( GL_LIGHT1, GL_DIFFUSE, ColorBlueish); + glLightfv( GL_LIGHT1, GL_SPECULAR, ColorBlueish); + glEnable( GL_LIGHT1); + + glMatrixMode( GL_PROJECTION); // define camera projection + glLoadIdentity(); // reset matrix to identity (otherwise existing matrix will be multiplied with) + glOrtho( -15, 15, -10, 10, -50, 50); // orthogonal projection (xmin xmax ymin ymax zmin zmax) + //glFrustum( -10, 10, -8, 8, 2, 20); // perspective projektion +} + +// define material color properties for front and back side +void SetMaterialColor( int side, float r, float g, float b){ + float amb[4], dif[4], spe[4]; + int i, mat; + + dif[0] = r; // diffuse color as defined by r,g, and b + dif[1] = g; + dif[2] = b; + for( i=0; i<3; i++){ + amb[i] = .1 * dif[i]; // ambient color is 10 percent of diffuse + spe[i] = .5; // specular color is just white / gray + } + amb[3] = dif[3] = spe[3] = 1.0; // alpha component is always 1 + switch( side){ + case 1: mat = GL_FRONT; break; + case 2: mat = GL_BACK; break; + default: mat = GL_FRONT_AND_BACK; break; + } + glMaterialfv( mat, GL_AMBIENT, amb); // define ambient, diffuse and specular components + glMaterialfv( mat, GL_DIFFUSE, dif); + glMaterialfv( mat, GL_SPECULAR, spe); + glMaterialf( mat, GL_SHININESS, 50.0); // Phong constant for the size of highlights +} + + +OGLWidget::OGLWidget(QWidget *parent) // constructor + : QOpenGLWidget(parent) +{ + // Setup the animation timer to fire every x msec + animtimer = new QTimer(this); + animtimer->start( 50 ); + + // Everytime the timer fires, the animation is going one step forward + connect(animtimer, SIGNAL(timeout()), this, SLOT(stepAnimation())); + + animstep = 0; +} + +OGLWidget::~OGLWidget() // destructor +{ +} + +void OGLWidget::stepAnimation() +{ + animstep++; // Increase animation steps + update(); // Trigger redraw of scene with paintGL +} + +void OGLWidget::initializeGL() // initializations to be called once +{ + initializeOpenGLFunctions(); + + InitLightingAndProjection(); // define light sources and projection + + loadData(); +} + +void OGLWidget::paintGL() // draw everything, to be called repeatedly +{ + glEnable(GL_NORMALIZE); // this is necessary when using glScale (keep normals to unit length) + + // set background color + glClearColor(0.8, 0.8, 1.0, 1.0); // bright blue + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // draw the scene + glMatrixMode( GL_MODELVIEW); + glLoadIdentity(); // Reset The Current Modelview Matrix + glTranslated( 0 ,0 ,-10.0); // Move 10 units backwards in z, since camera is at origin + glScaled( 2.0, 2.0, 2.0); // scale objects + glRotated( alpha, 0, 3, 1); // continuous rotation + alpha += 2; + + // define color: 1=front, 2=back, 3=both, followed by r, g, and b + SetMaterialColor( 1, 0, .2, .2); // front color is red + SetMaterialColor( 2, 0.2, 0.2, 1.0); // back color is blue + + // draw a cylinder with default resolution + DrawTriag(); + + // make it appear (before this, it's hidden in the rear buffer) + glFlush(); +} + +void OGLWidget::resizeGL(int w, int h) // called when window size is changed +{ + // adjust viewport transform + glViewport(0,0,w,h); +} + diff --git a/hw04/oglwidget.h b/hw04/oglwidget.h new file mode 100644 index 0000000..a536f6a --- /dev/null +++ b/hw04/oglwidget.h @@ -0,0 +1,34 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef OGLWIDGET_H +#define OGLWIDGET_H + +#include +#include +#include + +class OGLWidget : public QOpenGLWidget, + protected QOpenGLFunctions +{ + Q_OBJECT + +public: + OGLWidget(QWidget *parent = 0); + ~OGLWidget(); + +public slots: + void stepAnimation(); + +protected: + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + +protected: + QTimer* animtimer; // Timer needed to step animation every x msec + int animstep; // Current animation step (used to rotate triangle +}; + + +#endif // OGLWIDGET_H diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/mainwindow.h b/hw04/mainwindow.h new file mode 100644 index 0000000..b3aab25 --- /dev/null +++ b/hw04/mainwindow.h @@ -0,0 +1,25 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/hw04/mainwindow.ui b/hw04/mainwindow.ui new file mode 100644 index 0000000..5e1745e --- /dev/null +++ b/hw04/mainwindow.ui @@ -0,0 +1,64 @@ + + + MainWindow + + + + 0 + 0 + 548 + 415 + + + + MainWindow + + + + + + + + 0 + 0 + + + + + + + + + + 0 + 0 + 548 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + OGLWidget + QOpenGLWidget +
oglwidget.h
+ + setParamA(double) + setParamB(double) + +
+
+ + +
diff --git a/hw04/mesh1.obj b/hw04/mesh1.obj new file mode 100644 index 0000000..0af35e5 --- /dev/null +++ b/hw04/mesh1.obj @@ -0,0 +1,8 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +f 1 2 4 +f 2 3 4 +f 3 1 4 +f 3 2 1 \ No newline at end of file diff --git a/hw04/oglwidget.cpp b/hw04/oglwidget.cpp new file mode 100644 index 0000000..3be1f28 --- /dev/null +++ b/hw04/oglwidget.cpp @@ -0,0 +1,404 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "oglwidget.h" +#include +#include +#include +#include +#include +#include + +#define PI 3.14159265358979323846 +using namespace std; + +static double alpha = 45.0; // rotation angle + +class Vertex{ +public: + float p[3]; // coordinates + + Vertex(); + Vertex( float point[3]); + Vertex( float x, float y, float z); + void Print() { + cout << p[0] << " " << p[1] << " " << p[2] << endl; + } + // print coordinates on screen + +}; + +Vertex::Vertex(){}; // empty constructor + +Vertex::Vertex(float x, float y, float z){ // constructor with initialization + p[0] = x; + p[1] = y; + p[2] = z; +} + +Vertex::Vertex(float *point) { + copy_n(point, 3, p); +} + +Vertex operator+( Vertex a, Vertex b) // add pts or vectors +{ + return *new Vertex(a.p[0] + b.p[0], a.p[1] + b.p[1],a.p[2] + b.p[2]); +} +Vertex operator-( Vertex a, Vertex b)// subtract pts or vectors +{ + return *new Vertex(a.p[0] - b.p[0], a.p[1] - b.p[1],a.p[2] - b.p[2]); +} +Vertex operator*( float a, Vertex b) // product between scalar and vector +{ + return *new Vertex(a*b.p[0], a*b.p[1], a*b.p[2]); +} +float operator*( Vertex a, Vertex b) // scalar product +{ + return a.p[0] * b.p[0] + a.p[1] * b.p[1] + a.p[2] * b.p[2]; +} +Vertex operator%( Vertex a, Vertex b) // cross product +{ + return *new Vertex(a.p[1]*b.p[2] - a.p[2]*b.p[1], a.p[2]*b.p[0] - a.p[0]*b.p[2], a.p[0]*b.p[1] - a.p[1]*b.p[0]); +} +bool operator==(Vertex a, Vertex b) // check if coords are the same +{ + return (a.p[0] == b.p[0]) && (a.p[1] == b.p[1]) && (a.p[2] == b.p[2]); +} + +class Tri +{ +public: + int iv[3]; // vertex indices + int it[3]; // adjacent triangle indices + int ie[3]; // edge vertex indices + Tri(); + Tri( int i[3]); + Tri( int i, int j, int k); + void Print(); +}; + +void subDivEdgeMidpoint(); + +void saveData(); + +Tri::Tri() { + it[0] = -1; + it[1] = -1; + it[2] = -1; +} + +Tri::Tri(int *i) { + new (this) Tri(); + +} + +Tri::Tri(int i, int j, int k) { + new (this) Tri(); + + iv[0] = i; + iv[1] = j; + iv[2] = k; +} + + +vector pts; +vector valences; // valence list (no. of triangles for every point +vector tris; + +void Tri::Print() { + cout << "Tri:" << endl << " neighbour: " << it[0] << " " << it[1] << " " << it[2] << endl << " - "; + cout << valences[iv[0]] << "x "; + pts[iv[0]].Print(); + cout << " - " << valences[iv[1]] << "x "; + pts[iv[1]].Print(); + cout << " - " << valences[iv[2]] << "x "; + pts[iv[2]].Print(); + cout << endl; +} + + +void loadData() +{ + string fname = R"(C:\CLionProjects\cg\hw04\mesh1.obj)"; + ifstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + string key; + while( file){ + //getline( file, line); + file >> key; + if (key == "v") { + float x, y, z; + file >> x >> y >> z; + Vertex pVertex = *new Vertex(x, y, z); + pts.push_back(pVertex); + valences.push_back(0); + } else if (key == "f") { + int a, b, c; + file >> a >> b >> c; + Tri pTriangle = *new Tri(a-1, b-1, c-1); + tris.push_back(pTriangle); + } + } + file.close(); + + tris.pop_back(); + + subDivEdgeMidpoint(); + subDivEdgeMidpoint(); + + // Connectivity Algorithm + for (int i = 0; i < tris.size(); ++i) { + Tri triag = tris[i]; + int n = 0; + // Search for neighbour + for (int ti = 0; ti < tris.size(); ++ti) { + if (i == ti) continue; + Tri t = tris[ti]; + int count = 0; + for (int x : triag.iv) { + for (int y : t.iv) { + if (x == y) + count++; + } + } + // if two points are the same, they are neighbours + if (count == 2) + triag.it[n++] = ti; + } + + // increase valence for each vertex of triangle + valences[triag.iv[0]]++; + valences[triag.iv[1]]++; + valences[triag.iv[2]]++; + + tris[i] = triag; + } + + for (auto triag : tris) { + triag.Print(); + } + + +// saveData(); +} + +void saveData() { + string fname = R"(C:\CLionProjects\cg\hw04\test2.obj)"; + ofstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + + for (auto vert : pts) { + file << "v\t" << vert.p[0] << "\t" << vert.p[1] << "\t" << vert.p[2] << endl; + } + + for (auto triag : tris) { + file << "f\t" << triag.iv[0]+1 << "\t" << triag.iv[1]+1 << "\t" << triag.iv[2]+1 << endl; + } + + file.close(); +} + + +void subDivEdgeMidpoint() { + const unsigned long long int amountToSubDiv = tris.size(); + for (int i = 0; i < amountToSubDiv; ++i) { + Tri triag = tris[i]; + + Vertex a = pts[triag.iv[0]]; + Vertex b = pts[triag.iv[1]]; + Vertex c = pts[triag.iv[2]]; + +// int e = pts.size(); + int ei0 = -1; + int ei1 = -1; + int ei2 = -1; + Vertex e0 = 0.5f * (a+b); + Vertex e1 = 0.5f * (c+b); + Vertex e2 = 0.5f * (a+c); + for (int j = 0; j < pts.size(); ++j) { + if (pts[j] == e0) + ei0 = j; + if (pts[j] == e1) + ei1 = j; + if (pts[j] == e2) + ei2 = j; + } + if (ei0 == -1) { + ei0 = pts.size(); + pts.push_back(e0); + valences.push_back(0); + } + if (ei1 == -1) { + ei1 = pts.size(); + pts.push_back(e1); + valences.push_back(0); + } + if (ei2 == -1) { + ei2 = pts.size(); + pts.push_back(e2); + valences.push_back(0); + } + + tris.push_back(*new Tri(triag.iv[2], ei2, ei1)); + tris.push_back(*new Tri(triag.iv[1], ei0, ei1)); + tris.push_back(*new Tri(ei0, ei1, ei2)); + + + triag.iv[1] = ei0; + triag.iv[2] = ei2; + tris[i] = triag; + } +} + +void DrawTriag() { + + glPolygonMode(GL_FRONT_AND_BACK , GL_LINE); + glBegin( GL_TRIANGLES); + for (auto tri : tris) { + Vertex a = pts[tri.iv[0]]; + Vertex b = pts[tri.iv[1]]; + Vertex c = pts[tri.iv[2]]; + Vertex nvec = (b-a)%(c-a); + + glNormal3fv(nvec.p); + + glVertex3fv(a.p); + glVertex3fv(c.p); + glVertex3fv(b.p); + + } + glEnd(); +} + +// initialize Open GL lighting and projection matrix +void InitLightingAndProjection() // to be executed once before drawing +{ + // light positions and colors + GLfloat LightPosition1[4] = { 10, 5, 10, 0}; + GLfloat LightPosition2[4] = { -5, 5, -10, 0}; + GLfloat ColorRedish[4] = { 1.0, .8, .8, 1}; // white with a little bit of red + GLfloat ColorBlueish[4] = { .8, .8, 1.0, 1};// white with a little bit of blue + + glEnable( GL_DEPTH_TEST); // switch on z-buffer + glDepthFunc( GL_LESS); + + glShadeModel( GL_SMOOTH); // Gouraud shading + //glShadeModel( GL_FLAT); + + glEnable( GL_LIGHTING); // use lighting + glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1); // draw both sides + + // define and switch on light 0 + glLightfv( GL_LIGHT0, GL_POSITION, LightPosition1); + glLightfv( GL_LIGHT0, GL_DIFFUSE, ColorRedish); + glLightfv( GL_LIGHT0, GL_SPECULAR, ColorRedish); + glEnable( GL_LIGHT0); + + // define and switch on light 1 + glLightfv( GL_LIGHT1, GL_POSITION, LightPosition2); + glLightfv( GL_LIGHT1, GL_DIFFUSE, ColorBlueish); + glLightfv( GL_LIGHT1, GL_SPECULAR, ColorBlueish); + glEnable( GL_LIGHT1); + + glMatrixMode( GL_PROJECTION); // define camera projection + glLoadIdentity(); // reset matrix to identity (otherwise existing matrix will be multiplied with) + glOrtho( -15, 15, -10, 10, -50, 50); // orthogonal projection (xmin xmax ymin ymax zmin zmax) + //glFrustum( -10, 10, -8, 8, 2, 20); // perspective projektion +} + +// define material color properties for front and back side +void SetMaterialColor( int side, float r, float g, float b){ + float amb[4], dif[4], spe[4]; + int i, mat; + + dif[0] = r; // diffuse color as defined by r,g, and b + dif[1] = g; + dif[2] = b; + for( i=0; i<3; i++){ + amb[i] = .1 * dif[i]; // ambient color is 10 percent of diffuse + spe[i] = .5; // specular color is just white / gray + } + amb[3] = dif[3] = spe[3] = 1.0; // alpha component is always 1 + switch( side){ + case 1: mat = GL_FRONT; break; + case 2: mat = GL_BACK; break; + default: mat = GL_FRONT_AND_BACK; break; + } + glMaterialfv( mat, GL_AMBIENT, amb); // define ambient, diffuse and specular components + glMaterialfv( mat, GL_DIFFUSE, dif); + glMaterialfv( mat, GL_SPECULAR, spe); + glMaterialf( mat, GL_SHININESS, 50.0); // Phong constant for the size of highlights +} + + +OGLWidget::OGLWidget(QWidget *parent) // constructor + : QOpenGLWidget(parent) +{ + // Setup the animation timer to fire every x msec + animtimer = new QTimer(this); + animtimer->start( 50 ); + + // Everytime the timer fires, the animation is going one step forward + connect(animtimer, SIGNAL(timeout()), this, SLOT(stepAnimation())); + + animstep = 0; +} + +OGLWidget::~OGLWidget() // destructor +{ +} + +void OGLWidget::stepAnimation() +{ + animstep++; // Increase animation steps + update(); // Trigger redraw of scene with paintGL +} + +void OGLWidget::initializeGL() // initializations to be called once +{ + initializeOpenGLFunctions(); + + InitLightingAndProjection(); // define light sources and projection + + loadData(); +} + +void OGLWidget::paintGL() // draw everything, to be called repeatedly +{ + glEnable(GL_NORMALIZE); // this is necessary when using glScale (keep normals to unit length) + + // set background color + glClearColor(0.8, 0.8, 1.0, 1.0); // bright blue + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // draw the scene + glMatrixMode( GL_MODELVIEW); + glLoadIdentity(); // Reset The Current Modelview Matrix + glTranslated( 0 ,0 ,-10.0); // Move 10 units backwards in z, since camera is at origin + glScaled( 2.0, 2.0, 2.0); // scale objects + glRotated( alpha, 0, 3, 1); // continuous rotation + alpha += 2; + + // define color: 1=front, 2=back, 3=both, followed by r, g, and b + SetMaterialColor( 1, 0, .2, .2); // front color is red + SetMaterialColor( 2, 0.2, 0.2, 1.0); // back color is blue + + // draw a cylinder with default resolution + DrawTriag(); + + // make it appear (before this, it's hidden in the rear buffer) + glFlush(); +} + +void OGLWidget::resizeGL(int w, int h) // called when window size is changed +{ + // adjust viewport transform + glViewport(0,0,w,h); +} + diff --git a/hw04/oglwidget.h b/hw04/oglwidget.h new file mode 100644 index 0000000..a536f6a --- /dev/null +++ b/hw04/oglwidget.h @@ -0,0 +1,34 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef OGLWIDGET_H +#define OGLWIDGET_H + +#include +#include +#include + +class OGLWidget : public QOpenGLWidget, + protected QOpenGLFunctions +{ + Q_OBJECT + +public: + OGLWidget(QWidget *parent = 0); + ~OGLWidget(); + +public slots: + void stepAnimation(); + +protected: + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + +protected: + QTimer* animtimer; // Timer needed to step animation every x msec + int animstep; // Current animation step (used to rotate triangle +}; + + +#endif // OGLWIDGET_H diff --git a/hw04/test.obj b/hw04/test.obj new file mode 100644 index 0000000..678e09f --- /dev/null +++ b/hw04/test.obj @@ -0,0 +1,98 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +v 4 0 0 +v 4 0 4 +v 0 0 4 +v 4 4 0 +v 0 4 4 +v 0 4 0 +v 2 0 0 +v 2 0 2 +v 0 0 2 +v 6 2 0 +v 4 2 2 +v 6 0 2 +v 0 6 0 +v 0 4 2 +v 0 6 2 +v 2 6 0 +v 2 4 0 +v 0 0 6 +v 2 0 4 +v 2 0 6 +v 6 0 0 +v 4 0 2 +v 2 2 4 +v 0 2 6 +v 2 4 2 +v 0 2 4 +v 0 2 0 +v 0 2 2 +v 2 2 0 +v 4 2 0 +f 1 11 13 +f 2 14 16 +f 3 17 19 +f 3 20 17 +f 4 22 24 +f 2 25 16 +f 5 26 12 +f 4 24 28 +f 3 20 19 +f 8 29 15 +f 4 28 22 +f 1 31 13 +f 10 32 18 +f 1 31 11 +f 2 14 25 +f 8 34 21 +f 7 13 12 +f 5 11 12 +f 11 12 13 +f 6 16 15 +f 8 14 15 +f 14 15 16 +f 9 19 18 +f 10 17 18 +f 17 18 19 +f 10 17 21 +f 8 20 21 +f 20 21 17 +f 6 24 23 +f 7 22 23 +f 22 23 24 +f 6 16 26 +f 5 25 26 +f 25 26 16 +f 7 12 23 +f 6 26 23 +f 26 23 12 +f 9 28 27 +f 6 24 27 +f 24 27 28 +f 9 19 29 +f 8 20 29 +f 20 29 19 +f 6 15 27 +f 9 29 27 +f 29 27 15 +f 7 22 30 +f 9 28 30 +f 28 30 22 +f 7 13 32 +f 10 31 32 +f 31 32 13 +f 9 18 30 +f 7 32 30 +f 32 30 18 +f 5 11 33 +f 10 31 33 +f 31 33 11 +f 5 25 34 +f 8 14 34 +f 14 34 25 +f 10 21 33 +f 5 34 33 +f 34 33 21 \ No newline at end of file diff --git a/hw04/CMakeLists.txt b/hw04/CMakeLists.txt new file mode 100644 index 0000000..4d596e0 --- /dev/null +++ b/hw04/CMakeLists.txt @@ -0,0 +1,58 @@ +cmake_minimum_required(VERSION 3.19) +project(OpenGL_Example) + +set(CMAKE_CXX_STANDARD 14) +set(CMAKE_PREFIX_PATH "C:/Qt/Qt5.12.10/5.12.10/mingw73_64/lib/cmake") +set(CMAKE_AUTOMOC ON) +set(CMAKE_AUTORCC ON) +set(CMAKE_AUTOUIC ON) + +set(QT_VERSION 5) +set(REQUIRED_LIBS Core Gui Widgets) +set(REQUIRED_LIBS_QUALIFIED Qt5::Core Qt5::Gui Qt5::Widgets) + +find_package(OpenGL REQUIRED) +include_directories( ${OPENGL_INCLUDE_DIRS} ) +add_executable(OpenGL_Example + main.cpp + mainwindow.cpp + mainwindow.h + oglwidget.cpp + oglwidget.h) + +if (NOT CMAKE_PREFIX_PATH) + message(WARNING "CMAKE_PREFIX_PATH is not defined, you may need to set it " + "(-DCMAKE_PREFIX_PATH=\"path/to/Qt/lib/cmake\" or -DCMAKE_PREFIX_PATH=/usr/include/{host}/qt{version}/ on Ubuntu)") +endif () + +find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED) +target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED}) +target_link_libraries(${PROJECT_NAME} ${OPENGL_LIBRARIES}) +if (WIN32) + set(DEBUG_SUFFIX) + if (CMAKE_BUILD_TYPE MATCHES "Debug") + set(DEBUG_SUFFIX "d") + endif () + set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + if (NOT EXISTS "${QT_INSTALL_PATH}/bin") + set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..") + endif () + endif () + if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory + "$/plugins/platforms/") + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/plugins/platforms/qwindows${DEBUG_SUFFIX}.dll" + "$/plugins/platforms/") + endif () + foreach (QT_LIB ${REQUIRED_LIBS}) + add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy + "${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}${DEBUG_SUFFIX}.dll" + "$") + endforeach (QT_LIB) +endif () diff --git a/hw04/OpenGL_Example.pro b/hw04/OpenGL_Example.pro new file mode 100644 index 0000000..edf9575 --- /dev/null +++ b/hw04/OpenGL_Example.pro @@ -0,0 +1,27 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#------------------------------------------------- +# +# Project created by QtCreator 2016-03-04T23:07:51 +# +#------------------------------------------------- + +QT += core gui opengl + +LIBS += -lOpengl32 + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +TARGET = opengl1 +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + oglwidget.cpp + +HEADERS += mainwindow.h \ + oglwidget.h + +FORMS += mainwindow.ui diff --git a/hw04/main.cpp b/hw04/main.cpp new file mode 100644 index 0000000..515e368 --- /dev/null +++ b/hw04/main.cpp @@ -0,0 +1,14 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/hw04/mainwindow.cpp b/hw04/mainwindow.cpp new file mode 100644 index 0000000..ff258c4 --- /dev/null +++ b/hw04/mainwindow.cpp @@ -0,0 +1,17 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} diff --git a/hw04/mainwindow.h b/hw04/mainwindow.h new file mode 100644 index 0000000..b3aab25 --- /dev/null +++ b/hw04/mainwindow.h @@ -0,0 +1,25 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/hw04/mainwindow.ui b/hw04/mainwindow.ui new file mode 100644 index 0000000..5e1745e --- /dev/null +++ b/hw04/mainwindow.ui @@ -0,0 +1,64 @@ + + + MainWindow + + + + 0 + 0 + 548 + 415 + + + + MainWindow + + + + + + + + 0 + 0 + + + + + + + + + + 0 + 0 + 548 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + OGLWidget + QOpenGLWidget +
oglwidget.h
+ + setParamA(double) + setParamB(double) + +
+
+ + +
diff --git a/hw04/mesh1.obj b/hw04/mesh1.obj new file mode 100644 index 0000000..0af35e5 --- /dev/null +++ b/hw04/mesh1.obj @@ -0,0 +1,8 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +f 1 2 4 +f 2 3 4 +f 3 1 4 +f 3 2 1 \ No newline at end of file diff --git a/hw04/oglwidget.cpp b/hw04/oglwidget.cpp new file mode 100644 index 0000000..3be1f28 --- /dev/null +++ b/hw04/oglwidget.cpp @@ -0,0 +1,404 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#include "oglwidget.h" +#include +#include +#include +#include +#include +#include + +#define PI 3.14159265358979323846 +using namespace std; + +static double alpha = 45.0; // rotation angle + +class Vertex{ +public: + float p[3]; // coordinates + + Vertex(); + Vertex( float point[3]); + Vertex( float x, float y, float z); + void Print() { + cout << p[0] << " " << p[1] << " " << p[2] << endl; + } + // print coordinates on screen + +}; + +Vertex::Vertex(){}; // empty constructor + +Vertex::Vertex(float x, float y, float z){ // constructor with initialization + p[0] = x; + p[1] = y; + p[2] = z; +} + +Vertex::Vertex(float *point) { + copy_n(point, 3, p); +} + +Vertex operator+( Vertex a, Vertex b) // add pts or vectors +{ + return *new Vertex(a.p[0] + b.p[0], a.p[1] + b.p[1],a.p[2] + b.p[2]); +} +Vertex operator-( Vertex a, Vertex b)// subtract pts or vectors +{ + return *new Vertex(a.p[0] - b.p[0], a.p[1] - b.p[1],a.p[2] - b.p[2]); +} +Vertex operator*( float a, Vertex b) // product between scalar and vector +{ + return *new Vertex(a*b.p[0], a*b.p[1], a*b.p[2]); +} +float operator*( Vertex a, Vertex b) // scalar product +{ + return a.p[0] * b.p[0] + a.p[1] * b.p[1] + a.p[2] * b.p[2]; +} +Vertex operator%( Vertex a, Vertex b) // cross product +{ + return *new Vertex(a.p[1]*b.p[2] - a.p[2]*b.p[1], a.p[2]*b.p[0] - a.p[0]*b.p[2], a.p[0]*b.p[1] - a.p[1]*b.p[0]); +} +bool operator==(Vertex a, Vertex b) // check if coords are the same +{ + return (a.p[0] == b.p[0]) && (a.p[1] == b.p[1]) && (a.p[2] == b.p[2]); +} + +class Tri +{ +public: + int iv[3]; // vertex indices + int it[3]; // adjacent triangle indices + int ie[3]; // edge vertex indices + Tri(); + Tri( int i[3]); + Tri( int i, int j, int k); + void Print(); +}; + +void subDivEdgeMidpoint(); + +void saveData(); + +Tri::Tri() { + it[0] = -1; + it[1] = -1; + it[2] = -1; +} + +Tri::Tri(int *i) { + new (this) Tri(); + +} + +Tri::Tri(int i, int j, int k) { + new (this) Tri(); + + iv[0] = i; + iv[1] = j; + iv[2] = k; +} + + +vector pts; +vector valences; // valence list (no. of triangles for every point +vector tris; + +void Tri::Print() { + cout << "Tri:" << endl << " neighbour: " << it[0] << " " << it[1] << " " << it[2] << endl << " - "; + cout << valences[iv[0]] << "x "; + pts[iv[0]].Print(); + cout << " - " << valences[iv[1]] << "x "; + pts[iv[1]].Print(); + cout << " - " << valences[iv[2]] << "x "; + pts[iv[2]].Print(); + cout << endl; +} + + +void loadData() +{ + string fname = R"(C:\CLionProjects\cg\hw04\mesh1.obj)"; + ifstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + string key; + while( file){ + //getline( file, line); + file >> key; + if (key == "v") { + float x, y, z; + file >> x >> y >> z; + Vertex pVertex = *new Vertex(x, y, z); + pts.push_back(pVertex); + valences.push_back(0); + } else if (key == "f") { + int a, b, c; + file >> a >> b >> c; + Tri pTriangle = *new Tri(a-1, b-1, c-1); + tris.push_back(pTriangle); + } + } + file.close(); + + tris.pop_back(); + + subDivEdgeMidpoint(); + subDivEdgeMidpoint(); + + // Connectivity Algorithm + for (int i = 0; i < tris.size(); ++i) { + Tri triag = tris[i]; + int n = 0; + // Search for neighbour + for (int ti = 0; ti < tris.size(); ++ti) { + if (i == ti) continue; + Tri t = tris[ti]; + int count = 0; + for (int x : triag.iv) { + for (int y : t.iv) { + if (x == y) + count++; + } + } + // if two points are the same, they are neighbours + if (count == 2) + triag.it[n++] = ti; + } + + // increase valence for each vertex of triangle + valences[triag.iv[0]]++; + valences[triag.iv[1]]++; + valences[triag.iv[2]]++; + + tris[i] = triag; + } + + for (auto triag : tris) { + triag.Print(); + } + + +// saveData(); +} + +void saveData() { + string fname = R"(C:\CLionProjects\cg\hw04\test2.obj)"; + ofstream file( fname); + if (!file){ + cout << "error opening file" << endl; + return; + } + + for (auto vert : pts) { + file << "v\t" << vert.p[0] << "\t" << vert.p[1] << "\t" << vert.p[2] << endl; + } + + for (auto triag : tris) { + file << "f\t" << triag.iv[0]+1 << "\t" << triag.iv[1]+1 << "\t" << triag.iv[2]+1 << endl; + } + + file.close(); +} + + +void subDivEdgeMidpoint() { + const unsigned long long int amountToSubDiv = tris.size(); + for (int i = 0; i < amountToSubDiv; ++i) { + Tri triag = tris[i]; + + Vertex a = pts[triag.iv[0]]; + Vertex b = pts[triag.iv[1]]; + Vertex c = pts[triag.iv[2]]; + +// int e = pts.size(); + int ei0 = -1; + int ei1 = -1; + int ei2 = -1; + Vertex e0 = 0.5f * (a+b); + Vertex e1 = 0.5f * (c+b); + Vertex e2 = 0.5f * (a+c); + for (int j = 0; j < pts.size(); ++j) { + if (pts[j] == e0) + ei0 = j; + if (pts[j] == e1) + ei1 = j; + if (pts[j] == e2) + ei2 = j; + } + if (ei0 == -1) { + ei0 = pts.size(); + pts.push_back(e0); + valences.push_back(0); + } + if (ei1 == -1) { + ei1 = pts.size(); + pts.push_back(e1); + valences.push_back(0); + } + if (ei2 == -1) { + ei2 = pts.size(); + pts.push_back(e2); + valences.push_back(0); + } + + tris.push_back(*new Tri(triag.iv[2], ei2, ei1)); + tris.push_back(*new Tri(triag.iv[1], ei0, ei1)); + tris.push_back(*new Tri(ei0, ei1, ei2)); + + + triag.iv[1] = ei0; + triag.iv[2] = ei2; + tris[i] = triag; + } +} + +void DrawTriag() { + + glPolygonMode(GL_FRONT_AND_BACK , GL_LINE); + glBegin( GL_TRIANGLES); + for (auto tri : tris) { + Vertex a = pts[tri.iv[0]]; + Vertex b = pts[tri.iv[1]]; + Vertex c = pts[tri.iv[2]]; + Vertex nvec = (b-a)%(c-a); + + glNormal3fv(nvec.p); + + glVertex3fv(a.p); + glVertex3fv(c.p); + glVertex3fv(b.p); + + } + glEnd(); +} + +// initialize Open GL lighting and projection matrix +void InitLightingAndProjection() // to be executed once before drawing +{ + // light positions and colors + GLfloat LightPosition1[4] = { 10, 5, 10, 0}; + GLfloat LightPosition2[4] = { -5, 5, -10, 0}; + GLfloat ColorRedish[4] = { 1.0, .8, .8, 1}; // white with a little bit of red + GLfloat ColorBlueish[4] = { .8, .8, 1.0, 1};// white with a little bit of blue + + glEnable( GL_DEPTH_TEST); // switch on z-buffer + glDepthFunc( GL_LESS); + + glShadeModel( GL_SMOOTH); // Gouraud shading + //glShadeModel( GL_FLAT); + + glEnable( GL_LIGHTING); // use lighting + glLightModeli( GL_LIGHT_MODEL_TWO_SIDE, 1); // draw both sides + + // define and switch on light 0 + glLightfv( GL_LIGHT0, GL_POSITION, LightPosition1); + glLightfv( GL_LIGHT0, GL_DIFFUSE, ColorRedish); + glLightfv( GL_LIGHT0, GL_SPECULAR, ColorRedish); + glEnable( GL_LIGHT0); + + // define and switch on light 1 + glLightfv( GL_LIGHT1, GL_POSITION, LightPosition2); + glLightfv( GL_LIGHT1, GL_DIFFUSE, ColorBlueish); + glLightfv( GL_LIGHT1, GL_SPECULAR, ColorBlueish); + glEnable( GL_LIGHT1); + + glMatrixMode( GL_PROJECTION); // define camera projection + glLoadIdentity(); // reset matrix to identity (otherwise existing matrix will be multiplied with) + glOrtho( -15, 15, -10, 10, -50, 50); // orthogonal projection (xmin xmax ymin ymax zmin zmax) + //glFrustum( -10, 10, -8, 8, 2, 20); // perspective projektion +} + +// define material color properties for front and back side +void SetMaterialColor( int side, float r, float g, float b){ + float amb[4], dif[4], spe[4]; + int i, mat; + + dif[0] = r; // diffuse color as defined by r,g, and b + dif[1] = g; + dif[2] = b; + for( i=0; i<3; i++){ + amb[i] = .1 * dif[i]; // ambient color is 10 percent of diffuse + spe[i] = .5; // specular color is just white / gray + } + amb[3] = dif[3] = spe[3] = 1.0; // alpha component is always 1 + switch( side){ + case 1: mat = GL_FRONT; break; + case 2: mat = GL_BACK; break; + default: mat = GL_FRONT_AND_BACK; break; + } + glMaterialfv( mat, GL_AMBIENT, amb); // define ambient, diffuse and specular components + glMaterialfv( mat, GL_DIFFUSE, dif); + glMaterialfv( mat, GL_SPECULAR, spe); + glMaterialf( mat, GL_SHININESS, 50.0); // Phong constant for the size of highlights +} + + +OGLWidget::OGLWidget(QWidget *parent) // constructor + : QOpenGLWidget(parent) +{ + // Setup the animation timer to fire every x msec + animtimer = new QTimer(this); + animtimer->start( 50 ); + + // Everytime the timer fires, the animation is going one step forward + connect(animtimer, SIGNAL(timeout()), this, SLOT(stepAnimation())); + + animstep = 0; +} + +OGLWidget::~OGLWidget() // destructor +{ +} + +void OGLWidget::stepAnimation() +{ + animstep++; // Increase animation steps + update(); // Trigger redraw of scene with paintGL +} + +void OGLWidget::initializeGL() // initializations to be called once +{ + initializeOpenGLFunctions(); + + InitLightingAndProjection(); // define light sources and projection + + loadData(); +} + +void OGLWidget::paintGL() // draw everything, to be called repeatedly +{ + glEnable(GL_NORMALIZE); // this is necessary when using glScale (keep normals to unit length) + + // set background color + glClearColor(0.8, 0.8, 1.0, 1.0); // bright blue + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // draw the scene + glMatrixMode( GL_MODELVIEW); + glLoadIdentity(); // Reset The Current Modelview Matrix + glTranslated( 0 ,0 ,-10.0); // Move 10 units backwards in z, since camera is at origin + glScaled( 2.0, 2.0, 2.0); // scale objects + glRotated( alpha, 0, 3, 1); // continuous rotation + alpha += 2; + + // define color: 1=front, 2=back, 3=both, followed by r, g, and b + SetMaterialColor( 1, 0, .2, .2); // front color is red + SetMaterialColor( 2, 0.2, 0.2, 1.0); // back color is blue + + // draw a cylinder with default resolution + DrawTriag(); + + // make it appear (before this, it's hidden in the rear buffer) + glFlush(); +} + +void OGLWidget::resizeGL(int w, int h) // called when window size is changed +{ + // adjust viewport transform + glViewport(0,0,w,h); +} + diff --git a/hw04/oglwidget.h b/hw04/oglwidget.h new file mode 100644 index 0000000..a536f6a --- /dev/null +++ b/hw04/oglwidget.h @@ -0,0 +1,34 @@ +// Copyright (c) 2021. Pascal Syma and Antonio Martinez Casadesus . +// All rights reserved. + +#ifndef OGLWIDGET_H +#define OGLWIDGET_H + +#include +#include +#include + +class OGLWidget : public QOpenGLWidget, + protected QOpenGLFunctions +{ + Q_OBJECT + +public: + OGLWidget(QWidget *parent = 0); + ~OGLWidget(); + +public slots: + void stepAnimation(); + +protected: + void initializeGL(); + void resizeGL(int w, int h); + void paintGL(); + +protected: + QTimer* animtimer; // Timer needed to step animation every x msec + int animstep; // Current animation step (used to rotate triangle +}; + + +#endif // OGLWIDGET_H diff --git a/hw04/test.obj b/hw04/test.obj new file mode 100644 index 0000000..678e09f --- /dev/null +++ b/hw04/test.obj @@ -0,0 +1,98 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +v 4 0 0 +v 4 0 4 +v 0 0 4 +v 4 4 0 +v 0 4 4 +v 0 4 0 +v 2 0 0 +v 2 0 2 +v 0 0 2 +v 6 2 0 +v 4 2 2 +v 6 0 2 +v 0 6 0 +v 0 4 2 +v 0 6 2 +v 2 6 0 +v 2 4 0 +v 0 0 6 +v 2 0 4 +v 2 0 6 +v 6 0 0 +v 4 0 2 +v 2 2 4 +v 0 2 6 +v 2 4 2 +v 0 2 4 +v 0 2 0 +v 0 2 2 +v 2 2 0 +v 4 2 0 +f 1 11 13 +f 2 14 16 +f 3 17 19 +f 3 20 17 +f 4 22 24 +f 2 25 16 +f 5 26 12 +f 4 24 28 +f 3 20 19 +f 8 29 15 +f 4 28 22 +f 1 31 13 +f 10 32 18 +f 1 31 11 +f 2 14 25 +f 8 34 21 +f 7 13 12 +f 5 11 12 +f 11 12 13 +f 6 16 15 +f 8 14 15 +f 14 15 16 +f 9 19 18 +f 10 17 18 +f 17 18 19 +f 10 17 21 +f 8 20 21 +f 20 21 17 +f 6 24 23 +f 7 22 23 +f 22 23 24 +f 6 16 26 +f 5 25 26 +f 25 26 16 +f 7 12 23 +f 6 26 23 +f 26 23 12 +f 9 28 27 +f 6 24 27 +f 24 27 28 +f 9 19 29 +f 8 20 29 +f 20 29 19 +f 6 15 27 +f 9 29 27 +f 29 27 15 +f 7 22 30 +f 9 28 30 +f 28 30 22 +f 7 13 32 +f 10 31 32 +f 31 32 13 +f 9 18 30 +f 7 32 30 +f 32 30 18 +f 5 11 33 +f 10 31 33 +f 31 33 11 +f 5 25 34 +f 8 14 34 +f 14 34 25 +f 10 21 33 +f 5 34 33 +f 34 33 21 \ No newline at end of file diff --git a/hw04/test2.obj b/hw04/test2.obj new file mode 100644 index 0000000..23acc46 --- /dev/null +++ b/hw04/test2.obj @@ -0,0 +1,26 @@ +v 0 0 0 +v 8 0 0 +v 0 8 0 +v 0 0 8 +v 4 0 0 +v 4 0 4 +v 0 0 4 +v 4 4 0 +v 0 4 4 +v 0 4 0 +f 1 5 7 +f 2 8 6 +f 3 10 9 +f 3 8 10 +f 4 7 6 +f 2 5 6 +f 5 6 7 +f 4 6 9 +f 3 8 9 +f 8 9 6 +f 4 9 7 +f 1 10 7 +f 10 7 9 +f 1 10 5 +f 2 8 5 +f 8 5 10