gpt4 book ai didi

c++ - 在 OpenGL 3.3 上使用 texture2D

转载 作者:搜寻专家 更新时间:2023-10-31 01:27:25 28 4
gpt4 key购买 nike

所以我一直在摆弄一个在 OpenGL 3.3 (FreeGLUT + GLEW) 中完成的旧大学项目,但我遇到了一些问题。

就在开始时,我运行程序,但在编译 BumpMap 片段着色器时遇到错误:

#version 330 core    
#define lightCount 10

in vec4 vertPos;
in vec4 eyeModel;

in vec3 normalInterp;

in vec4 ambient;
in vec4 color;
in vec4 spec;
in vec2 texuv;

in vec4 lightPosition[lightCount];

struct LightSource {

vec4 Position;
vec4 Direction;

vec4 Color;

float CutOff;

float AmbientIntensity;
float DiffuseIntensity;
float SpecularIntensity;

float ConstantAttenuation;
float LinearAttenuation;
float ExponentialAttenuation;

int lightType;
};

layout(std140) uniform LightSources {

LightSource lightSource[10];
};

uniform sampler2D diffuse_tex;
uniform sampler2D normal_tex;

out vec4 out_Color;

void main() {
out_Color = vec4(0);

for(int i=0; i<lightCount; i++) {

if(lightSource[i].lightType == 0)
continue;

vec3 NormalMap = texture2D(normal_tex, texuv).rgb;
vec3 normal = normalize(NormalMap * 2.0 - 1.0); //normalize(normalInterp);

vec4 LightDirection = vertPos - lightSource[i].Position;
float Distance = length(LightDirection);
LightDirection = normalize(LightDirection);

vec4 ambientColor = ambient * lightSource[i].Color * lightSource[i].AmbientIntensity;
vec4 diffuseColor = vec4(0, 0, 0, 0);
vec4 dColor = texture2D(diffuse_tex, texuv);
vec4 specularColor = vec4(0, 0, 0, 0);

float DiffuseFactor = dot(normal, vec3(-LightDirection));

if (DiffuseFactor > 0) {

diffuseColor = dColor * lightSource[i].Color * lightSource[i].DiffuseIntensity * DiffuseFactor;

vec3 VertexToEye = normalize(vec3(eyeModel - vertPos));
vec3 LightReflect = normalize(reflect(vec3(LightDirection), normal));

float SpecularFactor = dot(VertexToEye, LightReflect);

SpecularFactor = pow(SpecularFactor, 255);
if(SpecularFactor > 0.0){
//SpecularFactor = pow( max(SpecularFactor,0.0), 255);
specularColor = spec * lightSource[i].Color * lightSource[i].SpecularIntensity * SpecularFactor;
}
}

out_Color += ambientColor + diffuseColor + specularColor;
}
}

ERROR: 0:55: 'function' : is removed in Forward Compatible context texture2D
ERROR: 0:55: 'texture2D' : no matching overloaded function found (using implicit conversion)

所以我查找了这个问题,尽管我认为这很奇怪,但我在一个我知道一直处于工作状态的项目中遇到了这个问题,我将 texture2D 调用切换为 texture 调用,现在着色器编译,但我得到一个不同的错误,为场景中的第一个对象创建缓冲区对象:

//Consts defined here for readability
#define VERTICES 0
#define COLORS 1
#define NORMALS 2
#define TEXUVS 3
#define AMBIENTS 4
#define TANGENTS 5
#define SPECULARS 6
#define SPECULARS_CONSTANTS 7
#define NOISE_SCALE 8

void BufferObject::createBufferObject() {

glGenVertexArrays(1, &_vertexArrayObjectID);
glBindVertexArray(_vertexArrayObjectID);

glGenBuffers(1, &_vertexBufferObjectID);

glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferObjectID);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex)*_vertexCount, _vertices, GL_STATIC_DRAW);

glEnableVertexAttribArray(VERTICES);
glVertexAttribPointer(VERTICES, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);

glEnableVertexAttribArray(COLORS);
glVertexAttribPointer(COLORS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)sizeof(_vertices[0].XYZW));

