创建原始 OpenGL 纹理数据 (C++)

Creating raw OpenGL texture data (C++)

所以基本上我是在尝试用代码创建我自己的纹理。具体来说,我正在编写一个光线追踪器,并希望将结果显示为在屏幕上渲染到四边形的纹理。

但是如何创建自己的纹理然后将其应用到屏幕四边形?我已经这样做了好几天了,没有任何特别的结果。我现在得到的是屏幕上的四边形,但纹理是整个四边形的一种颜色。但在我这里的示例中,我希望它是 1/3 白色和 2/3 黑色,这就是我想象的完成方式。

我想让 OpenGL 尽可能保持准系统。我不打算扩展它,所以只要它有效,任何包含 "ugly hack" 的答案都完全没问题。我正在使用 Glew、GLFW 和 GLM。

这就是我目前的做法:

GLFWwindow* window;

//Create a texture that should be 1 third black and 2 thirds white?
unsigned char texdata[SCREEN_HEIGHT * SCREEN_WIDTH * 3];
for(int i=0; i<SCREEN_HEIGHT * SCREEN_WIDTH * 3; i++)
{
    if(i < SCREEN_HEIGHT * SCREEN_WIDTH)
        texdata[i] = 255;
    else
        texdata[i] = 0;
}
//cam.createTexture(texdata);

initOpenGL(window);

GLuint t = 0;

glEnable(GL_TEXTURE_2D); // Required for glBuildMipmap() to work (!)
glGenTextures( 1, &t );
glBindTexture(GL_TEXTURE_2D, t);
// Set parameters to determine how the texture is resized
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MIN_FILTER , GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_MAG_FILTER , GL_LINEAR );
// Set parameters to determine how the texture wraps at edges
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_S , GL_REPEAT );
glTexParameteri ( GL_TEXTURE_2D , GL_TEXTURE_WRAP_T , GL_REPEAT );
// Read the texture data from file and upload it to the GPU
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, SCREEN_WIDTH, SCREEN_HEIGHT, 0,
             GL_RGB, GL_UNSIGNED_BYTE, texdata);
glGenerateMipmap(GL_TEXTURE_2D);

GLuint programID = LoadShaders("vertex.glsl", "fragment.glsl");
glUseProgram(programID);
// Create and compile our GLSL program from the shaders

Objects screenquad;
screenquad.createBox(1.0, 1.0, 0.0);

do{

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    screenquad.render();
    // Swap buffers
    glfwSwapBuffers(window);
    glfwPollEvents();
} // Check if the ESC key was pressed or the window was closed
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
       glfwWindowShouldClose(window) == 0 );

GLSL 着色器代码:

#version 450

in vec2 Texcoord;

out vec4 outColor;

uniform sampler2D tex;

void main(){
    outColor = texture(tex, Texcoord);
}

顶点着色器:

#version 450
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec3 Normal;
layout(location = 2) in vec2 texcoord;

out vec2 Texcoord;

void main(){
    Texcoord = texcoord;
    gl_Position.xyz = vertexPosition_modelspace;
    gl_Position.w = 1.0;
}

编辑: 创建 screenquad 的内部: https://raw.githubusercontent.com/ddavidtran/Fishualization/master/OpenGL/Objects.cpp

代码中没有设置tex制服。大致可以这样来做:

GLint texUniformLocation = glGetUniformLocation(programID, "tex");
glUniform1i(texUniformLocation, 0);

这里,0glUniform1i is the current active texture image unit, which is GL_TEXTURE0 by default. You can change it via glActiveTexture

然而,正如 BDL 所注意到的,0 实际上是默认值,因此您可以跳过这部分。

还有一个问题就是你的贴图坐标不对。如file you linked中所见,在函数Objects::createBox中,顶点数据为

const GLfloat vertex_array_data[] = {
    -xsize, -ysize, -zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Vertex 0
    xsize, -ysize, -zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Vertex 1
    -xsize, ysize, -zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,  // Vertex 2
    xsize, ysize, -zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,  // Vertex 3
    -xsize, -ysize, zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Vertex 0
    xsize, -ysize, zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Vertex 1
    -xsize, ysize, zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,  // Vertex 2
    xsize, ysize, zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f  // Vertex 3
};

每一行的最后两个浮点数是纹理坐标。可以看到,都是0。法线也很奇怪,反正你不用。

我不知道你想要什么样的盒子纹理坐标,但对于四边形我会做这样的事情:

const GLfloat vertex_array_data[] = {
    -xsize, -ysize, -zsize, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // Vertex 0
    xsize, -ysize, -zsize, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, // Vertex 1
    -xsize, ysize, -zsize, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,  // Vertex 2
    xsize, ysize, -zsize, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,  // Vertex 3
};