gpt4 book ai didi

c++ - 为 glm 翻译添加统一 vector 在 OpenGL 中不起作用

转载 作者:行者123 更新时间:2023-11-30 05:49:23 27 4
gpt4 key购买 nike

如果我删除 uniform 变量和以下行:

//in CompileShaders function:
uniformModel = glGetUniformLocation(shader,"model");
...
//defined in the main draw loop after glfwPollEvents():
//and the shader has compiled, linked and activated at this point
glm::mat4 modelMatrix;
modelMatrix = glm::translate(modelMatrix, glm::vec3(triOffset, 0.0f, 0.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE,glm::value_ptr(modelMatrix));

程序显示正常,但是一旦我添加了统一变量并使用矩阵平移三角形,我就找不到为什么会出现红色屏幕,为了便于引用,着色器是:

static const char* vShader = 
"#version 330\n"
"layout (location = 0) in vec3 pos;\n"
"uniform mat4 model;\n"
"void main(){\n"
" gl_Position = model * vec4(pos, 1.0);\n"
"}\n";

// fragment shader
static const char* fShader = ""
"#version 330\n"
"out vec4 color;\n"
"void main(){\n"
" color = vec4(1.0, 1.0, 0.0, 1.0);\n"
"}\n";

整个源码是:

#include <iostream>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cmath>
#include <string.h>

#include "glm/glm.hpp"
#include "glm/gtc/matrix_transform.hpp"
#include "glm/gtc/type_ptr.hpp"


// Window Dimensions
const GLint WIDTH=800, HEIGHT=600;
GLuint VAO, VBO, shader;
GLint uniformModel {};
GLfloat triOffset {};
GLfloat triMaxOffset {5.0f};
bool direction = true;
// vertex shader
static const char* vShader =
"#version 330\n"
"layout (location = 0) in vec3 pos;\n"
"uniform mat4 model;\n"
"void main(){\n"
" gl_Position = model * vec4(pos, 1.0);\n"
"}\n";

// fragment shader
static const char* fShader = ""
"#version 330\n"
"out vec4 color;\n"
"void main(){\n"
" color = vec4(1.0, 1.0, 0.0, 1.0);\n"
"}\n";

void AddShader(GLuint theProgram, const char* ShaderCode, GLenum shaderType, std::string info){
std::cerr <<"DEBUG: Adding "<<info<<" Shader"<<std::endl;
GLuint theShader = glCreateShader(shaderType);

const GLchar* theCode[1];
theCode[0] = ShaderCode;

GLint codeLength[1];
codeLength[0] = strlen(ShaderCode);

glShaderSource(theShader, 1, theCode, codeLength);
glCompileShader(theShader);

GLint result =0;
GLchar eLog[1024] ={0};

glGetShaderiv(theShader, GL_COMPILE_STATUS, &result);
if(!result){
glGetShaderInfoLog(shader, sizeof(eLog), NULL, eLog);
std::cerr<<"Error compiling program"<<std::endl;
return;
}
glAttachShader(theProgram, theShader);

}

void CompileShader(){
shader = glCreateProgram();
if(!shader){
std::cerr<<"Error creating shader"<<std::endl;
return;
}

AddShader(shader, vShader, GL_VERTEX_SHADER, "vertex");
AddShader(shader, fShader, GL_FRAGMENT_SHADER, "fragment");

GLint result =0;
GLchar eLog[1024] ={0};

glLinkProgram(shader);
glGetProgramiv(shader, GL_LINK_STATUS, &result);
if(!result){
glGetProgramInfoLog(shader, sizeof(eLog), NULL, eLog);
std::cerr<<"Error linking program"<<std::endl;
return;
}

glValidateProgram(shader);
glGetProgramiv(shader, GL_VALIDATE_STATUS, &result);
if(!result){
glGetProgramInfoLog(shader, sizeof(eLog), NULL, eLog);
std::cerr<<"Error Validating program"<<std::endl;
return;
}

uniformModel = glGetUniformLocation(shader,"model");

}

void CreateTriangles(){
GLfloat vertices[]={
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f
};

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

glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*9,vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}


int main(){
//initialize GLFW
if(!glfwInit()){
std::cerr << "GLFW initialization failed!" << std::endl;
glfwTerminate();
return 1;
}

//Setup GLFW window properties
//openGL version
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
// core profile = no backward compatibility
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
//allow forward compatibility
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

GLFWwindow *mainWindow = glfwCreateWindow(WIDTH, HEIGHT, "TEST WINDOW", NULL, NULL);

if(!mainWindow){
std::cerr << "GLFW Window creation failed" << std::endl;
glfwTerminate();
return 1;
}

// get Buffer size information
int bufferWidth, bufferHeight;
glfwGetFramebufferSize(mainWindow, &bufferWidth, &bufferHeight);
// set context for GLEW to use
glfwMakeContextCurrent(mainWindow);

// allow modern extension features

if(glewInit()!=GLEW_OK){
std::cerr << "GLEW initialization failed" << std::endl;
glfwDestroyWindow(mainWindow);
glfwTerminate();
return 1;
}

// setup viewport size
glViewport(0, 0, bufferWidth, bufferHeight);
CreateTriangles();
CompileShader();




while(!glfwWindowShouldClose(mainWindow)){
// get and handle user input events
glfwPollEvents();

glClearColor(1.0f, 0.0f, 0.0f, 1.0);
glClear(GL_COLOR_BUFFER_BIT);

if(direction){
triOffset += 0.0005f;
}else{
triOffset -= 0.0005f;
triOffset = abs(triOffset);
}

if(direction<=0.0 || direction> triMaxOffset){
direction = !direction;
}

glUseProgram(shader);

glm::mat4 modelMatrix;
modelMatrix = glm::translate(modelMatrix, glm::vec3(triOffset, 0.0f, 0.0f));
glUniformMatrix4fv(uniformModel, 1, GL_FALSE,glm::value_ptr(modelMatrix));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,0,3);
glBindVertexArray(0);
glUseProgram(0);
// swap buffers
glfwSwapBuffers(mainWindow);
}


return 0;
}

如何确定问题所在?

最佳答案

模型矩阵变量 glm::mat4 modelMatrix 必须由 Identity matrix 初始化.

glm API documentationThe OpenGL Shading Language specification .

5.4.2 Vector and Matrix Constructors

If there is a single scalar parameter to a vector constructor, it is used to initialize all components of the constructed vector to that scalar’s value. If there is a single scalar parameter to a matrix constructor, it is used to initialize all the components on the matrix’s diagonal, with the remaining components initialized to 0.0.

Identity matrix可以通过单个参数 1.0 初始化:

glm::mat4 modelMatrix(1.0f);

关于c++ - 为 glm 翻译添加统一 vector 在 OpenGL 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59191104/

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