gpt4 book ai didi

c++ - OpenGL程序画三角形给出蓝屏

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

我正在尝试用 OpenGL 绘制一个三角形。

我的代码编译正常,没有产生任何错误,但它仍然没有按预期执行。

我有一个顶点着色器和一个片段着色器,它们都在同一个程序中。

我的代码如下:

#include "../../include/sb7.h"

GLuint compile_shaders(void)
{
GLuint vertex_shader;
GLuint fragment_shader;
GLuint program;

// Source code for Vertex Shader
static const GLchar * vertex_shader_source[] =
{
"#version 450 core \n"
" \n"
"void main(void) \n"
"{ \n"
" //Decalre a hardcoded array of positions \n"
" const vec4 vertices[3] = vec4[3](vec4(0.25, -0.25, 0.5, 1.0), \n"
" vec4(-0.25, -0.25, 0.5, 1.0), \n"
" vec4(0.25, 0.25, 0.5, 1.0)); \n"
" //Index into our array using gl_VertexID \n"
" gl_Position = vertices[gl_VertexID]; \n"
"} \n"
};

// Source code for Fragment Shader
static const GLchar * fragment_shader_source[] =
{
"#version 450 core \n"
" \n"
"out vec4 color; \n"
" \n"
"void main(void) \n"
"{ \n"
" color = vec4(0.0, 0.8, 1.0, 1.0); \n"
"} \n"
};

// Create and compiler Vertex Shader
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
glCompileShader(vertex_shader);

// Create and compiler Fragment Shader
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
glCompileShader(fragment_shader);

// Create program, attach shaders to it, and link it
program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glLinkProgram(program);

// Delete shaders as program has them now
glDeleteShader(vertex_shader);
glDeleteShader(fragment_shader);

return program;
};

class my_application : public sb7::application
{
public:

void startup()
{
rendering_program = compile_shaders();
glCreateVertexArrays(1, &vertex_array_object);
glBindVertexArray(vertex_array_object);
}

void shutdown()
{
glDeleteVertexArrays(1, &vertex_array_object);
glDeleteProgram(rendering_program);
glDeleteVertexArrays(1, &vertex_array_object);
}

// Our rendering function
void render(double currentTime)
{
// Sets Colour
static const GLfloat color[] = { 0.0f, 0.2f, 0.0f, 1.0f };
glClearBufferfv(GL_COLOR, 0, color);

// Use program object we created for rendering
glUseProgram(rendering_program);

// Draw one triangle
glDrawArrays(GL_TRIANGLES, 0, 3);
}

private:

GLuint rendering_program;
GLuint vertex_array_object;
};

我正在关注 OpenGL Superbible,第 7 版。

即使我更改了点的大小并且输出与我为此获得的相同屏幕(全黄色),相同的程序(有一些小的变化)也没有绘制点。

我在绘制三角形时看到过类似的问题,但它们都与我正在做的不同。

我该如何解决这个问题?

最佳答案

在这里工作得很好:

screenshot of blue triangle on dark green background

一起:

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

struct Program
{
static GLuint Load( const char* shader, ... )
{
GLuint prog = glCreateProgram();
va_list args;
va_start( args, shader );
while( shader )
{
const GLenum type = va_arg( args, GLenum );
AttachShader( prog, type, shader );
shader = va_arg( args, const char* );
}
va_end( args );
glLinkProgram( prog );
CheckStatus( prog );
return prog;
}

private:
static 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 << 15 ] = { 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( EXIT_FAILURE );
}

static 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 );
}
};

const char* vert = R"GLSL(
#version 450 core
void main(void)
{
//Decalre a hardcoded array of positions
const vec4 vertices[3] = vec4[3]
(
vec4(0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4(0.25, 0.25, 0.5, 1.0)
);
//Index into our array using gl_VertexID
gl_Position = vertices[gl_VertexID];
}
)GLSL";

const char* frag = R"GLSL(
#version 450 core
out vec4 color;
void main(void)
{
color = vec4(0.0, 0.8, 1.0, 1.0);
}
)GLSL";

int main( int argc, char** argv )
{
glfwInit();

glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 4 );
glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 5 );
glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
GLFWwindow* window = glfwCreateWindow( 640, 480, "51307782", NULL, NULL );
glfwMakeContextCurrent( window );
glewInit();
glfwSwapInterval( 1 );

GLuint rendering_program = Program::Load
(
vert, GL_VERTEX_SHADER,
frag, GL_FRAGMENT_SHADER,
NULL
);

GLuint vertex_array_object = 0;
glCreateVertexArrays( 1, &vertex_array_object );
glBindVertexArray( vertex_array_object );

while( !glfwWindowShouldClose( window ) )
{
glfwPollEvents();

static const GLfloat color[] = { 0.0f, 0.2f, 0.0f, 1.0f };
glClearBufferfv( GL_COLOR, 0, color );
glUseProgram( rendering_program );
glDrawArrays( GL_TRIANGLES, 0, 3 );

glfwSwapBuffers( window );
}

glDeleteVertexArrays( 1, &vertex_array_object );
glDeleteProgram( rendering_program );

glfwDestroyWindow( window );
glfwTerminate();
}

关于c++ - OpenGL程序画三角形给出蓝屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51307782/

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