簡體   English   中英

無法在OpenGL C ++中渲染三角形

[英]Can't render triangle in OpenGL C++

我已經嘗試在C中使用OpenGL的這個教程,但是當談到第二個教程時,那個應該在窗口上繪制三角形的教程,我看不到任何東西。 所以這就是我所做的,我采用了創建OpenGL上下文,窗口和東西的代碼,我試圖讓它變得更簡單:我沒有使用VAO,而是嘗試了glBegin / glEnd。

我收到此錯誤:1282“無效操作”。 我只是直接從我的LWJGL項目中使用相同的句子。 主循環是如此簡單,我無法理解它是如何工作的,1282錯誤沒有給我任何信息。 為什么我仍然會收到錯誤?

#include <stdio.h>
#include <stdlib.h>

#pragma comment(lib, "glfw3.lib")
#pragma comment(lib, "glew32s.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")

// Include GLEW. Always include it before gl.h and glfw.h, since it's a bit magic.
#define GLEW_STATIC
#include <GL/glew.h>

// Include GLFW
#include <GLFW/glfw3.h>

// Include GLM
#include <glm/glm.hpp>
using namespace glm;

void checkErrors() {
    int error = glGetError();
    if (error != GL_NO_ERROR) {
        printf("%s (%d)\n", gluErrorString(error), error);
    }
}

int main(void)
{
    // Initialise GLFW
    if (!glfwInit())
    {
        fprintf(stderr, "Failed to initialize GLFW\n");
        return -1;
    }

    glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // We want OpenGL 3.3
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // To make MacOS happy; should not be needed
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //We don't want the old OpenGL 

    // Open a window and create its OpenGL context 
    GLFWwindow* window; // (In the accompanying source code, this variable is global) 
    window = glfwCreateWindow(1024, 768, "Tutorial 01", NULL, NULL);
    if (window == NULL){
        fprintf(stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n");
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window); // Initialize GLEW 
    glewExperimental = true; // Needed in core profile 
    if (glewInit() != GLEW_OK) {
        fprintf(stderr, "Failed to initialize GLEW\n");
        return -1;
    }

    // Ensure we can capture the escape key being pressed below
    glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);

    do{
        glClear(GL_COLOR_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
            glVertex3f(-1.0f, -1.0f, 0.0f);
            glVertex3f( 1.0f, -1.0f, 0.0f);
            glVertex3f( 0.0f,  1.0f, 0.0f);
        glEnd();

        // Swap buffers
        glfwSwapBuffers(window);
        glfwPollEvents();

        // Check for errors
        checkErrors();

    } // Check if the ESC key was pressed or the window was closed
    while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
    glfwWindowShouldClose(window) == 0);

    return 0;
}

glBegin / glEnd以及所有其他立即模式繪制函數等等已被棄用,不能與OpenGL 3.1(及更高版本)核心和轉發兼容的上下文一起使用。

您可以嘗試請求3.0兼容性上下文(包括所有已棄用的功能)。 為此,請刪除glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); 線條,並將次要版本提示更改為0.實際上,根據OpenGL wiki ,您不應該明確請求3.1及更新版本的前向兼容上下文。 但是,最好的辦法是找出VAO代碼的錯誤,而不是使用已棄用的功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM