OpenGL without shaders

北城以北 提交于 2019-12-25 11:58:09

问题


I have read some tutorials to write the following code. The only difference is the original tutorials where using SDL instead of GLEW.

I do not understand what is wrong in this code. It compiles but i do not see the triangle. (the tutorial were not using shaders too)

#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>

GLFWwindow* window;

int main(int argc, const char * argv[])
{
    if (!glfwInit())
    {
        return -1;
    }
    glfwWindowHint(GLFW_SAMPLES, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);

    window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
    if (window==NULL)
    {
        return -1;
    }
    glfwMakeContextCurrent(window);

    glewExperimental = true;
    if (glewInit() != GLEW_OK)
    {
        return -1;
    }

    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
    glClearColor(0.0f, 1.0f, 1.0f, 1.0f);

    do
    {
        glfwPollEvents();

        float vertices[] = {-0.5, -0.5,   0.0, 0.5,   0.5, -0.5};
        glClear(GL_COLOR_BUFFER_BIT);
        glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
        glEnableVertexAttribArray(0);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glDisableVertexAttribArray(0);

        glfwSwapBuffers(window);
    }
    while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );

    glfwTerminate();

    return 0;
}

回答1:


If you're using the fixed-function pipeline, you cannot use generic vertex attributes like glVertexAttribPointer.

NVIDIA's implementation, however, illegally aliases between generic attributes and non-generic ones. This is probably why the initial writer of the tutorial got away with it on their machine.

If you want to write this in a cross-platform way, you have to use glVertexPointer and glEnableClientState:

glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);


来源:https://stackoverflow.com/questions/46515663/opengl-without-shaders

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!