Skip to content
This repository has been archived by the owner on Sep 8, 2022. It is now read-only.

Commit

Permalink
Full 3D cube with random colours
Browse files Browse the repository at this point in the history
Can now display a 3D cube from a hardcoded array of vertices with random vertex colours
  • Loading branch information
TommyGymer committed Sep 24, 2021
1 parent d40ae8c commit 4b7f951
Show file tree
Hide file tree
Showing 19 changed files with 168 additions and 24 deletions.
12 changes: 6 additions & 6 deletions .obsidian/workspace
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,14 @@
"active": "06c71da559f95b34",
"lastOpenFiles": [
"Building/Libraries/Assimp.md",
"Building/vcpkg.md",
"Index.md",
"Language/README.md",
"Building/Requirements.md",
"Untitled.md",
"Building/Libraries/AntTweakBar.md",
"Building/Libraries/Bullet.md",
"Building/vcpkg.md",
"Building/Libraries/GLFW.md",
"Building/Libraries/GLEW.md",
"Building/CMake.md",
"Building/Requirements.md",
"OpenGL/Opening a Window.md",
"Language/C++.md"
"Building/Libraries/GLEW.md"
]
}
6 changes: 5 additions & 1 deletion Building/Libraries/AntTweakBar.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
## AntTweakBar
---
From the AntTweakBar README:
`AntTweakBar is a small and easy-to-use C/C++ library that allows programmers to quickly add a light and intuitive GUI into OpenGL and DirectX based graphic programs to interactively tweak parameters.`
`AntTweakBar is a small and easy-to-use C/C++ library that allows programmers to quickly add a light and intuitive GUI into OpenGL and DirectX based graphic programs to interactively tweak parameters.`

No longer using AntTweakBar as it is not available on vcpkg

Using [[Imgui]] instead as it provides similar functionality and is available on vcpkg
2 changes: 1 addition & 1 deletion Building/Libraries/Assimp.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ Assimp (Asset importer) is an OpenGL library which enables the importing of a nu

