配置和下载lib文件请前往
https://blog.csdn.net/weixin_37615774/article/details/120679105
// ConsoleApplication1.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <glfw/glfw3.h>
#include <glm.hpp>
#include <gtc/matrix_transform.hpp>
#include <gtc/type_ptr.hpp>
float vertices[] = {
// positions // colors // texture coords
0.85f, 0.85f, 0.0f, 1.0f, 1.0f, // top right
0.85f, -0.85f, 0.0f, 1.0f, 0.0f, // bottom right
-0.85f, -0.85f, 0.0f, 0.0f, 0.0f, // bottom left
-0.85f, 0.85f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0,1,3,
1,2,3
};
const char* soctext1 =
"#version 330 core \n"
"layout (location = 0) in vec3 aPos; \n"
"layout (location = 1) in vec2 aTexCoord; \n"
"out vec2 TexCoord; \n"
"uniform mat4 model; \n"
"uniform mat4 view; \n"
"uniform mat4 projection;\n"
"void main() { \n"
" gl_Position = projection * view * model * vec4(aPos, 1.0f); \n"
" TexCoord = aTexCoord; \n"
"} \n";
const char* soctext2 =
"#version 330 core \n"
"out vec4 FragColor; \n"
"in vec2 TexCoord; \n"
"uniform sampler2D ourTexture; \n"
"void main() { \n"
"FragColor = texture(ourTexture, TexCoord); \n"
"} \n";
void pressImport(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
}
int main()
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 800, "TextWindow", 0, 0);
if (window == NULL) {
printf("Err Create Window");
glfwTerminate();
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
printf("30:Error");
glfwTerminate();
return -1;
}
glViewport(0, 0, 800, 800);
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &soctext1, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader;
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &soctext2, NULL);
glCompileShader(fragmentShader);
unsigned int shaderProgram;
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
stbi_set_flip_vertically_on_load(true);
unsigned int texture;
glGenTextures(1, &texture);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height, nrChannels;
unsigned char* data = stbi_load("3.png", &width, &height, &nrChannels, 0);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
stbi_image_free(data);
glm::mat4 model;
model = glm::rotate(model, glm::radians(-55.0f), glm::vec3(1.0f, 0.0f, 0.0f));
glm::mat4 view;
view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f));
glm::mat4 projection;
projection = glm::perspective(glm::radians(45.0f), 800.0f / 800.0f, 0.1f, 100.0f);
while (!glfwWindowShouldClose(window))
{
pressImport(window);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
int tmp = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(tmp, 1, GL_FALSE, glm::value_ptr(model));
tmp = glGetUniformLocation(shaderProgram, "view");
glUniformMatrix4fv(tmp, 1, GL_FALSE, glm::value_ptr(view));
tmp = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(tmp, 1, GL_FALSE, glm::value_ptr(projection));
glUseProgram(shaderProgram);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}