generated from cal-cs184/hw0-intro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quad_test.cpp
142 lines (115 loc) · 3.62 KB
/
quad_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#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) {
/* TODO */
return input;
}
class QuadDrawer : public Renderer {
public:
QuadDrawer() : mat(1. / sqrt(2), 1. / sqrt(2), 0, -1. / sqrt(2), 1. / sqrt(2), 0, 0, 0, 1), a(-.75, .5, 0), b(-.75, -.5, 0.0), c(.75, -.5, 0.0), d(.75,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!
#ifdef _MSC_VER
unsigned char* data = stbi_load("../../../wall.jpg", &width, &height, &nrChannels, 0);
#else
unsigned char* data = stbi_load("../wall.jpg", &width, &height, &nrChannels, 0);
#endif
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); // Comment this out to disable textures
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(600, 600);
viewer.start();
return 0;
}