Solution

Files changed (3) hide show
  1. lab2-textures/lab2_main.cpp +16 -1
  2. lab2-textures/simple.frag +2 -1
  3. lab2-textures/simple.vert +3 -0
lab2-textures/lab2_main.cpp CHANGED
@@ -43,7 +43,7 @@ GLuint shaderProgram;
43
43
 
44
44
  // The vertexArrayObject here will hold the pointers to
45
45
  // the vertex data (in positionBuffer) and color data per vertex (in colorBuffer)
46
- GLuint positionBuffer, colorBuffer, indexBuffer, vertexArrayObject;
46
+ GLuint positionBuffer, colorBuffer, indexBuffer, texcoordBuffer, vertexArrayObject;
47
47
 
48
48
 
49
49
 
@@ -90,6 +90,21 @@ void initialize()
90
90
  // Set up the attrib pointer.
91
91
  // Enable the vertex attrib array.
92
92
  ///////////////////////////////////////////////////////////////////////////
93
+ float texcoords[] = {
94
+ 0.0f, 0.0f, // (u,v) for v0
95
+ 0.0f, 1.0f, // (u,v) for v1
96
+ 1.0f, 1.0f, // (u,v) for v2
97
+ 1.0f, 0.0f // (u,v) for v3
98
+ };
99
+ // Create a handle for the vertex texture buffer
100
+ glGenBuffers(1, &texcoordBuffer);
101
+ // Set the newly created buffer as the current one
102
+ glBindBuffer(GL_ARRAY_BUFFER, texcoordBuffer);
103
+ // Send the texture coord data to the current buffer
104
+ glBufferData(GL_ARRAY_BUFFER, labhelper::array_length(texcoords) * sizeof(float), texcoords,
105
+ GL_STATIC_DRAW);
106
+ glVertexAttribPointer(1, 2, GL_FLOAT, false /*normalized*/, 0 /*stride*/, 0 /*offset*/);
107
+ glEnableVertexAttribArray(1);
93
108
 
94
109
  ///////////////////////////////////////////////////////////////////////////
95
110
  // Create the element array buffer object
lab2-textures/simple.frag CHANGED
@@ -4,6 +4,7 @@
4
4
  precision highp float;
5
5
 
6
6
  // Task 1: Receive the texture coordinates
7
+ in vec2 texCoord;
7
8
 
8
9
  // Task 3.4: Receive the texture as a uniform
9
10
 
@@ -13,5 +14,5 @@ void main()
13
14
  {
14
15
  // Task 1: Use the texture coordinates for the x,y of the color output
15
16
  // Task 3.5: Sample the texture with the texture coordinates and use that for the color
16
- fragmentColor = vec4(1.0, 1.0, 1.0, 1.0);
17
+ fragmentColor = vec4(texCoord.x, texCoord.y, 0.0, 0.0);
17
18
  }
lab2-textures/simple.vert CHANGED
@@ -3,6 +3,8 @@
3
3
  layout(location = 0) in vec3 in_position;
4
4
 
5
5
  // Task 1: Add input and output variables for the texture coordinates
6
+ layout(location = 1) in vec2 in_texCoord; // incoming texcoord from the texcoord array
7
+ out vec2 texCoord; // outgoing interpolated texcoord to fragshader
6
8
 
7
9
  uniform mat4 projectionMatrix;
8
10
  uniform vec3 cameraPosition;
@@ -21,4 +23,5 @@ void main()
21
23
  gl_Position = projectionMatrix * pos;
22
24
 
23
25
  // Task 1: Copy the value received for the texcoord to the out variable sent to the fragment shader
26
+ texCoord = in_texCoord;
24
27
  }