How to use fragment shader with an image - c++

I'm trying to perform simple color correction operations using GLSL(orange book).
I'm struggling to apply the shaders to images. This is my fragment shader to adjust saturation, stolen from the Orange book. I don't understand how to use this with an image?
const vec3 lumCoeff = vec3(0.2125,0.7154,0.0721);
uniform float Alpha;
void main()
{
vec3 intensity = vec3(dot(gl_Color.rgb, lumCoeff));
vec3 color = mix(intensity, gl_color.rgb, Alpha);
gl_FragColor = vec4(color, 1.0);
}
And then my vertex shader is
void main(void)
{
gl_Position = ftransform();
}
I've been trying to read an image in using OpenCv and then I use glTexImage2d to turn it into a texture but, I dont understand how shaders are used in OpenGL and C++.
When and how do I apply the shaders to the images?
Here is the code I am trying to run.
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
//#include <GL/glew.h>
#include <GLUT/glut.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include "textfile.h"
#include <opencv/cv.h>
#include <opencv/highgui.h>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#define VIEWPORT_WIDTH 320 // 1280
#define VIEWPORT_HEIGHT 320 // 800
IplImage *Image;
static GLuint texName;
GLuint v,f,f2,p;
float lpos[4] = {1,0.5,1,0};
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if(h == 0)
h = 1;
float ratio = 1.0* w / h;
// Reset the coordinate system before modifying
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45,ratio,1,1000);
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glGenTextures(1, &texName);
glActiveTexture(GL_TEXTURE0);
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);
glPixelStorei (GL_UNPACK_ALIGNMENT, Image->align);
glPixelStorei (GL_UNPACK_ROW_LENGTH, Image->widthStep / Image->nChannels);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Image->width, Image->height, 0, GL_BGR, GL_UNSIGNED_BYTE, Image->imageData);
glViewport(0, 0, VIEWPORT_WIDTH , VIEWPORT_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, VIEWPORT_WIDTH , 0, VIEWPORT_HEIGHT, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
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, texName);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, VIEWPORT_HEIGHT, 0);
glTexCoord2f(1, 1); glVertex3f(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, 0);
glTexCoord2f(1, 0); glVertex3f(VIEWPORT_WIDTH, 0, 0);
char *vs = NULL,*fs = NULL,*fs2 = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
//f2 = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead("toon.vert");
fs = textFileRead("toon.frag");
//fs2 = textFileRead("toon2.frag");
const char * ff = fs;
//const char * ff2 = fs2;
const char * vv = vs;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
//glShaderSource(f2, 1, &ff2,NULL);
free(vs);free(fs);
glCompileShader(v);
glCompileShader(f);
//glCompileShader(f2);
p = glCreateProgram();
glAttachShader(p,f);
//glAttachShader(p,f2);
glAttachShader(p,v);
glLinkProgram(p);
glUseProgram(p);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glLoadIdentity();
// gluLookAt(0.0,0.0,5.0,
// 0.0,0.0,-1.0,
// 0.0f,1.0f,0.0f);
// glLightfv(GL_LIGHT0, GL_POSITION, lpos);
// glutSolidTeapot(1);
glutSwapBuffers();
}
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27)
exit(0);
}
void setShaders() {
char *vs = NULL,*fs = NULL,*fs2 = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
//f2 = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead("toon.vert");
fs = textFileRead("toon.frag");
//fs2 = textFileRead("toon2.frag");
const char * ff = fs;
//const char * ff2 = fs2;
const char * vv = vs;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
//glShaderSource(f2, 1, &ff2,NULL);
free(vs);free(fs);
glCompileShader(v);
glCompileShader(f);
//glCompileShader(f2);
p = glCreateProgram();
glAttachShader(p,f);
//glAttachShader(p,f2);
glAttachShader(p,v);
glLinkProgram(p);
glUseProgram(p);
}
int main(int argc, char **argv) {
Image = cvLoadImage("lena.tiff",1);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("MM 2004-05");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);
glutKeyboardFunc(processNormalKeys);
glEnable(GL_DEPTH_TEST);
glClearColor(1.0,1.0,1.0,1.0);
// glEnable(GL_CULL_FACE);
// glewInit();
// if (glewIsSupported("GL_VERSION_2_0"))
// printf("Ready for OpenGL 2.0\n");
// else {
// printf("OpenGL 2.0 not supported\n");
// exit(1);
// }
//setShaders();
glutMainLoop();
// just for compatibiliy purposes
return 0;
}

