从文件中加载图像
#include<GL/gl3w.h>//与下面的glfw3.h的顺序不能交换
#include <GLFW/glfw3.h>
#include <iostream>
#include<fstream>
#include<string>
#define STB_IMAGE_IMPLEMENTATION
#include"stb_image.h"
using namespace std;
const GLchar*ReadShader(const char*filename) {
ifstream t;
int length;
t.open(filename);
t.seekg(0,ios::end);
length = t.tellg();
t.seekg(0,ios::beg);
GLchar*buffer = new GLchar[length+1];
memset(buffer,0x0,length+1);
t.read(buffer,length);
t.close();
return const_cast<GLchar*>(buffer);
}
int main() {
glfwInit();
GLFWwindow*window = glfwCreateWindow(640,480,"triangles",nullptr,nullptr);
glfwMakeContextCurrent(window);
gl3wInit();
//program and shader
GLuint program = glCreateProgram();
GLuint vert_shader = glCreateShader(GL_VERTEX_SHADER);
const GLchar*vert_shader_source = ReadShader("triangle.vert");
glShaderSource(vert_shader,1,&vert_shader_source,nullptr);
delete[] vert_shader_source;
glCompileShader(vert_shader);
glAttachShader(program,vert_shader);
GLuint frag_shader = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar*frag_shader_source = ReadShader("triangle.frag");
glShaderSource(frag_shader, 1, &frag_shader_source, nullptr);
delete[] frag_shader_source;
glCompileShader(frag_shader);
glAttachShader(program,frag_shader);
glLinkProgram(program);
glUseProgram(program);
//buffer and vertex
float vertices[] = {
// positions // colors // texture coords
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
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);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// load and create a texture
// -------------------------
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture); // all upcoming GL_TEXTURE_2D operations now have effect on this texture object
// set the texture wrapping parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // set texture wrapping to GL_REPEAT (default wrapping method)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
int width, height, nrChannels;
unsigned char *data = stbi_load("container.png", &width, &height, &nrChannels, STBI_rgb);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
while (!glfwWindowShouldClose(window)) {
static const float black[] = {0.0f,0.0f,0.0f,0.0f};
glClearBufferfv(GL_COLOR,0,black);
glBindTexture(GL_TEXTURE_2D,texture);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
顶点着色器:
#version 450 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
TexCoord = vec2(aTexCoord.x, aTexCoord.y);
}
片段着色器:
#version 450 core
out vec4 FragColor;
in vec3 ourColor;
in vec2 TexCoord;
// texture sampler
uniform sampler2D texture1;
void main()
{
FragColor = texture(texture1, TexCoord);
}
要加载的图片:
container.png
总结:
1.stbi_load()中的STBI_rgb要与glTexImage2D()中的GL_RGB对应
网友评论