gpt4 book ai didi

c++ - 使用顶点数组对象渲染三角形什么都不显示(OpenGL)

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

我使用 OpenGL 3.2、GLFW 和 GLEW。我尝试在 OS X (10.8.2) 上使用 VAO 和简单着色器渲染简单的三角形,但没有任何显示,只有白屏。着色器编译正常,GLEW 初始化正常,glGetString(GL_VERSION) 显示 3.2,尝试在每一行之后放置 glGetError,它没有报告任何错误。我不知道我做错了什么。这是代码:

#include "include/GL/glew.h"
#include "include/GL/glfw.h"
#include <cstdlib>
#include <iostream>

GLuint program;

char *textFileRead(char *fn) {


FILE *fp;
char *content = NULL;

int count=0;

if (fn != NULL) {
fp = fopen(fn,"rt");

if (fp != NULL) {

fseek(fp, 0, SEEK_END);
count = ftell(fp);
rewind(fp);

if (count > 0) {
content = (char *)malloc(sizeof(char) * (count+1));
count = fread(content,sizeof(char),count,fp);
content[count] = '\0';
}
fclose(fp);
}
}
return content;
}


void checkCompilationStatus(GLuint s) {
GLint status = 0;

glGetShaderiv(s, GL_COMPILE_STATUS, &status);
if (status == 0) {
int infologLength = 0;
int charsWritten = 0;

glGetShaderiv(s, GL_INFO_LOG_LENGTH, &infologLength);

if (infologLength > 0)
{
GLchar* infoLog = (GLchar *)malloc(infologLength);
if (infoLog == NULL)
{
printf( "ERROR: Could not allocate InfoLog buffer");
exit(1);
}
glGetShaderInfoLog(s, infologLength, &charsWritten, infoLog);
printf( "Shader InfoLog:\n%s", infoLog );
free(infoLog);
}
}

}


void setShaders() {
GLuint v, f;
char *vs = NULL,*fs = NULL;

v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);

vs = textFileRead("minimal.vert");
fs = textFileRead("minimal.frag");

const char * vv = vs;
const char * ff = fs;

glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);

free(vs);free(fs);

glCompileShader(v);
checkCompilationStatus(v);
glCompileShader(f);
checkCompilationStatus(f);

program = glCreateProgram();
glAttachShader(program,v);
glAttachShader(program,f);

GLuint error;
glLinkProgram(program);
glUseProgram(program);
}


int main(int argc, char* argv[]) {

glfwInit();

glfwOpenWindowHint(GLFW_OPENGL_VERSION_MAJOR, 3);
glfwOpenWindowHint(GLFW_OPENGL_VERSION_MINOR, 2);
glfwOpenWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwOpenWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

glfwOpenWindow(800, 600, 8, 8, 8, 8, 24, 8, GLFW_WINDOW);
glViewport(0, 0, 800, 600);
glfwSetWindowTitle("Triangle");

glewExperimental = GL_TRUE;
GLenum result = glewInit();
if (result != GLEW_OK) {
std::cout << "Error: " << glewGetErrorString(result) << std::endl;
}

std::cout << "VENDOR: " << glGetString(GL_VENDOR) << std::endl;
std::cout << "RENDERER: " << glGetString(GL_RENDERER) << std::endl;
std::cout << "VERSION: " << glGetString(GL_VERSION) << std::endl;
std::cout << "GLSL: " << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;

setShaders();

GLfloat vertices[] = {
1.0f, 1.0f, 0.f,
-1.f, -1.f, 0.f,
1.f, -1.f, 0.f
};


GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);

GLuint vertexbuffer;

glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

GLuint pos = glGetAttribLocation(program, "position");
glEnableVertexAttribArray(pos);
glVertexAttribPointer(pos, 3, GL_FLOAT, GL_FALSE, 0, 0);

glClearColor(1.0, 1.0, 1.0, 1.0);

while (glfwGetWindowParam(GLFW_OPENED)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glDrawArrays(GL_TRIANGLES, 0, 3);

glfwSwapBuffers();
glfwSleep(0.001);

}

}

这里是着色器,顶点着色器:

#version 150

in vec3 position;

void main()
{
gl_Position = vec4(position, 0);
}

片段着色器:

#version 150

out vec4 out_color;

void main()
{
out_color = vec4(1.0f, 0.0f, 0.0f, 1.0f);
}

最佳答案

顶点着色器中的 w 参数应设置为 1,而不是 0。

gl_Position = vec4(position, 1)

有关更多信息,请参阅 this page 上“光栅化概述”下标题为“标准化坐标”的部分

... The X, Y, and Z of each vertex's position is divided by W to get normalized device coordinates...

所以您的坐标被 0 除。除以 0 的数字是未定义的。

关于c++ - 使用顶点数组对象渲染三角形什么都不显示(OpenGL),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14295101/

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