You have not passed any uniform sampler to your shader.
Well there are many things that you should notice.
You need not to dump all initialization code in render loop. Your are just killing your program.
like this texture generation code. It only needs to execute once. Just move it to some init() function.
glGenTextures(1, &texName);
glActiveTexture(GL_TEXTURE0);
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);
glPixelStorei (GL_UNPACK_ALIGNMENT, Image->align);
glPixelStorei (GL_UNPACK_ROW_LENGTH, Image->widthStep / Image->nChannels);
glTexImage2D(GL_TEXTURE_2D, 0, 3, Image->width, Image->height, 0, GL_BGR, GL_UNSIGNED_BYTE, Image->imageData);
then this shader program creation, compilation and linking this this is usually done at application start-up and only once. After that you can just call glUseProgram(handle) in your render loop.
char *vs = NULL,*fs = NULL,*fs2 = NULL;
v = glCreateShader(GL_VERTEX_SHADER);
f = glCreateShader(GL_FRAGMENT_SHADER);
//f2 = glCreateShader(GL_FRAGMENT_SHADER);
vs = textFileRead("toon.vert");
fs = textFileRead("toon.frag");
//fs2 = textFileRead("toon2.frag");
const char * ff = fs;
//const char * ff2 = fs2;
const char * vv = vs;
glShaderSource(v, 1, &vv,NULL);
glShaderSource(f, 1, &ff,NULL);
//glShaderSource(f2, 1, &ff2,NULL);
free(vs);free(fs);
glCompileShader(v);
glCompileShader(f);
//glCompileShader(f2);
p = glCreateProgram();
glAttachShader(p,f);
//glAttachShader(p,f2);
glAttachShader(p,v);
glLinkProgram(p);
Its important to check for errors while shader initialization. Follow these excellent tutorials to know how do that.
You need to pass texture that you created using OpenCV as a Uniform Variable to shader. See here to know what uniform variables are.

Related

Problems with Frambuffer Object - limitation related to my screen resolution

