Creating a grid of squares
This commit is contained in:
parent
2c4b23b925
commit
8691d53b1a
@ -22,9 +22,3 @@ set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
|
||||
find_package(OpenGL REQUIRED)
|
||||
target_link_libraries(GraphicsTest OpenGL::GL)
|
||||
|
||||
|
||||
file(MAKE_DIRECTORY ./assets)
|
||||
file(MAKE_DIRECTORY ./assets/textures)
|
||||
file(COPY ./shaders DESTINATION ./assets)
|
||||
file(GLOB IMAGE_FILES ./images/*)
|
||||
file(COPY ${IMAGE_FILES} DESTINATION ./assets/textures)
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -1,3 +1,3 @@
|
||||
20240220-0435
|
||||
20240222-0551
|
||||
Experimental
|
||||
Experimental
|
||||
|
||||
81
data.cpp
81
data.cpp
@ -1,52 +1,41 @@
|
||||
float vertices[] = {
|
||||
// Positions // Texture Coords
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
|
||||
0.5f, -0.5f, 0.5f, 1.0f, 1.0f,
|
||||
float* makeGridPoints(unsigned int X=2, unsigned int Y=2) {
|
||||
int numPoints = 5*X*Y;
|
||||
float xStep = 1/ ((float)(X-1));
|
||||
float yStep = 1/ ((float)(Y-1));
|
||||
float* ret = new float[numPoints];
|
||||
for (int i = 0; i < Y; i++) {
|
||||
for (int j = 0; j < X; j++) {
|
||||
ret[5*(i*X+j)] = xStep*j-0.5;
|
||||
ret[5*(i*X+j)+1] = yStep*i-0.5;
|
||||
ret[5*(i*X+j)+2] = 0.0;
|
||||
ret[5*(i*X+j)+3] = yStep*i;
|
||||
ret[5*(i*X+j)+4] = xStep*j;
|
||||
}
|
||||
}
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
return ret;
|
||||
}
|
||||
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
0.5f, 0.5f, -0.5f, 1.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
|
||||
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
unsigned int* makeGridIndex(unsigned int X=2, unsigned int Y=2) {
|
||||
unsigned int tiles = (X-1)*(Y-1);
|
||||
unsigned int* ret = new unsigned int[tiles*6];
|
||||
unsigned int tileInd;
|
||||
for (int i = 0; i < Y-1; i++) {
|
||||
for (int j = 0; j < X-1; j++) {
|
||||
tileInd = i*(X-1)+j;
|
||||
ret[6*tileInd] = i*X+j;
|
||||
ret[6*tileInd+1] = (i+1)*X+j;
|
||||
ret[6*tileInd+2] = (i+1)*X+(j+1);
|
||||
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
|
||||
-0.5f, 0.5f, -0.5f, 1.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
|
||||
-0.5f, -0.5f, 0.5f, 0.0f, 1.0f,
|
||||
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f
|
||||
ret[6*tileInd+3] = i*X+j;
|
||||
ret[6*tileInd+4] = i*X+j+1;
|
||||
ret[6*tileInd+5] = (i+1)*X+(j+1);
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
/*
|
||||
unsigned int indices[] = { // note that we start from 0!
|
||||
0, 1, 3, // first triangle
|
||||
1, 2, 3
|
||||
};
|
||||
*/
|
||||
return ret;
|
||||
}
|
||||
62
funcs.cpp
62
funcs.cpp
@ -1,73 +1,11 @@
|
||||
#include "funcs.h"
|
||||
|
||||
extern float deltaTime;
|
||||
extern float lastX, lastY;
|
||||
extern const float mouseSensitivity = 0.08f;
|
||||
extern float zoom;
|
||||
|
||||
glm::vec3 camPos = glm::vec3(0.0f, 0.0f, 3.0f);
|
||||
glm::vec3 camDir = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||
glm::vec3 camUp = glm::vec3(0.0f, 1.0f, 0.0f);
|
||||
float yaw=-90.0f, pitch=0.0f, roll=0.0f;
|
||||
bool firstMouse = true;
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
||||
glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void mouse_callback(GLFWwindow*, double xpos, double ypos) {
|
||||
if (firstMouse) {
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
firstMouse = false;
|
||||
}
|
||||
|
||||
float xOffset = xpos - lastX;
|
||||
float yOffset = lastY - ypos;
|
||||
lastX = xpos;
|
||||
lastY = ypos;
|
||||
xOffset *= mouseSensitivity;
|
||||
yOffset *= mouseSensitivity;
|
||||
|
||||
yaw += xOffset;
|
||||
pitch += yOffset;
|
||||
pitch = (pitch > 89.0f)?89.0f: ((pitch < -89.0f)?-89.0f:pitch );
|
||||
camDir.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
camDir.y = sin(glm::radians(pitch));
|
||||
camDir.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
|
||||
camDir = glm::normalize(camDir);
|
||||
}
|
||||
|
||||
void scroll_callback(GLFWwindow*, double xOffset, double yOffset) {
|
||||
zoom -= (float)yOffset;
|
||||
if (zoom < 1.0f) {
|
||||
zoom = 1.0f;
|
||||
} else if ( zoom > 45.0f) {
|
||||
zoom = 45.0f;
|
||||
}
|
||||
}
|
||||
|
||||
void processInput(GLFWwindow* window) {
|
||||
float camSpeed = 3.5f * deltaTime;
|
||||
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, true);
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
|
||||
camPos += camSpeed * camDir;
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
|
||||
camPos -= camSpeed * camDir;
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
|
||||
camPos -= camSpeed * glm::cross(camDir, camUp);
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
|
||||
camPos += camSpeed * glm::cross(camDir, camUp);
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
|
||||
camPos += camSpeed * camUp;
|
||||
}
|
||||
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
|
||||
camPos -= camSpeed * camUp;
|
||||
}
|
||||
}
|
||||
BIN
images/wall.jpg
BIN
images/wall.jpg
Binary file not shown.
|
Before Width: | Height: | Size: 251 KiB After Width: | Height: | Size: 58 KiB |
@ -7,19 +7,9 @@
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
extern glm::vec3 camPos;
|
||||
extern glm::vec3 camDir;
|
||||
extern glm::vec3 camUp;
|
||||
extern float yaw, pitch, roll;
|
||||
extern bool firstMouse;
|
||||
extern const float mouseSensitivity;
|
||||
|
||||
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
|
||||
|
||||
void mouse_callback(GLFWwindow*, double xpos, double ypos);
|
||||
|
||||
void scroll_callback(GLFWwindow*, double xOffset, double yOffset);
|
||||
|
||||
void processInput(GLFWwindow* window);
|
||||
|
||||
#endif
|
||||
105
main.cpp
105
main.cpp
@ -5,23 +5,8 @@
|
||||
#include <stb_image.h>
|
||||
#include <funcs.h>
|
||||
#include <shader.h>
|
||||
#include <math.h>
|
||||
#include <iostream>
|
||||
#include "data.cpp"
|
||||
|
||||
glm::vec3 cubePos[] = {
|
||||
glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3(3.0f, 0.0f, 0.0f),
|
||||
glm::vec3(1.5f, 0.0f, 2.599f),
|
||||
glm::vec3(1.5f, 1.983f, 1.3f)
|
||||
};
|
||||
|
||||
float deltaTime = 0.0f;
|
||||
float lastFrame = 0.0f;
|
||||
float currentFrame = 0.0f;
|
||||
float lastX = 400, lastY = 300;
|
||||
float zoom = 45.0;
|
||||
|
||||
int main() {
|
||||
// Initalize GLFW
|
||||
glfwInit();
|
||||
@ -32,7 +17,7 @@ int main() {
|
||||
// Open Window
|
||||
GLFWwindow* window = glfwCreateWindow(800, 600, "Howdy Naif!", NULL, NULL);
|
||||
if (window == NULL) {
|
||||
std::cout << "Could not open the iwndow!\n";
|
||||
std::cout << "Could not open the window!\n";
|
||||
glfwTerminate();
|
||||
return -1;
|
||||
}
|
||||
@ -47,8 +32,17 @@ int main() {
|
||||
// Set viewport
|
||||
glViewport(0, 0, 800, 600);
|
||||
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
glfwSetCursorPosCallback(window, mouse_callback);
|
||||
// glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
// glfwSetCursorPosCallback(window, mouse_callback);
|
||||
|
||||
// Create Data
|
||||
unsigned int gridX = 20, gridY = 20;
|
||||
float* tileCoords = makeGridPoints(gridX, gridY);
|
||||
unsigned int* tileIndices = makeGridIndex(gridX, gridY);
|
||||
|
||||
// Use and set shader
|
||||
Shader myShader("../shaders/vert.vs", "../shaders/frag.fs");
|
||||
myShader.use();
|
||||
|
||||
// Create Vertex Buffer and bind it to array buffer, then specifiy how to use that data
|
||||
unsigned int VAO, VBO;
|
||||
@ -56,80 +50,32 @@ int main() {
|
||||
glGenBuffers(1, &VBO);
|
||||
glBindVertexArray(VAO);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
||||
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
|
||||
glBufferData(GL_ARRAY_BUFFER, 5*gridX*gridY*sizeof(float), tileCoords, GL_STATIC_DRAW);
|
||||
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
|
||||
glEnableVertexAttribArray(1);
|
||||
|
||||
// Element Buffers
|
||||
// unsigned int EBO;
|
||||
// glGenBuffers(1, &EBO);
|
||||
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
|
||||
unsigned int EBO;
|
||||
glGenBuffers(1, &EBO);
|
||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
|
||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 6*(gridX-1)*(gridY-1)*sizeof(unsigned int), tileIndices, GL_STATIC_DRAW);
|
||||
|
||||
std::cout << "LOADED DATA\n";
|
||||
|
||||
// Use and set shader
|
||||
Shader myShader("assets/shaders/vert.vs", "assets/shaders/frag.fs");
|
||||
myShader.use();
|
||||
|
||||
// Texture Loading
|
||||
unsigned int texture1, texture2;
|
||||
int width1, height1, nrChannels1, width2, height2, nrChannels2;
|
||||
stbi_set_flip_vertically_on_load(true);
|
||||
unsigned char* data1 = stbi_load("assets/textures/wall.jpg", &width1, &height1, &nrChannels1, 0);
|
||||
|
||||
if (data1) {
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glGenTextures(1, &texture1);
|
||||
glBindTexture(GL_TEXTURE_2D, texture1);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width1, height1, 0, GL_RGB, GL_UNSIGNED_BYTE, data1);
|
||||
glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
myShader.setInt("texture1", 0);
|
||||
|
||||
} else {
|
||||
std::cout << "COULD NOT LOAD TEXTUREs!\n";
|
||||
}
|
||||
stbi_image_free(data1);
|
||||
|
||||
// View Matrices
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
glm::mat4 view = glm::mat4(1.0f);
|
||||
view = glm::lookAt(camPos, camPos+camDir, camUp);
|
||||
glm::mat4 projection;
|
||||
projection = glm::perspective(glm::radians(zoom), 800.0f/600.0f, 0.1f, 100.0f);
|
||||
|
||||
myShader.setMat4("model", model);
|
||||
myShader.setMat4("view", view);
|
||||
myShader.setMat4("projection", projection);
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
const float radius = 10.0f;
|
||||
|
||||
glBindVertexArray(VAO);
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
// Window loop
|
||||
while(!glfwWindowShouldClose(window)) {
|
||||
// Input checking
|
||||
currentFrame = glfwGetTime();
|
||||
deltaTime = currentFrame - lastFrame;
|
||||
lastFrame = currentFrame;
|
||||
processInput(window);
|
||||
|
||||
// Camera View
|
||||
view = glm::lookAt(camPos, camPos+camDir, camUp);
|
||||
myShader.setMat4("view", view);
|
||||
|
||||
// Rendering Loop
|
||||
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
glBindVertexArray(VAO);
|
||||
for (unsigned int i = 0; i < 4; ++i) {
|
||||
glm::mat4 model = glm::mat4(1.0f);
|
||||
model = glm::translate(model, cubePos[i]);
|
||||
myShader.setMat4("model", model);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 36);
|
||||
}
|
||||
glClear(GL_COLOR_BUFFER_BIT);
|
||||
glDrawElements(GL_TRIANGLES, (gridX-1)*(gridY-1)*6, GL_UNSIGNED_INT, 0);
|
||||
|
||||
// Events and buffer swap
|
||||
glfwPollEvents();
|
||||
@ -137,5 +83,8 @@ int main() {
|
||||
}
|
||||
|
||||
glfwTerminate();
|
||||
|
||||
delete[] tileCoords;
|
||||
//delete[] tileIndices;
|
||||
return 0;
|
||||
}
|
||||
@ -1,11 +1,8 @@
|
||||
#version 330 core
|
||||
|
||||
in vec2 textureCoord;
|
||||
flat in vec3 ourColor;
|
||||
|
||||
out vec4 FragColor;
|
||||
|
||||
uniform sampler2D texture1;
|
||||
|
||||
void main() {
|
||||
FragColor = texture(texture1, textureCoord);
|
||||
FragColor = vec4(ourColor, 1.0);
|
||||
}
|
||||
@ -1,15 +1,11 @@
|
||||
#version 330 core
|
||||
|
||||
layout(location=0) in vec3 aPos;
|
||||
layout(location=1) in vec2 tCoord;
|
||||
layout(location=1) in vec2 val;
|
||||
|
||||
out vec2 textureCoord;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
flat out vec3 ourColor;
|
||||
|
||||
void main() {
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
textureCoord = tCoord;
|
||||
gl_Position = vec4(aPos, 1.0);
|
||||
ourColor = vec3(val.x, 0.0, val.y);
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user