opengl不报错却无法显示场景

OpenGL编码不报错,但是debug中断在delet_scalar.cpp里,运行不显示场景,只很快地闪了白屏就自动关掉了。

渲染器头文件

#pragma once

#include "../nclgl/OGLRenderer.h"

class HeightMap;
class Camera;
class Light; // Predeclare our new class type ...
class Shader;

class Renderer : public OGLRenderer {
public:
    Renderer(Window& parent);
    ~Renderer(void);

    void RenderScene() override;
    void UpdateScene(float dt) override;

protected:
    HeightMap* heightMap;
    Shader* shader;
    Camera* camera;
    Light* light; //A new thing!
    GLuint texture;
};

渲染器

#include "Renderer11.h"
#include "../nclgl/Light.h"
#include "../nclgl/Camera.h"
#include "../nclgl/HeightMap.h"

Renderer::Renderer(Window& parent) : OGLRenderer(parent) {
    heightMap = new HeightMap(TEXTUREDIR"noise.png");
    texture = SOIL_load_OGL_texture(TEXTUREDIR"Barren Reds.JPG", SOIL_LOAD_AUTO, SOIL_CREATE_NEW_ID, SOIL_FLAG_MIPMAPS);

    shader = new Shader("PerPixelVertex.glsl", "PerPixelFragment.glsl");

    if (!shader->LoadSuccess() || !texture) {
        return;
    }
    SetTextureRepeating(texture, true);
    Vector3 heightmapSize = heightMap->GetHeightmapSize();
    camera = new Camera(-45.0f, 0.0f, heightmapSize * Vector3(0.5f, 5.0f, 0.5f));
    light = new Light(heightmapSize * Vector3(0.5f, 1.5f, 0.5f), Vector4(1, 1, 1, 1), heightmapSize.x * 0.5f);

    projMatrix = Matrix4::Perspective(1.0f, 15000.0f, (float)width / (float)height, 45.0f);

    glEnable(GL_DEPTH_TEST);
    init = true;
}

Renderer ::~Renderer(void) {
    delete camera;
    delete heightMap;
    delete shader;
    delete light; //This bit is new ...
}

void Renderer::UpdateScene(float dt) {
    camera->UpdateCamera(dt);
    viewMatrix = camera->BuildViewMatrix();
}

void Renderer::RenderScene() {
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    BindShader(shader);

    glUniform1i(glGetUniformLocation(shader->GetProgram(), "diffuseTex"), 0);

    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);

    glUniform3fv(glGetUniformLocation(shader->GetProgram(), "cameraPos"), 1, (float*)&camera->GetPosition());

    UpdateShaderMatrices();
    SetShaderLight(*light);

    heightMap->Draw();
}

vs

#version 330 core
uniform mat4 modelMatrix;
uniform mat4 viewMatrix;
uniform mat4 projMatrix;

in vec3 position;
in vec4 colour;
in vec3 normal; //New Attribute!
in vec2 texCoord;

out Vertex {
    vec4 colour;
    vec2 texCoord;
    vec3 normal;
    vec3 worldPos;
} OUT;

void main(void) {
    OUT.colour = colour;
    OUT.texCoord = texCoord;

    mat3 normalMatrix = transpose(inverse(mat3(modelMatrix )));
    OUT.normal = normalize(normalMatrix * normalize(normal ));

    vec4 worldPos = (modelMatrix * vec4(position ,1));

    OUT.worldPos = worldPos.xyz;

    gl_Position = (projMatrix * viewMatrix) * worldPos;
}

fs

#version 330 core

uniform sampler2D diffuseTex;
uniform vec3 cameraPos;
uniform vec4 lightColour;
uniform vec3 lightPos;
uniform float lightRadius;

in Vertex {
    vec3 colour;
    vec2 texCoord;
    vec3 normal;
    vec3 worldPos;
} IN;

out vec4 fragColour;

void main(void) {
    vec3 incident = normalize(lightPos - IN.worldPos );
    vec3 viewDir = normalize(cameraPos - IN.worldPos );
    vec3 halfDir = normalize(incident + viewDir );

    vec4 diffuse = texture(diffuseTex , IN.texCoord );
    float lambert = max(dot(incident , IN.normal), 0.0f);
    float distance = length(lightPos - IN.worldPos );
    float attenuation = 1.0 - clamp(distance / lightRadius , 0.0, 1.0);
    float specFactor = clamp(dot(halfDir , IN.normal ) ,0.0 ,1.0);
    specFactor = pow(specFactor , 60.0 );
    vec3 surface = (diffuse.rgb * lightColour.rgb);
    fragColour.rgb = surface * lambert * attenuation;
    fragColour.rgb += (lightColour.rgb * specFactor )* attenuation *0.33;
    fragColour.rgb += surface * 0.1f; // ambient!
    fragColour.a = diffuse.a;
}

debug这里中断

img

运行显示

img