- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些代码,我直接按照教程生成三角形。作者在 Windows 中编写他的代码,但他说这也可以在 OSX 中完成。我能够让程序编译,但它在遇到 glCreateShader 时抛出异常。我到处都看过,但我不知道出了什么问题。这一定是一些新手错误。有谁知道可能出了什么问题?
#include <iostream>
#include <string>
#include <Dunjun/Common.hpp>
#include <GL/glew.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
#include <GLFW/glfw3.h>
#define GLOBAL static
#define internal static
#define LOCAL_PERSIST static
#define float32 float
#define float64 double
GLOBAL const int g_windowWidth = 854;
GLOBAL const int g_windowHeight = 480;
GLOBAL const char* g_gameTitle = "Dunjun";
GLFWwindow *toggleFullScreenWindow(GLFWwindow *window, int key);
void setColor(float32 red, float32 blue, float32 green, float32 alpha);
bool toggleExit(GLFWwindow *window, bool isRunning);
GLFWwindow* initialize_window(int width, int height, const char* title);
void init_glfw();
void init_glew();
void glfwHints();
int main(int argc, char **argv) {
GLFWwindow *window = initialize_window(g_windowWidth, g_windowHeight, g_gameTitle);
float vertices[] = {
+0.0f, -0.5f, //vertex 1
-0.5f, -0.5f, //vertex 2
+0.5f, -0.5f //vertex 3
};
const char* vertexShaderText = {
"version 120\n"
"\n"
"attribute vec2 position"
"void main()"
"{"
" gl_position = vec4(position, 0.0, 1.0);"
"}"
};
const char* framentShaderText = {
"version 120\n"
"\n"
"void main()"
"{"
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);"
"}"
};
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderText, nullptr);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &vertexShaderText, nullptr);
glCompileShader(vertexShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glBindAttribLocation(shaderProgram, 0, "position");
glLinkProgram(shaderProgram);
glUseProgram(shaderProgram);
GLuint vertexBufferObject;
glGenBuffers(1, &vertexBufferObject);
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
/*
* GL_STATIC_DRAW Things that are static
* GL_DYNAMIC_DRAW Things that are changed but not to often
* GL_STREAM_DRAW THings that change all the time.
*/
bool running = true;
/* Loop until the user closes the window */
while (running) {
/* Render here */
setColor(0.5f, 0.69f, 1.0f, 1.0f);
//draw things
{
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableVertexAttribArray(0);
}
/*Swap front and back buffers */
glfwSwapBuffers(window);
/*poll for and process events*/
glfwPollEvents();
running = toggleExit(window, running);
window = toggleFullScreenWindow(window, GLFW_KEY_F11);
}
glfwTerminate();
return EXIT_SUCCESS;
}
void glfwHints(){
glfwWindowHint(GLFW_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_VERSION_MINOR, 1);
}
void setColor(float32 red, float32 blue, float32 green, float32 alpha){
glClearColor(red, green, blue, alpha);
glClear(GL_COLOR_BUFFER_BIT);
}
bool toggleExit(GLFWwindow *window, bool isRunning) {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) ||
glfwWindowShouldClose(window)) {
isRunning = false;
}
return isRunning;
}
GLFWwindow* initialize_window(int width, int height, const char* title){
init_glfw();
/* Create a windowed mode window and its OpenGL context */
glfwHints();
GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
init_glew();
return window;
}
GLFWwindow *toggleFullScreenWindow(GLFWwindow *window, int key) {
if (glfwGetKey(window, key)) {
LOCAL_PERSIST bool isFullScreen = false;
isFullScreen = !isFullScreen;
GLFWwindow *newWindow;
if (isFullScreen) {
int count;
const GLFWvidmode *modes = glfwGetVideoModes(glfwGetPrimaryMonitor(), &count);
int monitorHeight = modes[count - 1].height;
int monitorWidth = modes[count - 1].width;
newWindow = glfwCreateWindow(monitorWidth, monitorHeight, g_gameTitle, glfwGetPrimaryMonitor(), window);
} else {
newWindow = glfwCreateWindow(g_windowWidth, g_windowHeight, g_gameTitle, nullptr, window);
}
glfwDestroyWindow(window);
glfwMakeContextCurrent(newWindow);
return newWindow;
}
}
void init_glew(){
if(!glewInit()){
std :: cout << "glew failed to init!";
exit(EXIT_FAILURE);
}
}
void init_glfw(){
if (!glfwInit()) {
std :: cout << "glfw failed to init!";
exit(EXIT_FAILURE);
}
}
更新:我已经编辑了代码以包含建议的更改,但 glewInit() 返回一个错误的值。还有什么可能是错的。
最佳答案
您必须决定是否要使用 GLEW,然后始终如一地坚持使用。 Mac OS 不需要它,我建议避免使用它。但是有些人仍然喜欢使用它,所以这是您的选择。
你现在拥有的是:
#include <GL/glew.h>
#ifndef __APPLE__
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
#endif
...
#ifndef __APPLE__
if(!glewInit()){
exit(EXIT_FAILURE);
}
#endif
您包含了 GLEW header ,但没有初始化 GLEW。 GLEW header 将包含 OpenGL 入口点的声明,但它们是函数指针,在 GLEW 初始化之前它们将为空。因此,如果您之后调用 glCreateShader()
,它将是一个空函数指针。
要更正此问题,您需要包含 native OpenGL header ,其中包含实际 OpenGL 入口点的声明,而不仅仅是函数指针:
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/glew.h>
#endif
关于c++ - glCreateShader 在 OSX 上抛出异常,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34984208/
当我尝试使用 glCreateShader 创建着色器并运行我的程序时,我刚刚创建的窗口变为白色并给出错误: OpenGl.exe 中的 0x03D312F0 (atioglxx.dll) 引发异常:
为什么会出现这些错误? error: 'GL_VERTEX_SHADER' was not declared in this scope error: 'glCreateShader' was not
我应该有最新版本的 Glew 和 Glut,所以这应该不是问题。一切都应该链接起来,我正在使用 MS visual studio 2010。我的程序可以编译,但是当我到达 glCreateShader
我有一个使用 cygwin 和 GCC 的 Windows 构建环境,并且正在链接到 GLEE、GLUT 和 opengl32 的库。这是 Win32 版本。 对 glCreateShader 的所有
背景: 我的 c++/OpenGL3.1/GLSL/Qt 程序中有一个 Shader 类。我的程序使用多个基于不同 GLSL 源文件的着色器。 我的应用程序可以运行许多基于 QGLWidget 实现的
我在 OpenGL 中有一个项目,我正在尝试加载着色器。为此,我使用 GLuint shader=glCreateShader(shaderType);。问题是,当它尝试运行这一行时,我得到了 EXC
我遇到了 glCreateShaders 的问题。它总是返回 0。我将 Glew 与 SDL 结合使用,每当我运行该程序时,它都会显示: 0(1) : error C0000: syntax erro
我遇到了一个非常奇怪的 OpenGL 错误,其中调用 glCreateShader工作了几次,然后稍后我尝试调用它,但它失败了,返回 0。调用 glGetError紧接着也返回0,表示没有错误。环顾四
我正在使用 glew、glut 和 GLM 编写 C++ 程序。当我像这样创建着色器时: GLuint VertexShaderID = glCreateShader(GL_VERTEX_SHADER
我有一些代码,我直接按照教程生成三角形。作者在 Windows 中编写他的代码,但他说这也可以在 OSX 中完成。我能够让程序编译,但它在遇到 glCreateShader 时抛出异常。我到处都看过,
我正在开发一个 OpenGL 程序来测试着色器并尝试编译它们。但是,对 glCreateShader() 的调用(以及其他着色器调用,如 glDeleteShader)会出现以下错误: (此错误来自
目前正在尝试让我的 C++ 源代码在 iOS 上运行。它在 Android 上的 OpenGLES 2.0 中运行,我已经完成了编译的所有内容,但在编译着色器时遇到了问题。由于某种原因 glCreat
我正在尝试学习如何编写 OpenGL 着色器。为什么这段代码在我的机器上运行时会段错误? (我使用的是 Ubuntu 10.04,我将其命名为 shader.cpp。) #include #incl
我在 android 上创建着色器程序时遇到了一个非常困难的问题。当我分别调用 glCreateShader 或 glCreateProgram 时,它们总是返回 0。 我已经介绍了有关故障排除的所有
我正在尝试使用 C++ 和 SDL 2 在 OpenGL ES 2.x 中渲染一个简单的三角形。但是 glCreateShader 和 glCreatProgram 返回零。下面是我正在使用的代码 #
我正在关注“OpenGL ES 2 for Android_快速入门指南”,并创建了使用 opengl 构建简单矩形表所需的一切。但是,当我运行应用程序时,我得到 glCreateShader 返回
我是一名优秀的程序员,十分优秀!