问题描述
我正在尝试在我的 OpenGL 项目中制作一个立方体。由于某种原因,我的代码没有呈现,我不知道为什么。我试图将 glm::mat4
更改为 glm::mat4(1)
;但是,它仍然不起作用。即使没有绘制,我也没有收到任何错误或警告。我看到的只是一个空白屏幕,但没有立方体。
main.cpp
#include <iostream>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "Shader.h"
#include "SOIL2/SOIL2.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace std;
const GLint WIDTH = 800;
const GLint HEIGHT = 600;
nt main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MInor,3);
glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE);
glfwWindowHint(GLFW_RESIZABLE,GL_TRUE );
GLFWwindow* window = glfwCreateWindow(WIDTH,HEIGHT,"Pemdas",nullptr,nullptr);
int screenWidth;
int screenHeight;
glfwGetFramebufferSize(window,&screenWidth,&screenWidth);
if(nullptr == window) {
cout << "Falled to create the window.";
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if(GLEW_OK != glewInit()) {
std::cout << "Failed to initialize window" << std::endl;
return -1;
}
glViewport(0,screenWidth,screenHeight);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
Shader ourShader("Resources/Shaders/core.vs","Resources/Shaders/core.frag");
GLfloat vertices[] =
{
-0.5f,-0.5f,0.0f,0.5f,1.0f,1.0f
};
GLint VAO;
glGenVertexArrays( 1,&VAO );
GLint VBO;
glGenBuffers( 1,&VBO );
glBindVertexArray( VAO );
glBindBuffer(GL_ARRAY_BUFFER,VBO);
glBufferData(GL_ARRAY_BUFFER,sizeof( vertices ),vertices,GL_STATIC_DRAW);
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5 * sizeof(GLfloat),(GLvoid*)0);
glEnabLevertexAttribArray(0);
glVertexAttribPointer(2,2,(GLvoid*)(3 * sizeof(GLfloat)));
glEnabLevertexAttribArray(2);
glBindVertexArray(0);
gluint texture;
glGenTextures(1,&texture);
glBindTexture(GL_TEXTURE_2D,texture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
int width;
int height;
unsigned char* image = SOIL_load_image("Resources/Shaders/pexels-adrien-olichon-2931270.jpg",&width,&hight,SOIL_LOAD_RGBA);
glTexImage2D( GL_TEXTURE_2D,GL_RGBA,width,hight,GL_UNSIGNED_BYTE,image);
glGenerateMipmap(GL_TEXTURE_2D);
SOIL_free_image_data(image);
glBindTexture(GL_TEXTURE_2D,0);
glm::mat4 projection(1);
projection = glm::perspective(45.0f,(GLfloat)screenWidth / (GLfloat)screenHeight,0.1f,1000.0f);
while(!glfwWindowShouldClose(window)) {
glfwPollEvents();
glClearColor(0.2f,0.3f,1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D,texture);
gluniform1i(glGetUniformlocation(ourShader.Program,"ourTexture"),0);
ourShader.Use();
glm::mat4 model(1);
glm::mat4 view(1);
model = glm::rotate(model,(GLfloat)glfwGetTime() * 1.0f,glm::vec3(0.5f,0.0f));
view = glm::translate(view,glm::vec3(0.0f,-3.0f));
GLint modelLoc = glGetUniformlocation(ourShader.Program,"model");
GLint viewLoc = glGetUniformlocation(ourShader.Program,"view");
GLint projLoc = glGetUniformlocation(ourShader.Program,"projection");
gluniformMatrix4fv(modelLoc,1,glm::value_ptr(model));
gluniformMatrix4fv(viewLoc,glm::value_ptr(view));
gluniformMatrix4fv(projLoc,glm::value_ptr(projection));
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES,36);
glBindVertexArray(0);
glfwSwapBuffers(window);
}
glDeleteVertexArrays(1,&VAO );
glDeleteBuffers(1,&VBO);
glfwTerminate();
return 0;
}
core.vs
#version 400 core
layout (location = 0) in vec3 position;
layout (location = 2) in vec2 texCoord;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = model * view * projection * vec4(position,1.0f);
TexCoord = vec2(texCoord.x,1.0 - texCoord.y);
}
core.frag
#version 400 core
in vec2 TexCoord;
out vec4 color;
uniform sampler2D texture1;
void main() {
color = texture(texture1,TexCoord);
}
解决方法
如评论中所述,变换顺序与矩阵乘法顺序相反。同样在您的顶点着色器中,您会在 color
中获得一个名为 location=1
的输入属性,但我在任何地方都看不到 glEnableVertexAttribArray(1);
。
我还建议您像这样检查着色器中的编译错误:
glGetShaderiv(vertex,GL_COMPILE_STATUS,&success);
if (!success)
{
glGetShaderInfoLog(vertex,512,NULL,infoLog);
std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
};