I'm trying to make a 3D shooter. I think the image is loading because its dimensions are displayed, but it is not displayed as a texture. The program compiles and runs without errors but no texture. I do not know where I am making a mistake.
main.cpp:
#include "Functions.h"
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(wnd_width, wnd_height);
glutInitWindowPosition(300, 100);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutCreateWindow("OpenGL my");
glutDisplayFunc(display);
glutIdleFunc(Idle);
glutSpecialFunc(KeyPressed);
TextureInit();
glutMainLoop();
return 0;
}
functions.h
#define STB_IMAGE_IMPLEMENTATION
#include <GL/glut.h>
#include <stb_image.h>
#include <iostream>
#include "point.h"
#include "camera.h"
int wnd_width=1300;
int wnd_height=900;
GLdouble aspect = wnd_width/wnd_height;
Camera cam;
unsigned int texture;
float text_coords[] = {0,0, 1,0, 1,1, 0,1};
void DrawFloor();
void DrawWall(float x, float width, float height);
void KeyPressed(int key, int x, int y);
void TextureInit();
void display();
void Idle();
void display(){
glClearColor(0.6, 0.8, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, aspect, 0.1, 3);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
cam.view.x, cam.view.y, cam.view.z,
0, 0.5, 0);
glBegin(GL_QUADS);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, text_coords);
//glColor3f(0, 0, 0.7);
DrawFloor();
//glColor3f(0, 0.8, 0.1);
DrawWall(-0.5, 2, 0.7);
DrawWall(0.5, 2, 0.7);
glEnd();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glutSwapBuffers();
}
void Idle(){
}
void DrawFloor(){
glVertex3d(1, 0, 2.5);
glVertex3d(1, 0, 0);
glVertex3d(-1, 0, 0);
glVertex3d(-1, 0, 2.5);
}
void DrawWall(float x, float width, float height){
glVertex3f(x, height, 0);
glVertex3f(x, height, width);
glVertex3f(x, 0, width);
glVertex3f(x, 0, 0);
}
void KeyPressed(int key, int x, int y){
switch (key) {
case GLUT_KEY_UP: { cam.MoveForward(); break; }
case GLUT_KEY_DOWN: { cam.MoveBack(); break; }
case GLUT_KEY_LEFT: {cam.TurnLeft(); break; }
case GLUT_KEY_RIGHT: {cam.TurnRight(); break; }
}
cam.PrintPosition();
glutPostRedisplay();
}
void TextureInit(){
int width, height, cnt;
unsigned char* data = stbi_load("whitemarble.jpg", &width, &height, &cnt, 0);
if(data==nullptr) std::cout<< "NO\n";
else std::cout<<width<<'\t'<<height<<'\n';
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
0, GL_RGB, GL_UNSIGNED_BYTE, data);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
}
redone. Now it works. Thanks. But the picture is very stretched.
#define STB_IMAGE_IMPLEMENTATION
#include <GL/glut.h>
#include <stb_image.h>
#include <iostream>
#include "point.h"
#include "camera.h"
int wnd_width=1300;
int wnd_height=900;
GLdouble aspect = wnd_width/wnd_height;
Camera cam;
unsigned int texture;
float text_coords[] = {0,0, 1,0, 1,1, 0,1};
void DrawFloor();
void DrawWall(float x, float width, float height);
void KeyPressed(int key, int x, int y);
void TextureInit();
void display();
void Idle();
void TextureInit(){
int width, height, cnt;
unsigned char* data = stbi_load("whitemarble.jpg", &width, &height, &cnt, 0);
if(data==nullptr) std::cout<< "NO\n";
else std::cout<<width<<'\t'<<height<<'\n';
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
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_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glBindTexture(GL_TEXTURE_2D, 0);
stbi_image_free(data);
}
void display(){
glClearColor(0.6, 0.8, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, aspect, 0.1, 3);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
cam.view.x, cam.view.y, cam.view.z,
0, 0.5, 0);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glBegin(GL_QUADS);
DrawFloor();
DrawWall(-0.5, 2, 0.7);
DrawWall(0.5, 2, 0.7);
glEnd();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glutSwapBuffers();
}
void DrawFloor(){
glTexCoord2d(0, 0);
glVertex3d(1, 0, 2.5);
glTexCoord2d(1, 0);
glVertex3d(1, 0, 0);
glTexCoord2d(1, 1);
glVertex3d(-1, 0, 0);
glTexCoord2d(0, 1);
glVertex3d(-1, 0, 2.5);
}
void DrawWall(float x, float width, float height){
glVertex3f(x, height, 0);
glVertex3f(x, height, width);
glVertex3f(x, 0, width);
glVertex3f(x, 0, 0);
}
void KeyPressed(int key, int x, int y){
switch (key) {
case GLUT_KEY_UP: { cam.MoveForward(); break; }
case GLUT_KEY_DOWN: { cam.MoveBack(); break; }
case GLUT_KEY_LEFT: {cam.TurnLeft(); break; }
case GLUT_KEY_RIGHT: {cam.TurnRight(); break; }
}
cam.PrintPosition();
glutPostRedisplay();
}
void Idle(){
}```
Your code is causing several Only a subset of GL commands can be used between glBegin and glEnd. You have to call glBegin immediately before specifying the vertices:
void display(){
glClearColor(0.6, 0.8, 1, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, aspect, 0.1, 3);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cam.pos.x, cam.pos.y, cam.pos.z,
cam.view.x, cam.view.y, cam.view.z,
0, 0.5, 0);
// glBegin(GL_QUADS); // <--- DELETE
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glTexCoordPointer(2, GL_FLOAT, 0, text_coords);
glBegin(GL_QUADS); // <--- INSERT
//glColor3f(0, 0, 0.7);
DrawFloor();
//glColor3f(0, 0.8, 0.1);
DrawWall(-0.5, 2, 0.7);
DrawWall(0.5, 2, 0.7);
glEnd();
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glutSwapBuffers();
}
You cannot mix fix function attributes and glBegin/glEnd sequences. Use glTexCoord instead:
glTexCoord2d(0, 0);
glVertex3d(1, 0, 2.5);
glTexCoord2d(1, 0);
glVertex3d(1, 0, 0);
glTexCoord2d(1, 1);
glVertex3d(-1, 0, 0);
glTexCoord2d(0, 1);
glVertex3d(-1, 0, 2.5);
Related
I have created a rotating wired sphere. I have done textures to a cube but sphere seems to be a problem.
I want to add world map as a texture to this sphere. Any suggestions?
#include <iostream>
#include <stdlib.h>
#include <GL/glut.h>
#include <thread>
#include <chrono>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
using namespace std;
GLuint texture;
int start = 1;
GLfloat xRotated, yRotated, zRotated;
GLdouble radius = 1;
void init() {
glOrtho(-1000 / 2, 1000 / 2, -1000 / 2, 1000 / 2, -500, 500);
}
GLuint glInitTexture(char* filename)
{
GLuint t = 0;
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
unsigned char* data = stbi_load(filename, &width, &height, &nrChannels, 0);
glGenTextures(1, &t);
glBindTexture(GL_TEXTURE_2D, t);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
//unsigned char data[] = { 255, 0, 0, 255 };
if (data)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
else
std::cout << "fail";
return t;
}
void drawImage(GLuint file, float x, float y, float w, float h)
{
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glPushMatrix();
//glTranslatef(x, y, 0.0);
//glRotatef(angle, 0.0, 0.0, 1.0);
//glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glBindTexture(GL_TEXTURE_2D, file);
glEnable(GL_TEXTURE_2D);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -4.5);
glRotatef(yRotated, 0.0, 1.0, 0.0);
glEnable(GL_TEXTURE_2D);
GLUquadric *qobj = gluNewQuadric();
gluQuadricTexture(qobj, GL_TRUE);
gluSphere(qobj, radius, 20, 20);
gluDeleteQuadric(qobj);
glDisable(GL_TEXTURE_2D);
glFlush();
yRotated += 0.01;
//glBindTexture(GL_TEXTURE_2D, 0);
glFlush();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
}
//Plots points of both graphs together
//Displays map on screen
void drawMap() {
std::cout << "\nDraw map\n";
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet(GLUT_WINDOW_WIDTH);
const double h = glutGet(GLUT_WINDOW_HEIGHT);
gluPerspective(90.0, w / h, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -15);
for (int i = 0; i < 10000; i++) {
glClear(GL_DEPTH_BUFFER_BIT);
drawImage(texture, 0, 0, 100, 200);
//std::this_thread::sleep_for(std::chrono::milliseconds(500));
}
glutSwapBuffers();
glEnd();
glFlush();
}
void render()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//glFlush();
glPointSize(5);
glColor3f(1, 1, 1);
glBegin(GL_LINES);
glVertex3f(-450, -450, 10);
glVertex3f(-450, -250, 10);
glEnd();
glBegin(GL_LINES);
glVertex3f(-450, -450, 10);
glVertex3f(-250, -450, 10);
glEnd();
drawMap();
//plotPoints();
glFlush();
}
void Kbevent(unsigned char key, int x, int y) {
if (key == 's') {
start = start % 2;
glutPostRedisplay();
}
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1560, 810);
glutCreateWindow("Applying Textures");
init();
xRotated = yRotated = zRotated = 30.0;
xRotated = 33;
yRotated = 40;
char fn[] = "map.jpg";
texture = glInitTexture(fn);
glutDisplayFunc(render);
//glutReshapeFunc(reshapeFunc);
//glutIdleFunc(idleFunc);
glutKeyboardFunc(Kbevent);
glutMainLoop();
return 0;
}
The problem with applying a 2D texture is that when you wrap a 2D texture onto a sphere, the top and bottom area of the sphere, the texture looks squeezed.
I suggest to use gluSphere and gluQuadricTexture rather than glutSolidSphere. e.g:
glEnable(GL_TEXTURE_2D);
GLUquadric *qobj = gluNewQuadric();
gluQuadricTexture(qobj, GL_TRUE);
gluSphere(qobj, radius, 20, 20);
gluDeleteQuadric(qobj);
glDisable(GL_TEXTURE_2D);
For an continuously rotation, you have increment the rotation angle and to continuously update the window (glutPostRedisplay). e.g.:
void redisplayFunc(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -4.5);
glRotatef(yRotated, 0.0, 1.0, 0.0);
glEnable(GL_TEXTURE_2D);
GLUquadric *qobj = gluNewQuadric();
gluQuadricTexture(qobj, GL_TRUE);
gluSphere(qobj, radius, 20, 20);
gluDeleteQuadric(qobj);
glDisable(GL_TEXTURE_2D);
glFlush();
yRotated += 1;
glutPostRedisplay();
}
I am struggling with loading src/up.bmp and showing it in 2D overlaid on my 3D environment.
But nothing does appear. Where is my mistake?
#define SDL_MAIN_HANDLED
#include <math.h>
#include <SDL.h>
#include <iostream>
#include <GL/freeglut.h>
using namespace std;
int w_width = 800;
int w_height = 500;
string w_title = "Model viewer";
float line_width = 2.0f;
float camera_radius = 100.0f;
float axis_size = 20.0;
SDL_Surface* button1;
GLuint TextureID;
void draw_button(SDL_Surface* button)
{
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, button->w, button->h, GL_RGB, GL_UNSIGNED_BYTE, button->pixels);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
const float ratio = (float)glutGet(GLUT_SCREEN_WIDTH) / (float)glutGet(GLUT_SCREEN_HEIGHT);
static double t = 0.0;
const float deg2rad = 3.1415926f / 180.0f;
float cam_x = camera_radius*float(cos(20.0f*deg2rad))*float(cos(0.5235*deg2rad));
float cam_y = camera_radius*float(sin(20.f*deg2rad))*float(cos(0.5235*deg2rad));
float cam_z = camera_radius*float(sin(0.5235*deg2rad));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, ratio, 0.01, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cam_x, cam_y, cam_z, 0, 0, 0, 0, 0, 1);
// sphere
glPushMatrix();
glColor3ub(255, 0, 0);
glTranslated(
0,
0,
0);
glutSolidSphere(1, 20, 20);
glPopMatrix();
draw_button(button1);
glFlush();
glutSwapBuffers();
}
void timer_event(int value)
{
glutPostRedisplay();
glutTimerFunc(100, timer_event, value);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
glutTimerFunc(30, timer_event, 1);
glutInitWindowSize(w_width, w_height);
glutInitWindowPosition(10, 10);
glutCreateWindow(argv[0]);
glutSetWindowTitle(w_title.c_str());
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glutDisplayFunc(display);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
button1 = SDL_LoadBMP("src/up.bmp");
if (button1 == NULL)
{
cout << "Image not found" << endl;
exit(1);
}
glutMainLoop();
SDL_FreeSurface(button1);
return 0;
}
Here is a minimal example I can come up with:
#define SDL_MAIN_HANDLED
#include <math.h>
#include <SDL.h>
#include <iostream>
#include <GL/gl.h>
#include <GL/freeglut.h>
using namespace std;
int w_width = 800;
int w_height = 500;
string w_title = "Model viewer";
float line_width = 2.0f;
float camera_radius = 100.0f;
float axis_size = 20.0;
SDL_Surface* button1;
GLuint TextureID;
static void prepare_texture(SDL_Surface *button)
{
glGenTextures(1, &TextureID);
glBindTexture(GL_TEXTURE_2D, TextureID);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// convert surface to RGB8
SDL_Surface *rgb = SDL_CreateRGBSurface(0, button->w, button->h, 24, 0xff, 0xff00, 0xff0000, 0);
SDL_BlitSurface(button, NULL, rgb, NULL);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, button->w, button->h, GL_RGB, GL_UNSIGNED_BYTE, rgb->pixels);
SDL_FreeSurface(rgb);
}
static void draw_button()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glBindTexture(GL_TEXTURE_2D, TextureID);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(0.1f, 0.1f);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.1f, 0.2f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f(0.2f, 0.2f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f(0.2f, 0.1f);
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
const float ratio = (float)glutGet(GLUT_SCREEN_WIDTH) / (float)glutGet(GLUT_SCREEN_HEIGHT);
static double t = 0.0;
const float deg2rad = 3.1415926f / 180.0f;
float cam_x = camera_radius*float(cos(20.0f*deg2rad))*float(cos(0.5235*deg2rad));
float cam_y = camera_radius*float(sin(20.f*deg2rad))*float(cos(0.5235*deg2rad));
float cam_z = camera_radius*float(sin(0.5235*deg2rad));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90, ratio, 0.01, 10000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(cam_x, cam_y, cam_z, 0, 0, 0, 0, 0, 1);
// sphere
glPushMatrix();
glColor3ub(255, 0, 0);
glTranslated(
0,
0,
0);
glutSolidSphere(1, 20, 20);
glPopMatrix();
draw_button();
glFlush();
glutSwapBuffers();
}
void timer_event(int value)
{
glutPostRedisplay();
glutTimerFunc(100, timer_event, value);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutSetOption(GLUT_MULTISAMPLE, 8);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
glutTimerFunc(30, timer_event, 1);
glutInitWindowSize(w_width, w_height);
glutInitWindowPosition(10, 10);
glutCreateWindow(argv[0]);
glutSetWindowTitle(w_title.c_str());
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glutDisplayFunc(display);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_TEXTURE_2D);
button1 = SDL_LoadBMP("up.bmp");
if (button1 == NULL)
{
cout << "Image not found" << endl;
exit(1);
}
prepare_texture(button1);
SDL_FreeSurface(button1);
glutMainLoop();
return 0;
}
Note identity projection, which maps screen to [-1,1] range; you may want something more like gluOrtho2D.
Idea:
I have been trying to develop code which can take several (ten) slices of 2D images and render them as a 3D texture. I have so far used glTexImage3D and glTexSubImage3D upon suggestions from my previous post here: OpenGL - 'glTexSubImage3D': identifier not found
I have been based my work off of NeHe's texture mapping tutorial here: http://nehe.gamedev.net/tutorial/lesson_06_texturing_update/47002/
The flow of function calls in the NeHe's tutorial in main() proceeds like this: CreateGLWindow(), InitGL(), LoadGLTextures(), DrawGLScene(). I have only made changes to the code commencing upwards from DrawGLScene() while, everything below that function is the same as in my code.
Problem:
Everything in the code seemingly looks right but, there is nothing being rendered on the screen. I have spent two days on this trying to get it to work to no avail. What am I missing? Is there something that I am doing incorrectly?
EDITED CODE
#include "windows.h"
#include "stdio.h"
#include "gl\gl.h"
#include "gl\glu.h"
#include "GLext.h"
#include "SOIL.h"
HDC hDC=NULL;
HGLRC hRC=NULL;
HWND hWnd=NULL;
HINSTANCE hInstance;
bool keys[256];
bool active=TRUE;
bool fullscreen=TRUE;
GLfloat xrot;
GLfloat yrot;
GLfloat zrot;
GLuint m_nTexId;
unsigned char tex;
int h = 1024;
int w = 256;
int slices = 10;
GLfloat dOrthoSize = 1.0f;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
// EDIT HERE
PFNGLTEXIMAGE3DPROC TexImage3D;
PFNGLTEXSUBIMAGE3DPROC TexSubImage3D;
PFNGLCOPYTEXSUBIMAGE3DPROC CopyTexSubImage3D;
int LoadGLTextures()
{
glGenTextures(1,(GLuint*)&m_nTexId );
if(m_nTexId == 0)
return false;
glBindTexture( GL_TEXTURE_3D, m_nTexId );
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_BORDER);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
return true;
}
GLvoid ReSizeGLScene(GLsizei width, GLsizei height)
{
if (height==0)
{
height=1;
}
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int InitGL(GLvoid)
{
if (!LoadGLTextures())
{
return FALSE;
}
glEnable(GL_TEXTURE_3D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
// EDIT HERE
TexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
TexSubImage3D = (PFNGLTEXSUBIMAGE3DPROC) wglGetProcAddress("glTexSubImage3D");
CopyTexSubImage3D = (PFNGLCOPYTEXSUBIMAGE3DPROC) wglGetProcAddress("glCopyTexSubImage3D");
return TRUE;
}
#define MAP_3DTEXT( TexIndex ) \
glTexCoord3f(0.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(-dOrthoSize,-dOrthoSize,TexIndex);\
glTexCoord3f(1.0f, 0.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(dOrthoSize,-dOrthoSize,TexIndex);\
glTexCoord3f(1.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(dOrthoSize,dOrthoSize,TexIndex);\
glTexCoord3f(0.0f, 1.0f, ((float)TexIndex+1.0f)/2.0f); \
glVertex3f(-dOrthoSize,dOrthoSize,TexIndex);
int DrawGLScene(GLvoid)
{
PFNGLTEXIMAGE3DPROC glTexImage3D = (PFNGLTEXIMAGE3DPROC) wglGetProcAddress("glTexImage3D");
PFNGLTEXSUBIMAGE3DPROC glTexSubImage3D = (PFNGLTEXSUBIMAGE3DPROC) wglGetProcAddress("glTexSubImage3D");
PFNGLCOPYTEXSUBIMAGE3DPROC glCopyTexSubImage3D = (PFNGLCOPYTEXSUBIMAGE3DPROC) wglGetProcAddress("glCopyTexSubImage3D");
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//glEnable( GL_ALPHA_TEST );
//glAlphaFunc( GL_GREATER, 0.2f );
//glEnable(GL_BLEND);
//glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt(0,0,-300,0,0,1,0,1,0);
glEnable(GL_TEXTURE_3D);
glBindTexture( GL_TEXTURE_3D, m_nTexId );
TexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, w, h , slices, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
glTranslatef( 0.0f, 0.0f, 100.0f );
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170090.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 1, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.0f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170091.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 2, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.1f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170092.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 3, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.2f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170093.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 4, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.3f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170094.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 5, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.4f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char)SOIL_load_image("Data/PA_170095.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 6, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.5f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170096.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 7, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.6f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170097.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 8, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.7f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170098.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 9, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.8f );
glEnd();
glBegin(GL_QUADS);
tex = (unsigned char) SOIL_load_image("Data/PA_170099.png", &w, &h, NULL, 0);
TexSubImage3D(GL_TEXTURE_3D, 0, 0, 0, 10, w, h, 1, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*) tex);
MAP_3DTEXT( 0.9f );
glEnd();
return TRUE;
}
Look at the function call TexSubImage3D, the argument depth is always 1, and you are incrementing only zoffset. Please keep the zoffset 0 and provide the depth/layer values from 8th argument (depth).
TexSubImage3D( enum target, int level, int xoffset,
int yoffset, int zoffset, sizei width, sizei height,
sizei depth, enum format, enum type, const
void *data );
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.
I've written a program for testing rendering a texture with the framebuffer. In the function render_texture() I want to render a triangle on to a texture, but when I display the rendered texture in the display function, I get just a simple yellow square.
#include "GL/glew.h"
#include "GL/glext.h"
#include "GL/glu.h"
#include "GL/freeglut.h"
#include <cstdio>
uint16_t tex_width = 75;
uint16_t tex_height = 24;
GLuint texture;
int w1;
int h1;
void orthogonalStart (int w, int h) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glScalef(1, -1, 1);
glTranslatef(0, -h1, 0);
glMatrixMode(GL_MODELVIEW);
}
void orthogonalEnd (void) {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
orthogonalStart(w1, h1);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(125, 125);
glTexCoord2f(0, 1); glVertex2f(125, 125+tex_height);
glTexCoord2f(1, 1); glVertex2f(125+tex_width, 125+tex_height);
glTexCoord2f(1, 0); glVertex2f(125+tex_width, 125);
glEnd();
orthogonalEnd();
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
w1 = w;
h1 = h;
glMatrixMode (GL_MODELVIEW);
}
void render_texture()
{
GLuint framebuffer = 0;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
GLenum draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, draw_buffers);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
fprintf(stderr, "[render_texture] Fehler im Framebuffer\n");
exit(1);
}
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glViewport(0, 0, tex_width, tex_height);
glClearColor (0.0,0.0,0.0,1.0);
orthogonalStart(tex_width, tex_height);
glColor4f(1, 1, 0, 0);
glBegin(GL_TRIANGLES);
glVertex2f(0.f,0.f);
glVertex2f(tex_width, 0.0f);
glVertex2f(tex_width, (GLfloat)tex_height/2.f);
//glVertex2f(tex_width-(GLfloat)tex_height/2.f, tex_height);
//glVertex2f(0, tex_height);
glEnd();
orthogonalEnd();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glewExperimental=true;
GLenum err=glewInit();
if(err!=GLEW_OK)
{
//Problem: glewInit failed, something is seriously wrong.
fprintf(stderr, "glewInit failed, aborting.\n");
}
render_texture();
glutMainLoop ();
return 0;
}
Edit 1: I've updated the code, now I have just a blank black screen
#include "GL/glew.h"
#include "GL/glext.h"
#include "GL/glu.h"
#include "GL/freeglut.h"
#include <cstdio>
uint16_t tex_width = 75;
uint16_t tex_height = 24;
GLuint texture;
void orthogonalStart (int w, int h) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
glScalef(1, -1, 1);
glTranslatef(0, -glutGet(GLUT_WINDOW_HEIGHT), 0);
glMatrixMode(GL_MODELVIEW);
}
void orthogonalEnd (void) {
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
orthogonalStart(glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT));
glColor3f(1,1,1);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex2f(125, 125);
glTexCoord2f(0, 1); glVertex2f(125, 125+tex_height);
glTexCoord2f(1, 1); glVertex2f(125+tex_width, 125+tex_height);
glTexCoord2f(1, 0); glVertex2f(125+tex_width, 125);
glEnd();
glDisable(GL_TEXTURE_2D);
orthogonalEnd();
glutSwapBuffers();
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0);
glMatrixMode (GL_MODELVIEW);
}
void render_texture()
{
GLuint framebuffer = 0;
glGenFramebuffers(1, &framebuffer);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex_width, tex_height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
GLenum draw_buffers[1] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, draw_buffers);
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
{
fprintf(stderr, "[render_texture] Fehler im Framebuffer\n");
exit(1);
}
glViewport(0, 0, tex_width, tex_height);
glBindFramebuffer(GL_FRAMEBUFFER, framebuffer);
orthogonalStart(tex_width, tex_height);
glColor4f(1, 1, 0, 0);
glBegin(GL_TRIANGLES);
glVertex2f(0.f,0.f);
glVertex2f(tex_width, 0.0f);
glVertex2f(tex_width, (GLfloat)tex_height/2.f);
//glVertex2f(tex_width-(GLfloat)tex_height/2.f, tex_height);
//glVertex2f(0, tex_height);
glEnd();
orthogonalEnd();
glBindFramebuffer(GL_FRAMEBUFFER, 0);
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitContextVersion(3, 1);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize (500, 500);
glutCreateWindow ("");
glutDisplayFunc (display);
glutIdleFunc (display);
glutReshapeFunc (reshape);
glewExperimental=true;
GLenum err=glewInit();
if(err!=GLEW_OK)
{
//Problem: glewInit failed, something is seriously wrong.
fprintf(stderr, "glewInit failed, aborting.\n");
}
//glEnable(GL_TEXTURE_2D);
render_texture();
glutMainLoop ();
return 0;
}
You are clearly doing something wrong in the setup of your projection matrix.
I'm not sure why you are calling glScalef() and glTranslatef(), if you comment out these lines in your orthogonalStart() function, you'll have your triangle.
void orthogonalStart (int w, int h) {
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, w, 0, h);
//glScalef(1, -1, 1);
//glTranslatef(0, -glutGet(GLUT_WINDOW_HEIGHT), 0);
glMatrixMode(GL_MODELVIEW);
}
On a sidenote, there is no need to call glBindFramebuffer(GL_FRAMEBUFFER, framebuffer) a 2nd time in your render_texture() function call.
Also, I would recommend you to start learning about vertex buffers to draw your objects, which is more elaborated here.