der Informatik Moritz Mhlhausen Prof. Marcus Magnor - - PowerPoint PPT Presentation

der informatik
SMART_READER_LITE
LIVE PREVIEW

der Informatik Moritz Mhlhausen Prof. Marcus Magnor - - PowerPoint PPT Presentation

Praktische Aspekte der Informatik Moritz Mhlhausen Prof. Marcus Magnor https://graphics.tu-bs.de/teaching/ss19/padi/ https://graphics.tu-bs.de/teaching/ss19/padi/ 1 Introduction What you need to know.


slide-1
SLIDE 1

Praktische Aspekte der Informatik

Moritz Mühlhausen

  • Prof. Marcus Magnor

https://graphics.tu-bs.de/teaching/ss19/padi/

https://graphics.tu-bs.de/teaching/ss19/padi/ 1

slide-2
SLIDE 2

Introduction

What you need to know.

https://graphics.tu-bs.de/teaching/ss19/padi/ 2

slide-3
SLIDE 3

What?

Praktische Aspekte Der Informatik = PADI

You will learn…

… how to program in C++. … how to work with libraries. … how to debug your code. … how to optimize your code. … how to organize your code. … much more!

https://graphics.tu-bs.de/teaching/ss19/padi/ 3

slide-4
SLIDE 4

Why?

  • If you’re a bachelor’s student…

… you already know Java! … you will have to do the SEP and the Teamprojekt. … you will have to write a Bachelor’s thesis.

  • If you’re a master’s student…

… you may want to write a Projektarbeit. … you will have to write a Master’s thesis.

  • Eventually, all of you will work in the real world!

https://graphics.tu-bs.de/teaching/ss19/padi/ 4

slide-5
SLIDE 5

How?

  • First Part: Weekly Assignments
  • Brief talk (15-20 min).
  • Work on assignment in small groups.
  • Develop a proposal for a project.
  • Second Part: Your Project
  • Brief talk (15-20 min).
  • Work on your own project.
  • Give regular updates on your progress.
  • Prototype: Present your current state
  • 19.06.2019 + 21.06.2019
  • Final Week: Present your Project
  • 10.07.2019 + 12.07.2019

https://graphics.tu-bs.de/teaching/ss19/padi/ 5

slide-6
SLIDE 6

About your Project

  • Your software project…

… can be anything you want (more or less) … must be written in C++! ... should highlight the skills you have learned. … must use at least one external library. … must have a visual component.

  • Your project will be graded on…

… function & quality. … polish & presentation. … whether you’re a Ba/Ma student.

  • You alone are responsible for your project!

https://graphics.tu-bs.de/teaching/ss19/padi/ 6

slide-7
SLIDE 7

Example: Bachelor’s Project

TU Abalone

  • Complete ruleset of

the Abalone game.

  • Various game setups.
  • Local multiplayer.
  • Responsive user interface.
  • Fancy animations!
  • Save / Load / Undo / …

https://graphics.tu-bs.de/teaching/ss19/padi/ 7

slide-8
SLIDE 8

Example: Bachelor’s Project

Project SnackmiX

  • Complete ruleset
  • f Chinese Chess.
  • Movement Hints.
  • Local multiplayer.
  • Cool 3D graphics!
  • Fancy animations!
  • Special Effects!

https://graphics.tu-bs.de/teaching/ss19/padi/ 8

slide-9
SLIDE 9

Example: Master’s Project

Closer

  • Jump & Run.
  • Simple 2D graphics.
  • Fancy design and animations.
  • Levels loaded from custom format.
  • Engine architecture from scratch.
  • Various helpful debug views.
  • Music and Sound Effects.

https://graphics.tu-bs.de/teaching/ss19/padi/ 9

slide-10
SLIDE 10

Final remarks

  • Be present!
  • Otherwise, you might miss important announcements.
  • Most importantly, I cannot help you if you’re not here.
  • A computer scientist must work in any environment!
  • You may do weekly assignments on your own computer.
  • Your project should run in the CIP-Pool

