Skip to content

Commit

Permalink
Adding new quad version with textures
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Sun authored and Henry Sun committed Jan 25, 2020
1 parent c10041a commit c63ac47
Show file tree
Hide file tree
Showing 4 changed files with 160 additions and 14 deletions.
10 changes: 5 additions & 5 deletions CMakeLists.txt
Expand Up @@ -135,7 +135,7 @@ cmake_minimum_required(VERSION 2.8)

# Application source
set(APPLICATION_SOURCE
triangle_test.cpp
quad_test.cpp
)

#-------------------------------------------------------------------------------
Expand Down Expand Up @@ -165,9 +165,9 @@ link_directories(
#-------------------------------------------------------------------------------
# Add executable
#-------------------------------------------------------------------------------
add_executable(triangle_test ${APPLICATION_SOURCE})
add_executable(quad_test ${APPLICATION_SOURCE})

target_link_libraries( triangle_test
target_link_libraries( quad_test
CGL ${CGL_LIBRARIES}
glew ${GLEW_LIBRARIES}
glfw ${GLFW_LIBRARIES}
Expand All @@ -180,6 +180,6 @@ target_link_libraries( triangle_test
# Platform-specific configurations for target
#-------------------------------------------------------------------------------
if(APPLE)
set_property( TARGET triangle_test APPEND_STRING PROPERTY COMPILE_FLAGS
set_property( TARGET quad_test APPEND_STRING PROPERTY COMPILE_FLAGS
"-Wno-deprecated-declarations -Wno-c++11-extensions")
endif(APPLE)
endif(APPLE)
28 changes: 19 additions & 9 deletions README.md
Expand Up @@ -44,32 +44,42 @@ to have CMake generate the appropriate Makefiles for your system, and then run

make

to actually compile your files. If both of these commands ran without any errors, you should see a ```triangle_test``` executable file under the ```build``` directory. Run this file by typing
to actually compile your files. If both of these commands ran without any errors, you should see a ```quad_test``` executable file under the ```build``` directory. Run this file by typing

./triangle_test
./quad_test

If all goes well, you should see a window pop up with a yellow triangle.
If all goes well, you should see a window pop up with a rectangle with a brick texture.


## Part 2 - Basic C++/Linear Algebra Practice

Fill in the ```mult``` function at the top of ```triangle_test.cpp``` by implementing a 3 dimensional matrix-vector multiplication. Do not use the built-in CGL multiplication function! Read the CGL source files for ```matrix3x3.cpp``` and ```vector3D.cpp``` in order to get a better idea for how to use the two parameters. Once the function is properly implemented, you should see the yellow triangle rotated 90 degrees.
Fill in the ```mult``` function at the top of ```quad_test.cpp``` by implementing a 3 dimensional matrix-vector multiplication. Do not use the built-in CGL multiplication function! Read the CGL source files for ```matrix3x3.cpp``` and ```vector3D.cpp``` in order to get a better idea for how to use the two parameters. Once the function is properly implemented, you should see the yellow triangle rotated 90 degrees.

Remember to run ```make``` to compile your edits.

If you are feeling ambitious, try instantiating the matrix to perform a scaling operation (possible) or shifting operation (not possible, why is that?). We'll learn more formally about these later on in the course.

## Part 3 - Going Above and Beyond (Optional)
## Part 3 - Types of Filters

In computer graphics, almost everything is made out of triangles. Shapes and objects are represented as [meshes composed of hundreds or even thousands of triangles](https://en.wikipedia.org/wiki/Triangle_mesh). Let's try and do something more interesting with our triangle.
There should be two sets of TODO comments in the code - one near a ```glTexParameteri``` function, and another near ```glTexCoord2f``` functions. These two are API calls to the ```OpenGL``` library, the first one setting extra options on texture processing, and another on texture coordinate binding.

Take a look at the ```render``` function in ```triangle_test.cpp```. Notice the following commands:
```OpenGL``` is totally optional now, and we don't need to go into the details (though it may be helpful knowledge, especially for the final project!) For now, simply use the two functions to explore the different filtering functions available in many standard libraries, and compare and contrast their effects. If it's hard to see, we can somewhat 'zoom in' by setting smaller values for the ```glTexCoord2f``` coordinates, essentially binding a smaller region of the texture to the entire rectangle.

How does `Linear` filtering compare to `Nearest`? How does adding `MipMap` options affect the outcome? How are they each implemented? (You will do this for the first assignment)

Optional: For further reading on filtering, specifically in `OpenGL`, as well as a great `OpenGL` beginner resource, [click here](https://learnopengl.com/Getting-started/Textures).

## Part 4 - Going Above and Beyond (Optional)

In computer graphics, almost everything is made out of triangles or quads. Shapes and objects are represented as [meshes composed of hundreds or even thousands of triangles](https://en.wikipedia.org/wiki/Triangle_mesh). Let's try and do something more interesting with our triangle.

Take a look at the ```render``` function in ```quad_test.cpp```. Notice the following commands:

* ```glColor3f(1.0, 1.0, 0.0)``` tells our computer to render the following shapes using the RGB value (1.0, 1.0, 0.0), or yellow.
* ```glBegin(GL_TRIANGLES)``` tells our computer that we want to render triangles with the following vertices we will specify.
* ```glBegin(GL_QUADS)``` tells our computer that we want to render triangles with the following vertices we will specify.
* ```glVertex3f(x, y, z)``` tells our computer to draw a vertex at a particular location.

With the above setup, if we specify six vertices, our computer will render one triangle with the first three vertices, and another triangle with the second three vertices. Try modifying the given render code to draw "something interesting". It could be a cool picture, your name, an interesting pattern... Feel free to share your creative work on Piazza! There should be a thread where you can share your creation.
With the above setup, if we specify eight vertices, our computer will render one quad with the first four vertices, and another quad with the second four vertices (One can do the same for triangles with ```glBegin(GL_TRIANGLES)```. Try modifying the given render code to draw "something interesting". (Note: If you want to disable the textures, you'll need to comment out the line ```glBindTexture(GL_TEXTURE_2D, texture)``` in the beginning of the ```render``` function) It could be a cool picture, your name, an interesting pattern... Feel free to share your creative work on Piazza! There should be a thread where you can share your creation.

## Submission
There is no submission for this homework. However, we highly recommend that you finish the homework, to prevent any issues when you start on the projects for this class.
136 changes: 136 additions & 0 deletions quad_test.cpp
@@ -0,0 +1,136 @@
#include <string>
#include <iostream>

#include "CGL/viewer.h"
#include "CGL/renderer.h"
#include "CGL/vector3D.h"
#include "CGL/matrix3x3.h"

#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"

using namespace std;
using namespace CGL;
unsigned int texture;

/**
* Part 2:
* Write your own matrix vector multiplication function. Do not use the built-in CGL function!
*/
Vector3D mult(Matrix3x3 mat, Vector3D input) {
return input; // FIXME
}

class QuadDrawer : public Renderer {
public:

QuadDrawer() : mat(0, 1, 0, -1, 0, 0, 0, 0, 1), a(-1, .5, 0), b(-1, -.5, 0.0), c(1, -.5, 0.0), d(1,0.5,0.0) { }

~QuadDrawer() { }

string name() {
return "Quad Drawing";
}

string info() {
return "Quad Drawing";
}

void init() {
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

/*TODO: Change GL_NEAREST, and compare the effects of the following filters.
The following are the following potential filter options:
GL_LINEAR
GL_NEAREST_MIPMAP_NEAREST
GL_LINEAR_MIPMAP_NEAREST
GL_NEAREST_MIPMAP_LINEAR
GL_LINEAR_MIPMAP_LINEAR
*/
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
// load and generate the texture
int width, height, nrChannels;
//TODO: (optional) Change the picture here!
unsigned char *data = stbi_load("../../wall.jpg", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
return;
}

void render() {
//glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glColor3f( 1.0, 1.0, 0.0); // Can play with RGB values here :)

Vector3D a_trans = mult(mat, a);
Vector3D b_trans = mult(mat, b);
Vector3D c_trans = mult(mat, c);
Vector3D d_trans = mult(mat, d);

glTexCoord2f(0,0);
glVertex3f(a_trans[0], a_trans[1], a_trans[2]);
/* TODO: change the (0,1) below to (0,.1) to zoom into the texture to see changes. */
glTexCoord2f(0,1);
glVertex3f(b_trans[0], b_trans[1], b_trans[2]);
/* TODO: change the (1,1) below to (.1,.1) to zoom into the texture to see changes. */
glTexCoord2f(1,1);
glVertex3f(c_trans[0], c_trans[1], c_trans[2]);
/* TODO: change the (1,0) to (.1,0) to zoom into the texture to see changes. */
glTexCoord2f(1,0);
glVertex3f(d_trans[0], d_trans[1], d_trans[2]);
glDeleteTextures(1, &texture);
glDisable(GL_TEXTURE_2D);
glEnd();
}

void resize(size_t w, size_t h) {

this->w = w;
this->h = h;

return;
}

private:
// frame buffer size
size_t w, h;
Matrix3x3 mat;
Vector3D a;
Vector3D b;
Vector3D c;
Vector3D d;

};

int main( int argc, char** argv ) {

// create viewer
Viewer viewer = Viewer();

// defined a user space renderer
Renderer* renderer = new QuadDrawer();

// set user space renderer
viewer.set_renderer(renderer);

// start the viewer
viewer.init();
viewer.start();

return 0;
}

Binary file added wall.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c63ac47

Please sign in to comment.