[On GitHub](https://github.com/assimp/assimp)

[Building Asset Importer](https://github.com/assimp/assimp/blob/master/BUILDBINARIES_EXAMPLE.bat)
[Building Asset Importer](https://github.com/assimp/assimp/blob/master)

[Open Asset Importer docs](https://assimp-docs.readthedocs.io/en/latest/API/API-Documentation.html)
5 changes: 5 additions & 0 deletions Building/Libraries/Imgui.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
## Imgui
---
A 2D interface library for C++

I will use this for placing a 2D interface over the 3D OpenGL display
3 changes: 2 additions & 1 deletion Building/Requirements.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
2. Visual Studio

#### Libraries
1. [[AntTweakBar]]
1. ~~[[AntTweakBar]]~~
2. [[assimp]]
3. [[bullet]]
4. [[glew]]
5. [[glfw]]
6. [[glm]]
7. [[Imgui]]

These are imported from a local `/external` folder using
```C++
Expand Down
6 changes: 6 additions & 0 deletions Index.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@
- ###### [[GLEW]]
- ###### [[GLFW]]
- ###### [[GLM]]
- ###### [[Imgui]]
- ##### [[CMake]]
- ##### [[vcpkg]]

---
### OpenGL
- ##### [[3D Rendering]]
- ##### [[Opening a Window]]
- ##### Shaders
- ###### [[Fragment Shaders]]
- ###### [[Vertex Shaders]]

---
### Input
Expand Down
Binary file modified RC-PC/.vs/RC-PC/v16/.suo
Binary file not shown.
Binary file modified RC-PC/.vs/RC-PC/v16/Browse.VC.db
Binary file not shown.
15 changes: 15 additions & 0 deletions RC-PC/RC-PC/ColorFragmentShader.fragmentshader
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#version 330 core

// Interpolated values from the vertex shaders
in vec3 fragmentColor;

// Ouput data
out vec3 color;

void main(){

// Output color = color specified in the vertex shader,
// interpolated between all 3 surrounding vertices
color = fragmentColor;

}
129 changes: 121 additions & 8 deletions RC-PC/RC-PC/RC-PC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,27 +81,126 @@ int main()
glBindVertexArray(VertexArrayID);

// Create and compile our GLSL program from the shaders
GLuint programID = LoadShaders("SimpleVertexShader.vertexshader", "SimpleFragmentShader.fragmentshader");

GLuint programID = LoadShaders("TransformVertexShader.vertexshader", "ColorFragmentShader.fragmentshader");

// Get a handle for our "MVP" uniform
GLuint MatrixID = glGetUniformLocation(programID, "MVP");

// Projection matrix : pi radians Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
glm::mat4 Projection = glm::perspective(glm::radians(45.0f), 4.0f / 3.0f, 0.1f, 100.0f);
// Camera matrix
glm::mat4 View = glm::lookAt(
glm::vec3(4, 3, -3), // Camera is at (4,3,-3), in World Space
glm::vec3(0, 0, 0), // and looks at the origin
glm::vec3(0, 1, 0) // Head is up (set to 0,-1,0 to look upside-down)
);
// Model matrix : an identity matrix (model will be at the origin)
glm::mat4 Model = glm::mat4(1.0f);
// Our ModelViewProjection : multiplication of our 3 matrices
glm::mat4 MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around

// A cube has 6 faces with 2 triangles each, so this makes 6*2=12 triangles, and 12*3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
1.0f,-1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
-1.0f,-1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
-1.0f,-1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f,-1.0f,
1.0f,-1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
1.0f, 1.0f,-1.0f,
-1.0f, 1.0f,-1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f,-1.0f,
-1.0f, 1.0f, 1.0f,
1.0f, 1.0f, 1.0f,
-1.0f, 1.0f, 1.0f,
1.0f,-1.0f, 1.0f
};

// One color for each vertex. They were generated randomly.
static const GLfloat g_color_buffer_data[] = {
0.583f, 0.771f, 0.014f,
0.609f, 0.115f, 0.436f,
0.327f, 0.483f, 0.844f,
0.822f, 0.569f, 0.201f,
0.435f, 0.602f, 0.223f,
0.310f, 0.747f, 0.185f,
0.597f, 0.770f, 0.761f,
0.559f, 0.436f, 0.730f,
0.359f, 0.583f, 0.152f,
0.483f, 0.596f, 0.789f,
0.559f, 0.861f, 0.639f,
0.195f, 0.548f, 0.859f,
0.014f, 0.184f, 0.576f,
0.771f, 0.328f, 0.970f,
0.406f, 0.615f, 0.116f,
0.676f, 0.977f, 0.133f,
0.971f, 0.572f, 0.833f,
0.140f, 0.616f, 0.489f,
0.997f, 0.513f, 0.064f,
0.945f, 0.719f, 0.592f,
0.543f, 0.021f, 0.978f,
0.279f, 0.317f, 0.505f,
0.167f, 0.620f, 0.077f,
0.347f, 0.857f, 0.137f,
0.055f, 0.953f, 0.042f,
0.714f, 0.505f, 0.345f,
0.783f, 0.290f, 0.734f,
0.722f, 0.645f, 0.174f,
0.302f, 0.455f, 0.848f,
0.225f, 0.587f, 0.040f,
0.517f, 0.713f, 0.338f,
0.053f, 0.959f, 0.120f,
0.393f, 0.621f, 0.362f,
0.673f, 0.211f, 0.457f,
0.820f, 0.883f, 0.371f,
0.982f, 0.099f, 0.879f
};

GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);

GLuint colorbuffer;
glGenBuffers(1, &colorbuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(g_color_buffer_data), g_color_buffer_data, GL_STATIC_DRAW);

do {
// Clear the screen. It's not mentioned before Tutorial 02, but it can cause flickering, so it's there nonetheless.
glClear(GL_COLOR_BUFFER_BIT);

// Use our shader
glUseProgram(programID);

// 1rst attribute buffer : vertices
// Send our transformation to the currently bound shader,
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);

// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
Expand All @@ -113,10 +212,23 @@ int main()
(void*)0 // array buffer offset
);

// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // 3 indices starting at 0 -> 1 triangle
// 2nd attribute buffer : colors
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, colorbuffer);
glVertexAttribPointer(
1, // attribute. No particular reason for 1, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);

// draw triangles
glDrawArrays(GL_TRIANGLES, 0, 12 * 3); // 12*3 indices starting at 0 -> 12 triangles

glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

// Swap buffers
glfwSwapBuffers(window);
Expand All @@ -130,6 +242,7 @@ int main()
glDeleteBuffers(1, &vertexbuffer);
glDeleteVertexArrays(1, &VertexArrayID);
glDeleteProgram(programID);
glDeleteVertexArrays(1, &VertexArrayID);

// Close OpenGL window and terminate GLFW
glfwTerminate();
Expand Down
14 changes: 7 additions & 7 deletions RC-PC/RC-PC/TransformVertexShader.vertexshader
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertexUV;
layout(location = 1) in vec3 vertexColor;

// Output data ; will be interpolated for each fragment.
out vec2 UV;

out vec3 fragmentColor;
// Values that stay constant for the whole mesh.
uniform mat4 MVP;

void main(){
void main(){

// Output position of the vertex, in clip space : MVP * position
gl_Position = MVP * vec4(vertexPosition_modelspace,1);

// UV of the vertex. No special space for this one.
UV = vertexUV;

// The color of each vertex will be interpolated
// to produce the color of each fragment
fragmentColor = vertexColor;
}

Binary file modified RC-PC/RC-PC/x64/Debug/RC-PC.ilk
Binary file not shown.
Binary file modified RC-PC/RC-PC/x64/Debug/RC-PC.obj
Binary file not shown.
Binary file modified RC-PC/RC-PC/x64/Debug/RC-PC.tlog/RC-PC.write.1u.tlog
Binary file not shown.
Binary file modified RC-PC/RC-PC/x64/Debug/RC-PC.tlog/link.read.1.tlog
Binary file not shown.
Binary file modified RC-PC/RC-PC/x64/Debug/vc142.idb
Binary file not shown.
Binary file modified RC-PC/RC-PC/x64/Debug/vc142.pdb
Binary file not shown.
Binary file modified RC-PC/x64/Debug/RC-PC.exe
Binary file not shown.
Binary file modified RC-PC/x64/Debug/RC-PC.pdb
Binary file not shown.

0 comments on commit 4b7f951

Please sign in to comment.