glDrawArrays 没有在屏幕上绘制任何东西,但使用 glVertex2d 进行绘制似乎可以工作。
我在 OpenGL 3.0 上。我的直觉是这是着色器的问题,但因为它们都编译得很好,所以我无法弄清楚问题是什么。任何人都知道可能导致问题的原因是什么?
// OpenGL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#define GLEW_STATIC
#include <GL\glew.h>
#include <fstream>
#include <iostream>
#include <GLFW\glfw3.h>
using namespace std;
const char* vertex_shader_data =
"#version 150\n"
"in vec2 position;"
"void main(){"
"gl_Position = vec4(position, 0.0, 1.0);"
"}";
const char* fragment_shader_data =
"#version 150\n"
"out vec4 outColor;"
"void main(){"
"outColor = vec4(1.0, 1.0, 1.0, 1.0);"
"}";
int _tmain(int argc, _TCHAR* argv[])
{
glewExperimental = GL_TRUE;
glfwInit();
GLFWwindow *window = glfwCreateWindow(640, 480, "OpenGL Fun", NULL, NULL);
glfwMakeContextCurrent(window);
glewInit();
GLuint buffer;
float vertices[] = { 0.0f, 0.5f, -0.5f, -0.5f, 0.5f, -0.5f };
glEnable(GL_VERTEX_ARRAY);
glGenBuffers(1, &buffer);
GLuint vao;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
GLuint vertexshader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragmentshader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(vertexshader, 1, &vertex_shader_data, NULL);
glShaderSource(fragmentshader, 1, &fragment_shader_data, NULL);
glCompileShader(vertexshader);
glCompileShader(fragmentshader);
GLint vstatus;
GLint fstatus;
glGetShaderiv(vertexshader, GL_COMPILE_STATUS, &vstatus);
glGetShaderiv(fragmentshader, GL_COMPILE_STATUS, &fstatus);
#pragma region Shader Compilation Test
if(!((vstatus == GL_TRUE) && (fstatus == GL_TRUE))) {
char info[512];
glGetShaderInfoLog(vertexshader, 512, NULL, info);
for(int i=0; i<512; i++) {
cout << info[i];
}
glGetShaderInfoLog(fragmentshader, 512, NULL, info);
for(int i=0; i<512; i++) {
cout << info[i];
}
}
#pragma endregion
GLuint shaderprogram = glCreateProgram();
glAttachShader(shaderprogram, vertexshader);
glAttachShader(shaderprogram, fragmentshader);
glBindFragDataLocation(shaderprogram, 0, "outColor");
glLinkProgram(shaderprogram);
glUseProgram(shaderprogram);
GLuint pos_attribute = glGetAttribLocation(vertexshader, "position");
glEnableVertexAttribArray(pos_attribute);
glVertexAttribPointer(pos_attribute, 2, GL_FLOAT, GL_FALSE, 0, 0);
while(!glfwWindowShouldClose(window)) {
glClearColor( 1.0f, 0.0f, 0.0f, 1.0f );
glClear( GL_COLOR_BUFFER_BIT );
glDrawArrays(GL_TRIANGLES, 0, 3);
/*
glBegin(GL_TRIANGLES);
glVertex2d(0.0f, 0.5f);
glVertex2d(-0.5f, -0.5f);
glVertex2d(0.5f, -0.5f);
glEnd();*/
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}