2017-10-01 3 views
0

J'ai lu quelques tutoriels pour écrire le code suivant. La seule différence est les didacticiels originaux où l'utilisation de SDL au lieu de GLEW.OpenGL sans shaders

Je ne comprends pas ce qui ne va pas dans ce code. Il compile mais je ne vois pas le triangle. (Le tutoriel n'utilisaient pas trop shaders)

#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; 
} 
+0

voir [complet GL + GLSL + VAO/VBO l'exemple C de] (https: // stackoverflow .com/a/31913542/2521214) pour un simple exemple de shader ... – Spektre

Répondre

6

Si vous utilisez le pipeline à fonction fixe, vous ne pouvez pas utiliser vertex générique attributs tels que glVertexAttribPointer.

La mise en œuvre de NVIDIA, cependant, alias illégalement entre les attributs génériques et non génériques. C'est probablement pourquoi l'auteur initial du tutoriel s'en est tiré avec leur machine.

Si vous voulez écrire ceci d'une manière multi-plateforme, vous devez utiliser glVertexPointer et glEnableClientState:

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

Merci. Existe-t-il dans OpenGL des shaders pré-construits? – Bob5421

+0

@ Bob5421: Non. C'est ce que le pipeline à fonction fixe est. Pourquoi êtes-vous si intéressé par * pas * en utilisant des shaders? –

+0

Parce que je veux d'abord écrire un programme très basique. Et je veux faire opengl moderne aussi – Bob5421