gpt4 book ai didi

c++ - 将 VAO+IBO 与 GLSL 着色器一起使用

转载 作者:行者123 更新时间:2023-12-01 14:41:42 26 4
gpt4 key购买 nike

我正在尝试将 VAO + VBO + IBO 与着色器一起使用,但没有绘制任何对象。我不确定我错过了什么。我对 C++ 和 GLSL 还是很陌生,所以我不确定我是否在一般情况下搞砸了 C++,或者我是否未能正确处理 OpenGL 上下文?

主要功能(缺少处理窗口创建的代码。如果您认为您可能还需要查看它,请告诉我。):

int main(int argc, char *argv[])
{

//INIT SDL
SDL_Init(SDL_INIT_VIDEO);
SDL_CreateWindowAndRenderer(400, 300, SDL_WINDOW_OPENGL, &displayWindow, &displayRenderer);
SDL_GetRendererInfo(displayRenderer, &displayRendererInfo);

/*TODO: Check that we have OpenGL */
if ((displayRendererInfo.flags & SDL_RENDERER_ACCELERATED) == 0 || (displayRendererInfo.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {}

SDL_GL_CreateContext(displayWindow);
//SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

glewInit();
int error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error during glewInit call: " << error << "\n"; };

//glEnable(GL_DEBUG_OUTPUT);

Display_InitGL();
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error during Display init: " << error << "\n"; };

Display_SetViewport(400, 300);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error during Display Set Viewport Issue: " << error << "\n"; };

// SET UP TEST OBJ
MainChar *player = new MainChar();
player->MainChar_VBO_Func();
GLushort size = player->MainChar_VBO_IndexBuffer_Func();
float count = 0.0;
// END SET UP OF TEST OBJ

GLint *length = new GLint;
const char* vertShdr = readFile("C:\\Users\\JRFerrell\\Documents\\Visual Studio 2013\\Projects\\GLEW Practice\\vertShader.vs", *length);

std::cout << vertShdr;

GLuint vertShaderId = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vertShaderId, 1, &vertShdr, length);
std::cout << "\n\nLength: " << *length;

glCompileShader(vertShaderId);

GLint *length2 = new GLint;
const char* fragShdr = readFile("C:\\Users\\JRFerrell\\Documents\\Visual Studio 2013\\Projects\\GLEW Practice\\fragShader.fs", *length2);

GLint fragShaderId = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fragShaderId, 1, &fragShdr, length2);

glCompileShader(fragShaderId);

GLuint shaderProgram = glCreateProgram();

glAttachShader(shaderProgram, vertShaderId);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error during glAttachShader: " << error << "\n"; };

glAttachShader(shaderProgram, fragShaderId);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error during glAttachShader: " << error << "\n"; };

glBindAttribLocation(shaderProgram, 0, "in_Position");
glBindAttribLocation(shaderProgram, 1, "in_Normal");

glLinkProgram(shaderProgram);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error during glLinkProgram: " << error << "\n"; };

// END SHADER PROGRAM DEFINITION

//Check info log for errors:

int Len = 0;

char *Buffer = nullptr;
glGetShaderiv(vertShaderId, GL_INFO_LOG_LENGTH, &Len);
Buffer = new char[Len];
glGetShaderInfoLog(vertShaderId, Len, &Len, Buffer);
std::cout << "Vertex Log:" << std::endl << Buffer << std::endl;
delete[] Buffer;

glGetShaderiv(fragShaderId, GL_INFO_LOG_LENGTH, &Len);
Buffer = new char[Len];
glGetShaderInfoLog(fragShaderId, Len, &Len, Buffer);
std::cout << "Fragment Log:" << std::endl << Buffer << std::endl;
delete[] Buffer;

glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &Len);
Buffer = new char[Len];
glGetProgramInfoLog(shaderProgram, Len, &Len, Buffer);
std::cout << "Shader Log:" << std::endl << Buffer << std::endl;
delete[] Buffer;

// Create VAO. Don't forget to enable all necessary states because the VAO starts with default state, cleaning all states prev called to do so.
GLuint VaoId;
glGenVertexArrays(1, &VaoId);
glBindVertexArray(VaoId);

// Bind buffers & set-up VAO vertex pointers
glBindBuffer(GL_ARRAY_BUFFER, player->vboID);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error glBindBuffer-vboID: " << error << "\n"; }
glEnableClientState(GL_VERTEX_ARRAY);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * GL_FLOAT, (const GLvoid *)0);
glEnableVertexAttribArray(0);

// Set-up VAO normal pointers
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error glBindBuffer-vbo init: " << error << "\n"; }
glEnableClientState(GL_NORMAL_ARRAY);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * GL_FLOAT, (void*)(3 * sizeof(GL_FLOAT)));
glEnableVertexAttribArray(1);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, player->vboIndexID);

GLint maxLength, nAttribs;
glGetProgramiv(shaderProgram, GL_ACTIVE_ATTRIBUTES, &nAttribs);
glGetProgramiv(shaderProgram, GL_ACTIVE_ATTRIBUTES, &maxLength);

//std::cout << "\nmax length: " << maxLength << "\nnAttribs: " << nAttribs;

glBindVertexArray(0);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error glBindVertexArray: " << error << "\n"; };
// End VAO init

while (1){

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error glClearColor: " << error << "\n"; };

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
error = glGetError();
if (error != GL_NO_ERROR){ std::cout << "Error in glClear: " << error << "\n"; };

glLoadIdentity();

glUseProgram(shaderProgram);
glBindVertexArray(VaoId);

glDrawElements(GL_TRIANGLES, size, GL_UNSIGNED_SHORT, 0);

glUseProgram(0);
glBindVertexArray(0);

SDL_GL_SwapWindow(displayWindow);

count -= .1;

}

SDL_Delay(5000);

SDL_Quit();

return 0;

}

::着色器代码::

顶点着色器:
#version 400

in vec3 in_Position;
in vec3 in_Normal;

void main()
{

gl_Position = vec4(in_Position, 1.0);

}

片段着色器:
#version 400

out vec4 FragColor;

void main()
{

FragColor = vec4(0.0f, 0.5f, 1.0f, 1.0f);

}

我确实在这里看过类似的问题,它们确实帮助我解决了一些可能的问题,但到目前为止,它们显然没有被证明对帮助我启动和运行代码有用。我还在 gamedev.net 上实时询问了其他一些人,但他们似乎也看不出我哪里出错了。我通过声明 glDoubles 而不是浮点数解决了一个可能的问题,但这实际上是在没有 vao 和着色器的情况下工作的,所以这不是(也不太可能是)问题,全部或部分。

最佳答案

我不知道以下任何一项是否能解决您的问题,但我确实在您的代码中看到了一些问题:

glEnableClientState(GL_VERTEX_ARRAY);



您在这里将旧的和已弃用的内置顶点属性与通用顶点属性混合在一起。你不需要这些 glEnableClientState调用 - 您的着色器不使用内置属性。 glLoadIdentity 也是如此。这也是完全不需要的,并且在核心配置文件上下文中是无效的。

我看到的第二件事是您没有指定属性索引,因此 GL 可以自由映射它们。您也不查询它们,而只是假设它们为 0 表示 in_Position和 1 代表 in_Normal - 绝不能保证是这样。使用 layout(location=)在顶点着色器中声明输入属性以实际定义映射时使用限定符,或使用 glBindAttribLocation .

关于c++ - 将 VAO+IBO 与 GLSL 着色器一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21964397/

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