gpt4 book ai didi

c++ - 无法使用现代 OpenGL 渲染基本三角形

转载 作者:行者123 更新时间:2023-11-28 01:45:33 25 4
gpt4 key购买 nike

<分区>

我正在尝试从头开始编写一个现代 OpenGL 应用程序,但我无法获得要渲染的基本三角形。这很奇怪,因为我在一些较旧的代码中对此进行了管理,但我找不到我所做的一切的不同之处。

我正在使用着色器,据我所知,它们已正确编译和链接。

我正在使用 GLFW 和 gl3w。

我从这段代码中得到的只是带有清晰颜色的窗口。

我将转储我的代码

Main.cpp

#include <GL/gl3w.h>
#include "Display/Display.h"
#include "Shader/Shader.h"
#include "Mesh/Mesh.h"

#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define WINDOW_TITLE "CompSci Coursework"

int main() {

//Create Display and Context
Display display( WINDOW_WIDTH, WINDOW_HEIGHT, WINDOW_TITLE );

//Create Basic Shaders
Shader basicShader( "BasicShader" );
basicShader.useShader();

//Create VAO
GLuint VAO;
glGenVertexArrays( 1, &VAO );
glBindVertexArray( VAO );

std::vector<float> verticies = {
-0.5f, -0.5f,
0.5f, -0.5f,
0.0f, 0.5f
};

//Create Mesh
Mesh triangle( verticies );

//While Open Loop
while ( !display.CheckShouldClose()) {

//Clear Buffer
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

//Draw Triangle
glDrawArrays( GL_TRIANGLES, 0, 3 );

//Update Display
display.Update();
}

return 0;
}

Display.cpp

#include <iostream>
#include <GL/gl3w.h>
#include "Display.h"

Display::Display( int width, int height, const char* title ) {

//Init GLFW
if ( glfwInit() == 0 ) {
std::cerr << "Could not initialise GLFW\n";
return;
} else {
std::cout << "GLFW initialised\n";
}

//Window Hints
glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 2 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
glfwWindowHint( GLFW_RESIZABLE, GL_FALSE );

//Create Window
m_window = glfwCreateWindow( width, height, title, NULL, NULL );

//Make Context
glfwMakeContextCurrent( m_window );

//Initialise GL3W
if ( gl3wInit() != 0 ) {
std::cerr << "Could not init GL3W\n";
return;
} else {
std::cout << "Initialised GL3W\n";
}
}

Display::~Display() {

//Delete Context
glfwMakeContextCurrent( 0 );

//Destroy Window
glfwDestroyWindow( m_window );

//Terminate GLFW
glfwTerminate();
}

bool Display::CheckShouldClose() {

return (bool)glfwWindowShouldClose( m_window );
}

void Display::Update() {

//Swap Buffers
glfwSwapBuffers( m_window );

//Poll Events
glfwPollEvents();
}

Mesh.cpp

#include <GL/gl3w.h>
#include <iostream>
#include "Mesh.h"

Mesh::Mesh( std::vector<float> verticies ) {

m_verticies = verticies;

//Generate VBO
glGenBuffers( 1, &m_vbo );

//Bind VBO
glBindBuffer( GL_ARRAY_BUFFER, m_vbo );;

//Pass Buffer Data
glBufferData( GL_ARRAY_BUFFER, m_verticies.size() * sizeof( float), &m_verticies[0], GL_STATIC_DRAW );
}

Mesh::~Mesh() {

glDeleteBuffers( 1, &m_vbo );
}

Shader.cpp

#include <fstream>
#include <iostream>
#include <vector>
#include <GL/gl3w.h>
#include "Shader.h"

#define SHADER_LIST { VERTEX_SHADER, FRAGMENT_SHADER }

Shader::Shader( std::string shaderName ) {

//Load Source Code
std::vector<std::string> shaderSources;
shaderSources.push_back( loadShaderSource( shaderName + ".vert" ));
shaderSources.push_back( loadShaderSource( shaderName + ".frag" ));

//Cast into C-Style Strings
const char* shaderSourcesCString[NUM_SHADER];
for ( int i : SHADER_LIST ) {
shaderSourcesCString[ i ] = shaderSources[ i ].c_str();
}

//Create Shaders
m_shaders[ VERTEX_SHADER ] = glCreateShader( GL_VERTEX_SHADER );
m_shaders[ FRAGMENT_SHADER ] = glCreateShader( GL_FRAGMENT_SHADER );

//Source Shaders
glShaderSource( m_shaders[ VERTEX_SHADER ], 1, &shaderSourcesCString[ VERTEX_SHADER ], NULL );
glShaderSource( m_shaders[ FRAGMENT_SHADER ], 1, &shaderSourcesCString[ FRAGMENT_SHADER ], NULL );

//Compile Shaders
for ( int i : SHADER_LIST ) {
glCompileShader( m_shaders[ i ] );

GLint status;
glGetShaderiv( m_shaders[ i ], GL_COMPILE_STATUS, &status );

if ( status != GL_TRUE ) {
char buffer[512];
glGetShaderInfoLog( m_shaders[ i ], 512, NULL, buffer );
std::cerr << "Shader Compilation Error on shader " << i << ": " << buffer << "\n";
return;
}
}

//Create Program
m_program = glCreateProgram();

//Attach Shaders
for ( int i : SHADER_LIST ) {
glAttachShader( m_program, m_shaders[ i ] );
}

//Link Shader
glLinkProgram( m_program );

//Find Link Error
{
GLint status;
glGetProgramiv(m_program, GL_LINK_STATUS, &status);

if (status != GL_TRUE){
std::cerr << "Unable to link shaders\n";

char buffer[512];
glGetProgramInfoLog(m_program, 512, NULL, buffer);
std::cerr << buffer;
return;
}
}

//Bind outColor
glBindFragDataLocation(m_program, 0, "outColor");

//Attributes
GLint posAttrib = glGetAttribLocation(m_program, "position");
glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(posAttrib);

std::cout << "Shaders Compiled Successfully\n";
}

Shader::~Shader() {

for ( int i : SHADER_LIST ) {
//Detach Shader
glDetachShader( m_program, m_shaders[ i ] );
//Delete Shader
glDeleteShader( m_shaders[ i ] );
}

//Delete Program
glDeleteProgram( m_program );
}

std::string Shader::loadShaderSource( std::string filename ) {

//Locals
std::string output, line;

//Open File
std::fstream file;
file.open( "Shaders/" + filename );

//Check for error loading file
if ( file.bad()) {
std::cerr << "Could not load shader file: " << filename << "\n";
return NULL;
} else {
std::cout << "Loaded shader file: " << filename << "\n";
}

//Read file into locals
while ( file.good()) {
getline( file, line );

if ( line != "" )
output += line + "\n";
}

//Return C-String
return output;
}

void Shader::useShader() {

glUseProgram( m_program );
}

GLuint Shader::getProgram() {

return m_program;
}

这些是我的着色器

顶点着色器

#version 150 core

in vec2 position;

void main(){
gl_Position = vec4(position, 0.0, 1.0);
}

片段着色器

#version 150 core

out vec4 outColor;

void main(){
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}

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