OpenGL - 带有着色器的三角形未显示

发布于 2024-11-06 21:58:58 字数 4803 浏览 0 评论 0原文

该程序构建没有问题,并且可执行文件启动,但没有出现三角形。我正在关注 GLSL 教程,其中创建了一个 Shader 类来处理 GLSL 文件。

Shader.h

#ifndef SHADER_H_
#define SHADER_H_

#include <GL/glew.h>
#include <GL/glfw.h>
#include <string>

class Shader {

    public:
        Shader();
        Shader(const char *vsFile, const char *fsFile);
        ~Shader();
        void init(const char *vsFile, const char *fsFile);
        void bind();
        void unbind();
        unsigned int id();

    private:
        unsigned int shader_id;
        unsigned int shader_vp;
        unsigned int shader_fp;
};

#endif // SHADER_H_

Shader.cpp

#include "Shader.h"
#include <cstring>
#include <iostream>
#include <ftream>
#include <cstdlib>
using namespace std;

static char* textFileRead(const char *fileName) {
    char* text;

    if (fileName != NULL) {
        FILE *file = fopen(fileName, "rt");

        if (file != NULL) {
            fseek(file, 0, SEEK_END);
            int count = ftell(file);
            rewind(file);

            if (count > 0) {
                text = (char*)malloc(sizeof(char) * (count + 1));
                count = fread(text, sizeof(char), count, file);
                text[count] = '\0';
            }
            fclose(file);
        }
    }
    return text;
}

Shader::Shader() {}

Shader::Shader(const char *vsFile, const char *fsFile)
{
    init(vsFile, fsFile);
}

void Shader::init(const char *vsFile, const char *fsFile)
{
    shader_vp = glCreateShader(GL_VERTEX_SHADER);
    shader_fp = glCreateShader(GL_FRAGMENT_SHADER);

    const char *vsText = textFileRead(vsFile);
    const char *fsText = textFileRead(fsFile);

    if (vsText == NULL || fsText == NULL)
    {
        cerr << "Either vertex shader or fragment shader file is not found" << endl;
        return;
    }

    glShaderSource(shader_vp, 1, &vsText, 0);
    glShaderSource(shader_fp, 1, &fsText, 0);

    glCompileShader(shader_vp);
    glCompileShader(shader_fp);

    shader_id = glCreateProgram();
    glAttachShader(shader_id, shader_fp);
    glAttachShader(shader_id, shader_vp);
    glLinkProgram(shader_id);
}

Shader::~Shader()
{
    glDetachShader(shader_id, shader_fp);
    glDetachShader(shader_id, shader_vp);

    glDeleteShader(shader_fp);
    glDeleteShader(shader_vp);
    glDeleteShader(shader_id);
}

unsigned int Shader::id()
{
    return shader_id;
}

void Shader::bind()
{
    glUseProgram(shader_id);
}

void Shader::unbind()
{
    glUseProgram(0);
}

Main.cppshader.vertshader.frag

#include "Shader.h"
#include <cstdlib>
#include <iostream>
using namespace std;

Shader shader;

void init()
{
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    shader.init("shader.vert", "shader.frag");
}

void resize(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

int main()
{
    int running = GL_TRUE;

    // init GLFW
    if (!glfwInit())
        exit(EXIT_FAILURE);

    if (!glfwOpenWindow(300, 300, 0, 0, 0, 0, 0, 0, GLFW_WINDOW))
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetWindowTitle("ohhai.");
    glfwSetWindowSizeCallback(resize);

    /* CHECK GLEW */
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        cout << "Error: " << glewGetErrorString(err) << endl;
    }
    cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
    if (!GLEW_ARB_vertex_buffer_object)
    {
        cerr << "VBO not supported\n";
        exit(1);
    }

    init();

    while (running)
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        glShadeModel(GL_SMOOTH);

        shader.bind();
        glBegin(GL_TRIANGLES);
            //glColor3f(0.2f, 0.5f, 0.54f);
            glVertex2f(0.0f, 0.5f);

            //glColor3f(0.75f, 0.8f, 0.1f);
            glVertex2f(-.5f, -.5f);

            //glColor3f(0.0f, 0.9f, 0.2f);
            glVertex2f(0.5f, -0.5f);
        glEnd();
        shader.unbind();

        glfwSwapBuffers();

        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
    }

    glfwTerminate();

    exit(EXIT_SUCCESS);
}

同样

