more stuff

This commit is contained in:
onezplpl 2024-07-13 03:13:32 -04:00
parent 7bf40240fe
commit 48ecd8d9e8
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
5 changed files with 96 additions and 48 deletions

75
main.c
View file

@ -5,11 +5,12 @@
#include "gl.h"
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include "const.h"
#include "cubes.h"
#include "linmath.h"
#include "stb_image.h"
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@ -17,13 +18,13 @@
static const char *vertex_shader_text =
"#version 330\n"
"layout (location = 0) in vec3 vPos;"
//"layout (location = 1) in vec3 vOff;"
"layout (location = 1) in vec3 vOff;"
"uniform mat4 MVP;\n"
"out vec2 vTex;\n"
"void main()\n"
"{\n"
" gl_Position = MVP * vec4(vPos, 1.0);\n"
" vTex=0.5*vPos.xy + vec2(0.5,0);\n"
" vTex=vec2(vOff);\n"
"}\n";
static const char *fragment_shader_text =
@ -34,13 +35,22 @@ static const char *fragment_shader_text =
"void main()\n"
"{\n"
//" color=vec4(vTex.rg,0.5,0.5);\n"
" color=vec4(texture(texSampler, vTex).rg+vTex/10.0,0.5,0.5);\n"
" if (vTex.x < 0.0 || vTex.y < 0.0) { discard; }\n"
" color=vec4(texture(texSampler, vTex).rgb,0.5);\n"
"}\n";
int isCursor = 0;
static void key_callback(GLFWwindow *window, int key, int scancode, int action,
int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
/*if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
*/
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
isCursor = glfwGetInputMode(window, GLFW_CURSOR) == GLFW_CURSOR_DISABLED;
glfwSetInputMode(window, GLFW_CURSOR,
isCursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
}
}
double cx = 0;
@ -48,8 +58,10 @@ double cy = 0;
int width, height;
static void cursor_position_callback(GLFWwindow *window, double x, double y) {
cx = (x-width/2.0) / -1000.0;
cy = (y-height/2.0) / -1000.0;
if (isCursor)
return;
cx = (x - width / 2.0) / -1000.0;
cy = (y - height / 2.0) / -1000.0;
cy = (cy > M_PI / 2) ? M_PI / 2 : ((cy < -M_PI / 2) ? -M_PI / 2 : cy);
return;
}
@ -74,12 +86,27 @@ int main(void) {
glfwSwapInterval(1);
Vertex *cube = malloc(CTRI_ALL);
gen_cubes(cube, 0, 0, 0);
Vertex *text = malloc(CTRI_ALL);
gen_cubes(cube, 0, 0, 0, 0);
gen_cubes(text, 0, 0, 0, 1);
GLuint vertex_buffer;
glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
GLuint vertex_buffer[2];
GLuint vertex_array;
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
glGenBuffers(2, vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
glBufferData(GL_ARRAY_BUFFER, CTRI_ALL, text, GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
glBufferData(GL_ARRAY_BUFFER, CTRI_ALL, cube, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)0);
GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, &vertex_shader_text, NULL);
@ -95,22 +122,12 @@ int main(void) {
glLinkProgram(program);
GLint tex_location = glGetUniformLocation(program, "texSampler");
GLint vpos_location = 0;
GLint mvp_location = glGetUniformLocation(program, "MVP");
GLuint vertex_array;
glGenVertexArrays(1, &vertex_array);
glBindVertexArray(vertex_array);
unsigned char *pixels;
glEnableVertexAttribArray(vpos_location);
glVertexAttribPointer(vpos_location, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex),
(void *)0);
unsigned char pixels[1024 * 1024 * 3];
for (int i = 0; i < 1024 * 1024 * 3; i++) {
pixels[i] = i * 69420;
}
int tx, ty, ch;
pixels = stbi_load("./test.png", &tx, &ty, &ch, 3);
glClearColor(0.2f, 0.5f, 0.7f, 1.0f);
glEnable(GL_TEXTURE_2D);
@ -122,9 +139,9 @@ int main(void) {
vec3 pos = {0, 0, 0};
int x = 0, y = 25, z = 0;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
while (!glfwWindowShouldClose(window)) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwGetFramebufferSize(window, &width, &height);
const float ratio = width / (float)height;
@ -136,8 +153,8 @@ int main(void) {
glBindTexture(GL_TEXTURE_2D, GL_TEXTURE0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 16, 16, 0, GL_RGB,
GL_UNSIGNED_BYTE, pixels);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, tx, ty, 0, GL_RGB, GL_UNSIGNED_BYTE,
pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
@ -168,8 +185,8 @@ int main(void) {
vec3 dir_pos;
vec3_add(dir_pos, direction, pos);
vec3 up = {0,1,0};
//vec3_mul_cross(up, right, direction);
vec3 up = {0, 1, 0};
// vec3_mul_cross(up, right, direction);
mat4x4_look_at(v, pos, dir_pos, up);
mat4x4_mul(mvp, p, v);