If it does not, you are responsible to be able to present it.

  • Challenge: Set up your project for multiple platforms.

https://graphics.tu-bs.de/teaching/ss19/padi/ 10

slide-11
SLIDE 11

Final remarks

  • Do not underestimate the workload!

6 credits 180 hours!

PADI is a lot of work! Pick a project you love!

https://graphics.tu-bs.de/teaching/ss19/padi/ 11

slide-12
SLIDE 12

C++ Basics

Compiler, Classes, Pointers, Inheritance and more!

https://graphics.tu-bs.de/teaching/ss19/padi/ 12

slide-13
SLIDE 13

Further Reading

Warning!

The following slides are meant to give you a very superficial introduction to C++ basics. If you want to learn more, have a look at:

http://www.cplusplus.com http://www.cppreference.com http://www.learncpp.com

https://graphics.tu-bs.de/teaching/ss19/padi/ 13

slide-14
SLIDE 14

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 14

slide-15
SLIDE 15

What is C++?

Benefits:

  • Very similar to Java (which you already know!)
  • Object oriented
  • Fast, powerful, and widely used
  • Many libraries and much code already available
  • A lot of tutorials and help.

Drawbacks:

  • Not very intuitive / fast to program
  • No Garbage Collector
  • Error prone

https://graphics.tu-bs.de/teaching/ss19/padi/ 15

slide-16
SLIDE 16

Hello World!

main.cpp // Import I/O functionality #include <iostream> // This is the main entry point. int main() { // Print "Hello World!" and end the program std::cout << "Hello World!" << std::endl; return 0; }

https://graphics.tu-bs.de/teaching/ss19/padi/ 16

slide-17
SLIDE 17

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 17

slide-18
SLIDE 18

Preprocessor, Compiler, and Linker

  • When creating an executable, your code is…

… preprocessed (“glued” together) … compiled (translated to be machine-readable) … and linked (all the parts are connected)

  • Different errors can occur at different stages
  • Not always easy to understand.
  • More on that next week.

https://graphics.tu-bs.de/teaching/ss19/padi/ 18

slide-19
SLIDE 19

Your first class (.h)

MyClass.h #ifndef MYCLASS_H #define MYCLASS_H class MyClass { public: MyClass(); // Constructor void doMagic(); // Some member function private: int counter; // Some member variable }; #endif

https://graphics.tu-bs.de/teaching/ss19/padi/ 19

slide-20
SLIDE 20

Your first class (.cpp)

MyClass.cpp #include <iostream> #include "MyClass.h" // Namespace::functionName() { definition; } MyClass::MyClass() { // Constructor this->counter = 0; } void MyClass::doMagic() { // Some member function std::cout << "MyClass::doMagic has been called" << ++this->counter << " times." << std::endl; }

https://graphics.tu-bs.de/teaching/ss19/padi/ 20

slide-21
SLIDE 21

Your first class (in action!)

main.cpp #include "MyClass.h" int main() { // Create an object of class MyClass MyClass my_object; for (int i = 0; i < 10; ++i) my_object.doMagic(); // Do magic! return 0; }

https://graphics.tu-bs.de/teaching/ss19/padi/ 21

slide-22
SLIDE 22

Building your code

MyClass.h <iostream> main.cpp MyClass.cpp

  • 1. Preprocessor
  • 2. Compiler

[main.o] [MyClass.o]

  • 3. Linker

Hello

https://graphics.tu-bs.de/teaching/ss19/padi/ 22

slide-23
SLIDE 23

Structs

Vector3D.h #ifndef VECTOR3D_H #define VECTOR3D_H struct Vector3D { float x,y,z; // public members }; #endif

They work just like classes*!

*but they are commonly used for storing data.

https://graphics.tu-bs.de/teaching/ss19/padi/ 23

slide-24
SLIDE 24

Structs

