我想澄清一下defGL行为的侮辱。据我所知,默认的活动纹理单位为0(GL_TEXTURE0
),除非通过glActiveTexture
明确更改,否则将保持不变。
我问这个是因为我得到的行为是我无法解释的,而且是有问题的。将纹理加载并绑定到GL_TEXTURE_2D
(假设GL_TEXTURE0
处于活动状态)后,在将活动纹理设置为GL_TEXTURE1
之前,我无法显示它。甚至在加载之后首先绑定它之前显式调用glActiveTexture(GL_TEXTUREX)
,然后在绘制期间使用它时重复此调用什么都不做。如果我在绑定和分配到着色器之前调用glActiveTexture(GL_TEXTURE1)
,则仅显示纹理。
当我尝试使用更多纹理时,我必须根据我加载的顺序显式设置每个glActiveTexture(GL_TEXTURE...)
并在绑定它们之前首先绑定纹理并将它们分配给着色器。即使我没有使用我加载的第一个纹理绘图,我仍然需要调用glActiveTexture(GL_TEXTURE2)
...而不是GL_TEXTURE1
,或者甚至更好GL_TEXTURE0
因为我甚至没有调用到目前为止glActiveTexture(...)
。后者我认为根据GL规范会发生什么。
https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glActiveTexture.xhtml
这是司机吗?感谢您提出任何建议。
Dell Precision M6800,nV K2200M,GL 4.1& 3.3。
更新:隔离示例。
#include <Windows.h>
#include <gl\GL.h>
#include "glext.h"
#include "wglext.h"
#include <vector>
#include <memory>
#include <atlimage.h>
namespace
{
class GLFunc
{
public:
GLFunc()
{
glCreateProgram = (PFNGLCREATEPROGRAMPROC)wglGetProcAddress("glCreateProgram");
glCreateShader = (PFNGLCREATESHADERPROC)wglGetProcAddress("glCreateShader");
glShaderSource = (PFNGLSHADERSOURCEPROC)wglGetProcAddress("glShaderSource");
glCompileShader = (PFNGLCOMPILESHADERPROC)wglGetProcAddress("glCompileShader");
glAttachShader = (PFNGLATTACHSHADERPROC)wglGetProcAddress("glAttachShader");
glDetachShader = (PFNGLDETACHSHADERPROC)wglGetProcAddress("glDetachShader");
glLinkProgram = (PFNGLLINKPROGRAMPROC)wglGetProcAddress("glLinkProgram");
glUseProgram = (PFNGLUSEPROGRAMPROC)wglGetProcAddress("glUseProgram");
glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC)wglGetProcAddress("glGenFramebuffers");
glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC)wglGetProcAddress("glBindFramebuffer");
glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC)wglGetProcAddress("glGenRenderbuffers");
glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC)wglGetProcAddress("glBindRenderbuffer");
glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC)wglGetProcAddress("glRenderbufferStorage");
glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC)wglGetProcAddress("glFramebufferRenderbuffer");
glFramebufferTexture = (PFNGLFRAMEBUFFERTEXTUREPROC)wglGetProcAddress("glFramebufferTexture");
glDrawBuffers = (PFNGLDRAWBUFFERSPROC)wglGetProcAddress("glDrawBuffers");
glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC)wglGetProcAddress("glCheckFramebufferStatus");
glGenVertexArrays = (PFNGLGENVERTEXARRAYSPROC)wglGetProcAddress("glGenVertexArrays");
glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress("glBindBuffer");
glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress("glBufferData");
glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC)wglGetProcAddress("glDeleteFramebuffers");
glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC)wglGetProcAddress("glDeleteRenderbuffers");
glDeleteVertexArrays = (PFNGLDELETEVERTEXARRAYSPROC)wglGetProcAddress("glDeleteVertexArrays");
glGetShaderiv = (PFNGLGETSHADERIVPROC)wglGetProcAddress("glGetShaderiv");
glGetShaderInfoLog = (PFNGLGETSHADERINFOLOGPROC)wglGetProcAddress("glGetShaderInfoLog");
glGetProgramiv = (PFNGLGETPROGRAMIVPROC)wglGetProcAddress("glGetProgramiv");
glGetProgramInfoLog = (PFNGLGETPROGRAMINFOLOGPROC)wglGetProcAddress("glGetProgramInfoLog");
glGenBuffers = (PFNGLGENBUFFERSPROC)wglGetProcAddress("glGenBuffers");
glEnableVertexAttribArray = (PFNGLENABLEVERTEXATTRIBARRAYPROC)wglGetProcAddress("glEnableVertexAttribArray");
glVertexAttribPointer = (PFNGLVERTEXATTRIBPOINTERPROC)wglGetProcAddress("glVertexAttribPointer");
glBindVertexArray = (PFNGLBINDVERTEXARRAYPROC)wglGetProcAddress("glBindVertexArray");
glGetUniformLocation = (PFNGLGETUNIFORMLOCATIONPROC)wglGetProcAddress("glGetUniformLocation");
glUniformMatrix2fv = (PFNGLUNIFORMMATRIX2FVPROC)wglGetProcAddress("glUniformMatrix2fv");
glUniformMatrix3fv = (PFNGLUNIFORMMATRIX3FVPROC)wglGetProcAddress("glUniformMatrix3fv");
glUniformMatrix4fv = (PFNGLUNIFORMMATRIX4FVPROC)wglGetProcAddress("glUniformMatrix4fv");
glUniform1f = (PFNGLUNIFORM1FPROC)wglGetProcAddress("glUniform1f");
glUniform2f = (PFNGLUNIFORM2FPROC)wglGetProcAddress("glUniform2f");
glUniform3f = (PFNGLUNIFORM3FPROC)wglGetProcAddress("glUniform3f");
glUniform4f = (PFNGLUNIFORM4FPROC)wglGetProcAddress("glUniform4f");
glUniform1i = (PFNGLUNIFORM1IPROC)wglGetProcAddress("glUniform1i");
glUniform2i = (PFNGLUNIFORM2IPROC)wglGetProcAddress("glUniform2i");
glUniform3i = (PFNGLUNIFORM3IPROC)wglGetProcAddress("glUniform3i");
glUniform4i = (PFNGLUNIFORM4IPROC)wglGetProcAddress("glUniform4i");
glActiveTexture = (PFNGLACTIVETEXTUREPROC)wglGetProcAddress("glActiveTexture");
glDeleteShader = (PFNGLDELETESHADERPROC)wglGetProcAddress("glDeleteShader");
}
PFNGLCREATEPROGRAMPROC glCreateProgram;
PFNGLCREATESHADERPROC glCreateShader;
PFNGLSHADERSOURCEPROC glShaderSource;
PFNGLCOMPILESHADERPROC glCompileShader;
PFNGLATTACHSHADERPROC glAttachShader;
PFNGLDETACHSHADERPROC glDetachShader;
PFNGLLINKPROGRAMPROC glLinkProgram;
PFNGLUSEPROGRAMPROC glUseProgram;
PFNGLGENFRAMEBUFFERSPROC glGenFramebuffers;
PFNGLBINDFRAMEBUFFERPROC glBindFramebuffer;
PFNGLGENRENDERBUFFERSPROC glGenRenderbuffers;
PFNGLBINDRENDERBUFFERPROC glBindRenderbuffer;
PFNGLRENDERBUFFERSTORAGEPROC glRenderbufferStorage;
PFNGLFRAMEBUFFERRENDERBUFFERPROC glFramebufferRenderbuffer;
PFNGLFRAMEBUFFERTEXTUREPROC glFramebufferTexture;
PFNGLDRAWBUFFERSPROC glDrawBuffers;
PFNGLCHECKFRAMEBUFFERSTATUSPROC glCheckFramebufferStatus;
PFNGLGENVERTEXARRAYSPROC glGenVertexArrays;
PFNGLBINDBUFFERPROC glBindBuffer;
PFNGLBUFFERDATAPROC glBufferData;
PFNGLDELETEFRAMEBUFFERSPROC glDeleteFramebuffers;
PFNGLDELETERENDERBUFFERSPROC glDeleteRenderbuffers;
PFNGLDELETEVERTEXARRAYSPROC glDeleteVertexArrays;
PFNGLGETSHADERIVPROC glGetShaderiv;
PFNGLGETSHADERINFOLOGPROC glGetShaderInfoLog;
PFNGLGETPROGRAMIVPROC glGetProgramiv;
PFNGLGETPROGRAMINFOLOGPROC glGetProgramInfoLog;
PFNGLGENBUFFERSPROC glGenBuffers;
PFNGLENABLEVERTEXATTRIBARRAYPROC glEnableVertexAttribArray;
PFNGLVERTEXATTRIBPOINTERPROC glVertexAttribPointer;
PFNGLBINDVERTEXARRAYPROC glBindVertexArray;
PFNGLGETUNIFORMLOCATIONPROC glGetUniformLocation;
PFNGLUNIFORMMATRIX2FVPROC glUniformMatrix2fv;
PFNGLUNIFORMMATRIX3FVPROC glUniformMatrix3fv;
PFNGLUNIFORMMATRIX4FVPROC glUniformMatrix4fv;
PFNGLUNIFORM1FPROC glUniform1f;
PFNGLUNIFORM2FPROC glUniform2f;
PFNGLUNIFORM3FPROC glUniform3f;
PFNGLUNIFORM4FPROC glUniform4f;
PFNGLUNIFORM1IPROC glUniform1i;
PFNGLUNIFORM2IPROC glUniform2i;
PFNGLUNIFORM3IPROC glUniform3i;
PFNGLUNIFORM4IPROC glUniform4i;
PFNGLACTIVETEXTUREPROC glActiveTexture;
PFNGLDELETESHADERPROC glDeleteShader;
};
static HGLRC hglrc_;
static GLFunc* gl_;
unsigned int points_;
unsigned int uvs_;
unsigned int indexes_;
unsigned int vao_;
unsigned int calibrationTexture_;
unsigned int vertexShader_;
unsigned int fragmentShader_;
unsigned int programID_;
struct Vector3
{
Vector3(float x, float y, float z)
: x_(x), y_(y), z_(z)
{
}
float x_;
float y_;
float z_;
};
struct Vector2
{
Vector2(float x, float y)
: x_(x), y_(y)
{
}
float x_;
float y_;
};
struct Texture
{
Texture(unsigned int width, unsigned int height)
: width_(width), height_(height)
{
bits_.insert(bits_.end(), width*height * 3, 0);
}
unsigned char* GetTexel(unsigned int x, unsigned int y)
{
return &bits_[((x * height_) + y)*3];
}
unsigned int width_;
unsigned int height_;
std::vector<unsigned char> bits_;
};
unsigned int CompileVertexShader(const std::string& sourceCode)
{
GLint result = GL_FALSE;
GLuint vertexShaderID = gl_->glCreateShader(GL_VERTEX_SHADER);
const char* src = sourceCode.c_str();
gl_->glShaderSource(vertexShaderID, 1, &src, NULL);
gl_->glCompileShader(vertexShaderID);
gl_->glGetShaderiv(vertexShaderID, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int infoLogLength;
gl_->glGetShaderiv(vertexShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<GLchar> error(infoLogLength);
gl_->glGetShaderInfoLog(vertexShaderID, infoLogLength, &infoLogLength, &error[0]);
std::string errStr(&error[0], error.size());
OutputDebugString(errStr.c_str());
return 0;
}
return vertexShaderID;
}
unsigned int CompileFragmentShader(const std::string& sourceCode)
{
GLint result = GL_FALSE;
GLuint fragmentShaderID = gl_->glCreateShader(GL_FRAGMENT_SHADER);
const char* src = sourceCode.c_str();
gl_->glShaderSource(fragmentShaderID, 1, &src, NULL);
gl_->glCompileShader(fragmentShaderID);
gl_->glGetShaderiv(fragmentShaderID, GL_COMPILE_STATUS, &result);
if (result == GL_FALSE)
{
int infoLogLength;
gl_->glGetShaderiv(fragmentShaderID, GL_INFO_LOG_LENGTH, &infoLogLength);
std::vector<GLchar> error(infoLogLength);
gl_->glGetShaderInfoLog(fragmentShaderID, infoLogLength, &infoLogLength, &error[0]);
std::string errStr(&error[0], error.size());
OutputDebugString(errStr.c_str());
return 0;
}
return fragmentShaderID;
}
unsigned int LinkShaderProgram(unsigned int vertexShader, unsigned int fragmentShader)
{
// attempt to link the program...
GLint result = GL_FALSE;
unsigned int programID = gl_->glCreateProgram();
gl_->glAttachShader(programID, vertexShader);
gl_->glAttachShader(programID, fragmentShader);
gl_->glLinkProgram(programID);
gl_->glGetProgramiv(programID, GL_LINK_STATUS, &result);
if (result == GL_FALSE)
{
int InfoLogLength;
gl_->glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &InfoLogLength);
std::vector<GLchar> error(InfoLogLength);
gl_->glGetProgramInfoLog(programID, InfoLogLength, &InfoLogLength, &error[0]);
std::string errStr(&error[0], error.size());
OutputDebugString(errStr.c_str());
return 0;
}
gl_->glDetachShader(programID, vertexShader);
gl_->glDetachShader(programID, fragmentShader);
return programID;
}
void SetUniform(const std::string& name, unsigned int ID)
{
int location = gl_->glGetUniformLocation(programID_, name.c_str());
if (location < 0)
OutputDebugString("Unable to find uniform location :(");
else
gl_->glUniform1i(location, ID);
}
std::shared_ptr<Texture> LoadTexture(const std::string& filename)
{
CImage image;
image.Load(filename.c_str());
std::shared_ptr<Texture> texture(new Texture(image.GetWidth(), image.GetHeight()));
for (int y = image.GetHeight() - 1; y >= 0; --y)
{
for (int x = 0; x < image.GetWidth(); ++x)
{
unsigned char* target = texture->GetTexel(x, y);
unsigned char* source = reinterpret_cast<unsigned char*>(image.GetPixelAddress(x, y));
for ( unsigned int c = 0; c < 3; ++c )
*(target+c) = *(source+c);
}
}
return texture;
}
}
extern "C" __declspec(dllexport) int OpenGLInit(void* hdc)
{
// We need to create a context or get an existing one that fits the supplied context.
// first check we have a valid device context
if (!hdc)
return 1;
HDC hDC = static_cast<HDC>(hdc);
if (!hDC)
return 2;
// we then need to get a valid DC...
PIXELFORMATDESCRIPTOR pfd;
memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 32;
pfd.cStencilBits = 8;
pfd.iLayerType = PFD_MAIN_PLANE;
int nPixelFormat = ChoosePixelFormat(hDC, &pfd);
if (nPixelFormat == 0)
return 3;
if (!SetPixelFormat(hDC, nPixelFormat, &pfd))
return 4;
HGLRC tempContext = wglCreateContext(hDC);
wglMakeCurrent(hDC, tempContext);
int attribs[] =
{
WGL_CONTEXT_MAJOR_VERSION_ARB, 4,
WGL_CONTEXT_MINOR_VERSION_ARB, 1,
WGL_CONTEXT_FLAGS_ARB, 0,
0
};
PFNWGLCREATECONTEXTATTRIBSARBPROC wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
if (wglCreateContextAttribsARB)
{
hglrc_ = wglCreateContextAttribsARB(hDC, 0, attribs);
wglMakeCurrent(NULL, NULL);
wglDeleteContext(tempContext);
if (!wglMakeCurrent(hDC, hglrc_))
return 5;
}
// Initialise the functions..
gl_ = new GLFunc();
// Cache the geometry for a quad
float v = 1.0f;
std::vector<Vector3> quadPoints = std::initializer_list<Vector3>({
Vector3(-v, -v, 0),
Vector3(v, -v, 0),
Vector3(v, v, 0),
Vector3(-v, v, 0)
});
std::vector<Vector2> quadUVs = std::initializer_list<Vector2>({
Vector2(0, 0),
Vector2(1.0f, 0),
Vector2(1.0f, 1.0f),
Vector2(0, 1.0f)
});
std::vector<unsigned int> quadIndexes = std::initializer_list<unsigned int>({
0, 1, 2, 2, 3, 0
});
gl_->glGenVertexArrays(1, &vao_);
gl_->glBindVertexArray(vao_);
// Cache the points...
gl_->glGenBuffers(1, &points_);
gl_->glBindBuffer(GL_ARRAY_BUFFER, points_);
gl_->glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 3 * quadPoints.size(), &quadPoints[0].x_, GL_STATIC_DRAW);
gl_->glEnableVertexAttribArray(0);
gl_->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Cache the UVs...
gl_->glGenBuffers(1, &uvs_);
gl_->glBindBuffer(GL_ARRAY_BUFFER, uvs_);
gl_->glBufferData(GL_ARRAY_BUFFER, sizeof(float) * quadUVs.size() * 2, &quadUVs[0].x_, GL_STATIC_DRAW);
gl_->glEnableVertexAttribArray(1);
gl_->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Cache the indexes...
gl_->glGenBuffers(1, &indexes_);
gl_->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexes_);
gl_->glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * quadIndexes.size(), &quadIndexes[0], GL_STATIC_DRAW);
gl_->glBindBuffer(GL_ARRAY_BUFFER, NULL);
gl_->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
gl_->glBindVertexArray(NULL);
// Load the texture...
std::shared_ptr<Texture> calibrationImage = LoadTexture("uvgrid.jpg");
glGenTextures(1, &calibrationTexture_);
glBindTexture(GL_TEXTURE_2D, calibrationTexture_);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, calibrationImage->width_, calibrationImage->height_, 0, GL_RGB, GL_UNSIGNED_BYTE, &calibrationImage->bits_[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glBindTexture(GL_TEXTURE_2D, NULL);
// Compile the shaders
const char* fragmentSource = R"(
#version 410 core
uniform sampler2D quadTexture;
in vec2 UV;
out vec3 COLOUR;
void main()
{
//COLOUR = vec3(UV.st, 0);
COLOUR = texture(quadTexture, UV).bgr;
}
)";
const char* vertexSource = R"(
#version 410 core
in vec3 inP;
in vec2 inUV;
out vec2 UV;
void main()
{
gl_Position = vec4(inP, 1.0);
UV = inUV;
}
)";
vertexShader_ = CompileVertexShader(vertexSource);
fragmentShader_ = CompileFragmentShader(fragmentSource);
programID_ = LinkShaderProgram(vertexShader_, fragmentShader_);
return 0;
}
extern "C" __declspec(dllexport) void OpenGLKill()
{
wglDeleteContext(hglrc_);
}
extern "C" __declspec(dllexport) void OpenGLDraw(void* hdc, float rotation)
{
HDC validHDC = static_cast<HDC>(hdc);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
gl_->glUseProgram(programID_);
gl_->glBindVertexArray(vao_);
gl_->glBindBuffer(GL_ARRAY_BUFFER, uvs_);
gl_->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexes_);
// Assign the texture...
gl_->glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, calibrationTexture_);
SetUniform("quadTexture", calibrationTexture_);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
gl_->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
gl_->glBindVertexArray(NULL);
gl_->glUseProgram(NULL);
SwapBuffers(validHDC);
}
extern "C" __declspec(dllexport) void OpenGLResize(int width, int height)
{
glViewport(0, 0, width, height);
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//glOrtho(width / static_cast<float>(height), -width / static_cast<float>(height), -1.0f, 1.0f, 0.1f, 100.0f);
}
包含wpf客户端的完整代码:https://github.com/spiroyster/wpfogl (分支“GL4”)。
如前所述,我必须在绘制例程期间绑定纹理之前显式调用gl_->glActiveTexture(GL_TEXTURE1)
,尽管事实上我之前从未设置过活动纹理单元(并且在上下文中只加载了一个纹理)。如果我删除该行,即活动纹理单元保留为GL_TEXTURE0
,则它不显示纹理。
接下来是我加载的所有后续纹理...即如果它是我在此实例/上下文中加载的第二个纹理,则必须将活动纹理单元设置为GL_TEXTURE2
....这也会发生即使我只使用所述纹理..即使我只使用第二个纹理进行绘制(而不是第一个加载的纹理),我仍然必须将活动纹理单元显式设置为GL_TEXTURE2
。
感谢先进的任何贡献。
答案 0 :(得分:3)
您必须设置为纹理采样器均匀的值是纹理单元的索引而不是纹理对象。
更改您的代码:
gl_->glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, calibrationTexture_);
// this is wrong
//SetUniform("quadTexture", calibrationTexture_);
SetUniform( "quadTexture", 1 ); // 1 because of GL_TEXTURE1
见OpenGL 4.6 API Compatibility Profile Specification; 7.10 Samplers; page 154:
采样器是OpenGL着色语言中用于识别的特殊制服 用于每个纹理查找的纹理对象。采样器的值表示正在访问的纹理图像单元。将采样器的值设置为
i
会选择纹理图像单元编号i
。
注意,&#34;名称&#34;
glGenTextures(1, &calibrationTexture_);
这个&#34;名称&#34; (整数)是硬件和驱动程序相关的 但由于代码中只生成了1个纹理对象,因此名称的值很可能为1。
这意味着,当您设置
时gl_->glActiveTexture(GL_TEXTURE1);
您的代码有效,因为calibrationTexture_
的值为1。