gpt4 book ai didi

无法让 OpenGL 3.3 绘制三角形,不会抛出任何错误

转载 作者:太空宇宙 更新时间:2023-11-04 00:03:44 25 4
gpt4 key购买 nike

这是第二天,我尝试使用带有着色器的 OpenGL 3.3 绘制一个三角形,但我无法让它工作。没有错误消息被打印出来。我还使用调试器遍历了每一行,并且 GL_GetError() 始终保持 0(无错误),除了紧接在 glewInit() 调用之后,但人们说没关系。我不知道代码或我的电脑有什么问题。我使用 lubuntu 15.04glxinfo | grep "OpenGL" 打印

OpenGL vendor string: Intel Open Source Technology Center
OpenGL renderer string: Mesa DRI Intel(R) Bay Trail
OpenGL core profile version string: 3.3 (Core Profile) Mesa 10.5.9
OpenGL core profile shading language version string: 3.30
OpenGL core profile context flags: (none)
OpenGL core profile profile mask: core profile
OpenGL core profile extensions:
OpenGL version string: 3.0 Mesa 10.5.9
OpenGL shading language version string: 1.30
OpenGL context flags: (none)
OpenGL extensions:
OpenGL ES profile version string: OpenGL ES 3.0 Mesa 10.5.9
OpenGL ES profile shading language version string: OpenGL ES GLSL ES 3.00
OpenGL ES profile extensions:

这是我的程序,只有 100 LOC,没有任何外部依赖:

#include <GL/glew.h>
#include <GL/freeglut.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

GLuint VBO;
GLuint prog;

const GLchar *vert_shader_src =
"#version 330\n"
"layout (location = 0) in vec3 Position;\n"
"void main() {\n"
"gl_Position = vec4(0.5*Position.x, 0.5*Position.y, Position.z, 1.0); }\n";
const GLchar *frag_shader_src =
"#version 330\n"
"out vec4 FragColor;\n"
"void main() {\n"
"FragColor = vec4(1.0, 0.0, 0.0, 1.0); }";

void render(void) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(prog);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glutSwapBuffers(); }

void initGlut(int *argc_ptr, char **argv, void (*renderCallback)(void)) {
glutInit(argc_ptr, argv);
glutInitContextVersion(3, 3);
glutInitWindowSize(600, 600);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("minigl");
glutDisplayFunc(renderCallback); }

void initVBO(void) {
float pts[9] = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f};
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(pts), pts, GL_STATIC_DRAW); }

int initProg(void) {
prog = glCreateProgram();
GLuint vshade = glCreateShader(GL_VERTEX_SHADER);
GLuint fshade = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar **v_src = &vert_shader_src;
const GLchar **f_src = &frag_shader_src;
int v_src_len_ptr[1] = { strlen(vert_shader_src) };
int f_src_len_ptr[1] = { strlen(frag_shader_src) };

glShaderSource(vshade, 1, v_src, v_src_len_ptr);
glShaderSource(fshade, 1, f_src, f_src_len_ptr);
glCompileShader(vshade);
glCompileShader(fshade);
GLint success;
glGetShaderiv(vshade, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetShaderInfoLog(vshade, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
glGetShaderiv(fshade, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetShaderInfoLog(fshade, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
glAttachShader(prog, vshade);
glAttachShader(prog, fshade);
glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Error linking shader program: '%s'\n", infolog); }
glValidateProgram(prog);
glGetProgramiv(prog, GL_VALIDATE_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Error validating shader program: '%s'\n", infolog); }
glDeleteShader(vshade);
glDeleteShader(fshade);
return 1; }

int main(int argc, char **argv) {
initGlut(&argc, argv, render);
// GL_GetError throws 1280 (invalid enum) after glewInit but all say it's OK
if(glewInit() != GLEW_OK) {
puts("GLEW failed to initialize!");
return EXIT_FAILURE; }
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
initProg();

glutMainLoop();
glDeleteProgram(prog);
glDeleteBuffers(1, &VBO);
return EXIT_SUCCESS; }

顺便说一句,我已经下载了这个Github repo最小的 OpenGL 3.3 示例,它工作得很好。我看到的唯一主要区别是除了 VBO 之外还使用了 VAO。

编辑:添加了 VAO,移植到 GLFW - 仍然是同样的问题。

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

GLuint VBO;
GLuint VAO;
GLuint prog;

const GLchar *vert_shader_src =
"#version 330\n"
"layout (location = 0) in vec3 Position;\n"
"void main() {\n"
"gl_Position = vec4(0.5*Position.x, 0.5*Position.y, Position.z, 1.0); }\n";
const GLchar *frag_shader_src =
"#version 330\n"
"out vec4 FragColor;\n"
"void main() {\n"
"FragColor = vec4(1.0, 0.0, 0.0, 1.0); }";

void render(GLFWwindow *window) {
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(prog);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window); }

void initVBO(void) {
float pts[9] = {-1.0f, -1.0f, 0.0f, 1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f};
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(pts), pts, GL_STATIC_DRAW); }

int initProg(void) {
prog = glCreateProgram();
GLuint vshade = glCreateShader(GL_VERTEX_SHADER);
GLuint fshade = glCreateShader(GL_FRAGMENT_SHADER);
const GLchar **v_src = &vert_shader_src;
const GLchar **f_src = &frag_shader_src;
int v_src_len_ptr[1] = { strlen(vert_shader_src) };
int f_src_len_ptr[1] = { strlen(frag_shader_src) };

glShaderSource(vshade, 1, v_src, v_src_len_ptr);
glShaderSource(fshade, 1, f_src, f_src_len_ptr);
glCompileShader(vshade);
glCompileShader(fshade);
GLint success;
glGetShaderiv(vshade, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetShaderInfoLog(vshade, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
glGetShaderiv(fshade, GL_COMPILE_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetShaderInfoLog(fshade, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Err compiling shader: '%s'\n", infolog); }
glAttachShader(prog, vshade);
glAttachShader(prog, fshade);
glLinkProgram(prog);
glGetProgramiv(prog, GL_LINK_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Error linking shader program: '%s'\n", infolog); }
glValidateProgram(prog);
glGetProgramiv(prog, GL_VALIDATE_STATUS, &success);
if (!success) {
GLchar infolog[1024];
glGetProgramInfoLog(prog, sizeof(infolog), NULL, infolog);
fprintf(stderr, "Error validating shader program: '%s'\n", infolog); }
glDeleteShader(vshade);
glDeleteShader(fshade);
return 1; }

int main() {
if(!glfwInit()) {
puts("GLFW failed to initialize!");
return EXIT_FAILURE; }

glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

GLFWwindow *window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
if(window == NULL) {
puts("Failed to create window!");
glfwTerminate();
return EXIT_FAILURE; }
glfwMakeContextCurrent(window);

if(glewInit() != GLEW_OK) {
puts("GLEW failed to initialize!");
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_FAILURE; }

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
initProg();

while(!glfwWindowShouldClose(window)) {
render(window); }

glDeleteProgram(prog);
glDeleteBuffers(1, &VBO);
glfwDestroyWindow(window);
glfwTerminate();
return EXIT_SUCCESS; }

最佳答案

原来我创建了一个函数来初始化 VBO 和 VAO...但是忘了调用它。两天。

关于无法让 OpenGL 3.3 绘制三角形,不会抛出任何错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32443058/

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