OpenGL三角形无法渲染

时间:2017-04-18 19:52:32

标签: c++ opengl glfw glew glm-math

我在c ++中编写了一些OpenGL代码,用于向屏幕呈现红色三角形。该程序使用opengl 3.3和glsl版本330.我已经缩小了对几个.cpp类文件或我的glsl代码不起作用的代码。我没有编译器或链接器错误(我确实得到一些链接器警告,但我不认为这些很重要)。我已经根据我认为最容易发生错误的地方给出了代码。

Renderer.cpp:

#include "Renderer.h"

#include <GL/glew.h>

void Renderer::render(int vbo){
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glDisableVertexAttribArray(0);
}

ShaderProgram.cpp

#pragma warning(disable:4101)

#include "ShaderProgram.h"

#include <fstream>
#include <sstream>

ShaderProgram::ShaderProgram(std::string vertexLocation, std::string fragmentLocation){
    programID = glCreateProgram();
    vertexID = glCreateShader(GL_VERTEX_SHADER);
    fragmentID = glCreateShader(GL_FRAGMENT_SHADER);
    createShader(vertexID, vertexLocation);
    createShader(fragmentID, fragmentLocation);
    glLinkProgram(programID);
    GLint success;
    glGetProgramiv(programID, GL_LINK_STATUS, &success);
    if (success == 0) {
        GLchar ErrorLog[1024];
        glGetProgramInfoLog(programID, sizeof(ErrorLog), nullptr, ErrorLog);
        fprintf(stderr, "Error linking shader program: '%s'\n", ErrorLog);
    }
    glValidateProgram(programID);
    glUseProgram(programID);
}

void ShaderProgram::createShader(GLint shaderID, std::string srcLocation){
    const std::string shaderText = loadShaderSource(srcLocation);
    const GLchar* shaderSource = shaderText.data();
    glShaderSource(shaderID, 1, &shaderSource, nullptr);
    glCompileShader(shaderID);
    GLint success;
    glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success);
    if (!success) {
        GLchar InfoLog[1024];
        glGetShaderInfoLog(shaderID, sizeof(InfoLog), nullptr, InfoLog);
        fprintf(stderr, "Error compiling shader: '%s'\n", InfoLog);
    }
    glAttachShader(programID, shaderID);
}

std::string ShaderProgram::loadShaderSource(std::string location){
    std::ifstream file (location);
    std::string source;
    std::stringstream stringStream;

    if (!file.is_open()){
        throw std::runtime_error("Could not open file " + location);
    }

    stringStream << file.rdbuf();
    source = stringStream.str();

    return source;
}

Entity.cpp

#include "Entity.h"

Entity::Entity(){
    vertices[0] = glm::vec3(-1.0f, -1.0f, 0.0f);
    vertices[1] = glm::vec3(1.0f, -1.0f, 0.0f);
    vertices[2] = glm::vec3(0.0f, 1.0f, 0.0f);
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glDisableVertexAttribArray(0);
}

int Entity::getID(){
    return vbo;
}

GLSL代码:
顶点着色器:

#version 330

layout (location = 0) in vec3 position;

void main(){
    gl_Position = vec4(0.5 * position.x, 0.5 * position.y, position.z, 1.0);
}

Fragment Shader:

#version 330

out vec4 colour;

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

1 个答案:

答案 0 :(得分:0)

你不能像这样使用glm :: vec3。它没有为你提供任何东西。它的实际数组的开始得到了:

glm::vec3 k;
float* t= &k[0];

但我怀疑你也可以用它做一些值得的事情。

只使用float GLfloat数组。