I need to have an image as output of my code (dimension: 3507x3281) composed by two triangles (upper-left corner and lower-right corner), each triangle will give me one different color/image on my output.
I read both images and bound them with 2 textures (texture_1 and texture_2).
Afterwards I created both triangles related to their respective image.
I created and bound the Framebuffer Object and attached the textures. Finally, I saved the content to a image file (.ppm).
PROBLEM:
My result (output image) is coherent and geometrically correct. However, only part of my output image is visible. The part that it is visible has 1366x768 as dimension, which is exactely my screen resolution. Am I missing something on the code?
Is there anything related to the options "GL_DRAW_FRAMEBUFFER, GL_READ_FRAMEBUFFER and GL_FRAMEBUFFER"? Only "GL_DRAW_FRAMEBUFFER" gives the coherent result.
Am I really using the Framebuffer? Because, I read that since you use the FBO this kind of problem does not appear.
CODE:
#include <windows.h>
#define GLEW_STATIC
#include <GL/glew.h>
#include <GL/glut.h>
#include <GL/freeglut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <cstdio>
#include <string>
/// Definitions
#define checkImageWidth 500 /// for image - texture 1
#define checkImageHeight 500
#define width 2464 /// for image - texture 2
#define height 1648
static GLuint texture_1, texture_2;
static GLubyte checkImage[checkImageHeight][checkImageWidth][3];
GLuint fb = 1; ///Frame-buffer Object
/// Headers
GLuint raw_texture_load();
void FBO_2_PPM_file(int output_width, int output_height);
/// Functions
GLuint raw_texture_load()
{
/// image used for texture 1
unsigned char *data;
FILE *file;
// open texture data
file = fopen("C:\\Dataset\\image_1.raw", "rb");
if (file == NULL) return 0;
// allocate buffer
data = (unsigned char*) malloc(width * height * 3);
// read texture data
fread(data, width * height * 3, 1, file);
fclose(file);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
/// Texture 1
// allocate a texture name
glGenTextures(1, &texture_1);
// select our current texture
glBindTexture(GL_TEXTURE_2D, texture_1);
// select modulate to mix texture with color for shading
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_DECAL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_DECAL);
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
// when texture area is large, bilinear filter the first mipmap
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// texture should tile
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// build our texture mipmaps
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
// free buffer
free(data);
/// image used for texture 2
FILE *image_4tx2;
if( (image_4tx2 = fopen("C:\\dataset\\image2.ppm","r")) == NULL )
{
printf("\n Problem with input for texture 2\n");
system("pause");
exit(1);
}
int i, j, max_ND, n_col, n_lin, Red, G, B;
char s1[20];
/// Read header
fscanf(image_4tx2,"%s",&s1);
fscanf(image_4tx2,"%s %s %s %s",&s1, &s1, &s1, &s1);
fscanf(image_4tx2,"%d %d",&n_col, &n_lin);
fscanf(image_4tx2,"%d",&max_ND);
for (i = 0; i < checkImageWidth; i++)
{
for (j = 0; j < checkImageHeight; j++)
{
fscanf(image_4tx2,"%d %d %d ",&Red, &G, &B);
checkImage[i][j][0] = (GLubyte) Red;
checkImage[i][j][1] = (GLubyte) G;
checkImage[i][j][2] = (GLubyte) B;
}
}
fclose(image_4tx2);
/// Texture 2
glGenTextures(2, &texture_2);
glBindTexture(GL_TEXTURE_2D, texture_2);
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_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, checkImageWidth, checkImageHeight,
0, GL_RGB, GL_UNSIGNED_BYTE, checkImage);
return texture_1;
}
void render()
{
GLdouble COORD_1[3], COORD_2[3], COORD_3[3], COORD[3];
/// Output image size
double col_siz_double = 3507.0;
double row_siz_double = 3281.0;
int col_siz_output_image = 3507;
int row_siz_output_image = 3281;
/// Aspect ratio
double ratio_col_row = col_siz_double/row_siz_double;
double aspect_E, aspect_N;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if(ratio_col_row>=0.0)
{
aspect_E = 1.0;
aspect_N = 1.0/ratio_col_row;
glOrtho(0.0, 1.0*aspect_E, 0.0, 1.0*aspect_N, 0.0, 1.0);
}
else
{
aspect_E = 1.0/ratio_col_row;
aspect_N = 1.0;
glOrtho(0.0, 1.0*aspect_E, 0.0, 1.0*aspect_N, 0.0, 1.0);
}
gluLookAt( 0.0, 0.0, 1.0, /* eye */
0.0, 0.0, 0.0, /* center */
0, 1, 0); /* up */
/// Viewport
glViewport(0,0,col_siz_output_image,row_siz_output_image);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushAttrib(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
/// Triangle 1 (upper-left half of the output)
glBindTexture(GL_TEXTURE_2D, texture_1);
glBegin(GL_TRIANGLES);
glTexCoord2d(0.0, 0.0);
COORD[0]= 0.0*aspect_E;
COORD[1]= 0.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(1.0, 1.0);
COORD[0]= 1.0*aspect_E;
COORD[1]= 1.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(0.0, 1.0);
COORD[0]= 0.0*aspect_E;
COORD[1]= 1.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glEnd();
/// Triangle 2 (low-right half of the output)
glBindTexture(GL_TEXTURE_2D, texture_2);
glBegin(GL_TRIANGLES);
glTexCoord2d(0.0, 0.0);
COORD[0]= 0.0*aspect_E;
COORD[1]= 0.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(1.0, 1.0);
COORD[0]= 1.0*aspect_E;
COORD[1]= 1.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glTexCoord2d(1.0, 0.0);
COORD[0]= 1.0*aspect_E;
COORD[1]= 0.0*aspect_N;
COORD[2]= 1.0;
glVertex3d(COORD[0], COORD[1], COORD[2]);
glEnd();
glFlush();
glDisable(GL_TEXTURE_2D);
static const GLenum draw_buffers[] =
{
GL_COLOR_ATTACHMENT0,
GL_COLOR_ATTACHMENT1
};
/// Generating and Biding the Framebuffer Object (FBO)
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fb);
///Attaching both 2D texture to the FBO as color attachments
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, draw_buffers[0], GL_TEXTURE_2D, texture_2, 0);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, draw_buffers[1], GL_TEXTURE_2D, texture_1, 0);
/// Specifying a list of color buffers to be drawn into
glDrawBuffers(2, draw_buffers);
/// Checking the FBO status
GLuint status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
if( status != GL_FRAMEBUFFER_COMPLETE)
{
printf("\n\nFramebuffer status: %s\n\n",status);
system("pause");
}
/// Saving the results from the FBO (fb) to a PPM image file
FBO_2_PPM_file(col_siz_output_image,row_siz_output_image);
}
void init()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
texture_1 = raw_texture_load();
render();
}
void FBO_2_PPM_file(int output_width, int output_height)
{
FILE *output_image;
/// READ THE PIXELS VALUES from FBO AND SAVE TO A .PPM FILE
int i, j, k;
unsigned char *pixels = (unsigned char*)malloc(output_width*output_height*4);
/// READ THE CONTENT FROM THE FBO
glReadBuffer(GL_COLOR_ATTACHMENT0);
glReadBuffer(GL_COLOR_ATTACHMENT1);
glReadPixels(0, 0, output_width, output_height, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
output_image = fopen("C:\\Dataset\\output.ppm", "wt");
if(output_image == NULL)
{
printf("Problem # output file!\n\n");
system("pause");
}
fprintf(output_image,"P3\n");
fprintf(output_image,"# Created by Ricao\n");
fprintf(output_image,"%d %d\n",output_width,output_height);
fprintf(output_image,"255\n");
k = 0;
for(i=output_height-1; i>-1; i--)
{
for(j=0; j<output_width; j++)
{
k = (i*output_width)+j;
/// saving only RGB
fprintf(output_image,"%u %u %u ",(unsigned int)pixels[4*k],(unsigned int)pixels[(4*k)+1],
(unsigned int)pixels[(4*k)+2]);
}
fprintf(output_image,"\n");
}
free(pixels);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(3507, 3281);
glutCreateWindow("Do not close this window!");
glutHideWindow();
/// Checking GLEW library
glewExperimental=TRUE;
GLenum err=glewInit();
if(err!=GLEW_OK)
{
printf("glewInit failed, aborting.");
printf("\tError: %s\n\n",glewGetErrorString(err));
system("pause");
}
if (GLEW_EXT_framebuffer_object != GL_TRUE)
{
printf("\n\n\t ** Error! GLEW_EXT_framebuffer_object != GL_TRUE \n\n");
system("pause");
}
/// To check the max size of the Framebuffer
int dims[2];
glGetIntegerv(GL_MAX_VIEWPORT_DIMS, &dims[0]);
///Executing the rendering process
init();
return 0;

I can't display a texture on a quad

I am trying to wrap a texture on a quad.
All I see is a white rectangle:
To load the texture I used freeimage.
I need help in order to fix this very simple demo:
#include <GL/glut.h>
#include <GL/gl.h>
#include <FreeImage.h>
#include <stdio.h>
GLfloat coordinates[] =
{
-0.5, 0.5, 1,
-0.5, -0.5, 0,
0.5, -0.5, 0,
0.5, 0.5, 0
};
GLfloat texCoords[] =
{
0, 1,
0, 0,
1, 0,
1, 1
};
BYTE* data;
FIBITMAP* bitmap;
GLuint texture;
void initGlutCallbacks();
void initGL();
void onReshape(int w, int h);
void display();
FIBITMAP* loadTexture(const char* fileName);
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(512, 512);
glutInitWindowPosition(64, 64);
glutCreateWindow("arrays");
initGlutCallbacks();
initGL();
// texture
bitmap = loadTexture("rufol.png");
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
data = FreeImage_GetBits(bitmap);
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, 256, 256, 0,
GL_RGBA8, GL_UNSIGNED_BYTE,
data
);
// enable arrays
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
// specifying data for the arrays
glVertexPointer
(
3, GL_FLOAT, 0, coordinates
);
glTexCoordPointer
(
2, GL_FLOAT, 0, texCoords
);
glutMainLoop();
}
void initGlutCallbacks(){
glutReshapeFunc(onReshape);
glutDisplayFunc(display);
}
void initGL(){
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glEnable ( GL_TEXTURE_2D );
}
void onReshape(int w, int h){
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glTexEnvf(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glDrawArrays(GL_QUADS, 0, 4);
glFlush();
glutSwapBuffers();
}
FIBITMAP* loadTexture(const char* fileName){
FIBITMAP *bitmap = FreeImage_Load(FIF_PNG, "rufol.png");
if(bitmap == 0) printf("error loading the image\n");
FIBITMAP *fbitmap = FreeImage_ConvertTo32Bits(bitmap);
FreeImage_Unload(bitmap);
return fbitmap;
}
As you can see I am not even using perspective. Also lighting is not enabled(I don't know if it is required to display textures). I have tested a very similar code but using colors for each vertex instead of texture coordinates and it worked. So I think it might be something wrong when loading the image.
Have you tried using GL_RGBA instead of GL_RGBA8 as second parameter (format)?

OpenGL Core Profile won't texture quad

I wrote the following code to display two images on two different monitors using GLFW. When I try running OPENGL_CORE_PROFILE it screws up and my image doesn't appear.
Instead the screen just stays black. But when I don't use core profile the image shows up on the screen just fine. I need to use core profile so I can use OpenGL 3.3 and 330 shaders.
Is my current method I'm using to texture a quad is wrong what would be the best approach to display and image using 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);
}
Your code is totally invalid in a core profile. In core profiles, all the old functionality which has been declared "deprecated" many years ago is actrually removed. GL_QUADS primitive types are not supported, immediate mode rendering with glBegin()/glEnd() is not supported, glEnable(GL_TEXTURE_2D); is not supported, builtin vertex attributes like the texcoord and vertex position are not supported and GL_TEXTURE_ENV_MODE is not supported, rendering without shaders ("fixed-function pipeline") in not supported. The same goes for glMatrixMode(), glLoadIdentity, glOrtho() and all other matrix and matrix stack related functions. The internal format "3" is also not supported for glTexImage2D(), you should use GL_RGB (which you should have used before, too).
When you want to use a modern core profile, you have to use the programmable pipeline with shaders and without all the builtin attributes. You have vo define your own generic attributes instead. You have to use VBOs for your geometry and VAOs for the vertex pointers instead of immediate mode. You also have to replace quad primitves by triangle-based primitives. You will have to use your own matrix functions, or use some library for that.
You might want to have a look at tutorials like arcsynthesis or open.gl which exclusively deal with modern OpenGL.

problems generating texture

Update 2 works, it was a wrong allert
Update 2 (using vbo vertex- and fragment-shaders) but it still don't works
#include "GL/glxew.h"
#include "GL/glext.h"
#include "GL/glu.h"
#include "GL/freeglut.h"
#include <iostream>
GLint attribute;
GLuint program;
void gen_texture(GLuint &texture, int width, int height)
{
GLuint fb;
glGenFramebuffers(1, &fb);
glGenTextures(1, &texture);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width, height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(program);
glEnableVertexAttribArray(attribute);
GLfloat vertex_data[]
{
1.0f, 0.0f,
0.0f, 0.0f,
0.0f, 1.0f,
1.0f, 0.0f,
0.0f, 1.0f,
1.0f, 1.0f
};
glVertexAttribPointer(
attribute,
2,
GL_FLOAT,
GL_FALSE,
0,
vertex_data
);
glDrawArrays(GL_TRIANGLES, 0, 6);
glDisableVertexAttribArray(attribute);
}
void init_layout()
{
GLint compile_ok = GL_FALSE, link_ok = GL_FALSE;
GLuint vs, fs;
vs = glCreateShader(GL_VERTEX_SHADER);
const char *vs_source =
"#version 120\n" // OpenGL 2.1
"attribute vec2 coord2d; "
"void main(void) { "
" gl_Position = vec4(coord2d, 0.0, 1.0); "
"}";
glShaderSource(vs, 1, &vs_source, 0);
glCompileShader(vs);
glGetShaderiv(vs, GL_COMPILE_STATUS, &compile_ok);
if (0 == compile_ok)
{
std::cerr << "[texture_layout/init_layout] fehler im vertex shader\n";
exit(1);
}
fs = glCreateShader(GL_FRAGMENT_SHADER);
const char *fs_source =
"#version 120\n" // OpenGL 2.1
"void main(void) { "
" gl_FragColor[0] = 0.8f; "
" gl_FragColor[1] = 0.5f;"
" gl_FragColor[2] = 0.0f; "
"}";
glShaderSource(fs, 1, &fs_source, 0);
glCompileShader(fs);
glGetShaderiv(fs, GL_COMPILE_STATUS, &compile_ok);
if (0 == compile_ok)
{
std::cerr << "[texture_layout/init_layout] fehler im fragment shader\n";
exit(1);
}
program = glCreateProgram();
glAttachShader(program, vs);
glAttachShader(program, fs);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok)
{
std::cerr << "[texture_layout/init_layout] fehler in glLinkProgram\n";
exit(1);
}
const char* attribute_name = "coord2d";
attribute = glGetAttribLocation(program, attribute_name);
if (attribute == -1) {
std::cerr << "Could not bind attribute " << attribute_name << "\n";
exit(1);
}
}
int main(int argc, char **argv)
{
glutInit (&argc, argv);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (500, 500);
glutCreateWindow ("");
glewExperimental=GL_TRUE;
GLenum err=glewInit();
if(err!=0)
{
std::cerr << glewGetErrorString(err) << std::endl;
exit(1);
}
GLenum error;
GLuint texture;
while ( ( error = glGetError() ) != GL_NO_ERROR)
{
std::cerr << std::hex << error << "\n";
}
init_layout();
gen_texture(texture, 200, 200);
GLvoid *tex_data = new GLubyte[4*200*200];
glBindTexture(GL_TEXTURE_2D, texture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
return 0;
}
Update 1 (using vbo instead of glBegin)
now my code should draw a red triangle using vbo, but it doesn't
void gen_texture(GLuint &color, int width, int height)
{
GLuint fb;
glGenFramebuffers(1, &fb);
glGenTextures(1, &color);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glBindTexture(GL_TEXTURE_2D, color);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width, height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glViewport(0,0,width,height);
glOrtho(0,width,0,height,0,128);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
//glDisable(GL_CULL_FACE);
glColor3f(1.f, .0f, .0f);
GLfloat vertices[6] =
{
0, 0,
0, (GLfloat)height,
(GLfloat)width, (GLfloat)height,
};
unsigned short indices[] = {0, 1, 2};
GLuint vbo;
glGenBuffersARB(1, &vbo);
glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo);
glBufferDataARB(GL_ARRAY_BUFFER_ARB, 6*sizeof(GLfloat), vertices, GL_STATIC_DRAW_ARB);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, 0);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, vertices);
}
Original 2:
I have a function that should draw a red square into a texture with the glBegin directive. But the only thing I got to work is
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
I've tested it with multiple colors. But that part do not work:
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glViewport(0,0,width,height);
glOrtho(0,width,0,height,0,128);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glColor3f(1.f, .0f, .0f);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(0, height);
glVertex2f(width, height);
glVertex2f(width, 0);
glEnd();
Here is the full function:
void gen_texture(GLuint &color, int width, int height)
{
GLuint fb;
glGenFramebuffers(1, &fb);
glGenTextures(1, &color);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glBindTexture(GL_TEXTURE_2D, color);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width, height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
glViewport(0,0,width,height);
glOrtho(0,width,0,height,0,128);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
//glDisable(GL_CULL_FACE);
glColor3f(1.f, .0f, .0f);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(0, height);
glVertex2f(width, height);
glVertex2f(width, 0);
glEnd();
}
Original Post:
I have a function that should draw a red square into a texture, but when I call the function for generating the texture and then I want to check the generated texture data, with the glGetTexImage function, I get a null-pointer.
#include "GL/glxew.h"
#include "GL/glext.h"
#include "GL/glu.h"
#include "GL/freeglut.h"
#include <iostream>
GLuint texture;
GLvoid *tex_data;
void gen_texture(GLuint &color, int width, int height)
{
GLuint fb;
glGenFramebuffers(1, &fb);
glGenTextures(1, &color);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
width, height,
0,
GL_RGBA,
GL_UNSIGNED_BYTE,
0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0);
glBindTexture(GL_TEXTURE_2D, 0);
glEnable(GL_TEXTURE_2D);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
glViewport(0, 0, width, height);
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, width, height, 0, 0, 1);
glMatrixMode (GL_MODELVIEW);
glDisable(GL_DEPTH_TEST);
glDisable(GL_CULL_FACE);
glColor4f(1.f, .0f, .0f, .5f);
glBegin(GL_QUADS);
glVertex2f(0, 0);
glVertex2f(0, height);
glVertex2f(width, height);
glVertex2f(width, 0);
glEnd();
}
int main(int argc, char **argv)
{
glutInit (&argc, argv);
glutInitContextVersion(3, 2);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (500, 500);
glutCreateWindow ("");
glewExperimental=GL_TRUE;
GLenum err=glewInit();
if(err!=0)
{
std::cerr << glewGetErrorString(err) << std::endl;
exit(1);
}
GLenum error;
while ( ( error = glGetError() ) != GL_NO_ERROR)
{
std::cerr << std::hex << error << "\n";
}
gen_texture(texture, 200, 200);
glBindTexture(GL_TEXTURE_2D, texture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_data);
if (tex_data == 0)
std::cerr << "Captain, eine Null an Board!\n";
return 0;
}
the tex_data is now a null-pointer
what am I doing wrong???
glutInitContextVersion(3, 2);
glutInitContextProfile(GLUT_CORE_PROFILE);
^^^^^^^^^^^^^^^^^ ok...
....
glBegin(GL_QUADS);
^^^^^^^^ wat
glBegin() and friends aren't valid calls in a Core context.
You'll have to get spun up on shaders and VBOs if you insist on Core.

