Код: Выделить всё
#define STB_IMAGE_IMPLEMENTATION
#include
#include
#include
#include
#include
#include
int main() {
int x = 900;
int y = 700;
int hx = 0;
int hy = 0;
glfwInit();
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_VERSION_MINOR, 3);
GLFWwindow* win = glfwCreateWindow(x, y, "Herbert Engine PA.1.0.0", nullptr, nullptr);
glfwMakeContextCurrent(win);
glfwGetFramebufferSize(win, &x, &y);
gladLoadGL();
glViewport(0, 0, x, y);
glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
float vert[]{
-0.5f, -5.0f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f
};
float data[] = {
//vert //col //cords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
-5.0f, 0.5f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f
};
unsigned int vbo;
unsigned int vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vert), vert, GL_DYNAMIC_DRAW);
float col[] = { 1.0f, 0.0f, 0.0f, 1.0f };
int w, h, nrChan;
const char* vertxSrc = "#version 330 core\n"
"layout (location = 0) in vec3 pos;\n"
"layout (location = 2) in vec3 cord;\n"
"layout (location = 3) in vec4 col;\n"
"out vec3 texCord;\n"
"out vec4 texCol;\n"
"void main() {\n"
" gl_Position = vec4(pos, 1.0f);\n"
" texCord = cord;\n"
" texCol = col;\n"
"}\0";
const char *fragSrc = "#version 330 core\n"
"out vec3 fragCol;\n"
"in vec3 texCord;\n"
"in vec4 texCol;\n"
"uniform sampler2D title;\n"
"void main() {\n"
" fragCol = (title, texCord);\n"
"}\0";
unsigned int tex;
glCreateTextures(GL_TEXTURE_2D, 1, &tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, col);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
unsigned char* title = stbi_load("png/title.png", &w, &h, &nrChan, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, title);
glGenerateMipmap(GL_TEXTURE_2D);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*) sizeof(float));
glEnableVertexAttribArray(2);
int sucess;
unsigned int vertxShd = glCreateShader(GL_VERTEX_SHADER);
unsigned int fragShd = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertxShd, 1, &vertxSrc, nullptr);
glShaderSource(fragShd, 1, &fragSrc, nullptr);
glCompileShader(vertxShd);
glCompileShader(fragShd);
glGetShaderiv(fragShd, GL_COMPILE_STATUS, &sucess);
unsigned int prg = glCreateProgram();
glAttachShader(prg, vertxShd);
glAttachShader(prg, fragShd);
glLinkProgram(prg);
glBindTexture(GL_TEXTURE_2D, tex);
glBindVertexArray(vao);
glDrawArrays(GL_TRIANGLES, 0, 1);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glUseProgram(prg);
glBindTexture(GL_TEXTURE_2D, tex);
glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
while (!glfwWindowShouldClose(win)) {
glClear(GL_COLOR_BUFFER_BIT);
glDrawArrays(GL_TRIANGLES, 0, 1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(win);
glfwPollEvents();
if (glfwGetKey(win, GLFW_KEY_UP)) {
hy -= 1;
}
if (glfwGetKey(win, GLFW_KEY_DOWN)) {
hy += 1;
}
if (glfwGetKey(win, GLFW_KEY_LEFT)) {
hx -= 1;
}
if (glfwGetKey(win, GLFW_KEY_RIGHT)) {
hx += 1;
}
}
glDeleteProgram(prg);
glDeleteVertexArrays(1, &vao);
glDeleteTextures(1, &tex);
stbi_image_free(title);
glfwDestroyWindow(win);
glfwTerminate();
return 0;
}
Подробнее здесь: https://stackoverflow.com/questions/793 ... he-texture