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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
| #include "../../Include/Common.h"
using namespace glm; using namespace std;
GLuint program; GLuint tex_object[2]; GLuint tex_index; int index_count; int vertex_count; struct { GLint mv_matrix; GLint proj_matrix; } uniforms;
static const char *render_fs_glsl[] = { "#version 410 core \n" " \n" "uniform sampler2D tex_object; \n" " \n" "in VS_OUT \n" "{ \n" " vec2 tc; \n" "} fs_in; \n" " \n" "out vec4 color; \n" " \n" "void main(void) \n" "{ \n" " color = texture(tex_object, fs_in.tc * vec2(3.0, 1.0)); \n" "} \n" };
static const char *render_vs_glsl[] = { "#version 410 core \n" " \n" "uniform mat4 mv_matrix; \n" "uniform mat4 proj_matrix; \n" " \n" "layout (location = 0) in vec3 position; \n" "layout (location = 1) in vec2 tc; \n" " \n" "out VS_OUT \n" "{ \n" " vec2 tc; \n" "} vs_out; \n" " \n" "void main(void) \n" "{ \n" " vec4 pos_vs = mv_matrix * vec4(position, 1.0); \n" " \n" " vs_out.tc = tc; \n" " \n" " gl_Position = proj_matrix * pos_vs; \n" "} \n" };
void My_Init() { program = glCreateProgram(); GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, render_fs_glsl, NULL); glCompileShader(fs);
GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, render_vs_glsl, NULL); glCompileShader(vs);
glAttachShader(program, vs); glAttachShader(program, fs); printGLShaderLog(vs); printGLShaderLog(fs);
glLinkProgram(program); glUseProgram(program);
uniforms.mv_matrix = glGetUniformLocation(program, "mv_matrix"); uniforms.proj_matrix = glGetUniformLocation(program, "proj_matrix");
#define B 0x00, 0x00, 0x00, 0x00 #define W 0xFF, 0xFF, 0xFF, 0xFF static const GLubyte tex_data[] = { B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, W, B, W, B, W, B, W, B, W, B, W, B, W, B, W, B, }; #undef B #undef W
glGenTextures(1, &tex_object[0]); glBindTexture(GL_TEXTURE_2D, tex_object[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 16, 16, GL_RGBA, GL_UNSIGNED_BYTE, tex_data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
TextureData tex = loadImg("../../Media/Textures/pattern1.png"); glGenTextures(1, &tex_object[1]); glBindTexture(GL_TEXTURE_2D, tex_object[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex.data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
vector<MeshData> meshes; meshes = loadObj("../../Media/Objects/torus_nrms_tc.obj");
GLuint vao; glGenVertexArrays(1, &vao); glBindVertexArray(vao);
GLuint position_buffer; GLuint texcoord_buffer; GLuint index_buffer;
glGenBuffers(1, &position_buffer); glBindBuffer(GL_ARRAY_BUFFER, position_buffer); glBufferData(GL_ARRAY_BUFFER, meshes[0].positions.size() * sizeof(float), meshes[0].positions.data(), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(0);
vertex_count = meshes[0].positions.size() / 3;
glGenBuffers(1, &texcoord_buffer); glBindBuffer(GL_ARRAY_BUFFER, texcoord_buffer); glBufferData(GL_ARRAY_BUFFER, meshes[0].texcoords.size() * sizeof(float), meshes[0].texcoords.data(), GL_STATIC_DRAW); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(1);
glGenBuffers(1, &index_buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, meshes[0].indices.size() * sizeof(unsigned int), meshes[0].indices.data(), GL_STATIC_DRAW); index_count = meshes[0].indices.size();
glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); }
void My_Display() { static const GLfloat gray[] = { 0.2f, 0.2f, 0.2f, 1.0f }; static const GLfloat ones[] = { 1.0f };
glClearBufferfv(GL_COLOR, 0, gray); glClearBufferfv(GL_DEPTH, 0, ones);
float currentTime = glutGet(GLUT_ELAPSED_TIME) * 0.001f;
glBindTexture(GL_TEXTURE_2D, tex_object[tex_index]);
glUseProgram(program);
mat4 proj_matrix = perspective(deg2rad(60.0f), 1.0f, 0.1f, 1000.0f); mat4 mv_matrix = translate(mat4(1.0f), vec3(0.0f, 0.0f, -3.0f)) * rotate(mat4(1.0f), deg2rad((float)currentTime * 19.3f), vec3(0.0f, 1.0f, 0.0f)) * rotate(mat4(1.0f), deg2rad((float)currentTime * 21.1f), vec3(0.0f, 0.0f, 1.0f));
glUniformMatrix4fv(uniforms.mv_matrix, 1, GL_FALSE, &mv_matrix[0][0]); glUniformMatrix4fv(uniforms.proj_matrix, 1, GL_FALSE, &proj_matrix[0][0]);
glDrawElements(GL_TRIANGLES, index_count, GL_UNSIGNED_INT, 0);
glutSwapBuffers(); }
void My_Keyboard(unsigned char key, int x, int y) { switch (key) { case 'T': case 't': tex_index++; if (tex_index > 1) tex_index = 0; glutPostRedisplay(); break; } }
void My_Reshape(int width, int height) { glViewport(0, 0, width, height); }
void My_Timer(int val) { glutPostRedisplay(); glutTimerFunc(16, My_Timer, val); }
int main(int argc, char *argv[]) { chdir(__FILEPATH__); glutInit(&argc, argv); #ifdef _MSC_VER glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); #else glutInitDisplayMode(GLUT_3_2_CORE_PROFILE | GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH); #endif glutInitWindowPosition(100, 100); glutInitWindowSize(600, 600); glutCreateWindow(__FILENAME__); #ifdef _MSC_VER glewInit(); #endif printGLContextInfo(); My_Init();
glutDisplayFunc(My_Display); glutReshapeFunc(My_Reshape); glutKeyboardFunc(My_Keyboard); glutTimerFunc(16, My_Timer, 0);
glutMainLoop(); return 0; }
|