Browse code

Merge branch 'refactor' into cpp

Robert Cranston authored on 27/02/2024 02:27:38
Showing 13 changed files

1 1
new file mode 100644
... ...
@@ -0,0 +1 @@
1
+/tsbk07
0 2
new file mode 100644
... ...
@@ -0,0 +1,67 @@
1
+/// Inlcudes
2
+#include "VectorUtils4.h"
3
+#include "LittleOBJLoader.h"
4
+#include "LoadTGA.h"
5
+
6
+
7
+/// Generate terrain
8
+Model * GenerateTerrain(const char * filename)
9
+{
10
+	TextureData textureData;
11
+	if (!LoadTGATextureData(filename, &textureData))
12
+		return nullptr;
13
+
14
+	int vertexCount   =  textureData.width    *  textureData.height;
15
+	int triangleCount = (textureData.width-1) * (textureData.height-1) * 2;
16
+
17
+	vec3   * position = (vec3 *)  malloc(sizeof(GLfloat) * 3 * vertexCount);
18
+	vec3   * normal   = (vec3 *)  malloc(sizeof(GLfloat) * 3 * vertexCount);
19
+	vec2   * texCoord = (vec2 *)  malloc(sizeof(GLfloat) * 2 * vertexCount);
20
+	GLuint * indices  = (GLuint *)malloc(sizeof(GLuint)  * 3 * triangleCount);
21
+
22
+	int bytesPerPixel = textureData.bpp / 8;
23
+	for (unsigned int x = 0; x < textureData.width;  ++x)
24
+	for (unsigned int z = 0; z < textureData.height; ++z)
25
+	{
26
+		int indexBase = (x + z * textureData.width);
27
+		// Vertex array. You need to scale this properly.
28
+		GLubyte data = textureData.imageData[indexBase * bytesPerPixel];
29
+		position[indexBase].x = x    / 1.0;
30
+		position[indexBase].y = data / 100.0;
31
+		position[indexBase].z = z    / 1.0;
32
+		// Normal vectors. You need to calculate these.
33
+		normal[indexBase].x = 0.0;
34
+		normal[indexBase].y = 1.0;
35
+		normal[indexBase].z = 0.0;
36
+		// Texture coordinates. You may want to scale them.
37
+		texCoord[indexBase].x = x; // (float)x / textureData.width;
38
+		texCoord[indexBase].y = z; // (float)z / textureData.height;
39
+	}
40
+	for (unsigned int x = 0; x < textureData.width-1;  ++x)
41
+	for (unsigned int z = 0; z < textureData.height-1; ++z)
42
+	{
43
+		int indexBase = (x + z * (textureData.width-1))*6;
44
+		// Triangle 1
45
+		indices[indexBase + 0] = (x+0) + (z+0) * textureData.width;
46
+		indices[indexBase + 1] = (x+0) + (z+1) * textureData.width;
47
+		indices[indexBase + 2] = (x+1) + (z+0) * textureData.width;
48
+		// Triangle 2
49
+		indices[indexBase + 3] = (x+1) + (z+0) * textureData.width;
50
+		indices[indexBase + 4] = (x+0) + (z+1) * textureData.width;
51
+		indices[indexBase + 5] = (x+1) + (z+1) * textureData.width;
52
+	}
53
+
54
+	// Create Model and upload to GPU:
55
+	Model * model = LoadDataToModel(
56
+		position,
57
+		normal,
58
+		texCoord,
59
+		nullptr,
60
+		indices,
61
+		vertexCount,
62
+		triangleCount * 3
63
+	);
64
+
65
+	free(textureData.imageData);
66
+	return model;
67
+}
0 68
deleted file mode 100644
... ...
@@ -1,87 +0,0 @@
1
-// Lab 1-1.
2
-// This is the same as the first simple example in the course book,
3
-// but with a few error checks.
4
-// Remember to copy your file to a new on appropriate places during the lab so you keep old results.
5
-// Note that the files "lab1-1.frag", "lab1-1.vert" are required.
6
-
7
-#include "GL_utilities.h"
8
-#include "MicroGlut.h"
9
-// uses framework OpenGL
10
-// uses framework Cocoa
11
-
12
-// Globals
13
-// Data would normally be read from files
14
-GLfloat vertices[] =
15
-{
16
-	-0.5f,-0.5f,0.0f,
17
-	-0.5f,0.5f,0.0f,
18
-	0.5f,-0.5f,0.0f
19
-};
20
-
21
-// vertex array object
22
-unsigned int vertexArrayObjID;
23
-
24
-void init(void)
25
-{
26
-	// vertex buffer object, used for uploading the geometry
27
-	unsigned int vertexBufferObjID;
28
-	// Reference to shader program
29
-	GLuint program;
30
-
31
-	dumpInfo();
32
-
33
-	// GL inits
34
-	glClearColor(0.2,0.2,0.5,0);
35
-	glDisable(GL_DEPTH_TEST);
36
-	printError("GL inits");
37
-
38
-	// Load and compile shader
39
-	program = loadShaders("lab1-1.vert", "lab1-1.frag");
40
-	printError("init shader");
41
-	
42
-	// Upload geometry to the GPU:
43
-	
44
-	// Allocate and activate Vertex Array Object
45
-	glGenVertexArrays(1, &vertexArrayObjID);
46
-	glBindVertexArray(vertexArrayObjID);
47
-	// Allocate Vertex Buffer Objects
48
-	glGenBuffers(1, &vertexBufferObjID);
49
-	
50
-	// VBO for vertex data
51
-	glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObjID);
52
-	glBufferData(GL_ARRAY_BUFFER, 9*sizeof(GLfloat), vertices, GL_STATIC_DRAW);
53
-	glVertexAttribPointer(glGetAttribLocation(program, "in_Position"), 3, GL_FLOAT, GL_FALSE, 0, 0); 
54
-	glEnableVertexAttribArray(glGetAttribLocation(program, "in_Position"));
55
-	
56
-	// End of upload of geometry
57
-	
58
-	printError("init arrays");
59
-}
60
-
61
-
62
-void display(void)
63
-{
64
-	printError("pre display");
65
-
66
-	// clear the screen
67
-	glClear(GL_COLOR_BUFFER_BIT);
68
-
69
-	glBindVertexArray(vertexArrayObjID);	// Select VAO
70
-	glDrawArrays(GL_TRIANGLES, 0, 3);	// draw object
71
-	
72
-	printError("display");
73
-	
74
-	glutSwapBuffers();
75
-}
76
-
77
-int main(int argc, char *argv[])
78
-{
79
-	glutInit(&argc, argv);
80
-	glutInitContextVersion(3, 2);
81
-	glutInitWindowSize(600, 600);
82
-	glutCreateWindow ("GL3 white triangle example");
83
-	glutDisplayFunc(display); 
84
-	init ();
85
-	glutMainLoop();
86
-	return 0;
87
-}
88 0
deleted file mode 100644
... ...
@@ -1,8 +0,0 @@
1
-#version 150
2
-
3
-in  vec3 in_Position;
4
-
5
-void main(void)
6
-{
7
-	gl_Position = vec4(in_Position, 1.0);
8
-}
9 0
deleted file mode 100644
... ...
@@ -1,11 +0,0 @@
1
-# set this variable to the director in which you saved the common files
2
-commondir = ../common/
3
-
4
-all : lab1-1
5
-
6
-lab1-1 : lab1-1.cpp $(commondir)GL_utilities.c $(commondir)LoadTGA.c $(commondir)Linux/MicroGlut.c
7
-	gcc -Wall -o lab1-1 -I$(commondir) -I../common/Linux -DGL_GLEXT_PROTOTYPES lab1-1.cpp $(commondir)GL_utilities.c $(commondir)LoadTGA.c $(commondir)Linux/MicroGlut.c -lXt -lX11 -lGL -lm -lstdc++
8
-
9
-clean :
10
-	rm lab1-1
11
-
12 0
deleted file mode 100644
... ...
@@ -1,157 +0,0 @@
1
-// Lab 4, terrain generation
2
-
3
-// uses framework Cocoa
4
-// uses framework OpenGL
5
-#define MAIN
6
-#include "MicroGlut.h"
7
-#include "GL_utilities.h"
8
-#include "VectorUtils4.h"
9
-#include "LittleOBJLoader.h"
10
-#include "LoadTGA.h"
11
-
12
-mat4 projectionMatrix;
13
-
14
-Model* GenerateTerrain(TextureData *tex)
15
-{
16
-	int vertexCount = tex->width * tex->height;
17
-	int triangleCount = (tex->width-1) * (tex->height-1) * 2;
18
-	unsigned int x, z;
19
-	
20
-	vec3 *vertexArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount);
21
-	vec3 *normalArray = (vec3 *)malloc(sizeof(GLfloat) * 3 * vertexCount);
22
-	vec2 *texCoordArray = (vec2 *)malloc(sizeof(GLfloat) * 2 * vertexCount);
23
-	GLuint *indexArray = (GLuint *) malloc(sizeof(GLuint) * triangleCount*3);
24
-	
25
-	printf("bpp %d\n", tex->bpp);
26
-	for (x = 0; x < tex->width; x++)
27
-		for (z = 0; z < tex->height; z++)
28
-		{
29
-// Vertex array. You need to scale this properly
30
-			vertexArray[(x + z * tex->width)].x = x / 1.0;
31
-			vertexArray[(x + z * tex->width)].y = tex->imageData[(x + z * tex->width) * (tex->bpp/8)] / 100.0;
32
-			vertexArray[(x + z * tex->width)].z = z / 1.0;
33
-// Normal vectors. You need to calculate these.
34
-			normalArray[(x + z * tex->width)].x = 0.0;
35
-			normalArray[(x + z * tex->width)].y = 1.0;
36
-			normalArray[(x + z * tex->width)].z = 0.0;
37
-// Texture coordinates. You may want to scale them.
38
-			texCoordArray[(x + z * tex->width)].x = x; // (float)x / tex->width;
39
-			texCoordArray[(x + z * tex->width)].y = z; // (float)z / tex->height;
40
-		}
41
-	for (x = 0; x < tex->width-1; x++)
42
-		for (z = 0; z < tex->height-1; z++)
43
-		{
44
-		// Triangle 1
45
-			indexArray[(x + z * (tex->width-1))*6 + 0] = x + z * tex->width;
46
-			indexArray[(x + z * (tex->width-1))*6 + 1] = x + (z+1) * tex->width;
47
-			indexArray[(x + z * (tex->width-1))*6 + 2] = x+1 + z * tex->width;
48
-		// Triangle 2
49
-			indexArray[(x + z * (tex->width-1))*6 + 3] = x+1 + z * tex->width;
50
-			indexArray[(x + z * (tex->width-1))*6 + 4] = x + (z+1) * tex->width;
51
-			indexArray[(x + z * (tex->width-1))*6 + 5] = x+1 + (z+1) * tex->width;
52
-		}
53
-	
54
-	// End of terrain generation
55
-	
56
-	// Create Model and upload to GPU:
57
-
58
-	Model* model = LoadDataToModel(
59
-			vertexArray,
60
-			normalArray,
61
-			texCoordArray,
62
-			NULL,
63
-			indexArray,
64
-			vertexCount,
65
-			triangleCount*3);
66
-
67
-	return model;
68
-}
69
-
70
-
71
-// vertex array object
72
-Model *m, *m2, *tm;
73
-// Reference to shader program
74
-GLuint program;
75
-GLuint tex1, tex2;
76
-TextureData ttex; // terrain
77
-
78
-void init(void)
79
-{
80
-	// GL inits
81
-	glClearColor(0.2,0.2,0.5,0);
82
-	glEnable(GL_DEPTH_TEST);
83
-	glDisable(GL_CULL_FACE);
84
-	printError("GL inits");
85
-
86
-	projectionMatrix = frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 50.0);
87
-
88
-	// Load and compile shader
89
-	program = loadShaders("terrain.vert", "terrain.frag");
90
-	glUseProgram(program);
91
-	printError("init shader");
92
-	
93
-	glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix.m);
94
-	glUniform1i(glGetUniformLocation(program, "tex"), 0); // Texture unit 0
95
-	LoadTGATextureSimple("maskros512.tga", &tex1);
96
-	
97
-// Load terrain data
98
-	
99
-	LoadTGATextureData("44-terrain.tga", &ttex);
100
-	tm = GenerateTerrain(&ttex);
101
-	printError("init terrain");
102
-
103
-	printf("Note: The call to DrawModel will report warnings about inNormal not existing. This is because inNormal is not used in the shader yet so it is optimized away.\n");
104
-}
105
-
106
-void display(void)
107
-{
108
-	// clear the screen
109
-	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110
-	
111
-	mat4 total, modelView, camMatrix;
112
-	
113
-	printError("pre display");
114
-	
115
-	glUseProgram(program);
116
-
117
-	// Build matrix
118
-	
119
-	vec3 cam = vec3(0, 5, 8);
120
-	vec3 lookAtPoint = vec3(2, 0, 2);
121
-	camMatrix = lookAt(cam.x, cam.y, cam.z,
122
-				lookAtPoint.x, lookAtPoint.y, lookAtPoint.z,
123
-				0.0, 1.0, 0.0);
124
-	modelView = IdentityMatrix();
125
-	total = camMatrix * modelView;
126
-	glUniformMatrix4fv(glGetUniformLocation(program, "mdlMatrix"), 1, GL_TRUE, total.m);
127
-	
128
-	glBindTexture(GL_TEXTURE_2D, tex1);		// Bind Our Texture tex1
129
-	DrawModel(tm, program, "inPosition", "inNormal", "inTexCoord");
130
-
131
-	printError("display 2");
132
-	
133
-	glutSwapBuffers();
134
-}
135
-
136
-void mouse(int x, int y)
137
-{
138
-	// This function is included in case you want some hints about using passive mouse movement.
139
-	// Uncomment to see mouse coordinates:
140
-	// printf("%d %d\n", x, y);
141
-}
142
-int main(int argc, char **argv)
143
-{
144
-	glutInit(&argc, argv);
145
-	glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
146
-	glutInitContextVersion(3, 2);
147
-	glutInitWindowSize (600, 600);
148
-	glutCreateWindow ("TSBK07 Lab 4");
149
-	glutDisplayFunc(display);
150
-	init ();
151
-	glutRepeatingTimer(20);
152
-	
153
-	glutPassiveMotionFunc(mouse);
154
-
155
-	glutMainLoop();
156
-	exit(0);
157
-}
158 0
deleted file mode 100644
... ...
@@ -1,10 +0,0 @@
1
-# set this variable to the director in which you saved the common files
2
-commondir = ../common/
3
-
4
-all : lab4-1
5
-
6
-lab4-1 : lab4-1.cpp $(commondir)GL_utilities.c $(commondir)LoadTGA.c $(commondir)Linux/MicroGlut.c
7
-	gcc -Wall -o lab4-1 -I$(commondir) -I../common/Linux -DGL_GLEXT_PROTOTYPES lab4-1.cpp $(commondir)GL_utilities.c $(commondir)LoadTGA.c $(commondir)Linux/MicroGlut.c -lXt -lX11 -lGL -lm
8
-
9
-clean :
10
-	rm lab4-1
11 0
deleted file mode 100644
... ...
@@ -1,10 +0,0 @@
1
-#version 150
2
-
3
-out vec4 outColor;
4
-in vec2 texCoord;
5
-uniform sampler2D tex;
6
-
7
-void main(void)
8
-{
9
-	outColor = texture(tex, texCoord);
10
-}
11 0
deleted file mode 100644
... ...
@@ -1,17 +0,0 @@
1
-#version 150
2
-
3
-in  vec3 inPosition;
4
-in  vec3 inNormal;
5
-in vec2 inTexCoord;
6
-out vec2 texCoord;
7
-
8
-// NY
9
-uniform mat4 projMatrix;
10
-uniform mat4 mdlMatrix;
11
-
12
-void main(void)
13
-{
14
-	mat3 normalMatrix1 = mat3(mdlMatrix);
15
-	texCoord = inTexCoord;
16
-	gl_Position = projMatrix * mdlMatrix * vec4(inPosition, 1.0);
17
-}
18 0
new file mode 100644
... ...
@@ -0,0 +1,11 @@
1
+# set this variable to the director in which you saved the common files
2
+commondir = common/
3
+
4
+all : tsbk07
5
+
6
+tsbk07 : tsbk07.cpp $(commondir)GL_utilities.c $(commondir)LoadTGA.c $(commondir)Linux/MicroGlut.c
7
+	gcc -Wall -o tsbk07 -I$(commondir) -Icommon/Linux -DGL_GLEXT_PROTOTYPES tsbk07.cpp $(commondir)GL_utilities.c $(commondir)LoadTGA.c $(commondir)Linux/MicroGlut.c -lXt -lX11 -lGL -lm -lstdc++
8
+
9
+clean :
10
+	rm tsbk07
11
+
0 12
new file mode 100644
... ...
@@ -0,0 +1,99 @@
1
+/// Inlcudes
2
+#include <iostream>
3
+#include "MicroGlut.h"
4
+#include "GL_utilities.h"
5
+
6
+
7
+/// Defines
8
+#define ARRAY_COUNT(ARRAY) (sizeof(ARRAY)/sizeof(*(ARRAY)))
9
+
10
+
11
+/// Globals
12
+GLuint  vertexArray;
13
+GLfloat positions[][3] =
14
+{
15
+    {-0.5f, -0.5f, 0.0f},
16
+    {-0.5f, +0.5f, 0.0f},
17
+    {+0.5f, -0.5f, 0.0f},
18
+};
19
+
20
+
21
+/// Debug message callback
22
+void APIENTRY debugMessageCallback(
23
+    GLenum          /* source    */,
24
+    GLenum          /* type      */,
25
+    GLuint          /* id        */,
26
+    GLenum          /* severity  */,
27
+    GLsizei         /* length    */,
28
+    GLchar  const *    message,
29
+    void    const * /* userParam */
30
+)
31
+{
32
+    std::cerr << message << std::endl;
33
+}
34
+
35
+
36
+/// Init
37
+void init(void)
38
+{
39
+    // Debug message callback.
40
+    // Requires OpenGL 4.3 or the GL_KHR_debug extension (which is not hardware
41
+    // dependent and supported almost everywhere *except* MacOS).
42
+    glEnable(GL_DEBUG_OUTPUT);
43
+    glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
44
+    glDebugMessageCallback(debugMessageCallback, nullptr);
45
+
46
+    // GL inits.
47
+    glClearColor(0.2, 0.2, 0.5, 0.0);
48
+    glDisable(GL_DEPTH_TEST);
49
+
50
+    // Load and compile shader.
51
+    GLuint program = loadShaders("tsbk07.vert", "tsbk07.frag");
52
+
53
+    // Allocate and activate Vertex Array Object (VAO), used for uploading
54
+    // the geometry.
55
+    glGenVertexArrays(1, &vertexArray);
56
+    glBindVertexArray(vertexArray);
57
+
58
+    // Allocate and activate Vertex Buffer Objects (VBO), for vertex data.
59
+    GLint  positionLocation = glGetAttribLocation(program, "inPosition");
60
+    GLuint positionBuffer;
61
+    glGenBuffers(1, &positionBuffer);
62
+    glBindBuffer(GL_ARRAY_BUFFER, positionBuffer);
63
+    glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);
64
+    glVertexAttribPointer(positionLocation, 3, GL_FLOAT, GL_FALSE, 0, 0);
65
+    glEnableVertexAttribArray(positionLocation);
66
+}
67
+
68
+
69
+/// Display
70
+void display(void)
71
+{
72
+    // Clear the screen.
73
+    glClear(GL_COLOR_BUFFER_BIT);
74
+
75
+    // Select VAO.
76
+    glBindVertexArray(vertexArray);
77
+
78
+    // Draw.
79
+    glDrawArrays(GL_TRIANGLES, 0, ARRAY_COUNT(positions));
80
+
81
+    // Show on the screen.
82
+    glutSwapBuffers();
83
+}
84
+
85
+
86
+/// Main
87
+int main(int argc, char * argv[])
88
+{
89
+    glutInit(&argc, argv);
90
+    glutInitContextVersion(3, 2);
91
+    glutInitWindowSize(600, 600);
92
+    glutCreateWindow("TSBK07");
93
+
94
+    glutDisplayFunc(display);
95
+
96
+    dumpInfo();
97
+    init();
98
+    glutMainLoop();
99
+}
0 100
similarity index 100%
1 101
rename from lab1/lab1-1.frag
2 102
rename to tsbk07.frag
3 103
new file mode 100644
... ...
@@ -0,0 +1,8 @@
1
+#version 150
2
+
3
+in  vec3 inPosition;
4
+
5
+void main(void)
6
+{
7
+	gl_Position = vec4(inPosition, 1.0);
8
+}