This commit is contained in:
leca 2025-01-19 00:54:19 +03:00
parent b012641dc9
commit 28e8c15fc1
5 changed files with 68 additions and 12 deletions

View File

@ -1,6 +1,9 @@
#version 330 core #version 330 core
in vec3 frag_col;
out vec4 FragColor; out vec4 FragColor;
uniform vec4 MyColor;
void main() void main()
{ {
FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f); FragColor = vec4(frag_col, 1.0f);//vec4(posInWorld.x, posInWorld.y, posInWorld.z, 1.0f);
} }

View File

@ -1,6 +1,10 @@
#version 330 core #version 330 core
layout (location = 0) in vec3 aPos; layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 col;
out vec3 frag_col;
void main() void main()
{ {
frag_col = col;
gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0); gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);
} }

View File

@ -3,6 +3,10 @@
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#include <cmath>
#include <cstdlib>
#define RAND_0_1 ((float)rand()) / RAND_MAX
void framebufferSizeCallback(GLFWwindow *window, int width, int height) { void framebufferSizeCallback(GLFWwindow *window, int width, int height) {
glViewport(10, 10, width-10, height-10); glViewport(10, 10, width-10, height-10);
@ -36,11 +40,23 @@ std::string readFile(std::string path) {
} }
content += '\0'; content += '\0';
std::cout << content << std::endl;
file.close(); file.close();
return content; return content;
} }
void updateVertices(float (&vertices)[24]) {
// if (RAND_0_1 < 0.99f) return;
float a[] = {
-1.0f, 1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // upper left
-1.0f, -1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // bottom left
1.0f, 1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1, // upper right
1.0f, -1.0f, 0.0f, RAND_0_1, RAND_0_1, RAND_0_1 // bottom right
};
// vertices = a;
memcpy(vertices, a, sizeof(a));
}
unsigned int prepareShaders() { unsigned int prepareShaders() {
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER); unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
@ -91,6 +107,7 @@ unsigned int prepareShaders() {
} }
int main () { int main () {
srand(time(0));
glfwInit(); glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
@ -116,22 +133,34 @@ int main () {
unsigned int shaderProgram = prepareShaders(); unsigned int shaderProgram = prepareShaders();
float vertices[] = { float vertices[] = {
-0.5, 0.0, 0.0, -1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper left
0.5, 0.5, 0.0, -1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, // bottom left
0.5, -0.5, 0.0 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, // upper right
1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f // bottom right
}; };
unsigned int indices[] = {
unsigned int VBO, VAO; 1, 2, 3,
1, 2, 0
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO); glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO); glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO); glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0); glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
glEnableVertexAttribArray(1);
glBindVertexArray(0); glBindVertexArray(0);
while(!glfwWindowShouldClose(window)) { while(!glfwWindowShouldClose(window)) {
@ -141,9 +170,20 @@ int main () {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(shaderProgram); glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
// float time = glfwGetTime();
// float greenValue = sin(time);
// int vertexColorLocation = glGetUniformLocation(shaderProgram, "MyColor");
// glUniform4f(vertexColorLocation, 0, greenValue, 0, 1);
glBindVertexArray(VAO);
updateVertices(vertices);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
} }

5
src/ui/ui.cpp Normal file
View File

@ -0,0 +1,5 @@
#include "ui.hpp"
void UI::draw() {
};

4
src/ui/ui.hpp Normal file
View File

@ -0,0 +1,4 @@
class UI {
void draw();
};