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.
Related
I'm trying to render a frame captured on OpenCV using OpenGL (imshow is too slow), however I'm failing to update the texture with each new frame. I know I could use GPU on OpenCV but I just don't want to rebuild it, I also thought it should be pretty easy to render it on OpenGL.
The following is the code I'm working on - It should be pretty straight forward yet I don't know where I'm messing up.
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#include "opencv2/opencv.hpp"
using namespace cv;
void render();
void initFrame();
void createTexture();
GLuint textureId;
GLuint fb;
int width = 400;
int height = 400;
VideoCapture cap;
int main(int argc, char **argv)
{
cap.set(CV_CAP_PROP_FRAME_WIDTH, width);
cap.set(CV_CAP_PROP_FRAME_HEIGHT, height);
if(!cap.open(0))
exit(-1);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("Test");
glutDisplayFunc(render);
glutIdleFunc(glutPostRedisplay);
createTexture();
initFrame();
glutMainLoop();
return 0;
}
void createTexture()
{
//create texture id
glGenTextures(1, &textureId);
glBindTexture(GL_TEXTURE_2D, textureId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, //Type of texture
0, //pyramid level (for mip-mapping) - 0: top level
GL_RGB, //Internal color format to convert to
width, //Image width
height, //Image height
0, //border width in pix (either 1 or 0)
GL_RGB, //Input image format
GL_UNSIGNED_BYTE,// Image data type
0 // image data
);
/*
else if(mat.channels() == 4)
{
glTexImage2D(GL_TEXTURE_2D, //Type of texture
0, //pyramid level (for mip-mapping) - 0: top level
GL_RGBA, //Internal color format to convert to
width, //Image width
height, //Image height
0, //border width in pix (either 1 or 0)
GL_RGBA, //Input image format
GL_UNSIGNED_BYTE,// Image data type
0 // image data
);
}
*/
//free(imgPtr);
//glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
}
void initFrame()
{
glGenFramebuffers(1, &fb);
glBindFramebuffer(GL_FRAMEBUFFER, fb);
}
void render()
{
Mat frame;
cap >> frame;
if(frame.empty())
exit(-1);
glTexSubImage2D(textureId,
0,
0,
0,
frame.cols,
frame.rows,
GL_RGB,
GL_UNSIGNED_BYTE,
frame.ptr()
);
glBindTexture(GL_TEXTURE_2D,0);
glFlush();
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureId, 0);
glBindFramebuffer(GL_FRAMEBUFFER,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, textureId);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
float cube[][2] = {
{-1, -1},
{ 1, -1},
{ 1, 1},
{-1, 1}
};
float textCor[][2] = {
{0,0},
{1,0},
{1,1},
{0,1}
};
unsigned int faces[] = {1,0,3,2};
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(2, GL_FLOAT, 2*sizeof(float), &cube[0][0]);
glTexCoordPointer(2, GL_FLOAT, 2*sizeof(float), &textCor[0][0]);
glCullFace(GL_FRONT);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, faces);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,0);
glFlush();
glutSwapBuffers();
}
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)?
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.
I'm having a problem rendering my sprite even though I have initialized glew.
Here's my code:
#include <glew.h>
#include <wglew.h>
#include <stdio.h>
#include <iostream>
#include <gl\GL.h>
#include <SDL.h>
#include "SDL_image.h"
GLuint _texturebufferID;
GLuint _vertexBufferID;
GLuint texturebufferID;
int SCREEN_WIDTH = 740;
int SCREEN_HEIGHT = 520;
int mode;
bool processing = true;
SDL_Surface* image;
SDL_Window* window;
SDL_Renderer* renderer;
SDL_GLContext context;
SDL_Surface* surface;
SDL_Event window_key;
typedef struct {
GLfloat positionCoordinates[3];
GLfloat textureCoordinates[2];
} Texture;
Texture vertices[] =
{
// | Pixels |--|coords|
{{1.0f, 0.0f, 0.0f}, {0.0f, 0.0f}},
{{0.0f, 43.0f, 0.0f}, {0.0f, 1.0f}},
{{168.0f, 43.0f, 0.0f}, {1.0f, 1.0f}},
{{168.0f, 0.0f, 0.0f}, {1.0f, 0.0f}}
};
GLuint loadandbuffersprite(const char *filename)
{
image = IMG_Load(filename);
if (image->format->BytesPerPixel == 3)
{
mode = GL_RGB;
}
else if (image->format->BytesPerPixel == 4)
{
mode = GL_RGBA;
}
else
{
SDL_FreeSurface(image);
return 0;
}
glGenTextures(1, &texturebufferID);
glBindTexture(GL_TEXTURE_2D, texturebufferID);
glTexImage2D(GL_TEXTURE_2D, 0, mode, image->w, image->h,
0, mode, GL_UNSIGNED_BYTE, image->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
SDL_FreeSurface( image );
return texturebufferID;
}
void render()
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
glGenBuffers(1, &_vertexBufferID);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices,
GL_STATIC_DRAW);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Texture), (GLvoid *)
offsetof(Texture, positionCoordinates));
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, sizeof(Texture), (GLvoid *)
offsetof(Texture, textureCoordinates));
glLoadIdentity();
_texturebufferID = loadandbuffersprite("hane_stand.png");
}
// |----------------------------------------|
// | Main function |
// |----------------------------------------|
int main(int argc, char *argv[])
{
SDL_Init(SDL_INIT_EVERYTHING);
// Intialize everything.
window = SDL_CreateWindow
(
"render example",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH,
SCREEN_HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE
);
// Create window for rendering.
glewInit();
renderer = SDL_CreateRenderer(window, -1, 0);
context = SDL_GL_CreateContext(window);
SDL_GL_MakeCurrent(window, context);
SDL_GL_LoadLibrary( NULL );
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// Clearing color
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// Set viewport
// |----------------------------------------|
// | Game loop |
// |----------------------------------------|
while (processing)
{
glClear(GL_COLOR_BUFFER_BIT);
render();
glDrawArrays(GL_QUADS, 0, 4);
SDL_RenderPresent(renderer);
SDL_GL_SwapWindow(window);
}
glDeleteTextures( 1, &texturebufferID);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
It's just this problem that's been hammering my coding progression for months and really need help. Anyone have any ideas on how I could fix this?
As for my computer information I use a Toshiba satellite with an AMD C-50 Processor and 2.0 GB of RAM.
So I've been running through a tutorial on texture mapping in OpenGL and I'm using the function from the tutorial to attempt to map a texture in my own program.
I think I must be missing some necessary calls to something or going horribly wrong somewhere, but at the moment, I'm managing to achieve the black screen effect as per usual.
I've ran through this code and to my knowledge I see no reason why i should be getting a blank screen and was hoping anybody around here could spot where I'm going wrong
I'm using SDL just to clarify which I think might have some relevance to the problem maybe:
#include <iostream>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <GL/glu.h>
void Draw()
{
glTranslatef(320,240,0);
glBegin(GL_QUADS);
glColor3f(1,0,0);
glTexCoord2f(0,0); glVertex2f(0,0);
glTexCoord2f(100,0); glVertex2f(100,0);
glTexCoord2f(100,100); glVertex2f(100,100);
glTexCoord2f(0,100); glVertex2f(0,100);
glEnd();
glLoadIdentity();
}
void Init(void)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
SDL_WM_SetCaption("Texture Test", NULL);
}
void Set_States(void)
{
glClearColor(0,0,0,0);
glViewport(0, 0, 640, 480);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
GLuint LoadTextureRAW( const char * filename, int wrap )
{
GLuint texture;
int width, height;
BYTE * data;
FILE * file;
// open texture data
file = fopen( filename, "rb" );
if ( file == NULL ) return 0;
// allocate buffer
width = 256;
height = 256;
data = (BYTE*)malloc( width * height * 3 );
// read texture data
fread( data, width * height * 3, 1, file );
fclose( file );
// allocate a texture name
glGenTextures( 1, &texture );
// select our current texture
glBindTexture( GL_TEXTURE_2D, texture );
// select modulate to mix texture with color for shading
glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
// when texture area is small, bilinear filter the closest mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST );
// when texture area is large, bilinear filter the first mipmap
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
// if wrap is true, the texture wraps over at the edges (repeat)
// ... false, the texture ends at the edges (clamp)
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
wrap ? GL_REPEAT : GL_CLAMP );
glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
wrap ? GL_REPEAT : GL_CLAMP );
// build our texture mipmaps
gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
GL_RGB, GL_UNSIGNED_BYTE, data );
// free buffer
free( data );
return texture;
}
int main (int argc, char ** argv)
{
GLuint texture;
SDL_Event event;
Init();
Set_States();
bool running = true;
glEnable( GL_TEXTURE_2D );
texture = LoadTextureRAW("texture.raw", 1);
glBindTexture( GL_TEXTURE_2D, texture );
while (running == true)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT: running = false; break;
}
glClear(GL_COLOR_BUFFER_BIT);
Draw();
SDL_GL_SwapBuffers();
}
}
SDL_Quit();
glDeleteTextures( 1, &texture );
return 0;
}
Well one problem is that you're not resetting your model-view matrix each time, so your quad gets quickly translated out of view (assuming it was in view to start with, which I haven't checked).
Give this a shot (texturing removed for brevity and lack of referenced file):
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
void Draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(320,240,0);
glScalef( 100, 100, 0 );
glBegin(GL_QUADS);
glColor3f(1,0,0);
glTexCoord2f(0,0); glVertex2f(0,0);
glTexCoord2f(1,0); glVertex2f(1,0);
glTexCoord2f(1,1); glVertex2f(1,1);
glTexCoord2f(0,1); glVertex2f(0,1);
glEnd();
glPopMatrix();
}
int main (int argc, char ** argv)
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640,480,32,SDL_OPENGL);
SDL_WM_SetCaption("Texture Test", NULL);
glClearColor(0,0,0,0);
glViewport(0, 0, 640, 480);
bool running = true;
while (running == true)
{
SDL_Event event;
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT: running = false; break;
}
}
Draw();
SDL_GL_SwapBuffers();
}
SDL_Quit();
return 0;
}