OpenGL texture mapping coordinates

I need to be able to stretch the image texture i'm importing over the entire 2d or 3d (face) shape. It will only render in the top right of the shape, and either repeat - if I enable GL_REPEAT, or the image will stretch from the sides projecting to the edge horribly if i enable GL_CLAMP.
Here's my code:
#include "stdafx.h"
#include "glut.h"
#include <stdio.h>
#include <windows.h>
#include <stdlib.h>
GLuint texture;
float xRotation = 0.0f;
void drawScene1 (void) {
//glRotatef(xRotation,0.0f,1.0f,0.0f);
glBindTexture(GL_TEXTURE_2D, texture);
//glutSolidCube(1.0f);
glBegin(GL_POLYGON);
glTexCoord2d(0,1);
glVertex2d(-1.5,-1.5);
glTexCoord2d(1,1);
glVertex2d(1.0,-2.0);
glTexCoord2d(1,0);
glVertex2d(+1.5,+1.5);
glTexCoord2d(0,0);
glVertex2d(-1.5,+1.5);
glEnd();
}
void FreeTexture(GLuint texture) {
glDeleteTextures(1, &texture);
}
GLuint LoadTexture(const char * filename, int width, int height) {
GLuint texture;
unsigned char * data;
FILE * file;
file = fopen(filename, "rb");
if ( file == NULL ) return 0;
data = (unsigned char *)malloc(width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
//glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
free(data);
return texture;
}
void init (void) {
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
//gluOrtho2D(0,500,0,500);
//glClearDepth(1);
//glEnable (GL_DEPTH_TEST);
//glEnable (GL_LIGHTING);
//glEnable (GL_LIGHT0);
//glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
}
void display (void) {
glClearColor(0.05,0.05,0.1,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
texture = LoadTexture("img.raw", 256, 256);
drawScene1();
FreeTexture(texture);
glutSwapBuffers();
xRotation++;
}
int main (int argc, char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Virgin");
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutMainLoop();
return 0;
}
Please decide: Implicit texture coordinate generation:
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
or explicit texture coordinate supplications:
glTexCoord(...)
Implicit will override explicit.
void display (void) {
/*... */
texture = LoadTexture("img.raw", 256, 256);
drawScene1();
FreeTexture(texture);
/* ... */
}
Don't load and delete the texture for each rendered frame. Load the textures at startup and only bind them when rendering.
Remove the following lines:
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
You already have texture coordinates entered via glTexCoord2f(), no need to generate them.