gpt4 book ai didi

c++ - GLFW程序不画三角形

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:24:30 25 4
gpt4 key购买 nike

我的 OpenGL 程序坏了,我不知道为什么。

下面是我的代码。

我使用的是 Mac OS X 10.9。这是重现错误的最小程序

片段着色器

#version 410 core
out vec4 FragColor;
void main()
{
FragColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
}

顶点着色器

#version 410 core 
layout(location=0) in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0f, 1.0f);
}

绘图函数

void draw()
{
glUseProgram(program);

glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}

初始化函数

void initData()
{
unsigned int indexes[] =
{
0,1,2
};

float data[] =
{
0.0f,0.8f,
-0.8f, 0.0f,
0.8f,0.0f
};

glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);

glGenBuffers(1, &vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data,GL_STATIC_DRAW);

glGenBuffers(1, &index_buffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index_buffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW);

glEnableVertexAttribArray(0);

glVertexAttribPointer(0,2,GL_FLOAT, GL_FALSE,0,0);
glBindVertexArray(0);
}

UPD

下面的程序接受这个输出

GL_VERSION                  : 4.1 INTEL-8.18.29
GL_VENDOR : Intel Inc.
GL_RENDERER : Intel HD Graphics 5000 OpenGL Engine
GL_SHADING_LANGUAGE_VERSION : 4.10

UPD 2

已解决,发现着色器编译函数中的错误

最佳答案

在这里工作得很好:

GLFW version                : 3.0.4 Win32 WGL VisualC LoadLibrary(winmm)
GLEW_VERSION : 1.10.0
GL_VERSION : 4.1.0
GL_VENDOR : NVIDIA Corporation
GL_RENDERER : GeForce GTX 765M/PCIe/SSE2
GL_SHADING_LANGUAGE_VERSION : 4.10 NVIDIA via Cg compiler

screenshot

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

#include <vector>
#include <iostream>
using namespace std;


void CheckStatus( GLuint obj )
{
GLint status = GL_FALSE;
if( glIsShader(obj) ) glGetShaderiv( obj, GL_COMPILE_STATUS, &status );
if( glIsProgram(obj) ) glGetProgramiv( obj, GL_LINK_STATUS, &status );
if( status == GL_TRUE ) return;
GLchar log[ 1 << 16 ] = { 0 };
if( glIsShader(obj) ) glGetShaderInfoLog( obj, sizeof(log), NULL, log );
if( glIsProgram(obj) ) glGetProgramInfoLog( obj, sizeof(log), NULL, log );
std::cerr << log << std::endl;
exit( -1 );
}

void AttachShader( GLuint program, GLenum type, const char* src )
{
GLuint shader = glCreateShader( type );
glShaderSource( shader, 1, &src, NULL );
glCompileShader( shader );
CheckStatus( shader );
glAttachShader( program, shader );
glDeleteShader( shader );
}

GLuint LoadProgram( const char* vert, const char* geom, const char* frag )
{
GLuint prog = glCreateProgram();
if( vert ) AttachShader( prog, GL_VERTEX_SHADER, vert );
if( geom ) AttachShader( prog, GL_GEOMETRY_SHADER, geom );
if( frag ) AttachShader( prog, GL_FRAGMENT_SHADER, frag );
glLinkProgram( prog );
CheckStatus( prog );
return prog;
}

#define GLSL(version, shader) "#version " #version "\n" #shader


GLuint program;
GLuint VAO;
void initData()
{
const char* vert = GLSL
(
410 core,
layout( location = 0 ) in vec2 position;
void main()
{
gl_Position = vec4( position, 0.0, 1.0 );
}
);

const char* frag = GLSL
(
410 core,
out vec4 FragColor;
void main()
{
FragColor = vec4( 0.6, 1.0, 1.0, 1.0 );
}
);

program = LoadProgram( vert, NULL, frag );

glGenVertexArrays( 1, &VAO );
glBindVertexArray( VAO );

GLuint vertex_buffer = 0;
glGenBuffers( 1, &vertex_buffer );
glBindBuffer( GL_ARRAY_BUFFER, vertex_buffer );
float data[] =
{
0.0f,0.8f,
-0.8f, 0.0f,
0.8f,0.0f
};
glBufferData( GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW );

GLuint index_buffer = 0;
glGenBuffers( 1, &index_buffer );
glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, index_buffer );
unsigned int indexes[] =
{
0,1,2
};
glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, GL_STATIC_DRAW );

glEnableVertexAttribArray( 0 );

glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, 0, 0 );

glBindVertexArray( 0 );
}

int main( int argc, char** argv )
{
if( !glfwInit() )
return -1;

glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 1 );
glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

GLFWwindow* window = glfwCreateWindow( 640, 480, "Hello window", NULL, NULL );
if( !window )
{
cerr << "Error on window creating" << endl;
glfwTerminate();
return -1;
}

glfwMakeContextCurrent( window );

cout << "GLFW version : " << glfwGetVersionString() << endl;
cout << "GLEW_VERSION : " << glewGetString( GLEW_VERSION ) << endl;
cout << "GL_VERSION : " << glGetString( GL_VERSION ) << endl;
cout << "GL_VENDOR : " << glGetString( GL_VENDOR ) << endl;
cout << "GL_RENDERER : " << glGetString( GL_RENDERER ) << endl;
cout << "GL_SHADING_LANGUAGE_VERSION : " << glGetString( GL_SHADING_LANGUAGE_VERSION ) << endl;

glewExperimental = true;
GLenum err = glewInit();
if( err!= GLEW_OK )
{
cerr << "Glew init failed!" << endl;
cerr << "Error: " << glewGetErrorString( err ) << endl;
}

initData();
while( !glfwWindowShouldClose(window) )
{
glClearColor( 0, 0, 0, 0 );
glClear( GL_COLOR_BUFFER_BIT );

glUseProgram( program );
glBindVertexArray( VAO );
glDrawElements( GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0 );
glBindVertexArray( 0 );

glfwSwapBuffers( window );

glfwPollEvents();
}

glfwTerminate();
return 0;
}

确保苹果supports OpenGL 4.1 Core在您的卡上。

关于c++ - GLFW程序不画三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21368383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com