我编写了以下代码,使用GLFW在两个不同的显示器上显示两个图像。当我尝试运行OPENGL_CORE_PROFILE时,它会搞砸,我的图像不会出现。
相反,屏幕只是保持黑色。但是,当我不使用核心配置文件时,图像显示在屏幕上就好了。我需要使用核心配置文件,因此我可以使用OpenGL 3.3和330着色器。
我目前用于纹理四边形的方法是错误的,使用OpenGL 3.3显示和图像的最佳方法是什么?
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
static GLuint tex_lite;
static GLuint tex_dark;
static GLuint tex_front;
static GLuint tex_back;
float ratio;
int width, height;
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 printLog(GLuint obj)
{
int infologLength = 0;
int maxLength;
if(glIsShader(obj))
glGetShaderiv(obj,GL_INFO_LOG_LENGTH,&maxLength);
else
glGetProgramiv(obj,GL_INFO_LOG_LENGTH,&maxLength);
char infoLog[maxLength];
if (glIsShader(obj))
glGetShaderInfoLog(obj, maxLength, &infologLength, infoLog);
else
glGetProgramInfoLog(obj, maxLength, &infologLength, infoLog);
if (infologLength > 0)
printf("%s\n",infoLog);
}
void image2texture(cv::Mat Image,GLuint &texName, int i)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glActiveTexture(GL_TEXTURE0+i);
glGenTextures(1, &texName);
glBindTexture(GL_TEXTURE_2D, texName);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Image.cols, Image.rows, 0, GL_BGR,
GL_UNSIGNED_BYTE, Image.data);
}
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
GLuint linkShader(char* vert, char* frag)
{
GLuint v,f,p;
char *vs = NULL,*fs = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead(vert);
fs = textFileRead(frag);
const char * ff = fs;
const char * vv = vs;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
free(vs);free(fs);
glCompileShader(v);
glCompileShader(f);
p = glCreateProgram();
glAttachShader(p,f);
glAttachShader(p,v);
glLinkProgram(p);
return p;
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void checkWindow(GLFWwindow* window)
{
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
}
int main(void)
{
GLFWwindow* window_back;
GLFWwindow* window_front;
glfwSetErrorCallback(error_callback);
if ( !glfwInit() )
{
exit(EXIT_FAILURE);
}
cv::Mat image_lite = cv::imread("lena.tiff");
cv::Mat image_dark = cv::imread("lena.tiff");
cv::flip(image_lite, image_lite, 0);
cv::flip(image_dark, image_dark, 0);
int count;
GLFWmonitor** monitors = glfwGetMonitors(&count);
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);
if (count >= 2)
{
window_front = glfwCreateWindow(1680,1050,"Front Screen",monitors[0],NULL);
window_back = glfwCreateWindow(1280,800,"Back Screen",monitors[1],NULL);
glewInit();
}
else
{
exit(EXIT_FAILURE);
}
checkWindow(window_front);
checkWindow(window_back);
glfwMakeContextCurrent(window_front);
glfwSetKeyCallback(window_front, key_callback);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if(err!=GLEW_OK)
{
std::cout<<"glewInit failed, aborting."<<std::endl;
}
std::cout << glGetString(GL_RENDERER) << std::endl;
std::cout << glGetString(GL_VENDOR) << std::endl;
std::cout << glGetString(GL_VERSION) << std::endl;
std::cout << glGetString(GL_SHADING_LANGUAGE_VERSION) << std::endl;
while (!glfwWindowShouldClose(window_front) && !glfwWindowShouldClose(window_back))
{
glfwMakeContextCurrent(window_front);
glfwSetKeyCallback(window_front, key_callback);
glfwGetFramebufferSize(window_front, &width, &height);
ratio = width / (float) height;
glViewport(0,0,width,height);
image2texture(image_lite, tex_lite,0);
// GLuint prg = linkShader("toon.vert","toon.frag");
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio,ratio,-1.f,1.f,-1.f,1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glActiveTexture(GL_TEXTURE0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, tex_lite);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0,0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,1.0,0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0,1.0,0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0,-1.0,0.0);
glEnd();
glFlush();
glfwSwapBuffers(window_front);
glfwPollEvents();
glfwMakeContextCurrent(window_back);
glfwSetKeyCallback(window_back, key_callback);
glfwGetFramebufferSize(window_back, &width, &height);
ratio = width / (float) height;
glViewport(0,0,width,height);
image2texture(image_dark, tex_dark,1);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-ratio,ratio,-1.f,1.f,-1.f,1.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glActiveTexture(GL_TEXTURE0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBindTexture(GL_TEXTURE_2D, tex_lite);
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0); glVertex3f(-1.0,-1.0,0.0);
glTexCoord2f(0.0, 1.0); glVertex3f(-1.0,1.0,0.0);
glTexCoord2f(1.0, 1.0); glVertex3f(1.0,1.0,0.0);
glTexCoord2f(1.0, 0.0); glVertex3f(1.0,-1.0,0.0);
glEnd();
glFlush();
glfwSwapBuffers(window_back);
glfwPollEvents();
}
glfwMakeContextCurrent(window_front);
glfwDestroyWindow(window_front);
glfwMakeContextCurrent(window_back);
glfwDestroyWindow(window_back);
glfwTerminate();
exit(EXIT_SUCCESS);
}
答案 0 :(得分:4)
您的代码在核心配置文件中完全无效。在核心配置文件中,多年前声明为“已弃用”的所有旧功能都是删除。不支持GL_QUADS
基元类型,不支持使用glBegin()/glEnd()
的立即模式渲染,不支持glEnable(GL_TEXTURE_2D);
,不支持内置顶点属性,如texcoord和顶点位置,{{1}不支持渲染,不支持着色器(“固定功能管道”)。 GL_TEXTURE_ENV_MODE
,glMatrixMode()
,glLoadIdentity
以及所有其他矩阵和矩阵堆栈相关函数也是如此。 glOrtho()
也不支持内部格式“3”,您应该使用glTexImage2D()
(之前也应该使用)。
如果要使用现代核心配置文件,则必须使用带有着色器的可编程管道,而不使用所有内置属性。您可以改为定义自己的通用属性。您必须将VBO用于几何体,将VAO用于顶点指针而不是立即模式。您还必须使用基于三角形的基元替换quad primitves。您将不得不使用自己的矩阵函数,或使用一些库。
您可能希望查看专门处理现代OpenGL的arcsynthesis或open.gl等教程。