glEnableVertexAttribArray(NORMALS);
glVertexAttribPointer(NORMALS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)));

glEnableVertexAttribArray(TEXUVS);
glVertexAttribPointer(TEXUVS, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)));

glEnableVertexAttribArray(AMBIENTS);
glVertexAttribPointer(AMBIENTS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)));

glEnableVertexAttribArray(TANGENTS);
glVertexAttribPointer(TANGENTS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)+sizeof(_vertices[0].AMBIENT)));

glEnableVertexAttribArray(SPECULARS);
glVertexAttribPointer(SPECULARS, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)+sizeof(_vertices[0].AMBIENT)+sizeof(_vertices[0].TANGENT)));

glEnableVertexAttribArray(SPECULARS_CONSTANTS);
glVertexAttribPointer(SPECULARS_CONSTANTS, 1, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)(sizeof(_vertices[0].XYZW)+sizeof(_vertices[0].RGBA)+sizeof(_vertices[0].NORMAL)+sizeof(_vertices[0].TEXUV)+sizeof(_vertices[0].AMBIENT)+sizeof(_vertices[0].TANGENT)+sizeof(_vertices[0].SPECULAR)));

glBindVertexArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_UNIFORM_BUFFER, 0);

glDisableVertexAttribArray(VERTICES);
glDisableVertexAttribArray(COLORS);
glDisableVertexAttribArray(NORMALS);
glDisableVertexAttribArray(TEXUVS);
glDisableVertexAttribArray(AMBIENTS);
glDisableVertexAttribArray(TANGENTS);
glDisableVertexAttribArray(SPECULARS);
glDisableVertexAttribArray(SPECULARS_CONSTANTS);

Utility::checkOpenGLError("ERROR: Buffer Object creation failed.");
}

OpenGL ERROR [Invalid Operation] = 1282

这就是我得到的所有信息。我移动了 checkOpenGLError 并发现 glDisableVertexAttribArray(VERTICES) 行给出了错误。经过更多的挖掘,我发现你不应该设置 glBindVertexArray(0)(至少在你 glDisableVertexAttribArray 之前,根据我的内存,我们设置了这些标志到 0 这样我们就不会意外地影响我们不想要的任何东西)

此时错误移至我们绘制场景对象之一的位置。在这一点上,我有点碰壁了,不知道下一步该去哪里。我想我的问题是在运行需要设置的项目时是否有配置,或者是否只是在更新的显卡上运行它可以解释不同的行为。最后一点,这是在 widnows 中运行在 Visual Studio 10(或 15,当恢复所有更改并且没有重新定位解决方案时切换到 10)的 windows 上,程序配置如下:

//GLUT Init
glutInit(&argc, argv);

glutInitContextVersion(3, 3);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_CORE_PROFILE);

glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,GLUT_ACTION_GLUTMAINLOOP_RETURNS);

glutInitWindowSize(windowWidth, windowHeight);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);

windowHandle = glutCreateWindow(CAPTION);

//GLEW Init
glewExperimental = GL_TRUE;

GLenum result = glewInit();

//GLUT Init
std::cerr << "CONTEXT: OpenGL v" << glGetString(GL_VERSION) << std::endl;

glClearColor(0.1f, 0.1f, 0.1f, 1.0f);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_TRUE);
glDepthRange(0.0,1.0);
glClearDepth(1.0);

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);

上面的上下文是:

CONTEXT: OpenGL v3.3.0 - Build 22.20.16.4749

如果需要任何其他信息,请告诉我,我不想再添加任何不必要的困惑,而且项目太大,无法将其全部粘贴在这里...

最佳答案

在您的着色器中,您使用的是 glsl 版本 330 核心,这意味着 texture2D() 已弃用,您应该改用 texture()

至于您的 INVALID OPERATION 错误,问题是您使用 glBindVertexArray(0); 解除了 vao 的绑定(bind),然后调用了 glDisableVertexAttribArray(VERTICES);,它在目前绑定(bind)vao。您应该在这些调用下移动 glBindVertexArray(0);

关于c++ - 在 OpenGL 3.3 上使用 texture2D,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53362912/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com