void main()
{
    // set the posistion of the current matrix
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

,当在 g ++

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

下编译时,它运行良好,但没有显示三角形。

This program builds with no problem, and the executable starts up, but no triangle shows up. I am following a GLSL tutorial where a Shader class is made to handle GLSL files.

Shader.h

#ifndef SHADER_H_
#define SHADER_H_

#include <GL/glew.h>
#include <GL/glfw.h>
#include <string>

class Shader {

    public:
        Shader();
        Shader(const char *vsFile, const char *fsFile);
        ~Shader();
        void init(const char *vsFile, const char *fsFile);
        void bind();
        void unbind();
        unsigned int id();

    private:
        unsigned int shader_id;
        unsigned int shader_vp;
        unsigned int shader_fp;
};

#endif // SHADER_H_

Shader.cpp

#include "Shader.h"
#include <cstring>
#include <iostream>
#include <ftream>
#include <cstdlib>
using namespace std;

static char* textFileRead(const char *fileName) {
    char* text;

    if (fileName != NULL) {
        FILE *file = fopen(fileName, "rt");

        if (file != NULL) {
            fseek(file, 0, SEEK_END);
            int count = ftell(file);
            rewind(file);

            if (count > 0) {
                text = (char*)malloc(sizeof(char) * (count + 1));
                count = fread(text, sizeof(char), count, file);
                text[count] = '\0';
            }
            fclose(file);
        }
    }
    return text;
}

Shader::Shader() {}

Shader::Shader(const char *vsFile, const char *fsFile)
{
    init(vsFile, fsFile);
}

void Shader::init(const char *vsFile, const char *fsFile)
{
    shader_vp = glCreateShader(GL_VERTEX_SHADER);
    shader_fp = glCreateShader(GL_FRAGMENT_SHADER);

    const char *vsText = textFileRead(vsFile);
    const char *fsText = textFileRead(fsFile);

    if (vsText == NULL || fsText == NULL)
    {
        cerr << "Either vertex shader or fragment shader file is not found" << endl;
        return;
    }

    glShaderSource(shader_vp, 1, &vsText, 0);
    glShaderSource(shader_fp, 1, &fsText, 0);

    glCompileShader(shader_vp);
    glCompileShader(shader_fp);

    shader_id = glCreateProgram();
    glAttachShader(shader_id, shader_fp);
    glAttachShader(shader_id, shader_vp);
    glLinkProgram(shader_id);
}

Shader::~Shader()
{
    glDetachShader(shader_id, shader_fp);
    glDetachShader(shader_id, shader_vp);

    glDeleteShader(shader_fp);
    glDeleteShader(shader_vp);
    glDeleteShader(shader_id);
}

unsigned int Shader::id()
{
    return shader_id;
}

void Shader::bind()
{
    glUseProgram(shader_id);
}

void Shader::unbind()
{
    glUseProgram(0);
}

Main.cpp

#include "Shader.h"
#include <cstdlib>
#include <iostream>
using namespace std;

Shader shader;

void init()
{
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    shader.init("shader.vert", "shader.frag");
}

void resize(int w, int h)
{
    glViewport(0, 0, (GLsizei)w, (GLsizei)h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(60, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
    glMatrixMode(GL_MODELVIEW);
}

int main()
{
    int running = GL_TRUE;

    // init GLFW
    if (!glfwInit())
        exit(EXIT_FAILURE);

    if (!glfwOpenWindow(300, 300, 0, 0, 0, 0, 0, 0, GLFW_WINDOW))
    {
        glfwTerminate();
        exit(EXIT_FAILURE);
    }

    glfwSetWindowTitle("ohhai.");
    glfwSetWindowSizeCallback(resize);

    /* CHECK GLEW */
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        cout << "Error: " << glewGetErrorString(err) << endl;
    }
    cout << "Status: Using GLEW " << glewGetString(GLEW_VERSION) << endl;
    if (!GLEW_ARB_vertex_buffer_object)
    {
        cerr << "VBO not supported\n";
        exit(1);
    }

    init();

    while (running)
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        glShadeModel(GL_SMOOTH);

        shader.bind();
        glBegin(GL_TRIANGLES);
            //glColor3f(0.2f, 0.5f, 0.54f);
            glVertex2f(0.0f, 0.5f);

            //glColor3f(0.75f, 0.8f, 0.1f);
            glVertex2f(-.5f, -.5f);

            //glColor3f(0.0f, 0.9f, 0.2f);
            glVertex2f(0.5f, -0.5f);
        glEnd();
        shader.unbind();

        glfwSwapBuffers();

        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam(GLFW_OPENED);
    }

    glfwTerminate();

    exit(EXIT_SUCCESS);
}

shader.vert

void main()
{
    // set the posistion of the current matrix
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}

shader.frag

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

Again, when this compiles under g++, it goes through fine, but no triangle was shown.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

落墨 2024-11-13 21:58:58

我还没有找到使用 gluLookAt 或 glTranslate/glRotate/glScale 设置相机(模型视图矩阵)的部分。因此,您使用默认值,对应于原点处的相机并观察 -z,因此您的三角形(位于 z=0 平面中)位于近平面后面。

I haven't found the part where you setup the camera (modelview matrix) with gluLookAt or glTranslate/glRotate/glScale. So you use the default, corresponding to a camera at the origin and looking into -z, thus your triangle (which lies in the z=0 plane) is behind the near plane.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文