Vector3D.h #ifndef VECTOR3D_H #define VECTOR3D_H struct Vector3D { Vector3D(float x = 0, float y = 0, float z = 0) { this->x = x; this->y = y; this->z = z; } float x,y,z; }; #endif

https://graphics.tu-bs.de/teaching/ss19/padi/ 24

slide-25
SLIDE 25

Structs

Vector3D.h #ifndef VECTOR3D_H #define VECTOR3D_H struct Vector3D { Vector3D(float x = 0, float y = 0, float z = 0) { this->x = x; this->y = y; this->z = z; } float x,y,z; }; Vector3D operator+(Vector3D a, Vector3D b) { // a+b return Vector3D(a.x+b.x, a.y+b.y, a.z+b.z); } #endif

https://graphics.tu-bs.de/teaching/ss19/padi/ 25

slide-26
SLIDE 26

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 26

slide-27
SLIDE 27

Generic Data Structures

  • Many generic containers already exist.
  • There are lists, sets, stacks, maps...
  • There are algorithms, too!
  • You can count, find, merge, sort, replace…
  • They are all fast and well documented.
  • http://www.cplusplus.com/reference/stl/
  • http://www.cplusplus.com/reference/algorithm/

https://graphics.tu-bs.de/teaching/ss19/padi/ 27

slide-28
SLIDE 28

Generic Data Structures

STL Vector #include <vector> [...] // Create a vector and add some values std::vector<float> values; values.push_back(1.1); values.push_back(2.3); values.push_back(4.2); std::cout << "Size of vector: " << values.size() << std::endl; // Size: 3 std::cout << "Value at index 1: " << values[1] << std::endl; // values[1]: 2.3 values.clear(); std::cout << "Size of vector: " << values.size() << std::endl; // Size: 0

https://graphics.tu-bs.de/teaching/ss19/padi/ 28

slide-29
SLIDE 29

Generic Data Structures

STL Vector #include <vector> #include "Vector3D.h" [...] // A vector of Vector3D objects std::vector<Vector3D> vectors; Vector3D v(1.1, 2.3, 4.2); vectors.push_back(v); vectors.push_back( Vector3D(1.1, 2.3, 4.2) );

https://graphics.tu-bs.de/teaching/ss19/padi/ 29

slide-30
SLIDE 30

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 30

slide-31
SLIDE 31

Pointers and References

  • Easy to spot: & and *
  • Reference:

float & value;

  • Pointer:

float * value;

  • References are easier to use.
  • There is no special syntax.
  • You cannot change where they point.
  • Pointers give you more freedom.
  • You can change pointers at runtime.
  • You have to be careful where you point.
  • You have to clean up after yourself: new and delete

https://graphics.tu-bs.de/teaching/ss19/padi/ 31

slide-32
SLIDE 32

Pointers and References

int a = 5; // integer save values

https://graphics.tu-bs.de/teaching/ss19/padi/ 32

1 1

slide-33
SLIDE 33

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL)

https://graphics.tu-bs.de/teaching/ss19/padi/ 33

1 1

slide-34
SLIDE 34

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL) b = &a; // b save address of a

https://graphics.tu-bs.de/teaching/ss19/padi/ 34

1 1

slide-35
SLIDE 35

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL) b = &a; // b save address of a int & c; // error! NULL not allowed

https://graphics.tu-bs.de/teaching/ss19/padi/ 35

1 1

slide-36
SLIDE 36

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL) b = &a; // b save address of a int & c; // error! NULL not allowed int & c = a;// reference to a

https://graphics.tu-bs.de/teaching/ss19/padi/ 36

1 1

slide-37
SLIDE 37

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL) b = &a; // b save address of a int & c; // error! NULL not allowed int & c = a;// reference to a *b = 6; // change value of a

https://graphics.tu-bs.de/teaching/ss19/padi/ 37

1 1

slide-38
SLIDE 38

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL) b = &a; // b save address of a int & c; // error! NULL not allowed int & c = a;// reference to a *b = 6; // change value of a int d = 7;

https://graphics.tu-bs.de/teaching/ss19/padi/ 38

