这是读取一个png文件,对图片文件做ldc。 我已经将图像绑定到纹理,但我只看到黑色......不确定它是否来自片段,确实将 fragment fragcolor 更改为蓝色,但只看到黑色屏幕不知道哪里出了问题......
关于这方面的任何帮助......它是我丢失或未按顺序使用的一些库吗?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <iostream>
#include <fstream>
#include <cmath>
#include <png.h>
GLuint program;
GLint uniform_c;
GLint uniform_sxy;
GLint uniform_xh;
GLint uniform_a1;
GLint uniform_a2;
GLint uniform_a3;
GLint uniform_b1;
GLint uniform_b2;
GLint uniform_c2;
GLint uniform_xi;
GLint uniform_uk1;
GLint uniform_uk2;
GLint uniform_up1;
GLint uniform_up2;
GLint attribute_texcoord;
GLuint texture_id;
// Load the texture image
unsigned int texWidth=1920, texHeight=1536;
unsigned char* texData;
// Function to load a PNG image using libpng
// Function to load a PNG image and extract the pixel data
void loadTextureImage(const char* fileName, unsigned int& width, unsigned int& height, unsigned char*& data)
{
// Open the PNG file
FILE* file = fopen(fileName, "rb");
if (!file) {
std::cerr << "Failed to open file: " << fileName << std::endl;
exit(EXIT_FAILURE);
}
// Create the PNG read and info structs
png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, nullptr, nullptr, nullptr);
if (!pngPtr) {
std::cerr << "Failed to create PNG read struct" << std::endl;
fclose(file);
exit(EXIT_FAILURE);
}
png_infop infoPtr = png_create_info_struct(pngPtr);
if (!infoPtr) {
std::cerr << "Failed to create PNG info struct" << std::endl;
png_destroy_read_struct(&pngPtr, nullptr, nullptr);
fclose(file);
exit(EXIT_FAILURE);
}
// Set up the error handling
if (setjmp(png_jmpbuf(pngPtr))) {
std::cerr << "Error: PNG read failed" << std::endl;
png_destroy_read_struct(&pngPtr, &infoPtr, nullptr);
fclose(file);
exit(EXIT_FAILURE);
}
// Set up the IO functions
png_init_io(pngPtr, file);
// Read the PNG header and get the image dimensions
png_read_info(pngPtr, infoPtr);
width = png_get_image_width(pngPtr, infoPtr);
height = png_get_image_height(pngPtr, infoPtr);
// Set up transformations to expand color to RGBA and to flip the image
png_set_expand(pngPtr);
png_set_strip_16(pngPtr);
png_set_gray_to_rgb(pngPtr);
png_set_filler(pngPtr, 0xFF, PNG_FILLER_AFTER);
png_set_bgr(pngPtr);
png_set_interlace_handling(pngPtr);
png_read_update_info(pngPtr, infoPtr);
// Allocate memory for the image data and set up row pointers
int rowBytes = png_get_rowbytes(pngPtr, infoPtr);
data = new unsigned char[rowBytes * height];
png_bytep* rowPtrs = new png_bytep[height];
for (unsigned int i = 0; i < height; ++i) {
rowPtrs[i] = data + i * rowBytes;
}
// Read the PNG image
png_read_image(pngPtr, rowPtrs);
// Clean up
png_destroy_read_struct(&pngPtr, &infoPtr, nullptr);
fclose(file);
delete[] rowPtrs;
}
char* read_shader_source(const char* shader_file) {
// Open the shader file for reading
FILE* file = fopen(shader_file, "r");
// Check if the file was opened successfully
if (file == NULL) {
// If the file could not be opened, return null pointer
return NULL;
}
// Seek to the end of the file to determine its size
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
rewind(file);
// Allocate memory for a character array to hold the source code
char* source_code = (char*) malloc((file_size + 1) * sizeof(char));
// Read the contents of the file into the character array
size_t result = fread(source_code, sizeof(char), file_size, file);
if (result != file_size) {
// If the file could not be read completely, return null pointer
free(source_code);
fclose(file);
return NULL;
}
// Null-terminate the character array
source_code[file_size] = '\0';
// Close the file
fclose(file);
// Return the character array
return source_code;
}
This is to load the shaders
void init_shaders()
{
GLuint vs, fs;
char* source;
GLint compileStatus;
vs = glCreateShader(GL_VERTEX_SHADER);
source = read_shader_source("vertex.glsl");
glShaderSource(vs, 1, (const GLchar**) &source, NULL);
free(source);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus == GL_FALSE)
{
GLint logLength;
glGetShaderiv(vs, GL_INFO_LOG_LENGTH, &logLength);
GLchar* log = (GLchar*)malloc(logLength);
glGetShaderInfoLog(vs, logLength, NULL, log);
printf("Error compiling vertex shader: %s\n", log);
free(log);
}
fs = glCreateShader(GL_FRAGMENT_SHADER);
source = read_shader_source("fragment.glsl");
glShaderSource(fs, 1, (const GLchar**) &source, NULL);
free(source);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compileStatus);
if (compileStatus == GL_FALSE)
{
GLint logLength;
glGetShaderiv(fs, GL_INFO_LOG_LENGTH, &logLength);
GLchar* log = (GLchar*)malloc(logLength);
glGetShaderInfoLog(fs, logLength, NULL, log);
printf("Error compiling fragment shader: %s\n", log);
free(log);
}
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
uniform_c = glGetUniformLocation(program, "c");
uniform_sxy = glGetUniformLocation(program, "sxy");
uniform_xh = glGetUniformLocation(program, "xh");
uniform_a1 = glGetUniformLocation(program, "a1");
uniform_a2 = glGetUniformLocation(program, "a2");
uniform_a3 = glGetUniformLocation(program, "a3");
uniform_b1 = glGetUniformLocation(program, "b1");
uniform_b2 = glGetUniformLocation(program, "b2");
uniform_c2 = glGetUniformLocation(program, "c2");
uniform_xi = glGetUniformLocation(program, "xi");
uniform_uk1 = glGetUniformLocation(program, "uk1");
uniform_uk2 = glGetUniformLocation(program, "uk2");
uniform_up1 = glGetUniformLocation(program, "up1");
uniform_up2 = glGetUniformLocation(program, "up2");
attribute_texcoord = glGetAttribLocation(program, "TexCoord");
}
void display()
{
// Create an OpenGL texture from the image data
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_2D, texture_id);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, texData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Render the distorted image using the shader program
glUseProgram(program);
glUniform1f(uniform_c, 1093.411813);
glUniform1f(uniform_sxy, 0.999688);
glUniform2f(uniform_xh, 967.148762, 777.752122);
glUniform1f(uniform_a1, 8.108004e-01);
glUniform1f(uniform_a2, -1.868585e+00);
glUniform1f(uniform_a3, 1.640623e+00);
glUniform1f(uniform_b1, 1.068417e-03);
glUniform1f(uniform_b2, 2.196730e-03);
glUniform1f(uniform_c2, 0.000000e+00);
glUniform1f(uniform_xi, 1.468465);
glUniform1f(uniform_uk1, 1.449585e-05);
glUniform1f(uniform_uk2, 3.130669e-05);
glUniform1f(uniform_up1, -1.723591e-03);
glUniform1f(uniform_up2, -8.2924404e-04);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_id);
glUniform1i(glGetUniformLocation(program, "inputImage"), 0);
// Render the distorted image
glBegin(GL_QUADS);
glVertexAttrib2f(attribute_texcoord, 0.0, 0.0);
glVertex2f(-1.0, -1.0);
glVertexAttrib2f(attribute_texcoord, 1.0, 0.0);
glVertex2f(1.0, -1.0);
glVertexAttrib2f(attribute_texcoord, 1.0, 1.0);
glVertex2f(1.0, 1.0);
glVertexAttrib2f(attribute_texcoord, 0.0, 1.0);
glVertex2f(-1.0, 1.0);
glEnd();
// Swap buffers
glutSwapBuffers();
}
int main(int argc, char** argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <image_file>\n", argv[0]);
exit(1);
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(1920, 1536);
glutCreateWindow("LDC");
glewInit();
init_shaders();
// load_texture();
loadTextureImage(argv[1], texWidth, texHeight, texData);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
fragment is
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D inputImage;
uniform float c;
uniform float sxy;
uniform vec2 xh;
uniform float A1;
uniform float A2;
uniform float A3;
uniform float B1;
uniform float B2;
uniform float C2;
uniform float XI;
uniform float U_k1;
uniform float U_k2;
uniform float U_p1;
uniform float U_p2;
void main()
{
vec2 xy = (TexCoord - xh) / XI;
float x = xy.x;
float y = xy.y;
float r2 = x*x + y*y;
float r4 = r2 * r2;
float k = 1.0 + A1*r2 + A2*r4 + A3*r2*r4;
float dx = 2.0*B1*x*y + B2*(r2 + 2.0*x*x);
float dy = B1*(r2 + 2.0*y*y) + 2.0*B2*x*y;
//float u = (k*x + dx)c + sxy*y + U_p1*(r2 + 2.0*x*x) + U_p2*(r2 + 2.0*y*y) + U_k1*r4 + U_k2*r4*r2;
float u = (k * x + dx) * c + sxy * y + U_p1 * (r2 + 2.0 * x * x) + U_p2 * (r2 + 2.0 * y * y) + U_k1 * r4 + U_k2 * r4 * r2;
float v = (k*y + dy)* c + U_p1* (r2 + 2.0*y*y) + U_p2*(r2 + 2.0*x*x) + U_k2*r4 + U_k1*r4*r2 - sxy*x;
float cx = 2.0*(u + 0.5)/textureSize(inputImage, 0).x - 1.0;
float cy = 2.0*(v + 0.5)/textureSize(inputImage, 0).y - 1.0;
if (cx < -1.0 || cx > 1.0 || cy < -1.0 || cy > 1.0)
{
FragColor = vec4(0.0, 0.0, 1.0, 1.0);
return;
}
FragColor = texture(inputImage, vec2((cx + 1.0)/2.0, (cy + 1.0)/2.0));
}
vertex is
// Vertex Shader
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;
out vec2 TexCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
TexCoord = aTexCoord;
}
i tried chaging the fragment but no result it give black screen only...