没有着色器的OpenGL [英] OpenGL without shaders
问题描述
我已经阅读了一些教程来编写以下代码. 唯一的不同是原始教程,其中使用SDL而不是GLEW.
I have read some tutorials to write the following code. The only difference is the original tutorials where using SDL instead of GLEW.
我不明白这段代码有什么问题.它可以编译,但我看不到三角形. (本教程也没有使用着色器)
I do not understand what is wrong in this code. It compiles but i do not see the triangle. (the tutorial were not using shaders too)
#include <iostream>
#include <GL/glew.h>
#include <GL/gl.h>
#include <GLFW/glfw3.h>
GLFWwindow* window;
int main(int argc, const char * argv[])
{
if (!glfwInit())
{
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
window = glfwCreateWindow(640, 480, "Test", NULL, NULL);
if (window==NULL)
{
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
do
{
glfwPollEvents();
float vertices[] = {-0.5, -0.5, 0.0, 0.5, 0.5, -0.5};
glClear(GL_COLOR_BUFFER_BIT);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
glfwSwapBuffers(window);
}
while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS && glfwWindowShouldClose(window) == 0 );
glfwTerminate();
return 0;
}
推荐答案
如果使用的是固定功能管道,则不能使用glVertexAttribPointer
之类的通用顶点属性.
If you're using the fixed-function pipeline, you cannot use generic vertex attributes like glVertexAttribPointer
.
但是,NVIDIA的实现在通用属性和非通用属性之间非法地使用了别名.这可能就是本教程的最初编写者在其计算机上放弃使用它的原因.
NVIDIA's implementation, however, illegally aliases between generic attributes and non-generic ones. This is probably why the initial writer of the tutorial got away with it on their machine.
如果要跨平台编写此代码,则必须使用glVertexPointer
和glEnableClientState
:
If you want to write this in a cross-platform way, you have to use glVertexPointer
and glEnableClientState
:
glVertexPointer(2, GL_FLOAT, 0, vertices);
glEnableClientState(GL_VERTEX_ARRAY);
这篇关于没有着色器的OpenGL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!