Optimizing draw function for texured triangle

https://github.com/asdru22/3D-Graphics/blob/rasterization/src/main/java/geom/Triangle.java My current implementation basically implements a rasterization algorithm from scratch, which isn't very efficent. But i don't know how i would otherwise make a triangles texture rotate with it
GitHub
3D-Graphics/src/main/java/geom/Triangle.java at rasterization · asd...
Contribute to asdru22/3D-Graphics development by creating an account on GitHub.
14 Replies
JavaBot
JavaBot3mo ago
This post has been reserved for your question.
Hey @asdru! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
asdru
asdruOP3mo ago
If there's a better solution that involves built in functions im all ears but i couldn't find anything
asdru
asdruOP3mo ago
i tried reducing the calls to the draw function by - grouping textures with the same color img 1 - combining the pixels in a new image and drawing that (img 2), this also looked worse
No description
No description
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
asdru
asdruOP3mo ago
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());


// Creazione del BufferedImage e del zBuffer
BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
double[] zBuffer = new double[img.getWidth() * img.getHeight()];
Arrays.fill(zBuffer, Double.NEGATIVE_INFINITY);

// Draw the scene
//scene.draw(img,zBuffer);
GrassBlock gblock = new GrassBlock(0, 0, 0);
gblock.draw(img, zBuffer, new Camera(this.getWidth(), this.getHeight(), new Vertex(0, 500, 1000)));
g2.drawImage(img, 0, 0, null);
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setColor(Color.BLACK);
g2.fillRect(0, 0, getWidth(), getHeight());


// Creazione del BufferedImage e del zBuffer
BufferedImage img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
double[] zBuffer = new double[img.getWidth() * img.getHeight()];
Arrays.fill(zBuffer, Double.NEGATIVE_INFINITY);

// Draw the scene
//scene.draw(img,zBuffer);
GrassBlock gblock = new GrassBlock(0, 0, 0);
gblock.draw(img, zBuffer, new Camera(this.getWidth(), this.getHeight(), new Vertex(0, 500, 1000)));
g2.drawImage(img, 0, 0, null);
}
do you know why this draws a cube but if i call the scene.draw method
public class RenderingScene {
private final RenderingPanel renderingPanel;
private Camera camera;
private final ArrayList<Cube> cubes = new ArrayList<>();

public RenderingScene(RenderingPanel renderingPanel) {
this.renderingPanel = renderingPanel;
this.camera = new Camera(renderingPanel.getWidth(), renderingPanel.getHeight(), new Vertex(0, 500, 1000));
this.cubes.add(new GrassBlock(0, 0, 0));
this.cubes.add(new Dirt(0, -100, 0));

}

public void update() {

}

public void draw(BufferedImage img, double[] zBuffer) {
for (Cube cube : cubes) {
cube.draw(img, zBuffer, camera);
}
}

}
public class RenderingScene {
private final RenderingPanel renderingPanel;
private Camera camera;
private final ArrayList<Cube> cubes = new ArrayList<>();

public RenderingScene(RenderingPanel renderingPanel) {
this.renderingPanel = renderingPanel;
this.camera = new Camera(renderingPanel.getWidth(), renderingPanel.getHeight(), new Vertex(0, 500, 1000));
this.cubes.add(new GrassBlock(0, 0, 0));
this.cubes.add(new Dirt(0, -100, 0));

}

public void update() {

}

public void draw(BufferedImage img, double[] zBuffer) {
for (Cube cube : cubes) {
cube.draw(img, zBuffer, camera);
}
}

}
it doesnt draw the cubes?
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
rao
rao3mo ago
.
teixayo
teixayo3mo ago
Why dont you use OpenGL? You can even implement your custom algorithms on fragment shader
asdru
asdruOP3mo ago
lets say its a personal challenge and im trying to learn 3d graphics i tried lwjgl too but didn't like it since ur working with ints
teixayo
teixayo3mo ago
It uses int for the addresses, in any library we have it... If you want to learn 3D graphics, you must know how these work. If you want faster rendering of triangles and cubes, I think the best choice is to go to openGL. If not, you can use multi threads and simp (special occasions) I think it's slow cause of creating a buffer for every rendering call. You should avoid creating this every frame
asdru
asdruOP3mo ago
its not super slow so far i have 50 triangles on my screen and its still running at 60fps but i should test it further thanks for the advice!
JavaBot
JavaBot3mo ago
If you are finished with your post, please close it. If you are not, please ignore this message. Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
teixayo
teixayo3mo ago
I think at least you can render 1-5 milion triangles with your gpu(with ancient one) :kekw:
JavaBot
JavaBot3mo ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?