我已经设置了GLFW库,根据它的用户指南,我应该使用glfwInit()函数来初始化它.之后我应该能够 - 例如 - 在我按下键盘上的键后调用回调函数,但是这个函数没有被调用,所以我尝试了更简单的东西 - 获得鼠标位置.但它也不起作用,所以我认为我的整个GLFW出了问题.
这是我的应用程序的主循环:
while (!m_exit)
{
int x, y;
glfwGetMousePos(&x, &y);
glfwPollEvents();
gameCycle(); // Do one cycle
while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
{
// Check the pressence of commands
if (!GetMessage (&msg, NULL, 0, 0))
{
return msg.wParam; // Hand the control on to system
}
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
Run Code Online (Sandbox Code Playgroud)
在此循环之前,我初始化我的GLFW:
// Initialize GLFW
if( !glfwInit() )
{
exit( EXIT_FAILURE );
}
glfwSetKeyCallback(processKeys);
// Main …Run Code Online (Sandbox Code Playgroud) 我一直在使用Xcode中的GLFW 2进行OpenGL项目,一切都运行得很好.我能够创建一个3.2 OpenGL上下文,并渲染一切.
但是,昨天我安装了GLFW3 lib并进行了修改建议.现在我根本无法创建3.2上下文,它总是返回3.0.3上下文.我能做错什么?
我在glfw之前加入了glew标题
这是我的初始化代码:
if(!glfwInit()){
std::cout << "ERROR IN glfwInit()" << std::endl;
return;
}
mWindow = glfwCreateWindow(mWidth, mHeight, "GLFW Renderer", NULL, NULL);;
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);
if(!mWindow){
std::cout << "ERROR IN glfwOpenWindow" << std::endl;
glfwTerminate();
return;
}
/* Make the window's context current */
glfwMakeContextCurrent(mWindow);
int major, minor, rev;
glfwGetVersion(&major, &minor, &rev);
std::cout << "OpenGL " << major << "." << minor << "." << rev << std::endl;
glewExperimental = GL_TRUE;
if(glewInit() != …Run Code Online (Sandbox Code Playgroud) 每当我调用一个函数来交换缓冲区时,我都会glDebugMessageCallback说出很多错误:
glVertex2f已从OpenGL Core上下文(GL_INVALID_OPERATION)中删除
我已经尝试过同时使用GLFW和freeglut,但都无法正常工作。
我glVertex2f当然没用过。我什至甚至删除了所有渲染代码,以查看是否可以找到导致该问题的原因,但是错误仍然存在,就在glutSwapBuffers/ 之后glfwSwapBuffers。
使用单缓冲也不会导致任何错误。
我已经将上下文初始化为4.3,核心配置文件,并标记了前向兼容性。
我是OpenGL的新手,并且有一个简单的程序,我现在正在搞乱.
我遇到的问题是当我传递一个数组来表示点的颜色时,颜色最终变成黑色.如果我只是在片段着色器中明确定义颜色,它可以正常工作,如果我在VAO中切换数据的索引,则三角形将移动到作为颜色的点.
我尝试使用API跟踪并获得以下内容:
6872: message: major api error 1282: GL_INVALID_OPERATION error generated. <program> is not a program object, or <shader> is not a shader object.
6872 @0 glAttachShader(program = 1, shader = 1)
6872: warning: glGetError(glAttachShader) = GL_INVALID_OPERATION
6876: message: major api error 1282: GL_INVALID_OPERATION error generated. Handle does not refer to the expected type of object (GL_SHADER_OBJECT_ARB).
6876 @0 glDeleteShader(shader = 1)
6876: warning: glGetError(glDeleteShader) = GL_INVALID_OPERATION
6878: message: major api error 1282: GL_INVALID_OPERATION error generated. Handle does not …Run Code Online (Sandbox Code Playgroud) 据我所知,当我在GLFW上设置这些上下文约束时:
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Run Code Online (Sandbox Code Playgroud)
我应该在运行的机器上获得最大可用的OpenGL上下文,前提是它高于OpenGL 3.3.但是,通过使用glGetString获取OpenGL上下文版本,我发现情况并非如此.每次我查询glGetString上下文版本时,我只会得到我设置的主要和次要版本glfwWindowHint,上面没有.请记住,我的GPU支持OpenGL 4.5.
另外需要注意的是,当我没有设置任何约束时,我确实得到了一个OpenGL 4.5上下文.
这是完整的源代码,似乎可以复制问题:
#define GLEW_STATIC
#include <iostream>
#include <GL\glew.h>
#include <GLFW\glfw3.h>
#include <glm\glm.hpp>
int main(int argc, char argv[])
{
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW 3.0.4" << std::endl;
getchar();
glfwTerminate();
return -1;
}
std::cout << "Initialized GLFW 3.0.4" << std::endl;
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* m_window = glfwCreateWindow(640, 480, "Koala", NULL, NULL);
if (!m_window)
{ …Run Code Online (Sandbox Code Playgroud) 我很确定这是因为我对GL_MODELVIEW矩阵的工作方式缺乏了解.这是一个屏幕录制正在发生的事情:http://youtu.be/3F7FLkVI7kA
如您所见,最底部的三角形是第一个绘制的三角形,并且随着我预期其他两个三角形移动而移动.第二个三角形相对于第一个三角形移动和旋转,第三个三角形相对于该组合移动和旋转.
我想要的是所有三个三角形在3D空间中是静止的,但是旋转(就像第一个三角形).
资源:
// Main loop
do {
// Clear Screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Update camera
glfwGetCursorPos(window, &cursorX, &cursorY);
cam.update(0.001f, (int)cursorX, (int)cursorY);
// Reset Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// move camera
glRotatef(cam.rotation.x, 1.0f, 0.0f, 0.0f);
glRotatef(cam.rotation.y, 0.0f, 1.0f, 0.0f);
// translate modelview matrix to position of the camera - everything should now draw relative to camera position
glTranslatef(-cam.position.x, cam.position.y, -cam.position.z);
// Draw ground
drawGroundGrid(-25.0f);
drawSpinningTriangle(0.0f, 0.0f, -5.0f);
drawSpinningTriangle(3.14f, 3.0f, -6.0f);
drawSpinningTriangle(-6.0f, 12.0f, -5.0f);
// Swap buffers …Run Code Online (Sandbox Code Playgroud) 我试图在GLFW的帮助下在OpenGL中创建一个空白窗口.下面是我的代码
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <glfw3.h>
GLFWwindow* window;
#include <glm/glm.hpp>
using namespace glm;
int main( void )
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
window = glfwCreateWindow(800,600,"learnopengl",NULL,NULL);
if (window == NULL)
{
fprintf(stderr,"there is a problem with window creation\n");
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
{
fprintf(stderr,"Failed to initialize GLEW\n");
return -1;
}
int width, height;
glfwGetFramebufferSize(window,&width,&height);
glViewport(0,0,width,height);
while(!glfwWindowShouldClose(window))
{
glfwPollEvents();
glfwSwapBuffers(window);
}
glfwTerminate();
}
Run Code Online (Sandbox Code Playgroud)
但是当我尝试运行上面的代码而不是黑色空白时,它会在新创建的窗口中显示当前屏幕的实例.
我最初使用glfw编写游戏。但是由于缺少android的可移植性,我不得不用SDL替换所有glfw代码,因为SDL更易于移植。
我使用glfw框架工作的原始游戏循环非常出色。这里是:
// game loop
while (!glfwWindowShouldClose(window)) {
//if-else statements that create
//the games state machine
//player input is received here
//rendering done here
}
Run Code Online (Sandbox Code Playgroud)
好。编写此游戏循环可能有更好的方法,但重点是适用于游戏原型的代码。
但是,现在我切换到SDL时遇到了一个问题。似乎必须将SDL_Event放入while循环中以不断检查事件。并且此while_loop在另一个while_loop中。所以我的代码看起来像这样:
while(!Quit) {
SDL_Event event;
while(SDL_PollEvent(&event)) {
//if-else statements that create
//the games state machine
//player input is received here
//rendering done here
}
}
Run Code Online (Sandbox Code Playgroud)
但是while_loop中的while_loop完全弄乱了游戏的渲染!我现在忽隐忽现!是否有其他方法可以像我之前使用glfw一样在单个while_loop中表达此代码。还是有任何方法可以在不使用嵌入式while_loop的情况下检查事件
我已经阅读了一些教程来编写以下代码.唯一的区别是使用SDL而不是GLEW的原始教程.
我不明白这段代码有什么问题.它编译但我没有看到三角形.(教程也没有使用着色器)
#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, …Run Code Online (Sandbox Code Playgroud) 我想为我的应用程序设置一个鼠标滚动回调,但总是有一个错误.这是一个代码片段:
void RambleController::scrollCallback(GLFWwindow *window, double xoffset, double yoffset)
{
_owner->move(_owner->getForward() * (float)yoffset * _zoomSpeed);
}
RambleController::RambleController(float moveSpeed, float lookAroundSpeed, float zoomSpeed, float slideAroundSpeed)
:_moveSpeed{ moveSpeed }, _lookAroundSpeed{ lookAroundSpeed },
_zoomSpeed{ zoomSpeed }, _slideAroundSpeed{ slideAroundSpeed } {
glfwSetScrollCallback(window, scrollCallback);//Error here
}
Run Code Online (Sandbox Code Playgroud)
错误C3867
'violet::RambleController::scrollCallback': non-standard syntax; use '&' to create a pointer to member
Run Code Online (Sandbox Code Playgroud)
在我添加&参数之后,还有其他错误,例如:
glfwSetScrollCallback(window, &scrollCallback);//
Error C2276'&': illegal operation on bound member function expression
Run Code Online (Sandbox Code Playgroud)
任何人都知道如何解决这个问题?