Related
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);
I've already constructed a 15x15 grid of cubes with glutSolidCube(). Then i have a menu handler in which when I click "Start Game", loads the texture I used to all of the cubes, calling a custom glutSolidCube and having glTexCoord2d before each declaration of vertices, cause we can't have textures on the latter I think. For uploading the texture from an image, I'm using a STB_IMAGE_IMPLEMENTATION implementation having also a header file included. The function loadTextureFromFile(const char *filename) does the loading part.
How can I upload more textures (I want 2 more, in the same loadTextureFromFile() function preferably) and how to handle each texture with the glTexCoord2d()?
Here's my entire code:
#include<iostream>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <GL/gl.h> // openGL header
#include <GL/glu.h> // glut header
#include <GL/glut.h> // glut header
#define STB_IMAGE_IMPLEMENTATION
/////////////////////////////////////Textures==============================================/////////////////////////////////////
#include "stb_image.h"
GLuint texture; //the array for our texture
void loadTextureFromFile(const char *filename)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
//glShadeModel(GL_FLAT);
//glEnable(GL_DEPTH_TEST);
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
// set the texture wrapping/filtering options (on the currently bound texture object)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
unsigned char *data = stbi_load("paper.bmp", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
//glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
void FreeTexture(GLuint texture)
{
glDeleteTextures(1, &texture);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static void
drawBox(GLfloat size, GLenum type)
{
static GLfloat n[6][3] =
{
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, -1.0}
};
static GLint faces[6][4] =
{
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
GLfloat v[8][3];
GLint i;
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -size / 2;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = size / 2;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -size / 2;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = size / 2;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = -size / 2;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = size / 2;
for (i = 5; i >= 0; i--) {
glBegin(type);
glNormal3fv(&n[i][0]);
glTexCoord2d(0.0,0.0);
glVertex3fv(&v[faces[i][0]][0]);
glTexCoord2d(0.0,1.0);
glVertex3fv(&v[faces[i][1]][0]);
glTexCoord2d(1.0,1.0);
glVertex3fv(&v[faces[i][2]][0]);
glTexCoord2d(1.0,0.0);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}
void APIENTRY
myglutSolidCube(GLdouble size)
{
drawBox(size, GL_QUADS);
}
//int red_color[]={255,0,0};
//int blue_colot[]={0,0,255};
//////////////////////////////=========MENU============/////////////
enum MENU_TYPE //menu options-values
{
MENU_START,
MENU_EXIT,
};
//create the menu - Prototype
void my_createmenu(void);
// Menu handling function declaration - Prototype
void menu(int);
void init()
{ //for 3d lighting
glEnable(GL_DEPTH_TEST); //depth test
glEnable(GL_LIGHTING); //enable light from a single source
glEnable(GL_LIGHT0); //enable white light , diffuse and specular components
glEnable(GL_COLOR_MATERIAL); //track the current color
}
void display()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Black and opaque
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//define the projection matrix just once and use the modelview matrix all other times
glMatrixMode(GL_PROJECTION); //Applies subsequent matrix operations to the projection matrix stack
glLoadIdentity();//Reset
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport); //The params parameter returns four values: the x and y window coordinates of the viewport, followed by its width and height
double aspect = (double)viewport[2] / (double)viewport[3]; // y/width would be 1.0
gluPerspective(60,aspect, 1, 100); //using perspective projection
//gluOrtho2D(0.0,600.0,-60.0,600.0);
glMatrixMode(GL_MODELVIEW); //for trasformations - Applies subsequent matrix operations to the texture matrix stack
glLoadIdentity();
// move back a bit for viewer , cause of gluPerspective
glTranslatef( 0, 0, -35 );
float e=0,f=0;
//construct the grid with reference the central cube
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
glPushMatrix();
glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
glColor3ub(245, 245, 220); //Beige
glutSolidCube(2.25);
glPopMatrix();
f += -2.63;
}
f=0;
e+=2.63;
}
glutSwapBuffers(); //implicit glFlush
}
//for the second part of program
void display_game()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Black and opaque
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//define the projection matrix just once and use the modelview matrix all other times
glMatrixMode(GL_PROJECTION); //Applies subsequent matrix operations to the projection matrix stack
glLoadIdentity();//Reset
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport); //The params parameter returns four values: the x and y window coordinates of the viewport, followed by its width and height
double aspect = (double)viewport[2] / (double)viewport[3]; // y/width would be 1.0
gluPerspective(60,aspect, 1, 100); //using perspective projection
//glOrtho(0.0f, 600.0f, 600.0f, 0.0f, 0.0f, 1.0f);
glMatrixMode(GL_MODELVIEW); //for trasformations - Applies subsequent matrix operations to the texture matrix stack
glLoadIdentity();
// move back a bit for viewer , cause of gluPerspective
glTranslatef( 0, 0, -35 );
float e=0,f=0;
glEnable(GL_TEXTURE_2D);
//construct the grid with reference the central cube
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
glPushMatrix();
glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
//glColor3ub(245, 245, 220); //Beige
//glColor3ub( rand()%255,rand()%255, rand()%255 );
myglutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
//glColor3ub( rand()%255,rand()%255, rand()%255 );
myglutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glPushMatrix();
glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
f += -2.63;
}
f=0;
e+=2.63;
}
//SEED to some constant value for 2nd part of the program. If it is not used , cubes would change color in runtime
//srand(0x98765432);
glutSwapBuffers(); //implicit glFlush
glDisable(GL_TEXTURE_2D);
}
void reshape(GLsizei width, GLsizei height) {
// GLsizei for non-negative integer // Compute aspect ratio of the new window
if (height == 0) height = 1; // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height; // Set the viewport to cover the new window
glViewport(0, 0, width, height); // Set the aspect ratio of the clipping volume glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
glLoadIdentity(); // Reset // Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
void mouseEscape( int button, int state, int x, int y )
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN &&button==MENU_EXIT)
{
int windowID = glutCreateWindow("CUBES");
glutDestroyWindow(windowID);
exit(0);
}
glutPostRedisplay();
}
//for loading the texture
const char* filename = "salt_on_spoon.bmp";
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(600,600);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE| GLUT_MULTISAMPLE);
glEnable(GL_MULTISAMPLE); //anti-alliasing
glutCreateWindow("CUBES");
//create and handle the menu
my_createmenu();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
init();
//glEnable(GL_TEXTURE_2D); //for texture
//glutMouseFunc(mouseEscape);
//glutKeyboardFunc(keyEscape);
//Load our texture
//loadTextureFromFile(filename);
glutMainLoop();
//Free our texture
//FreeTexture(texture);
return 0;
}
//create the menu-entries
void my_createmenu(void) {
// Create a menu
glutCreateMenu(menu);
// Add menu items
glutAddMenuEntry("Start Game", MENU_START);
glutAddMenuEntry("Exit", MENU_EXIT);
// Associate a mouse button with menu
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
// Menu handling function-what to do in each value
void menu(int item)
{
switch (item)
{
case MENU_START: {
//glEnable(GL_TEXTURE_2D); //for texture
//Load our texture
loadTextureFromFile(filename);
glutDisplayFunc(display_game);
}
break;
case MENU_EXIT:
{
int windowID = glutCreateWindow("CUBES"); //exit game
glutDestroyWindow(windowID);
exit(0);
}
break;
default:
{ /* Nothing */ }
break;
}
glutPostRedisplay();
return;
}
I'm doing the texture loading part in the menu function. How will I be able to handle three textures? The ultimate goal is to make a rand call also for the three textures to be rendered on the cubes.
I also have two pictures: 1st: when the program begins:
2nd: after clicking "Star Game" where you can see the texture rendered in all of the cubes:
The goal is for more 2 types of textures and all of them render in random cubes.
You can create more than 1 texture object.
glBindTexture binds a named texture to a texturing target, that is a global state. glTexImage2D specify a two-dimensional texture image for the texture, which is currently bound to the specified target. glTexParameter set parameter to the texture object.
I recommend to write a function which loads a texture form a file to a given texture object (name id):
void loadTextureFromFile(const char *filename, unsigned int texture)
{
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load and generate the texture
int width, height, nrChannels;
unsigned char *data = stbi_load(filename, &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0,
GL_RGB, GL_UNSIGNED_BYTE, data);
//glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
}
const char* filename1 = "salt_on_spoon.bmp";
const char* filename2 = ...;
const char* filename3 = ...;
unsigned int tob[3];
int main(int argc, char **argv)
{
// [...]
glGenTextures(3, &tob[0]);
loadTextureFromFile(filename1, tob[0]);
loadTextureFromFile(filename2, tob[1]);
loadTextureFromFile(filename3, tob[2]);
// [...]
}
When two-dimensional texturing is enabled, then the image of the texture object, which is currently bound to the target GL_TEXTURE_2D is wrapped on the mesh.
You've to bind the proper texture object, before you draw the geometry. e.g:
for(int i=0;i<8;i++) {
for(int j=0;j<8;j++) {
glBindTexture(GL_TEXTURE_2D, tob[0]);
glPushMatrix();
glTranslatef(0.0f+e,0.0f+f,0.0f); //right and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, tob[1]);
glPushMatrix();
glTranslatef(0.0f-e,0.0f+f,0.0f); //left and below
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, tob[2]);
glPushMatrix();
glTranslatef(0.0f+e,0.0f-f,0.0f); //right and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
glBindTexture(GL_TEXTURE_2D, tob[0]);
glPushMatrix();
glTranslatef(0.0f-e,0.0f-f,0.0f); //left and up
glRotatef(20.0f,1.0f,-2.0f,0.0f); //looking 3d
myglutSolidCube(2.25);
glPopMatrix();
f += -2.63;
}
f=0;
e+=2.63;
}
Note, the distribution of the textures is just an example. You've to ensure that unsigned int tob[3]; is declared before, in global namespace.
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.
I am implementing a 3d game in OpenGL but I'm stuck on collision detection, game contains multiple pendulums and a cannon which shoots pendulum like:
I try to implement collision detection by taking distance between two objects:
dx=ax-bx
dy=ay-by
dz=az-bz
distance = sqrt(dx*dx + dy*dy + dz*dz);
where ax,ay,az is pendulum points and bx,by,bz is cannon bomb point.
distance<=A_radius+B_radius for collision detection checking distance less then determine collision detection occur.
But unfortunately this logic did not work.
Pendulum is made using gluSphere and gluCylinder and this code here
glPushMatrix();
glTranslated(0,0,40);
drawCanon();
glPopMatrix();
glPushMatrix();
glTranslatef(600, 0, -410);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(bx, by, bz);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
//GLUquadricObj *ctrees;
for (int i = 0; i < allPendulum.size(); i++)
{
glPushMatrix();
glTranslatef(400, 50, -400);
allPendulum[i].drawRope();
glPopMatrix();
glPushMatrix();
glTranslatef(400, 50, -400);
allPendulum[i].drawPend();
glPopMatrix();
}
drawCanon code is here
void drawCanon()
{
glPushMatrix();
glTranslatef(600, 0, -380);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(600, 0, -410);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(300, 0, -40);
//glRotatef(rorate, 0, 0, 0);
drawWheelAttacher();
glPopMatrix();
//canon fire with bomb
glPushMatrix();
glTranslatef(605, 3, -390);
glRotatef(longFire, xRotate, yRotate, zRotate);
//glTranslatef(0, 0, -25);
drawFireHolder();
//printf_s("Long fire is %d", longFire);
glPopMatrix();
glPushMatrix();
glTranslatef(605, 3, -390);
glRotatef(longFire, xRotate, yRotate, zRotate);
glTranslatef(0, 0, bombX);
glTranslatef(0, 0, 25);
drawBomb();
glPopMatrix();
}
Complete code if any one want is here
#include <iostream>
#include <fstream>
#include <math.h>
#include <glut.h>
#include <glut.h>
#include "Turtle.h"
#include "cube.h"
#include"pixMap.h"
#include "pendulum.h"
#include "vector"
void timer(int);
void drawRope(void);
void drawBob(void);
void drawLongPipe(void);
void initializePendulum(void);
void addPendulumInVector(void);
void drawCanon(void);
void drawCar(void);
void drawWheel(void);
void drawWheelAttacher(void);
void drawFireHolder(void);
void drawBomb(void);
void mouseControl(int x,int y);
void mouseClick(int button, int state, int x, int y);
void specialKeyHandler(int key, int x, int y);
void handlerMove();
RGBpixmap pix[5];
int zaxis = -300;
int xaxis=0, yaxis;
int rorate = 0;
bool check = false;
int carX = 0, carY, carZ;
int bombX=0;
void key(unsigned char key, int x, int y);
void drawFlorr();
float L = 100;
int longFire = 200;
bool fire = false;
const int screenWidth = 1000; // width of screen window in pixels
const int screenHeight = 1000; // height of screen window in pixels
float ww = 800;
float wh = 800;
float f = 520, n = 10.0;
static GLdouble ort1[] = { -200, 200, -33, 140 };
//static GLdouble viewer[] = { 525, 25, -180 };
static GLdouble viewer[] = { 729, 25, -334 };
// 729, 25, -334 ,525, 25 ,-350
static GLdouble up[] = { 0, 1, 0 };
static GLdouble objec[] = { 525, 25, -350 };
//
//static GLdouble objec[] = { 605.0, 0, -300 };
float x, y = 0.0, z, z1;
float xmax = screenWidth - 200.0;
float zmax = screenWidth - 200.0;
float xmin, zmin;
float step = 10.0;
float bx = bombX, by = 0, bz = 450;
float dx, dy, dz;
float fov = 39; // previous 80
using std::cout;
using std::fstream;
using std::ios;
#define PI 3.1415926535898
pendulum Pendulum,PedulumTwo,PendulumThree;
std::vector<pendulum> allPendulum;
float camAngle = 10;
int xRotate = 0, yRotate = 0, zRotate = 1;
void myInit(void)
{
//glClearColor(0.0,0.0,0.0,0.0); // background color is white
//glPointSize(2.0); // a 'dot' is 2 by 2 pixels
//glMatrixMode(GL_PROJECTION);
//glLoadIdentity();
gluOrtho2D(0.0, screenWidth, 0.0, screenHeight);//dino window
//gluOrtho2D(1.0, 1.0, 1.0,1.0);//house window
//gluOrtho2D(1.0, 1.0, 1.0, 1.0);//bird window
glViewport(0, 0, screenWidth, screenHeight);
}
void myDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
//glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluLookAt(viewer[0], viewer[1], viewer[2], objec[0], objec[1], objec[2], 0, 1, 0);
glMatrixMode(GL_PROJECTION);
//glOrtho(-1, 1, -1, 1, -1, 100);
glLoadIdentity();
gluPerspective(fov, 1.333, n, f);
//gluPerspective(90, 1, 1.333, 1000);
//gluPerspective(50, screenWidth / screenHeight, 0.000001, 2000);
glPointSize(2.0);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(viewer[0], viewer[1], viewer[2]); // Translation to the camera center
glRotatef(-camAngle * 57.2957795, 0, 1, 0); // Rotate to correspond to the camera
glTranslatef(0.016, 0, -0.05); // Offset to draw the object
//glutWireCone(0.005, 0.9, 20, 20);
glPopMatrix();
//cube.drawFace(10, 20, 10, 22);
drawFlorr();
glPushMatrix();
//glTranslated(0, 0, 40);
drawPipe();
glPopMatrix();
glPushMatrix();
glTranslated(0,0,40);
drawCanon();
glPopMatrix();
/*
glPushMatrix();
glTranslatef(600, 0, -410);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(bx, by, bz);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
*/
// drawWheelAttacher();
//Line.turn(90);
/*
*/
//glLineWidth(10.0);
//Turtle Line(Point2(600, 70, -300), -90);
//glColor3f(1, 0, 0);
//Line.forward(1, 1);
//Line.turnTo(-45);
//Line.forward(100, 1);
//drawRope();
//GLUquadricObj *ctrees;
for (int i = 0; i < allPendulum.size(); i++)
{
glPushMatrix();
glTranslatef(400, 50, -400);
allPendulum[i].drawRope();
glPopMatrix();
glPushMatrix();
glTranslatef(400, 50, -400);
allPendulum[i].drawPend();
glPopMatrix();
}
//drawEveryThing();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // set display mode
glutInitWindowSize(screenWidth, screenHeight); // set window size
glutInitWindowPosition(10, 10); // set window position on screen
glEnable(GL_DEPTH_TEST);
glutCreateWindow("Dino Line Drawing"); // open the screen window
glutDisplayFunc(myDisplay); // register redraw function
myInit();
glEnable(GL_TEXTURE_2D);
pix[0].readBMPFile("grass.bmp");
pix[0].setTexture(2001);
pix[1].readBMPFile("wood.bmp");
pix[1].setTexture(2002);
pix[2].readBMPFile("ROPE.bmp");
pix[2].setTexture(2003);
pix[3].readBMPFile("bob.bmp");
pix[3].setTexture(2004);
glutKeyboardFunc(key);
initializePendulum();
addPendulumInVector();
glutTimerFunc(1,timer,1);
glutMouseFunc(mouseClick);
glutPassiveMotionFunc(mouseControl);
glutSpecialFunc(specialKeyHandler);
glutMainLoop(); // go into a perpetual loop
return 1;
}
void timer(int t)
{
//CarX + cos(someangle)*(distance from car)
if (fire == true)
{
bx = 0;
bz = 415-bombX;
dx = 400 - 605-bombX;
dy = 50 - 3;
dz = 400 +bz;
float distance = sqrt(dx*dx + dy*dy + dz*dz);
printf("DISTANCE IS %f",distance);
// distance=floor(distance);
if (distance <=12)
{
printf("Collision Occur");
}
if (distance <= (12))// radius is the radus of our bounding sphere for each object
{
printf("Collision Occur");
}
bombX++;
}
else
{
rorate++;
}
for (int i = 0; i < allPendulum.size(); i++)
{
allPendulum[i].changeDirection();
}
glutTimerFunc(10, timer, t);
zaxis++;
glutPostRedisplay();
}
void drawHook()
{
//glColor3f(1.0f, 0.0f, 0.0f); // drawing color is black
glPointSize(5.0);
}
void drawFlorr()
{
glEnable(GL_TEXTURE_2D);
glColor3ub(255,255,255);
xmin = -100;
zmin = -100;
// glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
for (x = xmin; x < xmax; x += step)
{
for (z = zmin; z < zmax; z += step)
{
z1 = -z;
glBindTexture(GL_TEXTURE_2D, 2001);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex3f(x, y, z1);
glTexCoord2f(1, 0);
glVertex3f(x, y, z1-step);
glTexCoord2f(0, 1);
glVertex3f(x + step, y, z1 - step);
glTexCoord2f(1, 1);
glVertex3f(x+step, y, z1);
glEnd();
}
}
glDisable(GL_TEXTURE_2D);
}
void key(unsigned char key, int x, int y)
{
if (key == 't' || key == 'T')
{
xRotate = 1, yRotate = 0, zRotate = 0;
handlerMove();
}
if (key == 'r' || key == 'R')
{
xRotate = 0, yRotate = 1, zRotate = 0;
handlerMove();
}
if (key == 'x' && up[0] == 0)
{
viewer[0] -= 1.0;
}
if (key == 'X' && up[0] == 0)
{
viewer[0] += 1.0;
}
if (key == 'y' && up[0] == 0)
{
viewer[1] -= 1.0;
}
if (key == 'Y' && up[0] == 0)
{
viewer[1] += 1.0;
}
if (key == 'z' && up[0] == 0)
{
viewer[2] += 1.0;
}
if (key == 'Z' && up[0] == 0)
{
viewer[2] -= 1.0;
}
// zomm changing
if (key == 'v')
{
fov+=1.0;
if (fov > 180)
{
fov = 180.0;
}
}
if (key == 'V')
{
fov -= 1.0;
if (fov < 0)
{
fov = 0;
}
}
//change near clipping
if (key == 'f')
{
f -= 1.0;
}
if (key == 'F')
{
f += 1.0;
}
if (key == 'l')
{
carX++;
}
if (key == 'k')
{
carX--;
}
if (key == 'o')
{
carY++;
}
if (key == 'm')
{
carY--;
}
glutPostRedisplay();
}
void drawEveryThing()
{
drawFlorr();
drawLine();
glPushMatrix();
//glTranslatef(0, 0, -500);
drawHouse();
glPopMatrix();
glPushMatrix();
glTranslatef(300.0, 0, -500);
drawMountain(96, 150);
glPopMatrix();
glPushMatrix();
glTranslatef(380.0, 0, -500);
drawMountain(90, 100);
glPopMatrix();
glPushMatrix();
glTranslatef(400.0, 0, -300);
drawTrees(3,20);
glPopMatrix();
glPushMatrix();
glTranslatef(525.0, 0, -550);
drawTrees(6, 25);
glPopMatrix();
}
void drawPipe()
{
int circle_points;
float size, angle;
int i;
//House Main Vertices
GLfloat housevert[] = { 400.0, 50.0, -500.0, //0
400.0, 60.0, -500.0, //1
415.0, 25.0, -500.0, //2
410.0, 60, -500.0, //3
410.0, 50.0, -500.0, //4
400.0, 50.0, -300.0, //5
400.0, 60, -300.0, //6
415.0, 25.0, -300.0, //7
410.0, 60, -300.0, //8
410.0, 50.0, -300.0 }; //9
glEnable(GL_TEXTURE_2D);
glLineWidth(2.0);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, housevert);
//glColor3f(1.0, 0.0, 0.0);
glBindTexture(GL_TEXTURE_2D, 2002);
// Front Wall
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glArrayElement(0);
glTexCoord2f(1, 0);
glArrayElement(1);
glTexCoord2f(0, 1);
glArrayElement(3);
glTexCoord2f(1, 1);
glArrayElement(4);
glEnd();
glBindTexture(GL_TEXTURE_2D, 2002);
//Right Wall
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glArrayElement(4);
glTexCoord2f(1, 0);
glArrayElement(3);
glTexCoord2f(0, 1);
glArrayElement(8);
glTexCoord2f(1, 1);
glArrayElement(9);
glEnd();
glBindTexture(GL_TEXTURE_2D, 2002);
//Left Wall
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glArrayElement(0);
glTexCoord2f(0, 1);
glArrayElement(1);
glTexCoord2f(1, 0);
glArrayElement(6);
glTexCoord2f(1, 1);
glArrayElement(5);
glEnd();
glBindTexture(GL_TEXTURE_2D, 2002);
//Back Wall
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glArrayElement(5);
glTexCoord2f(0, 1);
glArrayElement(6);
glTexCoord2f(1, 0);
glArrayElement(8);
glTexCoord2f(1, 1);
glArrayElement(9);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void drawRope()
{
GLUquadricObj *mount;
//glMatrixMode(GL_TEXTURE);
//glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2003);
mount = gluNewQuadric();
gluQuadricDrawStyle(mount, GL_QUADS);
gluQuadricTexture(mount, true);
glPushMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//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_CLAMP);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glColor3f(0.0, 0.1, 0.3);
glColor3f(0.0, 1.0, 0.0);
//glTranslatef(-100, 20, 0);
glRotatef(rorate, 1, 0, 0);
gluCylinder(mount, 1, 1, 100, 2, 1);
glDisable(GL_TEXTURE_2D);
glTranslatef(0, 0, 100);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2004);
gluQuadricTexture(mount, true);
gluSphere(mount, 1 * 6, 10, 10);
glDisable(GL_TEXTURE_2D);
//gluQuadricTexture(mount, true);
glPopMatrix();
}
void drawBob()
{
GLUquadricObj *trees;
trees = gluNewQuadric();
gluQuadricDrawStyle(trees, GLU_LINE);
glPushMatrix();
glRotatef(0,0,0,0);
glColor3f(0.0, 1.0, 0.0);
gluSphere(trees, 1 * 10, 20, 2);
glPopMatrix();
}
void initializePendulum()
{
Pendulum.Amplitude = 25;
PendulumThree.Amplitude = 133;
PedulumTwo.Amplitude = 100;
}
void addPendulumInVector()
{
allPendulum.push_back(Pendulum);
allPendulum.push_back(PedulumTwo);
allPendulum.push_back(PendulumThree);
}
void drawWheel()
{
glPushMatrix();
glTranslatef(600, 0, -380);
glRotatef(rorate, 1, 0, 0);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(600, 0, -350);
glRotatef(rorate, 1, 0, 0);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(630, 0, -380);
glRotatef(rorate, 1, 0, 0);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(630, 0, -350);
glRotatef(rorate, 1, 0, 0);
drawBob();
glPopMatrix();
}
void drawCanon()
{
glPushMatrix();
glTranslatef(600, 0, -380);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(600, 0, -410);
glRotatef(rorate, 0, 0, 1);
drawBob();
glPopMatrix();
glPushMatrix();
glTranslatef(300, 0, -40);
//glRotatef(rorate, 0, 0, 0);
drawWheelAttacher();
glPopMatrix();
//canon fire with bomb
glPushMatrix();
glTranslatef(605, 3, -390);
glRotatef(longFire, xRotate, yRotate, zRotate);
//glTranslatef(0, 0, -25);
drawFireHolder();
//printf_s("Long fire is %d", longFire);
glPopMatrix();
glPushMatrix();
glTranslatef(605, 3, -390);
glRotatef(longFire, xRotate, yRotate, zRotate);
glTranslatef(0, 0, bombX);
glTranslatef(0, 0, 25);
drawBomb();
glPopMatrix();
}
void drawWheelAttacher()
{
glPushMatrix();
glTranslatef(300, 0, -380);
//glRotatef(0, 0, 0, 1);
drawLongPipe();
glPopMatrix();
}
void drawLongPipe()
{
GLUquadricObj *trees;
trees = gluNewQuadric();
gluQuadricDrawStyle(trees, GLU_LINE);
glPushMatrix();
glRotatef(0, 0, 0, 0);
glColor3f(0.0, 1.0, 0.0);
gluCylinder(trees, 1, 1, 50, 5, 5);
glPopMatrix();
}
void drawFireHolder()
{
GLUquadricObj *trees;
trees = gluNewQuadric();
gluQuadricDrawStyle(trees, GL_EYE_LINEAR);
glPushMatrix();
glRotatef(0, 0, 0, 1);
glColor3f(0.0, 1.0, 0.0);
gluCylinder(trees, 1, 1, 20, 5, 5);
glPopMatrix();
}
void drawBomb()
{
GLUquadricObj *mount;
//glMatrixMode(GL_TEXTURE);
//glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2003);
mount = gluNewQuadric();
gluQuadricDrawStyle(mount, GL_QUADS);
gluQuadricTexture(mount, true);
glPushMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//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_CLAMP);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glDisable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2004);
gluQuadricTexture(mount, true);
gluSphere(mount, 1 * 6, 10, 10);
glDisable(GL_TEXTURE_2D);
//gluQuadricTexture(mount, true);
glPopMatrix();
}
void mouseClick(int button, int state, int x, int y)
{
fire = true;
}
void mouseControl(int x, int y)
{
// printf_s("mouse x is %d", x);
if (x > 280)
{
check = true;
}
else if (x == 150)
{
check = false;
}
}
void handlerMove()
{
if (check == true && fire == true)
{
}
else if (check == true)
{
longFire--;
}
else
{
longFire++;
}
if (longFire > 280)
{
check = true;
}
else if (longFire == 150)
{
check = false;
}
}
Pendulum Class is here
#pragma once
#include <glut.h>
class pendulum
{
public:
int Amplitude = 0;
bool isCheck = false;
int radius = 6;
void drawRope()
{
GLUquadricObj *mount;
//glMatrixMode(GL_TEXTURE);
//glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2003);
mount = gluNewQuadric();
gluQuadricDrawStyle(mount, GL_QUADS);
gluQuadricTexture(mount, true);
glPushMatrix();
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//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_CLAMP);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glColor3f(0.0, 0.1, 0.3);
glColor3f(0.0, 1.0, 0.0);
//glTranslatef(-100, 20, 0);
glRotatef(Amplitude, 1, 0, 0);
gluCylinder(mount, 1, 1, 50, 2, 1);
glDisable(GL_TEXTURE_2D);
//gluQuadricTexture(mount, true);
glPopMatrix();
}
void changeDirection()
{
if (isCheck)
{
Amplitude--;
}
else
{
Amplitude++;
}
if (Amplitude == 150)
{
isCheck = true;
}
else if (Amplitude < 25)
{
isCheck = false;
Amplitude = 25;
}
}
void drawPend()
{
GLUquadricObj *mount;
//glMatrixMode(GL_TEXTURE);
//glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2003);
mount = gluNewQuadric();
gluQuadricDrawStyle(mount, GL_QUADS);
gluQuadricTexture(mount, true);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
//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_CLAMP);
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glColor3f(0.0, 0.1, 0.3);
//gluQuadricTexture(mount, true);
glPushMatrix();
glRotatef(Amplitude, 1, 0, 0);
glTranslatef(0, 0, 50);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 2004);
gluQuadricTexture(mount, true);
gluSphere(mount, 1 * radius, 10, 10);
glDisable(GL_TEXTURE_2D);
glPopMatrix();
}
};
your logic works only if the two objects are spheres.
you need to compute the distance between the center of the sphere and the line crossing the centers of the up and down circles of the cylinder; then compare that distance to the sum of the radius of the sphere and the radius of the cylinder.
Alright, so I'm using cairo to turn an SVG into image data for openGL textures.
That part works.
But now the texture I'm using won't map to the quad I'm making. It's just showing up as a blank square.
Is there something up with the order I'm calling things in or is there some secret function I forgot to use?
const int SCREEN_WIDTH = 1280;
const int SCREEN_HEIGHT = 720;
const int SCREEN_BPP = 32;
int frame = 0;
SDL_Event event;
bool quit;
GLuint texture[1];
int main(int argc, char *argv[]) {
g_type_init();
rsvg_init();
SDL_Surface *screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL );
SDL_WM_SetCaption ("Cairo", NULL);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
/*2D stuff - it worked here
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable (GL_BLEND);
glEnable (GL_TEXTURE_2D);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable (GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
glEnable(GL_COLOR_MATERIAL);
*/
//An attempt at setting up 3D stuff
glEnable(GL_TEXTURE_2D);
glMatrixMode( GL_MODELVIEW );
glMatrixMode( GL_PROJECTION );
glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT);
glShadeModel(GL_SMOOTH);
glClearColor(0.0,0.0,0.0,0.0);
glClearDepth(1.0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
glEnable(GL_BLEND);
//glLoadIdentity();
float FlowerWidth = .5;
float FlowerHeight = .5;
float FlowerTextureWidth = 80;
float FlowerTextureHeight = 80;
float FlowerScaleWidth = 1;
float FlowerScaleHeight = 1;
cairo_surface_t* Flower;
cairo_t* context;
Flower = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, FlowerTextureWidth, FlowerTextureHeight);
context = cairo_create(Flower);
const gchar* Filename = "resources/area/haneda/lavender.svg";
RsvgHandle* SvgData = rsvg_handle_new_from_file(Filename, NULL);
rsvg_handle_render_cairo_sub(SvgData, context,"#1000");
unsigned char *buffer = cairo_image_surface_get_data(Flower);
cairo_surface_write_to_png(Flower,"flower.png");
//Make a texture
glGenTextures(1, &texture[1]);
glBindTexture(GL_TEXTURE_2D, texture[1]);
glPixelStoref(GL_UNPACK_ALIGNMENT, 1);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glGetError();
//or am I supposed to use GL_TEXTURE_2D?
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGBA,
FlowerHeight,
FlowerWidth,
0,
GL_BGRA,
GL_UNSIGNED_BYTE,
buffer);
//done
while (quit==false) {
while(SDL_PollEvent(&event)) {
if(event.type == SDL_QUIT) {
quit = true;
}
}
/*
FlowerScaleWidth+=.001;
FlowerScaleHeight+=.001;
cairo_scale(context,FlowerScaleWidth,FlowerScaleHeight);
*/
glBindTexture (GL_TEXTURE_2D, texture[1]);
glBegin (GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex3f (0.0, 0.0, 0.0);
glTexCoord2f (FlowerWidth, 0.0);
glVertex3f (FlowerWidth, 0.0, 0.0);
glTexCoord2f (FlowerWidth, FlowerHeight);
glVertex3f (FlowerWidth, FlowerHeight, 0.0);
glTexCoord2f (0.0, FlowerHeight);
glVertex3f (0.0, FlowerHeight, 0.0);
glEnd ();
glDeleteTextures(1, &texture[1]);
cairo_save (context);
cairo_set_source_rgba (context, 0, 0, 0, 0);
cairo_set_operator (context, CAIRO_OPERATOR_SOURCE);
cairo_paint (context);
cairo_restore (context);
SDL_GL_SwapBuffers();
//glClear( GL_COLOR_BUFFER_BIT );
//SDL_Delay(100);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glGetError();
SDL_Delay(400);
}
}
For some reason, you've made an int array of length 1, but you pass the (non-existing) element 2 to glGenTextures. That reads beyond the array bounds and is undefined behavior. You also seem to delete the texture name inside your rendering loop. The same illegal indexing is used there too, as well in your call to glBindTexture.