我无法绘制高度和宽度相同的对象。我本来是想做一些更复杂的事情,但是我已经尽力将其分解:
我有一个分辨率为600x600px的窗口以及以下顶点和片段着色器:
顶点着色器
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0f);
}
片段着色器
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f,1.0f,1.0f,1.0f);
}
我收到的输出如下:
这是我用来绘制正方形的代码:
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include "shader.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Object.h"
int width = 600;
int height = 600;
int main()
{
//window creation -----------------------------------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 16);
GLFWwindow* window = glfwCreateWindow(width, height, "Mumel", NULL, NULL);
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Error during GLAD initialization" << std::endl;
glfwTerminate();
return -1;
}
glViewport(0, 0, width, height);
//----------------------------------------------------------------------
Shader sh("./shader/vShader.vSh", "./shader/fShader.fSh");
Circle * circle = new Circle(0.5); //is explained later in the post
while (!glfwWindowShouldClose(window))
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
sh.use();
circle->draw();
glfwSwapBuffers(window);
glfwPollEvents();
}
}
最后但并非最不重要的一点是,我的“圆”(应该是一个“圆”,但是正方形更易于调试)由以下代码创建:
#ifndef OBJECT_H
#define OBJECT_H
#include <glad/glad.h>
#include <iostream>
#include "shader.h"
#include <vector>
class Circle
{
public:
static const int size = 6;
Circle()
{
std::vector<float> vertexData(3 * size);
// center
vertexData.at(0) = 0.0f;
vertexData.at(1) = 0.0f;
vertexData.at(2) = 0.0f;
vertexData.at(3) = 0.5f;
vertexData.at(5) = 0.0f;
vertexData.at(4) = -0.5f;
vertexData.at(6) = -0.5f;
vertexData.at(8) = 0.0f;
vertexData.at(7) = -0.5f;
vertexData.at(9) = -0.5f;
vertexData.at(11) = 0.0f;
vertexData.at(10) = 0.5f;
vertexData.at(12) = 0.5f;
vertexData.at(14) = 0.0f;
vertexData.at(13) = 0.5f;
vertexData.at(15) = 0.5f;
vertexData.at(17) = 0.0f;
vertexData.at(16) = -0.5f;
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
float * dataArr = vertexData.data(); //The GLSL shader needs a C Array, this returns the pointer to the first array object used by std::vector
int dataSize = vertexData.size();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, dataSize * sizeof(float), dataArr, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw()
{
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_FAN, 0, 3 * (size - 2));
glBindVertexArray(0);
}
private:
unsigned int VBO, VAO;
};
#endif
答案 0 :(得分:0)
为我工作很正常:
系统/构建信息:
代码:
#include <glad/glad.h>
#define GLFW_INCLUDE_NONE
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <vector>
#include <iostream>
void CheckStatus( GLuint obj, bool isShader )
{
GLint status = GL_FALSE, log[ 1 << 11 ] = { 0 };
( isShader ? glGetShaderiv : glGetProgramiv )( obj, isShader ? GL_COMPILE_STATUS : GL_LINK_STATUS, &status );
if( status == GL_TRUE ) return;
( isShader ? glGetShaderInfoLog : glGetProgramInfoLog )( obj, sizeof( log ), NULL, (GLchar*)log );
std::cerr << (GLchar*)log << "\n";
std::exit( EXIT_FAILURE );
}
void AttachShader( GLuint program, GLenum type, const char* src )
{
GLuint shader = glCreateShader( type );
glShaderSource( shader, 1, &src, NULL );
glCompileShader( shader );
CheckStatus( shader, true );
glAttachShader( program, shader );
glDeleteShader( shader );
}
const char* const vert = 1 + R"GLSL(
#version 330 core
layout (location = 0) in vec3 aPos;
void main()
{
gl_Position = vec4(aPos, 1.0f);
}
)GLSL";
const char* const frag = 1 + R"GLSL(
#version 330 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f,1.0f,1.0f,1.0f);
}
)GLSL";
class Circle
{
public:
static const int size = 6;
Circle()
{
std::vector<float> vertexData(3 * size);
// center
vertexData.at(0) = 0.0f;
vertexData.at(1) = 0.0f;
vertexData.at(2) = 0.0f;
vertexData.at(3) = 0.5f;
vertexData.at(5) = 0.0f;
vertexData.at(4) = -0.5f;
vertexData.at(6) = -0.5f;
vertexData.at(8) = 0.0f;
vertexData.at(7) = -0.5f;
vertexData.at(9) = -0.5f;
vertexData.at(11) = 0.0f;
vertexData.at(10) = 0.5f;
vertexData.at(12) = 0.5f;
vertexData.at(14) = 0.0f;
vertexData.at(13) = 0.5f;
vertexData.at(15) = 0.5f;
vertexData.at(17) = 0.0f;
vertexData.at(16) = -0.5f;
glGenBuffers(1, &VBO);
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
float * dataArr = vertexData.data(); //The GLSL shader needs a C Array, this returns the pointer to the first array object used by std::vector
int dataSize = vertexData.size();
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, dataSize * sizeof(float), dataArr, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void draw()
{
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_FAN, 0, 3 * (size - 2));
glBindVertexArray(0);
}
private:
unsigned int VBO, VAO;
};
int main( int, char** )
{
glfwSetErrorCallback( []( int, const char* desc ) { std::cerr << desc << "\n"; std::exit( EXIT_FAILURE ); } );
glfwInit();
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
GLFWwindow* window = glfwCreateWindow( 600, 600, "GLFW", NULL, NULL );
glfwMakeContextCurrent( window );
gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress );
Circle * circle = new Circle();
GLuint prog = glCreateProgram();
AttachShader( prog, GL_VERTEX_SHADER, vert );
AttachShader( prog, GL_FRAGMENT_SHADER, frag );
glLinkProgram( prog );
CheckStatus( prog, false );
glUseProgram( prog );
while( !glfwWindowShouldClose( window ) )
{
glfwPollEvents();
int w, h;
glfwGetFramebufferSize( window, &w, &h );
glViewport( 0, 0, w, h );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
circle->draw();
glfwSwapBuffers( window );
}
glfwTerminate();
}