OpenGL核心配置文件不会纹理四边形
Posted
技术标签:
【中文标题】OpenGL核心配置文件不会纹理四边形【英文标题】:OpenGL Core Profile won't texture quad 【发布时间】:2014-09-14 02:02:11 【问题描述】:我编写了以下代码来使用 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);
【问题讨论】:
【参考方案1】:您的代码在核心配置文件中完全无效。在核心配置文件中,所有多年前已宣布“弃用”的旧功能实际上已删除。不支持GL_QUADS
原始类型,不支持使用glBegin()/glEnd()
的即时模式渲染,不支持glEnable(GL_TEXTURE_2D);
,不支持texcoord 和顶点位置等内置顶点属性,不支持GL_TEXTURE_ENV_MODE
,不支持着色器渲染(“固定功能管道”)不支持。 glMatrixMode()
、glLoadIdentity
、glOrtho()
和所有其他矩阵和矩阵堆栈相关的函数也是如此。 glTexImage2D()
也不支持内部格式“3”,您应该使用GL_RGB
(您之前也应该使用过)。
当您想使用现代核心配置文件时,您必须使用带有着色器且没有所有内置属性的可编程管道。您可以改为定义自己的通用属性。您必须对几何图形使用 VBO,对顶点指针使用 VAO,而不是立即模式。您还必须用基于三角形的图元替换四边形图元。您将不得不使用自己的矩阵函数,或者为此使用一些库。
您可能想看看像 arcsynthesis 或 open.gl 这样专门处理现代 OpenGL 的教程。
【讨论】:
以上是关于OpenGL核心配置文件不会纹理四边形的主要内容,如果未能解决你的问题,请参考以下文章