1 1 1 1 1

slide-39
SLIDE 39

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL) b = &a; // b save address of a int & c; // error! NULL not allowed int & c = a;// reference to a *b = 6; // change value of a int d = 7; b = &d; // change saved address

https://graphics.tu-bs.de/teaching/ss19/padi/ 39

1 1 1 1 1

slide-40
SLIDE 40

Pointers and References

int a = 5; // integer save values int * b; // pointer save addresses (NULL) b = &a; // b save address of a int & c; // error! NULL not allowed int & c = a;// reference to a *b = 6; // change value of a int d = 7; b = &d; // change saved address c = d; // change value of a

https://graphics.tu-bs.de/teaching/ss19/padi/ 40

1 1 1 1 1 1

slide-41
SLIDE 41

Pointers and References

float fooCopy(float float_copy) { return ++float_copy; } float fooRef(float & float_reference) { return ++float_reference; } float fooConstRef(float const& float_const_reference) { return float_const_reference+1; } float fooPtr(float * float_pointer) { return ++(*float_pointer); } float fooConstPtr(float const* float_const_pointer) { return (*float_const_pointer)+1; }

https://graphics.tu-bs.de/teaching/ss19/padi/ 41

slide-42
SLIDE 42

Prevent unwanted changes using const!

const usually applies to what’s left of it…

float const value=.5f; // const float float const* ptr; // pointer to a const float float *const ptr; // const pointer to a float float const*const ptr; // const pointer to const float

… except when it doesn’t…

const float value=.5f; // float const value const float * ptr; // float const* ptr; const float *const ptr;// float const*const ptr;

https://graphics.tu-bs.de/teaching/ss19/padi/ 42

slide-43
SLIDE 43

HEAP and Stack

  • By default, objects are allocated on the stack.
  • Automatically deleted at the end of their scope.
  • You can access memory addresses using pointers*

*but be careful with that!

  • If you use new, objects are stored in the heap.
  • Manually clean up using delete.
  • Pointers themselves are often on the stack,

i.e. they are automatically cleaned up, but the allocated object they point to is not!

  • If no valid pointer to an allocated object remains,

you have created a memory leak!

https://graphics.tu-bs.de/teaching/ss19/padi/ 43

slide-44
SLIDE 44

Memory allocation / deallocation

Explicitly allocate/deallocate memory:

Vector3D* my_ptr = new Vector3D(1.1, 2.3, 4.2); // … do stuff … delete my_ptr;

You can also allocate arrays:

// allocate 3 consecutive floats in memory float* my_ptr = new float[3]; my_ptr[0] = 4.2f; // initialize your values… […] float val = my_ptr[1]; // access the second float float err = my_ptr[5]; // this might/should crash! delete[] my_ptr; // deallocate the entire array

https://graphics.tu-bs.de/teaching/ss19/padi/ 44

slide-45
SLIDE 45

Pointer syntax

  • Important Operators when working with pointers:
  • Dereference using . and ->
  • Unfortunately, there’s also & and *.

Vector3D* vec = new Vector3D; vec->x = 4.2f; (*vec).x = 4.2f; […] Vector3D other_vector; vec = &other_vector; […] float value = fooPtr(&vec->x);

https://graphics.tu-bs.de/teaching/ss19/padi/ 45

slide-46
SLIDE 46

Why use pointers?

  • When you use pointers…
  • you can decide when your variables die.
  • you can control memory allocation.
  • You can share them—but beware!
  • Helpful advice:
  • When passing anything bigger than a primitive,

pass a const&

  • When an argument variable is altered by a function,

pass a pointer

https://graphics.tu-bs.de/teaching/ss19/padi/ 46

slide-47
SLIDE 47

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 47

slide-48
SLIDE 48

Inheritance

  • Classes may inherit from multiple other classes
  • Classes can be abstract (just like in JAVA)
  • You can define your own…

… Constructors, … Destructors, … and (assignment) operators

  • Virtual classes allow method overriding

https://graphics.tu-bs.de/teaching/ss19/padi/ 48

slide-49
SLIDE 49

Inheritance

main.cpp class A { // Parent class virtual std::string foo() { return "I am A!"; } }; class B : public A { // Child class std::string foo() { return "I am B!"; } }; int main() { std::vector< A* > vec; vec.push_back(new A); vec.push_back(new B); // an A* pointer is also valid for B* for (int i = 0; i < vec.size(); ++i) std::cout << vec[i]->foo() << std::endl; return 0; }

https://graphics.tu-bs.de/teaching/ss19/padi/ 49

slide-50
SLIDE 50

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 50

slide-51
SLIDE 51

File Input

void MyClass::readFile(std::string const& filename) { // Open the file for reading std::ifstream fin(filename.c_str()); // Read the title (string) fin >> this->title; // Read the points (float) int num_points; fin >> num_points; for (int i = 0; i < num_points; ++i) { float value; fin >> value; this->values.push_back(value); } fin.close(); }

https://graphics.tu-bs.de/teaching/ss19/padi/ 51

slide-52
SLIDE 52

File Output

void MyClass::writeFile(std::string const& fname) const { // Open the file for writing std::ofstream fout(fname.c_str(), std::ios::out); // Write the title and number of points fout << this->title + "*1.5" << std::endl; fout << this->values.size() << std::endl ; // Write the point values (multiplied by 1.5) for (int i = 0; i < values.size(); ++i) fout << (values[i]*1.5f) << std::endl; fout.close(); }

https://graphics.tu-bs.de/teaching/ss19/padi/ 52

slide-53
SLIDE 53

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 53

slide-54
SLIDE 54

Assignment

world.txt block 0 -0.125 0 20 0.25 20 # x,y,z, width, height, depth sphere 8 6 7 2 16 # x,y,z, radius, tesselation sphere 8 8 7 1.5 16 block 8 3 7 0.5 6 0.5 block -6 2.5 2 6 5 8 block -4 3.5 -5 10 7 6

https://graphics.tu-bs.de/teaching/ss19/padi/ 54

slide-55
SLIDE 55

Assignment

Wavefront .obj file format # List of vertices (nodes), with (x,y,z) coordinates v 0.25 0.53 0.763 # vertex 1 v ... # vertex 2 v ... # List of polygons (faces), with vertex indices f 1 2 3 # triangle f 2 3 4 f 2 5 6 4 # quad f ...

https://graphics.tu-bs.de/teaching/ss19/padi/ 55

slide-56
SLIDE 56

Assignment

  • A. Look at the examples

Have a look at, compile, and understand all examples.

  • B. Hello World!

Implement a small C++ program that reads the world.txt file and converts it to an .obj file. Look at assignment_stub/main.cpp for more information. Hint: If you’re stuck, the internet will help!

  • C. Have Fun!

How about adding more object types? (Cylinders, Pyramids, …) How about adding rotation? …

https://graphics.tu-bs.de/teaching/ss19/padi/ 56

slide-57
SLIDE 57

Outline

Introduction Building, Classes and Structs Standard Template Library Pointers and References Inheritance File I/O Assignment

https://graphics.tu-bs.de/teaching/ss19/padi/ 57

slide-58
SLIDE 58

Closing thoughts

  • PADI Philosophy
  • Gap knowledge.
  • Practical skills that glue together different aspects of CS.
  • Heterogeneous Audience
  • Some of you know nothing, some know a lot.
  • Adjust your ambitions for the final project.
  • Help each other!
  • Improve your individual programming skills!

https://graphics.tu-bs.de/teaching/ss19/padi/ 58

slide-59
SLIDE 59

Presentation

Wichtig!

Die Termine für die abschließenden Kolloquien sind: 10.07.2019 13:15 Uhr 12.07.2019 13:15 Uhr Ihr müsst PADI beim Prüfungsamt anmelden!

https://graphics.tu-bs.de/teaching/ss19/padi/ 59